-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
249 lines (204 loc) · 5.03 KB
/
snake.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
#include <iostream>
#include <conio.h>
#include <list>
#include <windows.h>
#include <algorithm>
#include <array>
using namespace std;
const int height = 30;
const int width = 50;
const int snake_lenght = 100;
const char wall = '#';
const char space = ' ';
const char snake_head = '%';
const char snake = '*';
const char fruit = 'X';
const char quit = 'q';
_SMALL_RECT rect;
int xfruit;
int yfruit;
int x, y;
int score;
int x_array[snake_lenght];
int y_array[snake_lenght];
const unsigned int speed = 70;
bool game_over;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
void set_fruit(){
xfruit = rand() %width;
yfruit = rand() %height;
}
// function to initialise the game
void setup(){
// Set console size
rect.Top = 0;
rect.Left = 0;
rect.Bottom = height + 10;
rect.Right = width + 10;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleWindowInfo(handle, 1, &rect);
game_over = false;
cout << game_over << endl;
score = 0;
x = width / 2;
y = height / 2;
dir = STOP;
set_fruit();
for(int i = 0; i < snake_lenght; i++){
x_array[i] = snake_lenght + 1;
y_array[i] = snake_lenght + 1;
}
}
bool array_index(int line, int row){
// function to return if part of snake body
for(int i = 0; i < snake_lenght; i++){
if ( x_array[i] == line && y_array[i] == row){
return true;
}
}
return false;
}
void print_array(){
for(int i = 0; i < snake_lenght; i++){
cout << x_array[i] << "-";
}
cout << endl;
for(int i = 0; i < snake_lenght; i++){
cout << y_array[i] << "-";
}
}
// function to draw the board
void draw(){
// Reset cursur Top left.
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), {0,0} );
// top wall
for (int i = 0; i < width + 2; i++){
cout << wall;
}
cout << endl;
for (int r = 0; r < height; r++){
// left vertical wall
cout << wall;
// line
for(int l = 0; l < width; l++){
// check what to draw
if (r == y && l == x){
// snake head
cout << snake_head;
}
else if(r == yfruit && l == xfruit && dir != STOP){
cout << fruit;
}
else if(array_index(l, r) && dir != STOP){
cout << snake;
}
else{
cout << space;
}
}
// Right vertical wall
cout << wall << endl;
}
// bottom wall
for (int i = 0; i < width + 2; i++){
cout << wall;
}
cout << endl;
cout << "Score: " << score << endl;
cout << "Use " << quit << " for Quit." << endl;
cout << "X:" << x << " Y:"<< y << endl;
cout << endl;
}
// check for keyboard entry
void input(){
if(_kbhit()){
switch(_getch()){
case 's':
dir = LEFT;
break;
case 75:
dir = LEFT;
break;
case 'f':
dir = RIGHT ;
break;
case 77:
dir = RIGHT;
break;
case 'e':
dir = DOWN;
break;
case 72:
dir = DOWN;
break;
case 'd':
dir = UP;
break;
case 80:
dir = UP;
break;
case quit:
game_over = true;
break;
}
}
}
int logic(){
// move snake head in the board
switch(dir){
case LEFT:
x = x - 1;
break;
case RIGHT:
x = x + 1;
break;
case UP:
y = y + 1;
break;
case DOWN:
y = y - 1;
}
for(int i = 0; i < snake_lenght; i++){
x_array[i] = x_array[i + 1];
y_array[i] = y_array[i + 1];
}
if (x < 0 || x >= width || y < 0 || y >= height ){
cout << "outside board" << endl;
game_over = true;
}
else if(x == xfruit && y == yfruit){
cout << "fruit found" << endl;
score = score + 100;
do {
set_fruit();
}while(array_index(xfruit, yfruit));
}
else if((array_index(x, y)) && dir !=STOP){
cout << "Hit Snake" << endl;
game_over = true;
}
x_array[snake_lenght - 1] = x;
y_array[snake_lenght - 1] = y;
}
int main(){
setup();
cout << "Game over value: " << game_over << endl;
while(!game_over){
if (dir != STOP){ //Start score only when it move
score = score + 1;
}
input();
logic();
draw();
Sleep(speed);
//game_over = true;
}
if(x == xfruit && y == yfruit){
cout << "Game Over, YOU WIN!! Score is: " << score << endl;
}
else{
cout << "Game Over, try again!" << endl;
}
return 0;
}