-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
263 lines (223 loc) · 6.86 KB
/
main.cpp
File metadata and controls
263 lines (223 loc) · 6.86 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
// Snake game implemented in c++
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <winuser.h>
using namespace std;
class SnakeGame {
bool left = true;
bool right = false;
bool up = false;
bool down = false;
// 1 = left, 2 = right, 3 = up, 4 = down
int prevDir = 1;
bool gameFinished = false;
// height and width of board
static const int height = 15;
static const int width = 35;
// Snake positions (X, Y) held in 2 vectors. The head of the snake (O) is always the 1st item in the array
vector<int> tailX{12, 12};
vector<int> tailY{5, 6};
int initialFruitPos[2] = {11, 4};
string game_board[height][width];
// Create the initial board (including the borders around it)
void setInitialBoard() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// Make the border or set tile to space
if (i == 0 || i + 1 == height) {
game_board[i][j] = "#";
} else if (j == 0 || j + 1 == width) {
game_board[i][j] = "#";
} else {
game_board[i][j] = " ";
}
}
}
}
// Change the board array to have O/o where the snake is
void paintSnake() {
for (int i = 0; i < tailX.size(); i++) {
int posY = tailY[i];
int posX = tailX[i];
if (i == 0) {
game_board[posY][posX] = "O";
} else {
game_board[posY][posX] = "o";
}
}
}
// Change the board array to have * where the fruit is
void paintFruit(const int posArr[]) {
int posX = posArr[1];
int posY = posArr[0];
game_board[posY][posX] = "*";
}
// Print out the whole board
void printBoard() {
for (auto & i : game_board) {
for (auto & j : i) {
cout << j << ends;
}
cout << "" << endl;
}
}
int getFutureXHead() {
int futureXHead;
// dir can only be UP, RIGHT, LEFT, or DOWN
if(up){
futureXHead = tailX.at(0);
}
else if(right){
futureXHead = tailX.at(0) + 1;
}
else if(left){
futureXHead = tailX.at(0) - 1;
}
else if (down){
futureXHead = tailX.at(0);
}
return futureXHead;
}
int getFutureYHead(){
int futureYHead;
// dir can only be UP, RIGHT, LEFT, or DOWN
if(up){
futureYHead = tailY.at(0) - 1;
}
else if(right) {
futureYHead = tailY.at(0);
}
else if(left){
futureYHead = tailY.at(0);
}
else if (down){
futureYHead = tailY.at(0) + 1;
}
return futureYHead;
}
void checkGame() {
// By this point the user has chosen a direction (dir) to move
// We need to:
// ☑ See if the move is valid
// - Did the user move into an empty space
// - Did the user move into themselves
// - Did the user move into a fruit
// - Did the user move into a wall
//
// ☑ Move the snake, remove the last tail (or not)
bool ateFruit = false;
int futureXHead = getFutureXHead();
int futureYHead = getFutureYHead();
// if(right){
// cout << "RIGHT" << endl;
// }
// if(left){
// cout << "LEFT" << endl;
// }
// if(down) {
// cout << "DOWN" << endl;
// }
// if(up){
// cout << "UP" << endl;
// }
// Now we will do checks on whether the move is valid and set the previous head to a lowercase o
if(game_board[futureYHead][futureXHead] == " "){
game_board[futureYHead][futureXHead] = "O";
int prevYHead = tailY.at(0);
int prevXHead = tailX.at(0);
game_board[prevYHead][prevXHead] = "o";
}
else if(game_board[futureYHead][futureXHead] == "o"){
gameFinished = true;
}
else if(game_board[futureYHead][futureXHead] == "#"){
gameFinished = true;
}
else if(game_board[futureYHead][futureXHead] == "*"){
ateFruit = true;
game_board[futureYHead][futureXHead] = "O";
}
// Now add the tail to the vectors
tailX.insert(tailX.begin() + 0, futureXHead);
tailY.insert(tailY.begin() + 0, futureYHead);
// If we did not eat the fruit we need to remove the last piece of the tail
if(!ateFruit){
int endTailX = tailX.back();
int endTailY = tailY.back();
game_board[endTailY][endTailX] = " ";
game_board[tailY.back()][tailX.back()] = " ";
tailX.pop_back();
tailY.pop_back();
}
if(ateFruit){
bool done = false;
while(!done){
int randY = (rand() % height - 1) + 1;
int randX = (rand() % width - 1) + 1;
if(game_board[randY][randX] == " "){
game_board[randY][randX] = "*";
done = true;
}
}
}
}
void getInput() {
if(GetAsyncKeyState(VK_UP) && !down) {
// cout << "UP" << endl;
down = false;
right = false;
left = false;
up = true;
}
if(GetAsyncKeyState(VK_DOWN) && !up) {
// cout << "DOWN" << endl;
down = true;
right = false;
left = false;
up = false;
}
if(GetAsyncKeyState(VK_LEFT) && !right) {
// cout << "LEFT" << endl;
left = true;
right = false;
down = false;
up = false;
}
if(GetAsyncKeyState(VK_RIGHT) && !left) {
// cout << "RIGHT" << endl;
right = true;
left = false;
down = false;
up = false;
}
}
public:
void startGame() {
cout << "Starting Game" << endl;
Sleep(1000);
setInitialBoard();
paintSnake();
paintFruit(initialFruitPos);
while (!gameFinished) {
// print board --> sleep (so the user has time to react) --> get input --> check game w/ input --> clear
// the screen and immediately start over again by printing board...
printBoard();
Sleep(100); // Time in between moves
getInput();
checkGame();
system("cls");
}
// User lost, allow them to exit
cout << "" << endl;
cout << "You lost!" << endl;
Sleep(2000);
exit(0);
}
};
int main() {
SnakeGame gameObj{};
gameObj.startGame();
return 0;
}