-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
529 lines (472 loc) · 16.6 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
std::string reset = "\033[0m";
std::string flashBlue = "\u001b[34;5;1m";
std::string red = "\u001b[31;1m";
std::string blue = "\u001b[34;1m";
std::string green = "\u001b[32;1m";
std::string yellow = "\u001b[33;1m";
int boardSize = 4;
int board[9][9];
int score = 0;
int highScore;
bool reachedGoal;
bool changesMade;
bool playing;
int tilePlacedX;
int tilePlacedY;
bool coloring = true;
bool logging = true;
void clearSpace(int lines);
int tipOfTheDay();
void tileGen();
int randomNum();
int help();
int settings();
/**
* @result All items in the board array are set to 0.
*/
void newGame();
int game();
/**
* @param userInput The move to make (up, down, left or right)
* @result An adjusted board array
*/
void adjust(char userInput);
/**
* @param horiz The horizontal start position
* @param vert The vertical start position
* @param moveHoriz The amount of spaces to move horizontally
* @param moveVert The amount of spaces to move vertically
* @result A board with pieces that have move in a certain direction based on the 0s in front of it
*/
void moveTiles(int vert, int horiz, int moveVert, int moveHoriz);
/**
* @param horiz The horizontal start position
* @param vert The vertical start position
* @param moveHoriz The amount of spaces to move horizontally
* @param moveVert The amount of spaces to move vertically
* @result A board with adjacent like pieces combined depending on the direction moved
*/
void combineTiles(int vert, int horiz, int moveVert, int moveHoriz);
/**
* @result A boolean stating true if the board is full
*/
bool checkFull();
/**
* @result Prints game over message and sets playing to false
*/
int gameOver();
/**
* @result Prints the game board
*/
void printBoard();
/**
* @param value The number to find a color for
* @result Prints an escape string based on the value given
*/
string coloredNum(int value);
int main() {
clearSpace(20);
srand(time(NULL));
std::fstream scoresheet("scoresheet.txt", std::ios_base::in);
scoresheet >> highScore;
scoresheet.close();
tipOfTheDay();
char userInput;
while (true) {
clearSpace(20);
cout << red << " .d8888b. " << green << ".d8888b. " << yellow << "d8888 " << blue << ".d8888b. \n"
<< red << "d88P Y88b " << green << "d88P Y88b " << yellow << "d8P888 " << blue << "d88P Y88b \n"
<< red << " 888 " << green << "888 888 " << yellow << "d8P 888 " << blue << "Y88b. d88P \n"
<< red << " .d88P " << green << "888 888 " << yellow << "d8P 888 " << blue << "\"Y88888\" \n"
<< red << " .od888P\" " << green << "888 888 " << yellow << "d88 888 " << blue << ".d8P\"\"Y8b. \n"
<< red << "d88P\" " << green << "888 888 " << yellow << "8888888888 " << blue << "888 888 \n"
<< red << "888\" " << green << "Y88b d88P " << yellow << "888 " << blue << "Y88b d88P \n"
<< red << "888888888 " << green << "\"Y8888P\" " << yellow << "888 " << blue << "\"Y8888P\" \n"
<< reset << endl;
cout << "Welcome to 2048!" << endl;
cout << " \n\n\u001b[7mN" << reset << "ew game" << endl;
if (playing) {
cout << "\u001b[7mC" << reset << "ontinue game" << endl;
}
cout << "\nView \u001b[7mh" << reset << "elp" << endl;
cout << "E\u001b[7mx" << reset << "it game" << endl;
cout << "\u001b[7mS" << reset << "ettings" << endl;
cout << "\n\n\u001b[7m Highscore: " << highScore <<
" Create by Steven Seiden © Steven Seiden 2019" << reset << endl;
scanf(" %c", &userInput);
if (userInput == 'n') {
newGame();
game();
} else if (userInput == 'c') {
game();
} else if (userInput == 'h') {
help();
} else if (userInput == 'x') {
return 0;
} else if (userInput == 's') {
settings();
}
}
}
void clearSpace(int lines) {
int n;
for (n = 0; n < lines; n++)
printf("\n");
}
int tipOfTheDay() {
cout << "\u001b[7m" << "Tip of the day" << reset << "\n" << endl;
int todaysTip = (rand() % 10) % 8;
if (todaysTip == 1) { cout << "Look several moves ahead" << endl; }
else if (todaysTip == 2) { cout << "Don’t choose the first move you see" << endl; }
else if (todaysTip == 3) { cout << "Try grouping numbers together by size" << endl; }
else if (todaysTip == 4) { cout << "Try only using 3 of 4 moves when possible" << endl; }
else if (todaysTip == 5) { cout << "Develop a strategy" << endl; }
else if (todaysTip == 6) { cout << "Try getting the larger numbers in the center" << endl; }
else if (todaysTip == 7) { cout << "Try to not have a small number surrounded by large ones" << endl; }
else { cout << "Star this project at https://github.com/StevenSeiden/2048!" << endl; }
cout << "\nPress \u001b[7mc" << reset << " to \u001b[7mc" << reset << "ontinue." << endl;
char userInput;
while(true){
scanf(" %c", &userInput);
if (userInput == 'c') {
return 0;
}
}
}
void tileGen() {
int pos2x = (rand() % 10) % boardSize;
int pos2y = (rand() % 10) % boardSize;
while (board[pos2x][pos2y] != 0) {
pos2x = (rand() % 10) % boardSize;
pos2y = (rand() % 10) % boardSize;
}
int newTile = randomNum();
if (newTile >= 9) {
board[pos2x][pos2y] = 4;
if (logging) {
cout << ("Adding a tile of ") << 4 << (" at position ") << pos2x << (", ") << pos2y << endl;
}
} else {
board[pos2x][pos2y] = 2;
if (logging) {
cout << ("Adding a tile of ") << 2 << (" at position ") << pos2x << (", ") << pos2y << endl;
}
}
tilePlacedX = pos2x;
tilePlacedY = pos2y;
}
int help() {
clearSpace(10);
cout << "\u001b[7m" << "Help" << reset << endl;
cout << "\u001b[1mWelcome to 2048!\n" << reset <<
"\n"
"2048 is a simple game in which\n"
"the goal is to combine like\n"
"tiles to reach the 2048 tile.\n"
"\n"
"\u001b[1mKeys:\n" << reset <<
"w = Move tiles up\n"
"s = Move tiles down\n"
"a = Move tiles left\n"
"d = Move tiles right\n"
"x = Main menu" << endl;
cout << "\nPress \u001b[7mc" << reset << " to \u001b[7mc" << reset << "ontinue." << endl;
char userInput;
scanf(" %c", &userInput);
if (userInput == 'c') {
return 0;
}
}
int settings() {
char userInput;
while (true) {
clearSpace(20);
cout << "\u001b[7m" << "Settings" << reset << "\n\n" << endl;
if (coloring) {
cout << "C\u001b[7mo" << reset << "loring: enabled (good for 256 colored terminals)" << endl;
} else {
cout << "C\u001b[7mo" << reset << "loring: disabled (good for terminals lacking 256 colors)" << endl;
}
cout << "\u001b[38;5;212m(If this line doesn't appear in color than disable coloring.)\n" << reset << endl;
if (logging) {
cout << "\u001b[7mL" << reset << "ogging: enabled" << endl;
} else {
cout << "\u001b[7mL" << reset << "ogging: disabled" << endl;
}
cout << "\u001b[7mB" << reset << "oard size: " << boardSize << endl;
cout << "\n\u001b[7mR" << reset << "eset high score" << endl;
cout << "\nPress \u001b[7mc" << reset << " to \u001b[7mc" << reset << "ontinue." << endl;
scanf(" %c", &userInput);
if (userInput == 'o') {
coloring = !coloring;
} else if (userInput == 'c') {
return 0;
} else if (userInput == 'l') {
logging = !logging;
} else if (userInput == 'b') {
bool userInputting = true;
int userNum;
while (userInputting) {
cout << "Note that setting a custom board size will disable \n"
"saving your high score until set back to 4.\n"
"This will also end your current game.\n" <<
"Please enter a new board size (2-9): ";
std::cin >> userNum;
if (!cin.fail()) {
boardSize = userNum;
userInputting = false;
playing = false;
}
}
} else if (userInput == 'r') {
cout << "Resetting your score will also end your game."
"Are you sure you want to continue? (y/n): ";
bool userInputting = true;
char yesNo;
while (userInputting) {
std::cin >> yesNo;
if (yesNo == 'y') {
ofstream scoresheet("scoresheet.txt");
scoresheet << 0 << endl;
scoresheet.close();
highScore = 0;
playing = false;
userInputting = false;
} else {
userInputting = false;
}
}
}
}
}
int randomNum() {
int randomNumber;
randomNumber = rand() % 10;
if (randomNumber == 10) {
return randomNum();
} else {
return randomNumber;
}
}
void newGame() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
board[i][j] = {0};
}
}
score = 0;
}
int game() {
tileGen();
tileGen();
/*board[0][0] = 2; <- A good test board
board[0][1] = 2;
board[0][2] = 2;
board[1][2] = 2;
board[2][2] = 2;*/
/*board[0][0] = 4; <- Another test board
board[1][0] = 4;
board[1][1] = 2;
board[1][2] = 2;*/
playing = true;
clearSpace(20);
printBoard();
char userInput;
while (playing) {
scanf(" %c", &userInput);
if (userInput == 'x') {
return 0;
} else if (userInput == 'w' || userInput == 'a' || userInput == 's' || userInput == 'd') {
adjust(userInput);
if (changesMade) {
tileGen();
changesMade = false;
}
clearSpace(20);
printBoard();
if (checkFull()) {
gameOver();
}
} else if (userInput == 'k') {
coloring = false;
}
}
return 0;
}
void adjust(char userInput) {
changesMade = false;
for (int i = 0; i < boardSize; i++) { //Running for the maximum amount of times necessary to adjust everything
if (userInput == 's' || userInput == 'w') {
for (int horiz = 0; horiz < boardSize; horiz++) {
if (userInput == 's') {
for (int vert = (boardSize - 1); vert > 0; vert--) {
moveTiles(vert, horiz, -1, 0);
combineTiles(vert, horiz, -1, 0);
}
} else {
for (int vert = 0; vert < (boardSize - 1); vert++) {
moveTiles(vert, horiz, 1, 0);
combineTiles(vert, horiz, 1, 0);
}
}
}
} else if (userInput == 'd') {
for (int horiz = (boardSize - 1); horiz > 0; horiz--) {
for (int vert = (boardSize - 1); vert >= 0; vert--) {
moveTiles(vert, horiz, 0, -1);
combineTiles(vert, horiz, 0, -1);
}
}
} else {
for (int horiz = 0; horiz < (boardSize - 1); horiz++) {
for (int vert = 0; vert < boardSize; vert++) {
moveTiles(vert, horiz, 0, 1);
combineTiles(vert, horiz, 0, 1);
}
}
}
}
}
void moveTiles(int vert, int horiz, int moveVert, int moveHoriz) {
if (board[vert][horiz] == 0 && board[vert + moveVert][horiz + moveHoriz] != 0) {
board[vert][horiz] = board[vert + moveVert][horiz + moveHoriz];
board[vert + moveVert][horiz + moveHoriz] = 0;
if (logging) {
printf("Moving (%d,%d) to (%d,%d).\n", (vert + moveVert), (horiz + moveHoriz), vert, horiz);
}
changesMade = true;
}
}
void combineTiles(int vert, int horiz, int moveVert, int moveHoriz) {
if (board[vert][horiz] == board[vert + moveVert][horiz + moveHoriz] && board[vert][horiz] != 0) {
board[vert][horiz] = (board[vert][horiz] * 2);
//The vert and horiz values are meant to be swapped here.
board[vert + moveVert][horiz + moveHoriz] = 0;
if (logging) {
printf("Adding (%d,%d) to (%d,%d).\n", (vert + moveVert), (horiz + moveHoriz), vert, horiz);
}
score = score + board[vert][horiz];
if (board[vert][horiz] == 16) {
reachedGoal = true;
}
changesMade = true;
}
}
bool checkFull() {
for (int i = 0; i <= (boardSize - 1); i++) {
for (int j = 0; j <= (boardSize - 1); j++) {
if (board[i][j] == 0) {
return false;
}
}
}
for (int i = 0; i <= (boardSize - 1); i++) {
for (int j = 0; j <= (boardSize - 1); j++) {
if (i >= (boardSize - 2) || j >= (boardSize - 2)) {
if (board[i][j] == board[i + 1][j] || board[i][j] == board[i][j + 1]) {
return false;
}
} else if (i >= (boardSize - 2)) {
if (board[i][j] == board[i][j + 1]) {
return false;
}
} else {
if (board[i][j] == board[i + 1][j]) {
return false;
}
}
}
}
return true;
}
int gameOver() {
clearSpace(10);
cout << red << " _____ _____ \n"
"| __ \\ | _ | \n"
"| | \\/ __ _ _ __ ___ ___ | | | |_ _____ _ __ \n"
"| | __ / _` | '_ ` _ \\ / _ \\ | | | \\ \\ / / _ \\ '__|\n"
"| |_\\ \\ (_| | | | | | | __/ \\ \\_/ /\\ V / __/ | \n"
" \\____/\\__,_|_| |_| |_|\\___| \\___/ \\_/ \\___|_| \n" << reset << endl;
printBoard();
cout << "\nPress " << "\u001b[7mc" << reset << " to " << "\u001b[7mc" << reset << "ontinue." << endl;
char userInput;
scanf(" %c", &userInput);
if (userInput == 'c') {
playing = false;
return 0;
}
}
void printBoard() {
if (reachedGoal) {
cout << flashBlue << ("***************************************") << reset << endl;
cout << flashBlue << ("* Congrats! You created a 16 tile! *") << reset << endl;
cout << flashBlue << ("***************************************") << reset << endl;
reachedGoal = false;
}
cout << "\u001b[7m" << ("Score: ") << score;
if (score > highScore) {
cout << " - High Score!" << endl << reset;
highScore = score;
if (boardSize == 4) {
ofstream scoresheet("scoresheet.txt");
scoresheet << highScore << endl;
scoresheet.close();
}
} else {
cout << endl << reset;
}
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (tilePlacedX == i && tilePlacedY == j) {
cout << coloredNum(board[i][j]) << "\033[5m" << setfill(' ') << setw(8) << board[i][j] << reset;
} else {
cout << coloredNum(board[i][j]) << setfill(' ') << setw(8) << board[i][j];
}
}
cout << "\n" << endl;
}
}
string coloredNum(int value) {
if (coloring) {
switch (value) {
case 0 :
return "\033[0m";
case 2 :
return "\u001b[38;5;141m";
case 4 :
return "\u001b[38;5;212m";
case 8 :
return "\u001b[38;5;208m";
case 16 :
return "\u001b[38;5;196m";
case 32 :
return "\u001b[38;5;93m";
case 64 :
return "\u001b[38;5;27m";
case 128 :
return "\u001b[38;5;31m";
case 256 :
return "\u001b[38;8;36m";
case 512 :
return "\u001b[38;5;34m";
case 1024 :
return "\u001b[38;5;214m";
case 2048 :
return "\u001b[38;5;222m";
case 4096 :
return "\u001b[38;5;189m";
case 8192 :
return "\u001b[38;5;117m";
default:
return "\u001b[31;1m";
}
} else {
return reset;
}
}