-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.cpp
236 lines (228 loc) · 5.54 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
// snake game by Arash_Rahmani
// please see all this file before using that
// it's object-oriented implementation of this popular game
#include <stdlib.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
#include <string>
#include <ctime>
#include <termio.h>
#include <random>
using namespace std;
struct point{
int x;
int y;
point(int a=0, int b=0){
x=a;
y=b;
}
};
int bytesWaiting;
int _kbhit() {
static const int STDIN = 0;
static bool initialized = false;
if ( !initialized ) {
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized=true;
}
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
class snake{
public:
snake(){}
snake(int center_y, int center_x){
body.push_back(point(center_x, center_y));
}
void set_dir();
void add();
void update(bool);
vector<point> get_body(){
return body;
}
private:
char dir;
vector<point> body;
vector<point> previous_pos;
char previous_dir;
};
void snake::add(){
body.push_back(previous_pos[previous_pos.size()-1]);
}
void snake::update(bool hit){
previous_pos=body;
if (hit){
previous_dir=dir;
cin>>dir;
if(body.size()>1)
set_dir();
}
switch(dir){
case('w'):
--body[0].x;
for(int i=1; i<body.size(); i++){
body[i]=previous_pos[i-1];
}
break;
case('s'):
++body[0].x;
for(int i=1; i<body.size(); i++){
body[i]=previous_pos[i-1];
}
break;
case('a'):
--body[0].y;
for(int i=1; i<body.size(); i++){
body[i]=previous_pos[i-1];
}
break;
case('d'):
++body[0].y;
for(int i=1; i<body.size(); i++){
body[i]=previous_pos[i-1];
}
break;
}
}
void snake::set_dir(){
switch(dir){
case('w'):
if(previous_dir=='s')
dir='s';
break;
case('s'):
if(previous_dir=='w')
dir='w';
break;
case('a'):
if(previous_dir=='d')
dir='d';
break;
case('d'):
if(previous_dir=='a')
dir='a';
break;
}
}
class blocks{
public:
blocks(){}
blocks(int, int);
vector<point> get_walls(){ return walls; }
private:
vector<point> walls;
};
blocks::blocks(int w, int h){
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
if(i==0)
walls.push_back(point (i, j));
else if(j==0)
walls.push_back(point (i, j));
else if(i==h-1)
walls.push_back(point (i, j));
else if(j==w-1)
walls.push_back(point (i, j));
}
}
}
class game{
public:
game(int, int);
void food_generator();
void update(uint);
void chk_losing();
void start();
private:
int width;
int height;
int score=0;
bool losing=false;
blocks b1;
snake s1;
vector<point> walls;
vector<point> body ;
point food;
};
game::game(int w, int h){
width=w;
height=h;
b1=blocks(w, h);
food_generator();
s1=snake(w/2, h/2);
body =s1.get_body();
walls=b1.get_walls();
}
void game:: start(){
string all_cells[height][width+1];
while(!losing){
if(_kbhit()){
s1.update(true);
}
else{
s1.update(false);
}
for(int i=0; i<height; i++){
for(int j=0; j<width+1; j++){
all_cells[i][j]=' ';
for(int z=0; z<walls.size(); z++){
if(i==walls[z].x && j==walls[z].y)
all_cells[i][j]='#';
}
if(i==food.x && j==food.y)
all_cells[i][j]='O';
for(int k=0; k<body.size(); k++){
if(i==body[k].x && j==body[k].y)
all_cells[i][j]='0';
}
if(j==width){
all_cells[i][j]="\n";
}
if(body[0].x==food.x && body[0].y==food.y){
s1.add();
food_generator();
score++;
}
cout<<all_cells[i][j];
}
}
cout<<"score:\t"<<score<<endl;
chk_losing();
}
}
void game::update(uint time_nanosecs){
this_thread::sleep_for(chrono::nanoseconds(time_nanosecs));
system("clear");
}
void game::food_generator(){
srand(time(0));
std::mt19937 mersenne(static_cast<unsigned int>(time(nullptr)));
std::uniform_int_distribution<> x_range(1, height-2);
std::uniform_int_distribution<> y_range(1, width -2);
int rand_x=x_range(mersenne);
int rand_y=y_range(mersenne);
food.x=rand_x;
food.y=rand_y;
}
void game::chk_losing(){
for(int i=0; i<walls.size(); i++){
if(body[0].x==walls[i].x && body[0].y==walls[i].y){
losing=true;
cout<<"\t GAME OVER :) \n";
}
}
if(!losing){
body=s1.get_body();
update(100000000);
}
}
int main(){
game g1(40, 21);
g1.start();
}