Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
2048cpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hamza2.Libah@live.uwe.ac.uk
2048cpp
Commits
9927d939
Commit
9927d939
authored
2 months ago
by
Hamza2.Libah@live.uwe.ac.uk
Browse files
Options
Downloads
Patches
Plain Diff
Delete Main.cpp
parent
7b6485b6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Main.cpp
+0
-307
0 additions, 307 deletions
Main.cpp
with
0 additions
and
307 deletions
Main.cpp
deleted
100644 → 0
+
0
−
307
View file @
7b6485b6
#include
"GameLogic.h"
#include
<ncurses.h>
#include
<string>
#include
<iostream>
#include
<vector>
#include
<random>
#include
<ctime>
using
namespace
std
;
#define BOARD_SIZE 4
#define EMPTY_CELL 0
#define COLOR_TEAL 8
#define MAX_RAINDROPS 80 // Limit the maximum number of raindrops
// Structure to represent a single raindrop in the code rain
struct
Raindrop
{
int
x
;
// X position
int
row
;
// Current Y position
int
length
;
// Length of the trail
int
speed
;
// How fast it falls (frames per step)
int
counter
;
// Counter for speed control
char
head
;
// Leading character (will generate others as needed)
bool
active
;
// Flag to mark if the raindrop is active
};
// Global variable for raindrops - to avoid recreating them
vector
<
Raindrop
>
raindrops
;
bool
rainInitialized
=
false
;
// Function to initialize the rain effect
void
initRain
(
int
width
,
int
height
)
{
// Clear any existing raindrops
raindrops
.
clear
();
// Random number generator
mt19937
rng
(
time
(
nullptr
));
uniform_int_distribution
<
int
>
x_dist
(
0
,
width
-
1
);
uniform_int_distribution
<
int
>
len_dist
(
5
,
15
);
uniform_int_distribution
<
int
>
speed_dist
(
1
,
3
);
uniform_int_distribution
<
int
>
char_dist
(
33
,
126
);
// ASCII printable characters
uniform_int_distribution
<
int
>
row_dist
(
-
20
,
height
-
1
);
// Some above screen, some on screen
// Create initial raindrops (fewer for better performance)
int
numDrops
=
min
(
width
/
8
,
MAX_RAINDROPS
);
// Limiting the number of drops
raindrops
.
resize
(
numDrops
);
for
(
int
i
=
0
;
i
<
numDrops
;
i
++
)
{
raindrops
[
i
].
x
=
x_dist
(
rng
);
raindrops
[
i
].
row
=
row_dist
(
rng
);
raindrops
[
i
].
length
=
len_dist
(
rng
);
raindrops
[
i
].
speed
=
speed_dist
(
rng
);
raindrops
[
i
].
counter
=
0
;
raindrops
[
i
].
head
=
char_dist
(
rng
);
raindrops
[
i
].
active
=
true
;
}
rainInitialized
=
true
;
}
// Function to update and render the rain effect
void
updateRain
(
int
width
,
int
height
)
{
if
(
!
rainInitialized
)
return
;
// Random number generator for new characters
static
mt19937
rng
(
time
(
nullptr
));
uniform_int_distribution
<
int
>
char_dist
(
33
,
126
);
// Update each raindrop
for
(
auto
&
drop
:
raindrops
)
{
if
(
!
drop
.
active
)
continue
;
drop
.
counter
++
;
// Only move the drop if it's time based on its speed
if
(
drop
.
counter
>=
drop
.
speed
)
{
drop
.
counter
=
0
;
drop
.
row
++
;
// Generate a new head character occasionally
if
(
rng
()
%
5
==
0
)
{
drop
.
head
=
char_dist
(
rng
);
}
// If the drop has fallen off screen, reset it
if
(
drop
.
row
-
drop
.
length
>
height
)
{
uniform_int_distribution
<
int
>
x_dist
(
0
,
width
-
1
);
drop
.
x
=
x_dist
(
rng
);
drop
.
row
=
0
;
// Keep the same length and speed to avoid creating new memory
drop
.
head
=
char_dist
(
rng
);
}
}
}
}
// Function to draw the code rain without overwriting text
void
drawRain
(
int
height
)
{
if
(
!
rainInitialized
)
return
;
// Set the color for the rain - a dim green
init_pair
(
20
,
COLOR_GREEN
,
COLOR_BLACK
);
// Random generator for characters in the trail
static
mt19937
rng
(
time
(
nullptr
));
uniform_int_distribution
<
int
>
char_dist
(
33
,
126
);
// Draw each raindrop
for
(
const
auto
&
drop
:
raindrops
)
{
if
(
!
drop
.
active
)
continue
;
attron
(
COLOR_PAIR
(
20
));
// Draw the trail of characters - generate them on the fly instead of storing
for
(
int
i
=
0
;
i
<
drop
.
length
;
i
++
)
{
int
y
=
drop
.
row
-
i
;
// Only draw if it's on the screen
if
(
y
>=
0
&&
y
<
height
)
{
// Fade the intensity as you go up the trail
int
intensity
=
700
-
(
i
*
600
/
drop
.
length
);
if
(
intensity
<
100
)
intensity
=
100
;
// Use a limited set of color pairs to avoid memory issues
int
colorPairIndex
=
21
+
(
i
%
5
);
// Reuse 5 color pairs for different intensities
init_color
(
colorPairIndex
,
0
,
intensity
,
0
);
init_pair
(
colorPairIndex
,
colorPairIndex
,
COLOR_BLACK
);
attron
(
COLOR_PAIR
(
colorPairIndex
));
// Get the character at the screen position
chtype
existing
=
mvinch
(
y
,
drop
.
x
);
// Only draw if there's nothing important there (space character)
if
((
existing
&
A_CHARTEXT
)
==
' '
)
{
// Use the head character for the first position, generate others on the fly
char
c
=
(
i
==
0
)
?
drop
.
head
:
char_dist
(
rng
)
%
94
+
33
;
mvaddch
(
y
,
drop
.
x
,
c
);
}
attroff
(
COLOR_PAIR
(
colorPairIndex
));
}
}
attroff
(
COLOR_PAIR
(
20
));
}
}
// Function to clean up rain resources
void
cleanupRain
()
{
raindrops
.
clear
();
rainInitialized
=
false
;
}
void
displayMenu
()
{
clear
();
// Get screen dimensions
int
height
,
width
;
getmaxyx
(
stdscr
,
height
,
width
);
// Set up color pairs for rainbow effect
int
rainbow
[]
=
{
COLOR_RED
,
COLOR_YELLOW
,
COLOR_GREEN
,
COLOR_CYAN
,
COLOR_BLUE
,
COLOR_MAGENTA
};
int
numColors
=
6
;
for
(
int
i
=
0
;
i
<
numColors
;
i
++
)
{
init_pair
(
i
+
1
,
rainbow
[
i
],
COLOR_BLACK
);
}
// Set up the static teal color for regular text
init_pair
(
10
,
COLOR_TEAL
,
COLOR_BLACK
);
// Initialize the code rain
initRain
(
width
,
height
);
int
colorIndex
=
0
;
int
frameCount
=
0
;
bool
exitMenu
=
false
;
// Menu animation loop
while
(
!
exitMenu
)
{
// Check for keypress
timeout
(
100
);
// Wait up to 100ms for input
int
ch
=
getch
();
if
(
ch
!=
ERR
)
{
if
(
ch
==
'q'
||
ch
==
'Q'
||
ch
==
10
)
{
// Q, q, or ENTER
ungetch
(
ch
);
// Put the key back for the main loop to read
exitMenu
=
true
;
cleanupRain
();
// Make sure to clean up rain resources
}
}
// Clear only the parts where we'll draw UI elements
for
(
int
i
=
3
;
i
<=
9
;
i
++
)
{
for
(
int
j
=
60
;
j
<=
width
-
10
;
j
++
)
{
mvaddch
(
i
,
j
,
' '
);
}
}
// Update and draw the code rain
updateRain
(
width
,
height
);
drawRain
(
height
);
// Update color every few frames
if
(
frameCount
%
5
==
0
)
{
colorIndex
=
(
colorIndex
+
1
)
%
numColors
;
}
// Draw UI elements on top of the rain
attron
(
COLOR_PAIR
(
colorIndex
+
1
));
mvprintw
(
3
,
65
,
"HAMZA LIBAH"
);
attroff
(
COLOR_PAIR
(
colorIndex
+
1
));
// Print the title with the "2048" part changing color
move
(
5
,
63
);
attron
(
COLOR_PAIR
(
10
));
// Regular teal text
printw
(
"Welcome to the "
);
// Rainbow "2048" part
attron
(
COLOR_PAIR
(
colorIndex
+
1
));
printw
(
"2048"
);
attroff
(
COLOR_PAIR
(
colorIndex
+
1
));
// Back to regular teal text
attron
(
COLOR_PAIR
(
10
));
printw
(
" Game!"
);
// Print the rest of the menu
mvprintw
(
7
,
63
,
"Press ENTER to Start"
);
mvprintw
(
9
,
63
,
"Press Q to Quit"
);
attroff
(
COLOR_PAIR
(
10
));
refresh
();
frameCount
++
;
}
// Reset timeout to blocking mode
timeout
(
-
1
);
// Make sure the rain effect is fully cleaned up
cleanupRain
();
// Clear the screen once more to remove any leftover rain
clear
();
refresh
();
}
int
main
(
int
argc
,
char
**
argv
)
{
// Initialize ncurses screen
initscr
();
noecho
();
cbreak
();
keypad
(
stdscr
,
TRUE
);
// Set up colors
start_color
();
if
(
can_change_color
())
{
init_color
(
COLOR_TEAL
,
0
,
500
,
500
);
}
// Display the animated menu with code rain
displayMenu
();
// Now read the key that was put back with ungetch()
int
ch
=
getch
();
if
(
ch
!=
'q'
&&
ch
!=
'Q'
)
{
if
(
ch
==
10
)
{
// ENTER key
// Start the game
initializeGame
();
bool
running
=
true
;
while
(
running
)
{
drawBoard
();
char
input
=
getch
();
if
(
input
==
'q'
||
input
==
'Q'
)
{
stopMusic
();
// Stop music before quitting
running
=
false
;
// Set flag to exit game loop
}
else
if
(
input
==
't'
||
input
==
'T'
)
{
if
(
isMusicPlaying
)
{
pauseMusic
();
}
else
{
playMusic
();
}
}
else
if
(
input
==
'y'
||
input
==
'Y'
)
{
stopMusic
();
}
else
{
makeMove
(
input
);
}
if
(
isGameOver
())
{
clear
();
mvprintw
(
10
,
10
,
"Game Over!"
);
mvprintw
(
11
,
10
,
"Final Score: %d"
,
score
);
mvprintw
(
12
,
10
,
"Press any key to exit..."
);
refresh
();
stopMusic
();
nodelay
(
stdscr
,
FALSE
);
// Disable non-blocking input
getch
();
// Wait for any key
running
=
false
;
// Exit game loop
}
}
clear
();
refresh
();
}
}
// End ncurses mode and clean up
endwin
();
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment