-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoraciousSnake.cpp
More file actions
335 lines (291 loc) · 6.53 KB
/
Copy pathVoraciousSnake.cpp
File metadata and controls
335 lines (291 loc) · 6.53 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
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<windows.h>
#include<deque>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<string>
using namespace std;
void PrintMap(){
int i, j;
for(i = 0; i < 30; i++){
for(j = 0; j < 60; j++){
if(i == 0 | i == 29) {
cout << '-';
}
else if(j == 0 | j == 59){
cout << '|';
}
else{
cout << ' ';
}
}
cout << endl;
}
}
void gotoxy(int x, int y){
COORD pos;
pos = {x, y};
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}
void HideCursor() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
bool searchdeque(deque<int> a, int n){
int i;
bool indicator = 0;
for( i = 0 ; i < a.size() && indicator != 1; i++ ){
if(a[i] == n){
indicator = 1;
}
}
return indicator;
}
class Snake {
private:
char head = '@';
char body = '*';
char food = '#';
string direction;
int length = 2;
int pos_food[1][2];
int score = 0;
int head_x = 20;
int head_y = 20;
public:
deque<int> snake_x; //x coordinate of snake's head and body
deque<int> snake_y; //y coordinate
bool gameover = 0;
public:
void InitPrint();
void PrintSnake();
void MoveUp(); // move 1 step each time;
void MoveDown();
void MoveLeft();
void MoveRight();
void ClearSnake();
void CreateFood();
void UserInput(); //get the direction from keyboard
void JudgeDir(); // judge the direction of the snake and then move 1 step
void CheckFood(); //Check if the food was eaten
void CheckCrush(); //Check if the snake crush wall;
bool WrongLocation();
void AddTail();
void ShowScore();
void Gameover();
};
void Snake::InitPrint(){
HideCursor();
snake_x.push_back(20);
snake_x.push_back(20);
snake_y.push_back(20);
snake_y.push_back(21); // set head(20, 20), body(20, 21)
PrintSnake();
CreateFood();
}
void Snake::PrintSnake(){
HideCursor();
int i;
for(i = 0; i < length; i++){
if(i == 0){
gotoxy(snake_x[i], snake_y[i]);
cout << head;
}
else {
gotoxy(snake_x[i], snake_y[i]);
cout << body;
}
}
}
void Snake::MoveUp(){
int h_x, h_y;
h_x = snake_x.front();
h_y = snake_y.front() - 1;
snake_x.push_front(h_x);
snake_y.push_front(h_y); //add a new head(x, y-1); (20, 19)
ClearSnake();
PrintSnake();
}
void Snake::MoveLeft(){
int h_x, h_y;
h_x = snake_x.front() - 1;
h_y = snake_y.front();
snake_x.push_front(h_x);
snake_y.push_front(h_y); //add a new head(x-1, y);(19, 20)
ClearSnake(); //judge if the snake find the food, if so don't clear its tail
PrintSnake();
}
void Snake::MoveDown(){
int h_x, h_y;
h_x = snake_x.front();
h_y = snake_y.front() + 1;
snake_x.push_front(h_x);
snake_y.push_front(h_y); //add a new head(x, y+1); (20, 21)
ClearSnake();
PrintSnake();
}
void Snake::MoveRight(){
int h_x, h_y;
h_x = snake_x.front() + 1;
h_y = snake_y.front();
snake_x.push_front(h_x);
snake_y.push_front(h_y); //add a new head(x, y-1); (21, 20)
ClearSnake();
PrintSnake();
}
void Snake::ClearSnake(){
HideCursor();
int i;
for(i = 1; i <= length; i++){
gotoxy(snake_x[i], snake_y[i]);
cout << ' ';
}
}
void Snake::CreateFood(){
do{
srand(time(NULL));
pos_food[0][0] = rand() % 59; // %59 0~58
pos_food[0][1] = rand() % 29;
}
while(WrongLocation());
gotoxy(pos_food[0][0], pos_food[0][1]);
cout << food;
}
void Snake::UserInput(){
char ch;
ch = getch();
switch(ch)
{
case 'a':
if(direction != "right"){
direction = "left";
}
break;
case 's':
if(direction != "up") {
direction = "down";
}
break;
case 'd':
if(direction != "left") {
direction = "right";
}
break;
case 'w':
if(direction != "down") {
direction = "up";
}
break;
case 'o':
Gameover();
break;
}
}
void Snake::JudgeDir(){
if(direction == "up"){
MoveUp();
}
else if(direction == "down"){
MoveDown();
}
else if(direction == "right"){
MoveRight();
}
else if(direction == "left"){
MoveLeft();
}
head_x = snake_x[0];
head_y = snake_y[0];
CheckFood();
CheckCrush();
}
void Snake::CheckFood(){
if(head_x == pos_food[0][0] && head_y == pos_food[0][1]){
score = score + 100;
CreateFood();
AddTail();
}
}
void Snake::CheckCrush(){
bool a1, a2, a3, a4;
a1 = searchdeque(snake_x, 0);
a2 = searchdeque(snake_x, 59);
a3 = searchdeque(snake_y, 0);
a4 = searchdeque(snake_y, 29);
if( a1 || a2 || a3 || a4){
Gameover();
}
}
void Snake::AddTail(){
length = length + 1;
if(direction == "up"){
snake_x.push_back(snake_x.back());
snake_y.push_back(snake_y.back() + 1);
}
else if(direction == "down"){
snake_x.push_back(snake_x.back());
snake_y.push_back(snake_y.back() - 1);
}
else if(direction == "left"){
snake_x.push_back(snake_x.back() + 1);
snake_y.push_back(snake_y.back());
}
else if(direction == "right"){
snake_x.push_back(snake_x.back() - 1);
snake_y.push_back(snake_y.back());
}
}
bool Snake::WrongLocation(){
bool wrong;
if(searchdeque(snake_x, pos_food[0][0]) && searchdeque(snake_y, pos_food[0][1])){
wrong = 1;
}
else if(pos_food[0][0] == 0 || pos_food[0][1] == 0){
wrong = 1;
}
else {
wrong = 0;
}
return wrong;
}
void Snake::ShowScore(){
cout << "Your score is " << score << endl;
}
void Snake::Gameover(){
gameover = 1;
}
int main(){
Snake snake;
char ch;
PrintMap();
snake.InitPrint();
gotoxy(0, 31);
cout << "Welcome to The Voracious Snake!" << endl;
cout << "Introduction:" << endl;
cout << "Press w/a/s/d to control the snake's direction" << endl;
cout << "#: Snake's Food @: Snake's Head *: Snake's Body" << endl;
cout << "And don't crash either the wall or its own body. Good luck!" << endl << endl;
Sleep(2000);
while(1){
if(kbhit()){
snake.UserInput();
}
if(snake.gameover == 1){
break; //finish the game
}
snake.JudgeDir();
Sleep(1000);
}
gotoxy(0, 40);
cout << "Game Over." << endl;
snake.ShowScore();
return 0;
system("pause");
}
//SetConsoleTextAttribute(hOut, FOREGROUND_INTENSITY);