-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
554 lines (457 loc) · 16.1 KB
/
main.cpp
File metadata and controls
554 lines (457 loc) · 16.1 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
547
548
549
550
551
552
553
#include "./main.h"
using std::cout;
using std::cin;
using std::string;
using std::vector;
//[GAME CONSTANTS]
//number of distortions to make to the board
const auto RANDOMIZE_MOVES = 80;
//possible moves [defined in main.h]
char MOVES[] = {UP, DOWN, LEFT, RIGHT};
//opposite of each move
char RMOVES[] = {DOWN, UP, RIGHT, LEFT};
//GLOBAL VARIABLE YHAT HOLD ALL MOVES MADE BY THE PLAYER AND
//THE RANDOM ONES MADE BY THE COMPUTER ie All moves of the game
std::vector<int> allMoves;
void displayInstructions();
void show_state(char state[],bool stay = false){
TEXTCOLOR(VIOLET);
GOTOXY(8, 3);
std::cout <<"CURRENT STATE : ";
TEXTCOLOR(GREEN);
for(int i =0; i < state[i] != '\0'; ++ i){
std::cout << state[i];
Sleep(50);
}
std::cout << " ";
if(not stay){
Sleep(2000);
}
}
void writeGameInfoToScreen();
bool checkForWin(int numbers[][4]){
int nCheck = 1;
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(numbers[i][j] != (nCheck++)%16)
return false;
return true;
}
void showWelcomeScreen(){
char message[] = "\n\n\n\n\n\tWELCOME TO SLIDE PUZZLE with CLI \n\n\n";
int index = 0;
clear_screen();
while (message[index] != '\0'){
TEXTCOLOR((index % 15)+1);
std::cout <<message[index++]; Sleep(20);
}
Sleep(3000);
}
///main game run here
void runGame();
//function modifies the allmoves variable by appending a random move
//and adjusting the value in the array of numbers accordingly
//also blits and updates the screen
void generateRandomMoves(int nums[][4]);
//used by generateRandomMoves(int nums[][4]) to validate a move before appending and
//in the game to validate a players move
bool isValidMove(int move, int nums[][4]);
//update the array to keep up with the changing values
void updateArrayValues(int move, int nums[][4]);
//updates the values in updateArrayValues visually on the screen
void updateScreen(int, int (*)[4]);
void drawBoard(void);//draw the grid
void writeNumbersToScreen(int first_x_pos, int first_y_pos,int nums[][4],
size_t wait_time=50, size_t size = 16);//16 ie 4X4
void showGameWonAnimation(){
clear_screen();
char message[100] = "\n\n\n\n\n\n\n\tEXCELLENT\n\n\n"
"\t\tYOU HAVE SOLVED THE PUZZLE, CONGRATULATIONS";
int index = 0;
clear_screen();
while (message[index] != '\0'){
TEXTCOLOR((index % 15)+1);
std::cout <<message[index++]; Sleep(70);
}
Beep(2000, 500);
std::cout<<" Press any key to continue .... ";
getch();
}
void showGameExitScreen(){
clear_screen();
TEXTCOLOR(SHINYGREEN);
char message[] = "\n\n\n\n\n\n\n\tTHANKS FOR TRYING OUT THIS SIMPLE GAME \n\n\n"
"\t\tCONGRATULATIONS\n\n\n\t\t\tSEE YOU!!!!!!!";
int index = 0;
clear_screen();
while (message[index] != '\0'){
TEXTCOLOR((index % 15)+1);
std::cout <<message[index++]; Sleep(70);
}
Beep(2000, 500);
clear_screen();
TEXTCOLOR(SHINYGREEN);
GOTOXY(15, 10), std::cout<<"EXITING IN ";
for(int i = 5; i ; --i){
TEXTCOLOR(RED);
std::cout << "\b\b" << std::right << std::setw(2) << i;//fillchar('0')<<i;
Sleep(1000);
}
exit(EXIT_SUCCESS);
}
void generateNewPuzzle(){
runGame();
}
/****************************************************************************
* *
* Function: main function AYK *
* *
* Purpose : executes the functions in logical order *
* *
* History : Date Reason --above *
* --/--/-- Created *
* *
****************************************************************************/
main(){
showWelcomeScreen();
clear_screen();
runGame();
getch();
}
//solve board won't check for valid moves
//because if a move is valid then so is the opposite
//uses allMoves to solve
void solveBoard(int board[][4], int wait){
char msg[40] = "SOLVING . . .";
show_state(msg,true);
//reverse moves
std::vector<int> reverseMoves(allMoves);
std::reverse(reverseMoves.begin(), reverseMoves.end());
for(auto move: reverseMoves){
auto reverseMove = move;
switch (move){
case UP:
case UPx:
case UPxl:
reverseMove = DOWN;
break;
case DOWN:
case DOWNx:
case DOWNxl:
reverseMove = UP;
break;
case LEFT:
case LEFTx:
case LEFTxl:
reverseMove = RIGHT;
break;
case RIGHT:
case RIGHTx:
case RIGHTxl:
reverseMove = LEFT;
}
updateScreen(reverseMove, board);
Sleep(wait);
}
strcpy(msg, "Puzzle Solved!!!");
show_state(msg);
allMoves.clear();
}
/****************************************************************************
* *
* Function: runGame *
* *
* Purpose : implements the logic of the game *
* *
* History : Date Reason *
* --/--/-- Created *
* *
****************************************************************************/
void runGame(){
clear_screen();
allMoves.clear();
char msg[] ="Generating new puzzle...";
show_state(msg, true);
TEXTCOLOR(SHINYWHITE);
drawBoard();
int playerMove = 0xE7;//lambda
int board[4][4],//represent boarditions
y_pos,//represent the x and y axis
x_pos,
in = 1;
//generating the main array for the game
for(int i =0; i< 4; ++i)
for(int j = 0; j < 4; ++j)
board[i][j] = (in++)%16;
writeNumbersToScreen(5, 7,board);
writeGameInfoToScreen();
Sleep(1000);
generateRandomMoves(board);
strcpy(msg, "Solving puzzle......");
show_state(msg, true);
bool running = true;
while(running){
if(kbhit()){
playerMove = getch();
//function or arrow key pressed?
if(not playerMove){
ungetch(playerMove);
playerMove = getch();
}
//validate and incoporate player move
if(isValidMove(playerMove,board)){
updateScreen(playerMove, board);
allMoves.push_back(playerMove);
}
//check for terminating condition [solved]
if(checkForWin(board)){
showGameWonAnimation();
generateNewPuzzle();
}
//Parse commands
switch (playerMove){
case 'G':
case 'g':
solveBoard(board, 100);
running = false;
break;
case 'E':
case 'e':
showGameExitScreen();
break;
case '?':
case 'h':
displayInstructions();
break;
case 'N':
case 'n':
generateNewPuzzle();
break;
case 'Q':
case 'q':
TEXTCOLOR(YELLOW);
clear_screen();
GOTOXY(10, 10),std::cout<<"WARNING: Saving not yet available";
Sleep(2000);
clear_screen();
showGameExitScreen();
}
}//end if (kbhit())
}
}
/****************************************************************************
* *
* Function: drawBoard *
* *
* Purpose : draw thegrid to the screen *
* *
* History : Date Reason *
* --/--/-- Created *
* *
****************************************************************************/
void drawBoard(void){
char ch[12] ;
int codes[12]= {179, 196,197,194,193,218,191,192,217,195,180};
for(int i = 0; i < 12; i++)ch[i] =int(codes[i]);
GOTOXY(2,1+4), std::cout <<ch[5];//top left corner
GOTOXY(30, 1+4), std::cout << ch[6]; //top right corner
GOTOXY(2,9+4+8), std::cout <<ch[7];//bottom left corner
GOTOXY(30, 9+4+8), std::cout <<ch[8];//bottom right corner
//middle vertical lines
for(int i = 2; i <=30; i += 7)
for(int j = 2; j <= 8+8; ++j){
GOTOXY(i, j+4), std::cout <<ch[0];
}
//joining of the box
for(int i = 1; i <=27; i++)
for(int j = 1; j <= 19; j +=4){//c
GOTOXY(i+2, j+4), std::cout <<ch[1];
}
//the junction
for(int i = 2; i <=21; i+=7)//fixed
for(int j = 1; j <= 7+6; j +=4)//c
GOTOXY(i+7, j+2+4-2), std::cout <<ch[2];
//first line
for(int i = 2; i <=22; i+=7)
GOTOXY(i+7, 1+4), std::cout <<ch[3];
//second line
for(int i = 2; i <=22; i+=7)
GOTOXY(i+7, 9+4+8), std::cout <<ch[4];
//third line
for(int i = 2; i <=10; i+=4)
GOTOXY(2, i+1+4+2), std::cout <<ch[9];
//fourth line
for(int i = 2; i <=10; i+=4)
GOTOXY(30, i+1+4+2), std::cout <<ch[10];
}
/****************************************************************************
* *
* Function: writeNumbersToScreen *
* *
* Purpose : updates the screen pn playermoves and computer choices *
* *
* History : Date Reason *
* --/--/-- Created *
* *
****************************************************************************/
void writeNumbersToScreen(int first_x_pos, int first_y_pos,int nums[][4],
size_t sleep_time, size_t size){//16 ie 4X4
int index = 0;
for(int i= 0; i < 4; ++i)
for(int j = 0; j < 4; j++){
TEXTCOLOR((index%15) + 1);
GOTOXY(first_x_pos + j*7, first_y_pos + i*4);
if(nums[i][j])
std::cout <<std::setw(2)<<std::left<< nums[i][j];
else
std::cout << " ";
Sleep(sleep_time);
++index;
}
}
void writeGameInfoToScreen(){
TEXTCOLOR(AQUA);
GOTOXY(36, 6), std::cout << "COMMANDS:";
GOTOXY(39, 8),std::cout<<"[N] = Start A New Game";
GOTOXY(39, 9),std::cout<<"[E] = Exit";
GOTOXY(39, 10),std::cout<<"[Q] = Save and Exit" "(Currently Unavailable)";
GOTOXY(39, 11),std::cout<<"[G] = Solve current Puzzle";
GOTOXY(39, 12),std::cout<<"[?] = Help(Not yet Available)";
//a reset to cancel all users moves
GOTOXY(36, 14),std::cout <<"NAVIGATION";
GOTOXY(39, 16),std::cout<<"[\x1E] or [W] = UP (Move next box in the same column)";
GOTOXY(39, 17),std::cout<<"[\x11] or [A] = LEFT (Move previous box in the same row)" ;
GOTOXY(39, 18),std::cout<<"[\x10] or [D] = RIGHT(Move next box in the same row)";
GOTOXY(39, 19),std::cout<<"[\x1F] or [S] = DOWN (Move previous box in the same column)";
}
//function modifies the allmoves variable by appending a random move
//and adjusting the value in the array of numbers accordingly
//also blits and updates the screen
void generateRandomMoves(int nums[][4]){
getch();
//seeding the random number generator
srand(time(NULL));
int nValidMoves = 0;
while(nValidMoves < RANDOMIZE_MOVES){
//get a random move
int move = MOVES[rand()%4];
if(isValidMove(move, nums)){
allMoves.push_back(move);
updateScreen(move, nums);
++nValidMoves;
//wait for effect
Sleep(100);
}
}
}
//used by generateRandomMoves(int nums[][4]) to validate a move before appending and
//in the game to validate a players move
bool isValidMove(int move, int nums[][4]){
//first find the null position in the array
//MUST be found or programme is brokken
//no need to validate that here .....
//4X4 is assumed the array size
int dx = -1, dy = -1;//weird values are for debugging
for(int i = 0; i < 4; ++i )
for(int j = 0; j < 4; ++j)
if(not nums[i][j]){//if 0 spot found set dx and dy
dx = i;
dy = j;
}
//there are 4 scenarios that can invalidate a move
bool
//moving up when the upper bound is already reached
movingUpCondition((move==UP or move==UPx or move==UPxl)and (dx == 0)),
//moving down when the lower bound is already reached
movingDownCondition((move==DOWN or move==DOWNx or move==DOWNxl) and (dx == 3)),//remember runs from 0 to 3
//moving left when the left bound is already reached
movingLeftCondition((move==LEFT or move==LEFTx or move==LEFTxl)and (dy == 0)),
//moving right when the right bound is already reached
movingRightCondition((move==RIGHT or move==RIGHTx or move==RIGHTxl) and (dy == 3));
//any of the above scenarios true?
//the move is invalid.....
if(movingUpCondition or movingDownCondition
or movingLeftCondition or movingRightCondition)
return false;
return true;
}
//function modifies the array variable by swapping values in response to a
// valid move (randomly or by the player)
//used exclusively(as of now) by generateRandomMoves(int nums[][4]){
void updateArrayValues(int move, int nums[][4]){
//get the null position(ie containing the 0)
//NOTE THAT THE MOVE IS VALIDATED BEFORE BEING PASSED TO THIS FUNCTION
//no need to validate that here ...
//4X4 is assumed the array size
//these positions are defined for the null value(0 in this case)
int curXPos = -1, curYPos = -1, newXPos = -1, newYPos = -1;
//all indices are negative for the sake of debugging
for(int i = 0; i < 4; ++i )
for(int j = 0; j < 4; ++j)
if(nums[i][j]==0){//if 0 spot found set dx and dy
curXPos = i;
curYPos = j;
}
//switch statement to handle all cases
//note that the default is not necesrry since the move can only be one of the the
//4 in the global MOVES[] array
if(isValidMove(move, nums)){
//parsing navigational entries
//commands are parsed in the rungame function
switch(move) {
case UP:
case UPx:
case UPxl:
//set new positions
//the column coordinate remains the same
newYPos = curYPos;//moving toward the top ie towards 0
newXPos = curXPos - 1;
break;
case DOWN:
case DOWNx:
case DOWNxl:
//set new positions
//the column coordinate remains the same
newYPos = curYPos;//moving toward the bottom ie towards 3
newXPos = curXPos + 1;
break;
case LEFT:
case LEFTx:
case LEFTxl:
//set new positions
//the row coordinate is invariant
newXPos = curXPos;//moving toward the left ie towards 0
newYPos = curYPos - 1;
break;
case RIGHT:
case RIGHTx:
case RIGHTxl:
//set new positions
//the y coordinate is invariant
newXPos = curXPos;//moving toward the right ie towards 3
newYPos = curYPos + 1;
break;
default:
/*do nothing actually????@@@*/
;
}
nums[curXPos][curYPos] = nums[newXPos][newYPos];
nums[newXPos][newYPos] = 0;
}
}
void updateScreen(int move, int nums[][4]){
updateArrayValues(move, nums);
writeNumbersToScreen(5, 7 ,nums, 0);
}
void displayInstructions(){
clear_screen();
printf("*************************************************\n");
printf("** CLI SLIDE PUZZLE ~ Help **\n");
printf("*************************************************\n");
printf("* -Start a new puzzle by giving the command *\n");
printf("* -Press any key to randomise the puzzle *\n");
printf("* -Follow the navigation steps to solve *\n");
printf("* -Use the instruction guide for any command *\n");
printf("*************************************************\n");
printf("*************************************************\n");
}