-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
335 lines (296 loc) · 8.4 KB
/
main.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
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
#include <iostream>
#include <conio.h>
#include <time.h>
#include <process.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
// dimensions
#define MAX 100
#define WIDTH 77
#define HEIGHT 22
#define INIT_SNAKE_LENGTH 4
#define FOOD 1
// direction
#define RIGHT 0
#define UP 1
#define LEFT 2
#define DOWN 3
// places off the permit
#define EXIT -1
#define WALL -2
#define SNAKE -1
#define NOTHING 0
static int dx[5] = {1, 0, -1, 0};
static int dy[5] = {0, -1, 0, 1};
// direction array ^^
//(1, 0) -> rigth cord
//(0, -1) -> up cord
//(-1, 0) -> left cord
//(0, 1) -> down cord
int input = RIGHT; // user's input
int item = NOTHING;
void gotoxy(int column, int row)
{
HANDLE outputSt;
COORD cord;
outputSt = GetStdHandle(STD_OUTPUT_HANDLE);
if (outputSt == INVALID_HANDLE_VALUE)
return;
cord.X = column;
cord.Y = row;
SetConsoleCursorPosition(outputSt, cord);
}
void clearScreen()
{
HANDLE outputSt;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = {0, 0};
outputSt = GetStdHandle(STD_OUTPUT_HANDLE);
if (outputSt == INVALID_HANDLE_VALUE)
return;
/* Get the number of cells and cell attributes in the current buffer */
if (!GetConsoleScreenBufferInfo(outputSt, &csbi))
return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
outputSt, // handle
(TCHAR)' ', // spaces to the buffer
cellCount, // number of cells
homeCoords, // first cell
&count // number of characters
))
return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
outputSt, // handle to console screen buffer
csbi.wAttributes, // Character attributes to use
cellCount, // Number of cells to set attribute
homeCoords, // Coordinate of first cell
&count // receives the number of characters written
))
return;
/* Move the cursor home */
SetConsoleCursorPosition(outputSt, homeCoords);
}
// check if 2 directions are opposite
int oppositeDirection(int input1, int input2)
{
if (input1 == LEFT && input2 == RIGHT)
return 1;
if (input1 == RIGHT && input2 == LEFT)
return 1;
if (input1 == UP && input2 == DOWN)
return 1;
if (input1 == DOWN && input2 == UP)
return 1;
return 0;
}
struct Coordinate
{
int x, y;
};
class snake
{
private:
int length;
Coordinate body[WIDTH * HEIGHT];
int direction;
int ground[MAX][MAX];
int foodCounter;
public:
void boardMatrix()
{
int i, j;
for (i = 0; i < MAX; i++)
for (j = 0; j < MAX; j++)
ground[i][j] = 0;
for (i = 0; i <= WIDTH + 1; i++)
{
// top bottom wall
ground[0][i] = WALL;
ground[HEIGHT + 1][i] = WALL;
}
for (i = 0; i <= HEIGHT + 1; i++)
{
// right left wall
ground[i][0] = WALL;
ground[i][WIDTH + 1] = WALL;
}
}
void snakeBody()
{
length = INIT_SNAKE_LENGTH; // set head of snake to be at the centre
body[0].x = WIDTH / 2;
body[0].y = HEIGHT / 2;
direction = input;
foodCounter = 0;
int i;
for (i = 1; i < length; i++)
{
body[i].x = body[i - 1].x - dx[direction]; // if moving right,
body[i].y = body[i - 1].y - dy[direction]; // body is on the left
}
// let the ground know the snake's position
for (i = 0; i < length; i++)
ground[body[i].y][body[i].x] = SNAKE;
}
void updateSnake(int delay)
{
int i;
Coordinate prev[WIDTH * HEIGHT];
for (i = 0; i < length; i++)
{
prev[i].x = body[i].x;
prev[i].y = body[i].y;
}
if (input != EXIT && !oppositeDirection(direction, input))
direction = input;
body[0].x = prev[0].x + dx[direction]; // head of snake
body[0].y = prev[0].y + dy[direction]; // follows the direction
if (ground[body[0].y][body[0].x] < NOTHING)
{
item = -1;
return;
}
if (ground[body[0].y][body[0].x] == FOOD)
{
length++; // length of snake increases when it eats food
item = FOOD;
}
else
{
ground[body[length - 1].y][body[length - 1].x] = NOTHING;
item = NOTHING;
gotoxy(body[length - 1].x, body[length - 1].y); // if snake does not get food,
cout << " "; // erase last part because the snake is moving
}
for (i = 1; i < length; i++)
{
body[i].x = prev[i - 1].x; // body follows the previous
body[i].y = prev[i - 1].y; // location that it was from
}
gotoxy(body[1].x, body[1].y);
cout << "+"; // change the previous head to a body
gotoxy(body[0].x, body[0].y);
cout << "O"; // add a head to the snake
// let the ground know the snake's position
for (i = 0; i < length; i++)
ground[body[i].y][body[i].x] = SNAKE;
Sleep(delay);
return;
}
void food()
{
int x, y;
do
{
x = rand() % WIDTH + 1;
y = rand() % HEIGHT + 1;
} while (ground[y][x] != NOTHING);
ground[y][x] = FOOD;
foodCounter++;
gotoxy(x, y);
cout << "\u2022"; // prints a middle dot
}
void go()
{
clearScreen();
int i, j;
for (i = 0; i <= HEIGHT + 1; i++)
{
for (j = 0; j <= WIDTH + 1; j++)
{
switch (ground[i][j])
{
case NOTHING:
cout << " ";
break;
case WALL:
if ((i == 0 && j == 0) || (i == 0 && j == WIDTH + 1) || (i == HEIGHT + 1 && j == 0) || (i == HEIGHT + 1 && j == WIDTH + 1))
cout << "+"; // the 4 corners
else if (j == 0 || j == WIDTH + 1)
cout << "|"; // left/right wall
else
cout << "-"; // top/bottom wall
break;
case SNAKE:
if (i == body[0].y && j == body[0].x)
cout << "O";
else
cout << "+";
break;
default: // food
cout << "\u2022"; // prints a middle dot
}
}
cout << endl;
}
}
int foodCount()
{
return foodCounter;
}
};
void userip(void *id)
{
do
{
int c = _getch();
switch (c)
{
case 'W':
case 'w':
input = UP;
break;
case 'S':
case 's':
input = DOWN;
break;
case 'D':
case 'd':
input = RIGHT;
break;
case 'A':
case 'a':
input = LEFT;
break;
case 27:
input = EXIT;
break;
}
} while (input != EXIT && item >= 0);
_endthread();
return;
}
int main()
{
cout<<"Welcome to the Snake Game!"<<endl;
cout<<"Enter your name"<<endl;
string name;
cin>>name;
int delay = 50;
srand(time(NULL));
snake S1;
S1.boardMatrix();
S1.snakeBody();
S1.food();
S1.go();
_beginthread(userip, 0, (void *)0);
do
{
S1.updateSnake(delay);
if (item == FOOD)
S1.food();
} while (item >= 0 && input != EXIT);
gotoxy(WIDTH / 2 - 5, HEIGHT / 2 - 2);
cout << "GAME OVER!";
gotoxy(WIDTH / 2 - 8, HEIGHT / 2 + 2);
cout << name<<", your score is " << S1.foodCount() - 1 << "!" << endl;
gotoxy(WIDTH / 2, HEIGHT / 2);
_getch();
return 0;
}