-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunMode.cpp
291 lines (243 loc) · 6.38 KB
/
RunMode.cpp
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
#include "RunMode.h"
//-----------------------------------------------------------------------------------------------//
bool RunMode::isValidFile(const string& fileName)
{
ifstream file(fileName);
string extension = ".screen";
if (!file.is_open())
{
return false;
}
size_t ind = fileName.size() - extension.size();
for (unsigned int i = 0; i < extension.size(); i++)
{
if (ind >= fileName.size() || fileName[ind] != extension[i])
{
return false;
}
ind++;
}
return true;
}
//-----------------------------------------------------------------------------------------------//
void RunMode::run()
{
if (!startGame()) // couldnt open the screen
{
if ((++indexScreen) == screensFiles.size()) // no more screens to run
{
cout << "\nThere is no more screens to run!" << endl << "Press any key to return to the main menu...";
_getch();
return;
}
else
{
cout << "\nPress any key to continue to the next screen...";
_getch();
run();
return;
}
}
if (has_Collision())
{
throw ExceptionBoard("Error: cannot play the game, the pacman and on of the ghosts is in the same place", screensFiles[indexScreen]);
}
do {
Sleep(speed);
getInputs();
move(); // move characters
FruitCollision();
// the pacman collapse with one of the ghosts
if (has_Collision())
{
init(false, false); //init board without breadcrunbs
player.lostLife();
}
} while (player.getRemainLives() > 0 && board.getTotalScore() > 0);
if (finishGame() == NEXT_SCREEN)
{
run(); // run the next screen
return;
}
else
{
return; // no more screens to run
}
}
//-----------------------------------------------------------------------------------------------//
void RunMode::countScreens()
{
int invalidScreens = 0;
vector<string> inValidNames;
const fs::path pathToShow = fs::current_path();
for (const auto& entry : fs::directory_iterator(pathToShow))
{
auto filenameStr = entry.path().filename().string();
if (entry.is_regular_file())
{
if (filenameStr.find(".screen") != string::npos)
{
if (isValidFile(filenameStr))
{
screensFiles.push_back(static_cast<string>(filenameStr));
}
else
{
invalidScreens++;
inValidNames.push_back(filenameStr);
}
}
}
}
sort(screensFiles.begin(), screensFiles.end());
}
//-----------------------------------------------------------------------------------------------//
void RunMode::init(bool start_screen, bool start_game)
{
if (start_game)
{
if(!specificScreen) // receiving screens from the working directory
{
countScreens();
}
if (screensFiles.size() == 0)
{
clear_screen();
throw "There is no any valid File found!";
}
else
{
indexScreen = 0;
}
}
if (start_screen)
{
clear_screen();
board.initBoard(screensFiles[indexScreen]);
ghosts.clear();
ghosts.resize(board.getGhostCounter());
//set init point for all the creaturs
for (int i = 0; i < board.getGhostCounter(); i++)
{
ghosts[i].setInitPoint(board.getLocation(i + 1));
}
player.setInitPoint(board.getLocation(0));
fruit.setStart(true);
key = 's';
countSteps = 0;
}
// init only characters
for (unsigned int i = 0; i < ghosts.size(); i++)
{
ghosts[i].init(start_screen);
}
player.init(start_screen, start_game);
holdGhost = true;
}
//-----------------------------------------------------------------------------------------------//
void RunMode::FruitCollision()
{
for (unsigned int i = 0; i < ghosts.size(); i++) // check if the ghost ate the fruit
{
if (fruit.getCurr() == ghosts[i].getCurr() || fruit.getLast() == ghosts[i].getCurr() && fruit.getCurr() == ghosts[i].getLast())
{
fruit.eraseFromBoard(); // the fruit should get erased
return;
}
}
if (fruit.getCurr() == player.getCurr() || fruit.getLast() == player.getCurr() && fruit.getCurr() == player.getLast()) // check if the pacman ate the fruit
{
int res = fruit.getRepresentiveCh() - '0';
player.setScore(player.getScore() + res);
fruit.eraseFromBoard();
}
}
//-----------------------------------------------------------------------------------------------//
bool RunMode::has_Collision()
{
for (unsigned int i = 0; i < ghosts.size(); i++)
{
if (player.getCurr() == ghosts[i].getCurr())
return true;
else if (player.getLast() == ghosts[i].getCurr() && player.getCurr() == ghosts[i].getLast()) // pacman and ghosts switched places
return true;
}
return false;
}
//-----------------------------------------------------------------------------------------------//
bool RunMode::startGame()
{
try
{
init(true, false);
return true;
}
catch (const ExceptionBoard& message)
{
message.printExceptions();
return false;
}
}
//-----------------------------------------------------------------------------------------------//
int RunMode::finishGame()
{
if (player.getRemainLives() == 0) // player LOST
{
return LOST; //player lost, the game is finished
}
else if (!specificScreen && (++indexScreen < screensFiles.size())) // run the next screen
{
return NEXT_SCREEN; // didnt finished
}
else
{
return WON; // player won, but no more screens to run
}
}
//-----------------------------------------------------------------------------------------------//
void RunMode::moveFruit()
{
if (!fruit.isAppearOnBoard()) // the fruit is not alive
{
Point initP;
char value;
if(startFruit(value, initP))
{
fruit.init(value, initP);
}
else
{
fruit.setWaitCounter(fruit.getwaitCounter() + 1);
}
}
else //the fruit is alive
{
if (eraseFruit())
{
fruit.eraseFromBoard();
}
else
{
Point nextStep = getFruitNextStep();
fruit.move(board, nextStep);
}
}
}
//-----------------------------------------------------------------------------------------------//
void RunMode::move()
{
if (!holdGhost) //move all creatures
{
moveGhosts();
holdGhost = true; // next time ghost will not move
}
else // move only pacman
{
holdGhost = false; // next time ghost will move
}
moveFruit();
player.setDirection(key); // move player
player.move(board);
countSteps++;
}
//-----------------------------------------------------------------------------------------------//