-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game.cpp
More file actions
194 lines (187 loc) · 5.37 KB
/
Copy pathsnake_game.cpp
File metadata and controls
194 lines (187 loc) · 5.37 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
#include <bits/stdc++.h>
#include <windows.h>
#include<conio.h>
using namespace std;
class snake
{
private:
int width,height;
int targetx,targety,score;
bool gameover=false;
int headx, heady;
enum direction{
up, down, left,right,stop
}dir;
deque<pair<int,int>> mysnake;
int prevx,prevy;
public:
int getscore(){
return score;
}
snake(int w, int h){
dir =up;
width=w;
height=h;
score=0;
targetx= rand()%(w-2)+1;
targety= rand()% (h-2)+1;
gameover=false;
headx=width/2;
heady=height/2;
mysnake.push_back({headx,heady});
}
bool gamecondition(){
return gameover;
}
void draw()
{
system("cls");
for (int i = 0; i < width; i++)
{
cout << "#";
}
cout << "\n";
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (col == 0 || col == width - 1)
{
cout << "#";
continue;
}
pair<int,int> xx={col,row};
auto iter=find(mysnake.begin(),mysnake.end(),xx);
if(iter!=mysnake.end())
{
if(headx==col && heady==row){
cout<<"H";
}
else{
cout<<"O";
}
}
else if(row==targety && col==targetx){
cout<<"X";
}
else{
cout<<" ";
}
}
cout << "\n";
}
for (int i = 0; i < width; i++)
{
cout << "#";
}
}
void input(){
if(_kbhit()){ //in conio.h
switch(_getch()){
case 'w':{
if(dir!=down){
dir=up;
}
break;
}
case 's':{
if(dir!=up){
dir=down;
}
break;
}
case 'a':{
if(dir!=right){
dir=left;
}
break;
}
case 'd':{
if(dir!=left){
dir=right;
}
break;
}
default:
break;
}
}
}
void changedirection(){
switch(dir){
case up:{
heady--;
mysnake.push_front({headx,heady});
auto x=mysnake.back();
prevx=x.first;
prevy=x.second;
mysnake.pop_back();
break;
}
case down:{
heady++;
mysnake.push_front({headx,heady});
auto x=mysnake.back();
prevx=x.first;
prevy=x.second;
mysnake.pop_back();
break;
}
case left:{
headx--;
mysnake.push_front({headx,heady});
auto x=mysnake.back();
prevx=x.first;
prevy=x.second;
mysnake.pop_back();
break;
}
case right:{
headx++;
mysnake.push_front({headx,heady});
auto x=mysnake.back();
prevx=x.first;
prevy=x.second;
mysnake.pop_back();
break;
}
default:{
break;
}
}
//if out the boundary
if(headx<0 || headx>=width || heady<0 || heady>=height){
dir=stop;
gameover=true;
return;
}
//if step on its own tail
pair<int,int> temphead={headx,heady};
auto iter=find(mysnake.begin()+1,mysnake.end(),temphead);
if(iter!=mysnake.end()){
dir=stop;
gameover=true;
return;
}
if(headx==targetx && heady==targety){
targetx= rand()%(width-2)+1;
targety= rand()% (height-2)+1;
mysnake.push_back({prevx,prevy});
score+=10;
}
}
};
int main()
{
srand( time(NULL) );
snake s(20,20);
while(s.gamecondition()==false){
s.draw();
s.input();
s.changedirection();
Sleep(100);
}
cout<<"\ngame is over";
cout<<"\nyour score is : "<<s.getscore();
getch();
}