-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
194 lines (153 loc) · 3.98 KB
/
game.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
#include "game.h"
#include <cassert>
#include <cstdlib> /* srand, rand */
#include <ctime> /* time */
Game::Game(int widthOfGrid, int heightOfGrid, DataFile& file) :
mWidthOfGrid(widthOfGrid),
mHeightOfGrid(heightOfGrid),
mFile(file)
{
// You must have at least a 2x2 grid
assert(mWidthOfGrid >= 2);
assert(mHeightOfGrid >= 2);
/* initialize random seed: */
srand(static_cast<unsigned int>(time(nullptr)));
// grid will alway be a square for now
int totalSize{ mWidthOfGrid * mHeightOfGrid };
// Head of the snake will be in the center of the grid
mSnake.push_back(totalSize / 2);
}
void
Game::Reset()
{
mScore = 0;
mFood.clear();
int totalSize{ mWidthOfGrid * mHeightOfGrid };
mSnake.delete_snake();
mSnake.push_back(totalSize / 2);
mGameState = GameState::RUN;
mNumOfGamesPlayed++;
if (not IsReplayMode())
{
mFile.WriteHeader(mPlayer, mWidthOfGrid, mHeightOfGrid);
//mFile.WriteSnakePos(GetSnakeHead());
}
}
bool
Game::Collision(int newIndexOfHead, bool foodCaller)
{
bool collided{ mSnake.collision(newIndexOfHead) };
if(not foodCaller and collided)
mGameState = GameState::STOP; //breaks game when unpausing
return collided;
}
std::optional<FoodType>
Game::UpdateSnake(int newIndexOfHead)
{
std::optional<FoodType> ateFood{};
// This need to be a fail condition
if(Collision(newIndexOfHead))
return ateFood;
// this moves snake forward
mSnake.push_front(newIndexOfHead);
// Snake eats food
for(auto& f : mFood)
{
if (newIndexOfHead == f.mIndex)
{
ateFood = std::optional<FoodType>{ f.mType };
// if the snake ate some food then increase
// increase points
mScore += f.GetPoints();
// update the food that was eaten to what ever the last food
// in mFood then pop the back
auto lastElement = --(mFood.end());
f.mIndex = lastElement->mIndex;
f.mType = lastElement->mType;
mFood.pop_back();
break;
}
}
if(not ateFood)
mSnake.pop_back();
if(not IsReplayMode())
mFile.WriteSnakePos(GetSnakeHead());
return ateFood;
}
bool
Game::UpdateFood(int newFoodIndex, FoodType foodtype)
{
if (newFoodIndex == -1)
{
// only let there be at most 4 pieces of food on
// the screen
if(mFood.size() > 4)
return false;
// need better solution but temp fix to stop the fruit on slaut
if (rand() % mWidthOfGrid != 0)
return false;
int pos{ rand() % (mWidthOfGrid * mHeightOfGrid) };
if ( Collision(pos, true) )
return false;
for (auto f : mFood)
{
if (f.mIndex == pos)
return false;
}
FoodType fT = static_cast<FoodType>(rand() % FoodType::NUMOFFOOD);
mFood.push_back(Food{ pos, fT });
if (not IsReplayMode())
mFile.WriteFoodPos(pos, fT);
}
else
{
if ( Collision(newFoodIndex, true) )
return false;
for (auto f : mFood)
{
if (f.mIndex == newFoodIndex)
return false;
}
// TODO: encode food type
mFood.push_back( Food{ newFoodIndex, foodtype } );
}
return true;
}
ReplayMode::ReplayMode(DataFile& file) :
Game(10,10,file) // dummy constructor
{
mGameState = GameState::RUN;
}
GameInput
ReplayMode::GetNextInput()
{
std::string move{};
char type, foodType{'-'};
if(mPlayerInputIndex >= mPlayerInput.size())
return { 'e', '-', -1 };
else if (mPlayerInput[mPlayerInputIndex] == 's')
{
type = 's';
mPlayerInputIndex++;
}
else if (mPlayerInput[mPlayerInputIndex] == 'f')
{
type = 'f';
mPlayerInputIndex++;
foodType = mPlayerInput[mPlayerInputIndex];
mPlayerInputIndex++;
}
else
{
return { 'e', '-', -1 };
}
while ( mPlayerInputIndex < mPlayerInput.size() and
mPlayerInput[mPlayerInputIndex] != 's' and
mPlayerInput[mPlayerInputIndex] != 'f' and
mPlayerInput[mPlayerInputIndex] != 'e' )
{
move += mPlayerInput[mPlayerInputIndex];
mPlayerInputIndex++;
}
return { type, foodType, std::stoi(move) };
}