-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleMode.cpp
More file actions
339 lines (291 loc) · 7.81 KB
/
Copy pathSimpleMode.cpp
File metadata and controls
339 lines (291 loc) · 7.81 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
#include "SimpleMode.h"
#include "Strategy.h"
#include "RandomStrategy.h"
#include "SmartStrategy.h"
#include "MediocreStrategy.h"
//-----------------------------------------------------------------------------------------------//
void SimpleMode::setSpeed(int _speed)
{
if (_speed == SLOW)
{
_speed = 100;
}
else if (_speed == REGULAR)
{
_speed = 75;
}
else if (_speed == FAST)
{
_speed = 50;
}
RunMode::setSpeed(_speed);
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::init(bool start_screen, bool start_game)
{
if (start_game)
{
screensFiles.clear();
if (isSpecificScreen()) // the user choose to play with specific screen
{
string fileName = UI.getFileName();
if (isValidFile(fileName))
{
screensFiles.push_back(fileName);
}
}
}
RunMode::init(start_screen, start_game);
strategy->setCountSteps(0); // initialize countSteps to zero
}
//-----------------------------------------------------------------------------------------------//
int SimpleMode::finishGame()
{
fruit.eraseFromBoard();
int res = RunMode::finishGame();
if (res == LOST)
{
UI.printFinishGame("LOST", player.getScore(), getColor(), input);
}
else if (res == WON)
{
printGame(true);
UI.printFinishGame("WON", player.getScore(), getColor(), input);
}
else // res == NEXT_SCREEN
{
UI.printFinishGame("Great Job, See you in the next one", player.getScore(), getColor(), input);
}
return res;
}
//-----------------------------------------------------------------------------------------------//
bool SimpleMode::startGame()
{
bool res = RunMode::startGame();
if (res)
{
UI.startScreen();
board.printBoard(getColor());
}
return res;
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::Game()
{
char res;
hideCursor();
do {
res = MainMenu();
} while (res != EXIT);
}
//-----------------------------------------------------------------------------------------------//
char SimpleMode::MainMenu()
{
bool validChoice = true;
char UserChoice;
do {
hideCursor();
UI.printMenu(getColor());
UserChoice = _getch();
if (UserChoice == RUN)
{
char res = UI.gameMode(getColor());
if (res == '1')
{
setSpecificScreen(false);
}
else if (res == '2')
{
setSpecificScreen(true);
}
else // return to main menu
{
return UserChoice;
}
clear_screen();
try { init(false, true); run(); } // first init for read all the files in the game
catch (const char* msg)
{
cout << msg << endl;
cout << "Press any key to return to the main menu...";
_getch();
clear_screen();
}
}
else if (UserChoice == COLORS)
{
setColor(UI.gameColor(getColor()));
}
else if (UserChoice == LEVEL)
{
setLevel(UI.levelGame(getColor()));
}
else if (UserChoice == SPEED)
{
setSpeed(UI.speedGame(getColor()));
}
else if (UserChoice == INSTRUCTIONS)
{
UI.showInstructions(getColor());
}
else if (UserChoice == EXIT)
{
return UserChoice;
}
else // pressed any other invalid Choice
{
gotoxy(8, 17);
cout << "Invalid choise! Please try again" << endl;
validChoice = false;
}
} while (!validChoice);
return UserChoice;
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::printGame(bool collapsed) const
{
if (getColor())
{
setTextColor(Colors::WHITE);
}
// prints the cells back after characters cross them
for (unsigned int i = 0; i < ghosts.size(); i++)
{
board.printCell(ghosts[i].getLast().get_X(), ghosts[i].getLast().get_Y());
}
board.printCell(player.getLast().get_X(), player.getLast().get_Y());
if (getColor())
{
setTextColor(Colors::WHITE);
}
//print Data
gotoxy(board.getLegend_x(), board.getLegend_y());
cout << " Score: " << player.getScore() << endl;
gotoxy(board.getLegend_x(), board.getLegend_y() + 1);
cout << " Lives left : " << player.getRemainLives();
if (fruit.getIserased()) // if the fruit just died print the last cell board
{
board.printCell(fruit.getLast().get_X(), fruit.getLast().get_Y());
board.printCell(fruit.getCurr().get_X(), fruit.getCurr().get_Y());
}
else if (fruit.isAppearOnBoard() == 1 && fruit.getLast() != NULL_POINT)// if there is a fruit on board
{
board.printCell(fruit.getLast().get_X(), fruit.getLast().get_Y());
fruit.print(getColor());
}
if (!collapsed)
{
player.print(getColor());
}
for (unsigned int i = 0; i < ghosts.size(); i++)
{
ghosts[i].print(getColor());
}
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::moveGhosts()
{
if (!getHoldGhost())
{
strategy->strategyMove(board, ghosts, player.getCurr());
}
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::move()
{
printGame(false);
RunMode::move();
}
//-----------------------------------------------------------------------------------------------//
bool SimpleMode::startFruit(char& value, Point& initP)
{
if (fruit.getStart()) // if there is no fruit on board and no initalization
{
fruit.setWaitRand(rand() % 40); // turns to wait untill the fruit shows
fruit.setLength(rand() % 220 + 150); // how long the next fruit is going to show
fruit.setStart(false);
}
if (fruit.getwaitCounter() >= fruit.getWaitRand())
{
int index = rand() % (board.getOptionalIndex()); //get a random index to choose from the points of bread
initP = board.getAPointForFruit(index);
value = ('0' + (rand() % 5) + 5);
return true;
}
return false;;
}
//-----------------------------------------------------------------------------------------------//
bool SimpleMode::eraseFruit()
{
if (fruit.getRemainingSteps() <= 0 || fruit.getIserased())
{
return true;
}
else
{
return false;
}
}
//-----------------------------------------------------------------------------------------------//
const Point& SimpleMode::getFruitNextStep()
{
fruit.setDirection(rand() % 4);
return (fruit.getDirection() + fruit.getCurr());
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::setLevel(int level)
{
delete strategy;
if (level == NOVICE)
{
strategy = new RandomStrategy(20);
}
else if (level == GOOD)
{
strategy = new MediocreStrategy(5);
}
else if (level == BEST)
{
strategy = new SmartStrategy;
}
if (!strategy)
{
throw "Error to allocate new strategy!";
}
}
//-----------------------------------------------------------------------------------------------//
void SimpleMode::getInputs()
{
if (_kbhit())
{
setKey(_getch());
if (getKey() == ESC)
{
UI.pasue(board.getLegend_x(), board.getLegend_y(), getColor());
}
else if (getKey() == GET_OUT || getKey() == GET_OUT + 32)
{
int res = UI.want_To_Exit(board.getLegend_x(), board.getLegend_y(), getColor());
if (res == 1)
{
clear_screen();
throw "You get out from the game!";
}
}
}
}
//-----------------------------------------------------------------------------------------------//
bool SimpleMode::has_Collision()
{
bool res = RunMode::has_Collision();
if (res)
{
printGame(true);
if (player.getRemainLives() > 1) // if the players has more lives
{
UI.lost_Announcement(player.getRemainLives() - 1, board.getLegend_x(), board.getLegend_y(), getColor());
}
}
return res;
}
//-----------------------------------------------------------------------------------------------//