-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048 Game.cpp
173 lines (142 loc) · 4.42 KB
/
2048 Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 4;
class Game2048 {
public:
void play() {
initBoard();
printBoard();
while (!isGameOver()) {
char move;
cout << "Enter move (W/A/S/D): ";
cin >> move;
if (makeMove(move)) {
addTile();
printBoard();
}
}
cout << "Game over!" << endl;
}
private:
int board[SIZE][SIZE];
void initBoard() {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
board[i][j] = 0;
}
}
addTile();
addTile();
}
void printBoard() {
cout << "Score: " << calculateScore() << endl;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
cout << board[i][j] << "\t";
}
cout << endl;
}
}
void addTile() {
vector<pair<int, int>> emptyCells;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (board[i][j] == 0) {
emptyCells.push_back(make_pair(i, j));
}
}
}
if (emptyCells.empty()) {
return;
}
srand(time(0));
int randomIndex = rand() % emptyCells.size();
int newValue = (rand() % 2 + 1) * 2;
board[emptyCells[randomIndex].first][emptyCells[randomIndex].second] = newValue;
}
bool isGameOver() {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (board[i][j] == 0) {
return false;
}
if (i < SIZE - 1 && board[i][j] == board[i + 1][j]) {
return false;
}
if (j < SIZE - 1 && board[i][j] == board[i][j + 1]) {
return false;
}
}
}
return true;
}
bool makeMove(char move) {
bool moved = false;
for (int i = 0; i < SIZE; ++i) {
vector<int> line;
for (int j = 0; j < SIZE; ++j) {
if (move == 'W' || move == 'w') {
line.push_back(board[j][i]);
} else if (move == 'A' || move == 'a') {
line.push_back(board[i][j]);
} else if (move == 'S' || move == 's') {
line.push_back(board[SIZE - 1 - j][i]);
} else if (move == 'D' || move == 'd') {
line.push_back(board[i][SIZE - 1 - j]);
}
}
mergeLine(line);
for (int j = 0; j < SIZE; ++j) {
if (move == 'W' || move == 'w') {
moved |= (board[j][i] != line[j]);
board[j][i] = line[j];
} else if (move == 'A' || move == 'a') {
moved |= (board[i][j] != line[j]);
board[i][j] = line[j];
} else if (move == 'S' || move == 's') {
moved |= (board[SIZE - 1 - j][i] != line[j]);
board[SIZE - 1 - j][i] = line[j];
} else if (move == 'D' || move == 'd') {
moved |= (board[i][SIZE - 1 - j] != line[j]);
board[i][SIZE - 1 - j] = line[j];
}
}
}
return moved;
}
void mergeLine(vector<int>& line) {
for (int i = 0; i < SIZE - 1; ++i) {
if (line[i] == line[i + 1] && line[i] != 0) {
line[i] *= 2;
line[i + 1] = 0;
}
}
vector<int> temp;
for (int i = 0; i < SIZE; ++i) {
if (line[i] != 0) {
temp.push_back(line[i]);
}
}
while (temp.size() < SIZE) {
temp.push_back(0);
}
line = temp;
}
int calculateScore() {
int score = 0;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
score += board[i][j];
}
}
return score;
}
};
int main() {
Game2048 game;
game.play();
return 0;
}