-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
392 lines (333 loc) · 8.33 KB
/
Copy pathmain.cpp
File metadata and controls
392 lines (333 loc) · 8.33 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
/**
* Final Project
* author: Pratinav Bagla
*
* main.cpp
*
* main.h and main.cpp build an interface and other features such as
* a scoring system and a menu around the main board class
*/
#include "main.h"
const int NUM_MENU_COMMANDS = 6;
const string MENU_COMMANDS[NUM_MENU_COMMANDS] = { "play", "load", "scores", "quit", "q", "help" };
const int NUM_GAME_COMMANDS = 14;
const string GAME_COMMANDS[] = { "up", "u", "down", "d", "left", "l", "right", "r", "save", "s", "quit", "q", "reset", "help"};
vector<int> scores;
ofstream scoreFile;
int main()
{
string input;
bool flag;
int index;
loadScores();
// open scoreFile in append mode for writing scores
scoreFile.open("scores.txt", ofstream::app);
if (scoreFile.fail())
cout << "Could not open score file. Scores for this session will not be saved." << endl;
displayMenu();
while (true)
{
flag = false;
do
{
if (flag)
cout << "Invalid command!" << endl;
else
flag = true;
cout << "\n> ";
cin >> input;
index = getIndex(input, MENU_COMMANDS, NUM_MENU_COMMANDS);
} while (index == -1);
switch (index)
{
case 0: // play
play();
break;
case 1: // load
loadGame();
break;
case 2: // scores
displayScores();
break;
case 3: // quit, q
case 4:
return 0;
case 5: // help
cout << endl;
displayMenu();
break;
default: // empty default just in case
;
}
}
return 0;
}
/**
* plays with a new board
*/
void play()
{
board *b = new board;
if (!b)
{
cout << "Error allocating memory for board" << endl;
return;
}
play(b);
}
/**
* provides an interface to play with the passed in board
*/
void play(board *b)
{
string input;
bool flag;
do
{
// display board and score
cout << endl << *b;
// get move from user
int index;
flag = false;
do
{
if (flag)
cout << "Invalid command!" << endl;
else
flag = true;
cout << "> ";
cin >> input;
index = getIndex(input, GAME_COMMANDS, NUM_GAME_COMMANDS);
} while (index == -1);
flag = false;
switch (index)
{
case 0: // up, u
case 1:
flag = b -> moveUp();
break;
case 2: // down, d
case 3:
flag = b -> moveDown();
break;
case 4: // left, l
case 5:
flag = b -> moveLeft();
break;
case 6: // right, r
case 7:
flag = b -> moveRight();
break;
case 8: // save, s
case 9:
saveGame(b);
break;
case 10: // quit, q
case 11:
// ask the user if they want to save the game before quitting
cout << "Save game? (y/n): ";
cin >> input;
// if there was an error saving the game, confirm if the user
// still wants to quit
if (input == "y" && !saveGame(b))
{
cout << "Continue to quit? (y/n): ";
cin >> input;
if (input != "y")
break;
}
return;
case 12: // reset
b -> reset();
break;
case 13: // help
cout << endl;
displayMenu();
break;
default:
; // empty default just in case
}
if (flag) // if any tiles were moved
b -> populate(); // populate a new spot
} while (!b -> isGameOver()); // exit once game is over
// print out the final score and baord
cout << endl << *b << "Game Over!" << endl;
// tell the user if it was a new highscore
if (scores.size() > 0 && scores.back() < b -> getScore())
cout << "New highscore!" << endl;
// add the score to the scores vector, also display the current highscore
if (b -> getScore() > 0)
pushNewScore(b -> getScore());
cout << "Highscore: " << scores.back() << endl;
delete b;
}
/**
* load scores from scores.txt if it exists
*/
void loadScores()
{
ifstream file("scores.txt");
if (file.fail())
{
cout << "Could not load scores" << endl;
return;
}
string line;
while (getline(file, line))
if (line != "") // in case a stray empty line is left out
scores.push_back(stoi(line));
bubbleSort(scores); // sort the scores vector
}
/**
* Saves the current game
*/
bool saveGame(board *b)
{
// get save file path from user
string path;
cin.ignore();
cout << "Enter save file name/path (without extension): "; // a .txt extension will be added regardless
getline(cin, path);
path += ".txt";
// if the save was successful, tell the user and return true
if (b -> save(path))
{
cout << "The game was saved at " << path << endl;
return true;
}
// else return false
return false;
}
/**
* loads pre-existing game
*/
void loadGame()
{
// get save file path from user
string path;
cin.ignore();
cout << "Enter save file path (with extension): ";
getline(cin, path);
// allocate a new board, loading it from the given path
board *b = new board(path);
if (!b)
{
cout << "Error allocating memory for board" << endl;
return;
}
// if the board was loaded successfully, play it
if (!b -> fail())
play(b);
}
/**
* bubble sorts the vector<int> passed to it by reference
*/
void bubbleSort(vector<int> &v)
{
// if the vector has less than 2 ints, we don't need to sort it
if (v.size() < 2)
return;
int *end = v.data() + v.size() - 1, temp;
bool flag = false;
do
{
flag = false;
for (int *x = v.data(); x < end; x++)
{
// if x > (x + 1), swap them
if (*x > *(x + 1))
{
flag = true;
temp = *(x + 1);
*(x + 1) = *x;
*x = temp;
}
}
end--; // keep deducting one from the end as we bubble the largest int up
} while (flag); // if there are no swaps in the entire iteration, it must be sorted
}
/**
* pushes new score to vector in the correct index
*/
void pushNewScore(int newScore)
{
if (newScore <= 0)
return;
// write score to score file, if it's open
if (scoreFile.is_open())
scoreFile << newScore << endl;
// if the scores vector is empty, or the new score being pushed is greater than
// or equal to the last score in the vector, insert it at the end
int lastIndex = scores.size() - 1;
if (lastIndex == -1 || newScore >= scores.back())
{
scores.push_back(newScore);
return;
}
// if newScore is smaller or equal to the smallest score, insert the newscore
// before it
if (newScore <= scores.front())
{
scores.insert(scores.begin(), newScore);
return;
}
// else, find the right index to insert the new score into
int index = 0;
for ( ; index < lastIndex; index++)
{
if (newScore >= scores[index] && newScore < scores[index + 1])
break;
}
// insert score at the index
scores.insert(scores.begin() + index + 1, newScore);
}
/**
* displays menu/help prompt
*/
void displayMenu()
{
cout << "2048\n\n"
<< "Enter 'play' to start a new game\n"
<< "Enter 'load' to load a game\n"
<< "Enter 'scores' to view the score table\n"
<< "Enter 'quit' or 'q' to quit the program\n"
<< "Enter 'help' to print this again\n\n"
<< "How to play:\n"
<< "Enter 'up' or 'u' to move the tiles up\n"
<< "Enter 'down' or 'd' to move the tiles down\n"
<< "Enter 'left' or 'l' to move the tiles left\n"
<< "Enter 'right' or 'r' to move the tiles right\n\n"
<< "When two matching tiles touch, they merge!\n"
<< "Try to get the highest score possible\n\n"
<< "Enter 'save' or 's' to save the game\n"
<< "Enter 'quit' or 'q' to quit to the menu\n"
<< "Enter 'reset' to reset the game\n";
}
/**
* displays score table
*/
void displayScores()
{
int len = scores.size();
if (len == 0) // if there are no scores in the vector, don't display any
{
cout << "No scores yet" << endl;
return;
}
cout << endl << "Score table" << endl;
// iterate through the vector of scores and print them out in descending order
for (int *begin = scores.data(), *x = begin + len - 1, c = 1; x >= begin; x--, c++)
cout << c << ". " << *x << endl;
}
/**
* gets index of string s from array of possible moves
*/
int getIndex(string s, const string ARR[], const int ARR_LEN)
{
for (int x = 0; x < ARR_LEN; x++)
{
if (s == ARR[x])
return x;
}
return -1; // if s was not found in ARR, return -1
}