-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
546 lines (494 loc) · 13.9 KB
/
Copy pathmain.cpp
File metadata and controls
546 lines (494 loc) · 13.9 KB
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include <iostream>
#include <thread>
#include <vector>
#include "graphics.hpp"
// variables
int boardSize = 4; // if i do a vector of vectors, i can do this, yay!!
// size, but array of an array dosn't like it
int startingPieces = 2; // Customize easyly
int piecesToSpawn = 1;
int numberToGet = 2048;
int score = 0;
int maxscore = 3950; // hard-coded highscore it works for now
int moves = 0;
// bool retry = true; //fuck the compiler, why does an int work and a bool not
int state = 1; // current game state 1 replay, 0 exit;
std::string ColorParser(
int numb) { // parses color depending on num using ascii color escapes
switch (numb) {
case 0:
return "\033[1;30m0\033[0m";
break;
case 2:
return "\033[1;32m2\033[0m";
break;
case 4:
return "\033[1;34m4\033[0m";
break;
case 8:
return "\033[1;36m8\033[0m";
break;
case 16:
return "\033[1;33m16\033[0m";
break;
case 32:
return "\033[1;31m32\033[0m";
break;
case 64:
return "\033[1;35m64\033[0m";
break;
case 128:
return "\033[1;37m128\033[0m";
break;
case 256:
return "\033[1;91m256\033[0m";
break;
case 512:
return "\033[1;95m512\033[0m";
break;
case 1024:
return "\033[1;96m1024\033[0m";
break;
case 2048:
return "\033[1;97m2048\033[0m";
break;
default:
return std::to_string(numb);
break;
}
}
bool IsFull(std::vector<std::vector<int>> &board) {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] == 0) { // empty state
return false;
}
}
}
// if not empty
return true;
}
void SpawnTile(std::vector<std::vector<int>> &board, int ammount) {
if (IsFull(board)) { // if it's full we can't spawn a numb
return;
}
for (int i = 0; i < ammount; i++) { // does iterations for each piece
if (IsFull(board)) { // if it's full we can't spawn a numb
return;
}
int num;
while (true) // keep repeating until we get an empty space
{
int row = rand() % boardSize; // Random pos
int col = rand() % boardSize;
if (rand() % 2 == 0) { // 50/50 prob to be 2 or 4
num = 2;
} else {
num = 4;
}
if (board[row][col] == 0) { // if the space is empty, set space and exit
// loop
board[row][col] = num;
break;
}
}
}
}
void InitBoard(std::vector<std::vector<int>> &board) { // Board initialization
srand(time(NULL)); // setup random
board.resize(boardSize);
for (int i = 0; i < boardSize; i++) {
board[i].resize(boardSize);
for (int j = 0; j < boardSize; j++) {
board[i][j] = 0; // initialize all of the array
}
}
SpawnTile(board, startingPieces);
}
int SeeNumlenght(int numb) {
std::string txt = std::to_string(numb);
return txt.size();
}
void DrawTile(int num, int pos) {
switch (SeeNumlenght(num)) {
case 1:
if (pos == 0) { // Change the display of the number if it is at the
// begining, end or between
std::cout << "│ " << ColorParser(num) << " ";
} else if (pos == boardSize - 1) {
std::cout << "│ " << ColorParser(num) << " │";
} else {
std::cout << "│ " << ColorParser(num) << " ";
}
break;
case 2:
if (pos == 0) { // Change the display of the number if it is at the
// begining, end or between
std::cout << "│" << ColorParser(num) << " ";
} else if (pos == boardSize - 1) {
std::cout << "│" << ColorParser(num) << " │";
} else {
std::cout << "│" << ColorParser(num) << " ";
}
break;
case 3:
if (pos == 0) { // Change the display of the number if it is at the
// begining, end or between
std::cout << "│" << ColorParser(num);
} else if (pos == boardSize - 1) {
std::cout << "│" << ColorParser(num) << "│";
} else {
std::cout << "│" << ColorParser(num);
}
break;
case 4:
if (pos == 0) { // Change the display of the number if it is at the
// begining, end or between
std::cout << "" << ColorParser(num);
} else if (pos == boardSize - 1) {
std::cout << "" << ColorParser(num) << "│";
} else {
std::cout << "" << ColorParser(num);
}
break;
default:
std::cout << "│! ";
break;
}
}
void DrawSeparation(int h) { // implements procedural box gen
switch (h) {
case -1:
std::cout << "┌";
for (int i = 0; i < boardSize - 1; i++) { // each column
std::cout << "───┬";
}
std::cout << "───┐\n";
break;
case 0:
std::cout << "├";
for (int i = 0; i < boardSize - 1; i++) { // each column
std::cout << "───┼";
}
std::cout << "───┤\n";
break;
case 1:
std::cout << "└";
for (int i = 0; i < boardSize - 1; i++) { // each column
std::cout << "───┴";
}
std::cout << "───┘\n";
break;
default:
std::cout << "!!!!!!!!!!!!!!!!\n"; // end line
break;
}
}
void DrawGame(std::vector<std::vector<int>> &board) { // Board Drawing
// Clear the console
std::cout << "\033[2J\033[1;1H";
// Draw Cover
std::cout << art[0];
std::cout << "Score: " << score << " | Highscore: " << maxscore
<< " | Moves: " << moves << '\n';
for (int i = 0; i < boardSize; i++) {
if (i == 0) { // create box initial separation
DrawSeparation(-1);
} else { // create box separation between rows
DrawSeparation(0);
}
for (int j = 0; j < boardSize; j++) {
DrawTile(board[i][j], j);
}
std::cout << '\n'; // Do new line
}
DrawSeparation(1);
std::cout << std::endl; // End and flush
}
bool IsGameOverState(std::vector<std::vector<int>> &board) {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] == 0) { // check if has empty cells
return false;
}
if (i >= 1 &&
board[i][j] == board[i - 1][j]) { // check if can move up/down
return false;
}
if (j >= 1 &&
board[i][j] == board[i][j - 1]) { // check if can move left right
return false;
}
}
}
return true;
}
bool IsWinState(std::vector<std::vector<int>> &board) {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] == numberToGet) { // if contains numb
return true;
}
}
}
// if not found
return false;
}
bool MoveUp(std::vector<std::vector<int>> &board) {
bool moved = false;
for (int j = 0; j < boardSize; j++) {
for (int i = 1; i < boardSize; i++) {
if (board[i][j] != 0) { // if cell is not empty
int k = i;
while (k > 0 && board[k - 1][j] == 0) { // Moves each full cell
// until none of
// them can be moved further
board[k - 1][j] = board[k][j];
board[k][j] = 0;
k--;
moved = true;
}
if (k > 0 && board[k - 1][j] == board[k][j]) {
board[k - 1][j] += board[k][j];
board[k][j] = 0;
score += board[k - 1][j]; // sum score
moved = true;
}
}
}
}
return moved; // return if any moves has been performed
}
bool MoveDown(std::vector<std::vector<int>> &board) {
bool moved = false;
for (int j = 0; j < boardSize; j++) {
for (int i = boardSize - 2; i >= 0; i--) {
if (board[i][j] != 0) { // if cell is not empty
int k = i;
while (k < boardSize - 1 &&
board[k + 1][j] == 0) { // moves each full
// cell until none of them
// can be moved further
board[k + 1][j] = board[k][j];
board[k][j] = 0;
k++;
moved = true;
}
if (k < boardSize - 1 &&
board[k + 1][j] == board[k][j]) { // if next moving cell
// is the same
// as the current cell,
// sum them
board[k + 1][j] += board[k][j];
board[k][j] = 0;
score += board[k + 1][j]; // sum score
moved = true;
}
}
}
}
return moved; // return if any moves has been performed
}
bool MoveLeft(std::vector<std::vector<int>> &board) {
bool moved = false;
for (int i = 0; i < boardSize; i++) {
for (int j = 1; j < boardSize; j++) {
if (board[i][j] != 0) { // if cell is not empty
int k = j;
while (k > 0 && board[i][k - 1] == 0) { // moves each full
// cell until none of them
// can be moved further
board[i][k - 1] = board[i][k];
board[i][k] = 0;
k--;
moved = true;
}
if (k > 0 && board[i][k - 1] == board[i][k]) { // if next moving cell
// is the same
// as the current cell,
// sum them
board[i][k - 1] += board[i][k];
board[i][k] = 0;
score += board[i][k - 1]; // sum score
moved = true;
}
}
}
}
return moved;
}
bool MoveRight(std::vector<std::vector<int>> &board) {
bool moved = false;
for (int i = 0; i < boardSize; i++) {
for (int j = boardSize - 2; j >= 0; j--) {
if (board[i][j] != 0) { // if cell is not empty
int k = j;
while (k < boardSize - 1 && board[i][k + 1] == 0) { // moves each full
// cell until none of them
// can be moved further
board[i][k + 1] = board[i][k];
board[i][k] = 0;
k++;
moved = true;
}
if (k < boardSize - 1 &&
board[i][k + 1] == board[i][k]) { // if next moving cell
// is the same
// as the current cell,
// sum them
board[i][k + 1] += board[i][k];
board[i][k] = 0;
score += board[i][k + 1]; // sum score
moved = true;
}
}
}
}
return moved;
}
void DoInput(std::vector<std::vector<int>> &board) {
std::this_thread::sleep_for(std::chrono::milliseconds(150)); // wait
bool moved = false;
char userMove;
std::cout << "Enter move [W] -> UP [S] -> DOWN [A] -> LEFT [D] -> RIGHT : ";
std::cin >> userMove;
userMove = toupper(userMove); // to upper so it can be put both
switch (userMove) {
case 'W':
moved = MoveUp(board);
break;
case 'S':
moved = MoveDown(board);
break;
case 'A':
moved = MoveLeft(board);
break;
case 'D':
moved = MoveRight(board);
break;
default:
std::cout << "\033[1;31mInvalid move (W,A,S,D)\033[0m\n";
std::this_thread::sleep_for(
std::chrono::milliseconds(1000)); // waits for user to read
DrawGame(board); // redraws screen again
return;
break;
}
if (moved) {
moves++;
SpawnTile(board, piecesToSpawn);
DrawGame(board);
} else {
std::cout << "\033[1;31mNo valid moves left in this direction!\033[0m\n"; //
return;
}
}
void InitialSetup() { // unused
// Clear the console
std::cout << "\033[2J\033[1;1H";
// Draw Cover
std::cout << art[0];
char sel;
std::cout << "[1] -> Quick start [2] -> Custom game : ";
std::cin >> sel;
switch (sel) {
case '1':
return;
break;
case '2':
std::cout << "Board size, min 2 - max 20 (default 4): ";
std::cin >> boardSize;
if (boardSize > 20) {
boardSize = 20;
} else if (boardSize < 2) {
boardSize = 2;
}
std::cout << "Starting pieces, min 1 - max " << (boardSize * boardSize) - 1
<< " (default 2): ";
std::cin >> startingPieces;
if (startingPieces > (boardSize * boardSize) - 5) {
startingPieces = (boardSize * boardSize) - 5;
} else if (startingPieces < 1) {
startingPieces = 1;
}
std::cout << "Pieces to spawn, min 1 - max " << (boardSize * boardSize) - 1
<< " (default 1): ";
std::cin >> piecesToSpawn;
if (piecesToSpawn > (boardSize * boardSize) - 5) {
piecesToSpawn = (boardSize * boardSize) - 5;
} else if (piecesToSpawn < 1) {
piecesToSpawn = 1;
}
std::cout
<< "Piece to get, min 16 - max 8192 (default 2048) multiple of 2!: ";
std::cin >> numberToGet;
if (numberToGet > 8192) {
numberToGet = 8192;
} else if (numberToGet < 16) {
numberToGet = 16;
}
return;
break;
}
}
void CheckHighscore() { // simple thingi
if (score > maxscore) {
std::cout << "New highscore: " << score << '\n';
maxscore = score;
}
score = 0;
moves = 0;
}
void MainGame() {
InitialSetup();
std::vector<std::vector<int>> board;
InitBoard(board);
DrawGame(board);
while (!IsGameOverState(board) && !IsWinState(board)) {
DoInput(board);
}
if (IsGameOverState(board)) {
std::cout << art[2];
CheckHighscore();
char retry;
std::cout << "Try again? (y|n): "; // try again
std::cin >> retry;
if (retry == 'y') {
state = 1;
return;
} else if (retry == 'n') {
state = 0;
return;
} else {
state = 1;
return;
}
} else if (IsWinState(board)) {
std::cout << art[1];
CheckHighscore();
char retry;
std::cout << "Try again? (y|n): "; // try again
std::cin >> retry;
if (retry == 'y') {
state = 1;
return;
} else if (retry == 'n') {
state = 0;
return;
} else {
state = 1;
return;
}
}
return;
}
int main() {
while (true) { // retry loop fuck the compiler
if (state != 0) {
MainGame();
} else {
break;
}
}
return 0;
}