-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveMod.cpp
More file actions
132 lines (106 loc) · 3.62 KB
/
Copy pathsaveMod.cpp
File metadata and controls
132 lines (106 loc) · 3.62 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
#include "saveMod.h"
//-----------------------------------------------------------------------------------------------//
void SaveMode::writeCreaturesSteps()
{
ofstream stepsFile(stepsName, std::ios_base::app);
stepsFile << "Pacman:" << getKey() << " ";
stepsFile.flush();
for (int i = 0; i < ghosts.size(); i++) // write steps for ghosts
{
stepsFile << "Ghost" << i << ":";
if (tempHoldGhost) // dont move the ghost
{
stepsFile << Creature::dirStr[Creature::STAY] << " ";
}
else
{
stepsFile << findDirection(ghosts[i]) << " ";
}
}
stepsFile << "Fruit:";
if (fruit.isAppearOnBoard() && fruit.getOnBoardCounter() == 0) // the fruit just appeard
{
stepsFile << "START|" << fruit.getRepresentiveCh() << "|" << "(" << fruit.getCurr().get_Y() << "," << fruit.getCurr().get_X() << ")";
}
else if (fruit.isAppearOnBoard()) // the fruit is alive
{
stepsFile << "ON|" << findDirection(fruit);
}
else // no any fruit on board
{
stepsFile << "OFF";
}
stepsFile << endl;
stepsFile.close();
}
//-----------------------------------------------------------------------------------------------//
const string SaveMode::findDirection(const Creature& creature) const
{
for (int j = 0; j < Creature::SIZE; j++)
{
if (creature.getLast() + Creature::directions[j] == creature.getCurr()) //check the next direction
{
if (Creature::dirStr[j] == "UP") // uniformity direction len word (len = 5)
{
return Creature::dirStr[j] + " ";
}
else if (Creature::dirStr[j] != "RIGHT")// uniformity direction len word (len = 5)
{
return Creature::dirStr[j] + " ";
}
else // direction == "RIGHT"
{
return Creature::dirStr[j];
}
}
}
throw TestFailed("Error: invalid direction!");
}
//-----------------------------------------------------------------------------------------------//
int SaveMode::finishGame()
{
ofstream resFile(resName, std::ios_base::app);
resFile << "The pacman finished the screen after: " << getCountSteps() << " steps" << endl;
resFile.close();
return SimpleMode::finishGame();
}
//-----------------------------------------------------------------------------------------------//
bool SaveMode::has_Collision()
{
if (SimpleMode::has_Collision())
{
ofstream resFile(resName, std::ios_base::app);
resFile << "The pacman died after: " << getCountSteps() << " steps" << endl;
resFile.close();
return true;
}
return false;
}
//-----------------------------------------------------------------------------------------------//
void SaveMode::move()
{
tempHoldGhost = getHoldGhost();
SimpleMode::move();
writeCreaturesSteps();
}
//-----------------------------------------------------------------------------------------------//
void SaveMode::init(bool start_screen, bool start_game)
{
if (start_screen)
{
resName = screensFiles[getIndexScreen()].substr(0, screensFiles[getIndexScreen()].size() - 6) + "result"; //create result file
stepsName = screensFiles[getIndexScreen()].substr(0, screensFiles[getIndexScreen()].size() - 6) + "steps"; //create steps file
ofstream resFile(resName);
ofstream stepsFile(stepsName);
if (!resFile.is_open())
{
if (!stepsFile.is_open())
{
throw TestFailed("Error: Could not opened result and steps file for " + screensFiles[getIndexScreen()]);
}
throw TestFailed("Error: Could not opened result file for " + screensFiles[getIndexScreen()]);
}
}
SimpleMode::init(start_screen, start_game);
}
//-----------------------------------------------------------------------------------------------//