-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
281 lines (262 loc) · 7.84 KB
/
Menu.cpp
File metadata and controls
281 lines (262 loc) · 7.84 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
#include "Menu.h"
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method prints the menu.
void Menu::printMenu()
{
printLineOf(45, '*');
cout << endl << "*";
printLineOf(19, ' ');
cout << "MENU";
printLineOf(20, ' ');
cout << "*" << endl;
cout << "*Please choose one of the following options:*" << endl;
cout << "*";
printLineOf(43, '_');
cout << "*" << endl;
cout << "*(0) Start a new game without colors *\n" <<
"*(1) Start a new game with colors *\n" <<
"*(8) Present instructions and keys *\n" <<
"*(9) EXIT *" << endl;
printLineOf(45, '*');
cout << endl;
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method manages the menu.
void Menu::startGame(int argc, char** argv)
{
bool save = false, load = false, silent = false, legal = true;
checkMode(argc, argv, save, load, silent, legal);
if (!legal)
return;
if (load)
{
ThePacmanGame playGame("", 'd', 0, save, load, silent);
system("cls");
playGame.init();
playGame.run(false);
}
else
{
printMenu();
string str;
string filesOption;
cin >> str;
system("cls");
while (str[0] != '9')
{
if ((str[0] == '1' || str[0] == '0') && str.size() == 1)
{
if (save)
filesOption = "0";
else
{
system("cls");
filesOptions();
cin >> filesOption;
}
while (filesOption[0] != '1' && filesOption[0] != '0' || str.size() != 1)
{
system("cls");
invalidChoice();
system("cls");
filesOptions();
cin >> filesOption;
}
playingChoice(str[0] - '0', filesOption[0] - '0', save, load, silent);
}
else if (str[0] == '8' && str.size() == 1)
{
rulesChioce();
}
else
{
system("cls");
invalidChoice();
}
system("cls");
printMenu();
cin >> str;
}
cout << "GoodBye my friend";
}
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method prints an error message.
void Menu::printProblem(char* path, bool& legal)
{
cout << "Usage: " << path << "[-save] [-load | -load -silent]" << endl;
cout << "You should enter a valid mode: -save, -load, -silent or nothing" << endl;
legal = false;
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method checks the mode of the game.
void Menu::checkMode(int argc, char** argv, bool& save, bool& load, bool& silent, bool& legal)
{
if (argc < 4)
{
if (argc == 3)
{
if (strcmp("-load", argv[1]) != 0)
printProblem(argv[0], legal);
else
{
load = true;
if (strcmp("-silent", argv[2]) != 0)
printProblem(argv[0], legal);
else
silent = true;
}
}
else if (argc == 2)
{
if (!strcmp("-load", argv[1]))
load = true;
else if (!strcmp("-save", argv[1]))
save = true;
else
printProblem(argv[0], legal);
}
}
else
{
printProblem(argv[0], legal);
return;
}
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
void Menu::invalidChoice()
{
cout << "Invalid choice" << endl;
Sleep(1000);
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method shows the rules and the arrow keys of the game.
void Menu::rulesChioce()
{
char delayer;
system("cls");
printRules();
cin >> delayer;
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method manages the game.
void Menu::playingChoice(const int& num, const int& filesOption, bool save, bool load, bool silent)
{
string level;
bool withColor, withSpecificFile, FileCouldNotOpend = false;
string str;
if (num == 1)
{
withColor = true;
}
else
withColor = false;
if (filesOption == 1)
{
gotoxy(0, 5);
cout << "Please enter a File's Name: " << endl;
cin>>str;
ifstream outFile(str);
if (!outFile)
{
system("cls");
cout << "File could not be opened!" << endl;
FileCouldNotOpend = true;
Sleep(1000);
}
else
{
outFile.close();
withSpecificFile = true;
}
}
else
{
str = "a";
withSpecificFile = false;
}
if (FileCouldNotOpend == false)
{
system("cls");
difficultyOptions();
cin >> level;
while (level[0] != 'a' && level[0] != 'b' && level[0] != 'c' || level .size() != 1)
{
system("cls");
invalidChoice();
system("cls");
difficultyOptions();
cin >> level;
}
ThePacmanGame playGame(str, level[0], withSpecificFile, save, load, silent);
system("cls");
playGame.setGameColor(withColor);
playGame.init();
playGame.run(withSpecificFile);
}
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method prints the rules and keys for the game.
void Menu::printRules()
{
printLineOf(63, '*');
cout << endl << "*";
printLineOf(28, ' ');
cout << "RULES";
printLineOf(28, ' ');
cout << "*" << endl;
cout << "*Your goal is to clear the maze by eating all the breadcrumbs.*" << endl;
cout << "*You start the game with 3 Pac-Man lives. *" << endl;
cout << "*When a ghost catchesthe Pac-Man, he loses a life. *" << endl;
cout << "*If the Pac-Man loses all his lives the game is over. *" << endl;
cout << "*";
printLineOf(61, '_');
cout << "*" << endl << "*";
printLineOf(28, ' ');
cout << "KEYS:";
printLineOf(28, ' ');
cout << "*" << endl;
cout << "*UP - w or W *" << endl;
cout << "*DOWN - x or X *" << endl;
cout << "*LEFT - a or A *" << endl;
cout << "*RIGHT - d or D *" << endl;
cout << "*STAY - s or S *" << endl;
cout << "*PAUSE - esc *" << endl;
printLineOf(20, '*');
cout << "Enter any key to return";
printLineOf(20, '*');
cout << endl;
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method prints the files options.
void Menu::filesOptions()
{
printLineOf(45, '*');
cout << endl;
cout << "*Please choose one of the following options:*" << endl;
cout << "*(0) Start from the first level *\n" <<
"*(1) Choose a specific file *" << endl;
printLineOf(45, '*');
cout << endl;
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method prints the difficulty options.
void Menu::difficultyOptions()
{
system("cls");
printLineOf(45, '*');
cout << endl;
cout << "*Please choose one of the following options:*" << endl;
cout << "*(a) BEST *\n" <<
"*(b) GOOD *\n" <<
"*(c) NOVICE *" << endl;
printLineOf(45, '*');
cout << endl;
}
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
//This method prints a line of the same shape.
void Menu::printLineOf(int amount, char shape)
{
for (int i = 0; i < amount; i++)
cout << shape;
}