-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_cmd-game.cpp
365 lines (365 loc) · 16.2 KB
/
snake_cmd-game.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <conio.h>
bool gameOver;
const int width=30,height=30;
const int widthField=((width*2)+1);
const int maxSizeField=((widthField+3)*(height+2)),
maxSizeSnake=(width*height);
bool _debugInfo=false,portalWalls=false;
double delayTime,delayTimeOg;
int x,y,
fruitX,fruitY,
score,scoreStep,
fieldCount,nTail,
tailX[maxSizeSnake],
tailY[maxSizeSnake];
char field[maxSizeField],
tailC[maxSizeSnake];
enum eDirection{STOP=0,LEFT,UP,RIGHT,DOWN};
eDirection dir,pdir;
bool screenClear(bool full){//~ https://www.cplusplus.com/articles/4z18T05o/#Windows
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count,cellCount;
COORD homeCoords={0,0};
hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
if(hStdOut==INVALID_HANDLE_VALUE){return false;}
if(full){
//~ Get the number of cells in the current buffer
if(!GetConsoleScreenBufferInfo(hStdOut,&csbi)){return false;}
cellCount=csbi.dwSize.X*csbi.dwSize.Y;
//~ Fill the entire buffer with spaces
if(!FillConsoleOutputCharacter(
hStdOut,
(TCHAR)' ',
cellCount,
homeCoords,
&count
)){return false;}
//~ Fill the entire buffer with the current colors and attributes
if(!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)){return false;}
}
//~ Move the cursor home
SetConsoleCursorPosition(hStdOut,homeCoords);
return true;
}
void Setup(){
gameOver=false;
dir=STOP;
x=width*.5;
y=height*.5;
fruitX=rand()%width;
fruitY=rand()%height;
score=0;
scoreStep=0;
fieldCount=0;
field[fieldCount]='\0';
tailX[0]=x;
tailY[0]=y;
nTail=0;
delayTime=delayTimeOg;
if(!screenClear(true)){exit(EXIT_FAILURE);}
}
void Draw(){
fieldCount=0;
field[fieldCount++]='+';
for(int i=0;i<widthField;i++){field[fieldCount++]='-';}//~ border top
field[fieldCount++]='+';
field[fieldCount++]='\n';
for(int i=0;i<height;i++){//~ draw map row
field[fieldCount++]='|';//~ border left
if(_debugInfo){//~ draw map col
if(i==y){for(int j=0;j<widthField;j++){field[fieldCount++]='.';}}
else{for(int j=0;j<widthField;j++){
if(j==((x*2)+1)){field[fieldCount++]='.';}
else{field[fieldCount++]=' ';}
}}
}else{for(int j=0;j<widthField;j++){field[fieldCount++]=' ';}}
field[fieldCount++]='|';//~ border right
field[fieldCount++]='\n';
}
field[fieldCount++]='+';
for(int i=0;i<widthField;i++){field[fieldCount++]='-';}//~ border bottom
field[fieldCount++]='+';
field[fieldCount]='\0';
field[(((widthField+3)*(fruitY+1))+((fruitX*2)+2))]='F';
if(nTail>0){//~ snake tail
int _tailpos=(((widthField+3)*(tailY[0]+1))+((tailX[0]*2)+2));
field[_tailpos]=tailC[0];
if(tailC[0]=='-'&&tailX[0]!=x){
field[_tailpos+1]='-';
field[_tailpos-1]='-';
}
if(portalWalls){
if(dir==LEFT&&x==width-1){field[(((widthField+3)*(y+1))+((x*2)+2))+1]='-';}
else if(dir==RIGHT&&x==0){field[(((widthField+3)*(y+1))+2)-1]='-';}
}
for(int i=1;i<nTail-1;i++){
_tailpos=(((widthField+3)*(tailY[i]+1))+((tailX[i]*2)+2));
field[_tailpos]=tailC[i];
if(tailC[i]=='-'){
field[_tailpos+1]='-';
field[_tailpos-1]='-';
}
}
if(nTail>1){
int _tailpos=(((widthField+3)*(tailY[nTail-1]+1))+((tailX[nTail-1]*2)+2));
field[_tailpos]=tailC[nTail-1];
if(tailC[nTail-1]=='-'){
if(tailX[nTail-1]==width-1||tailX[nTail-1]<tailX[nTail-2]){field[_tailpos+1]='-';}
else if(tailX[nTail-1]==0||tailX[nTail-1]>tailX[nTail-2]){field[_tailpos-1]='-';}
}
}
}
switch(dir){//~ snake head
case LEFT:field[(((widthField+3)*(y+1))+((x*2)+2))]='<';break;
case UP:field[(((widthField+3)*(y+1))+((x*2)+2))]='A';break;
case RIGHT:field[(((widthField+3)*(y+1))+((x*2)+2))]='>';break;
case DOWN:field[(((widthField+3)*(y+1))+((x*2)+2))]='V';break;
default:field[(((widthField+3)*(y+1))+((x*2)+2))]='O';break;
};
field[(((widthField+3)*(fruitY+1))+((fruitX*2)+2))-1]='[';
field[(((widthField+3)*(fruitY+1))+((fruitX*2)+2))+1]=']';
if(!screenClear(false)){exit(EXIT_FAILURE);}//~ clear screen
if(_debugInfo){//~ print info under game
std::cout
<<field
<<"\nidea:[https://youtu.be/E_-lMZDi7Uw] "
<<"\n[ESC/q] Exit [r] DelScore [p] Pause [wasd] Move [b] DebugInfo "
<<"\nscore: "<<score<<" "
<<"\n-------------------------------------------------------------------"
<<"\nscorestep: "<<scoreStep<<" "
<<"\ndelayTime: "<<delayTime<<" "
<<"\nnTail: "<<nTail<<" "
<<"\ndir: "<<dir<<" "
<<"\nX/Y: "<<x<<'/'<<y<<" "
<<"\nfruitX/Y: "<<fruitX<<'/'<<fruitY<<" "
<<"\nportalWalls: "<<portalWalls<<" "
<<"\n-------------------------------------------------------------------"
<<"\n. "
<<std::endl;
}else{
std::cout
<<field
<<"\nidea:[https://youtu.be/E_-lMZDi7Uw] "
<<"\n[ESC/q] Exit [r] DelScore [p] Pause [wasd] Move [b] DebugInfo "
<<"\nscore: "<<score<<" "
<<"\n-------------------------------------------------------------------"
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<std::endl;
}
}
void Input(){
pdir=dir;
if(_kbhit()){
switch(_getch()){
// wasd - HKPM (arrow_keys for _getch()) delay!
case 'w':if(nTail==0||dir!=DOWN){dir=UP;}break;
case 'a':if(nTail==0||dir!=RIGHT){dir=LEFT;}break;
case 's':if(nTail==0||dir!=UP){dir=DOWN;}break;
case 'd':if(nTail==0||dir!=LEFT){dir=RIGHT;}break;
case 'b':_debugInfo=!_debugInfo;break;
case 'p':
if(!screenClear(true)){exit(EXIT_FAILURE);}
if(_debugInfo){//~ print info under game
std::cout
<<field
<<"\nidea:[https://youtu.be/E_-lMZDi7Uw] "
<<"\n[ESC/q] Exit [r] DelScore [p] Pause [wasd] Move [b] DebugInfo "
<<"\nscore: "<<score<<" "
<<"\n-------------------------------------------------------------------"
<<"\nscorestep: "<<scoreStep<<" "
<<"\ndelayTime: "<<delayTime<<" "
<<"\nnTail: "<<nTail<<" "
<<"\ndir: "<<dir<<" "
<<"\nX/Y: "<<x<<'/'<<y<<" "
<<"\nfruitX/Y: "<<fruitX<<'/'<<fruitY<<" "
<<"\nportalWalls: "<<portalWalls<<" "
<<"\n-------------------------------------------------------------------"
<<"\n. ## PAUSED ## press any key to continue ## "
<<std::endl;
}else{
std::cout
<<field
<<"\nidea:[https://youtu.be/E_-lMZDi7Uw] "
<<"\n[ESC/q] Exit [r] DelScore [p] Pause [wasd] Move [b] DebugInfo "
<<"\nscore: "<<score<<" "
<<"\n-------------------------------------------------------------------"
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. ## PAUSED ## press any key to continue ## "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<std::endl;
}
_getch();
break;
case 'r':
fruitX=rand()%width;
fruitY=rand()%height;
score=0;
scoreStep=0;
tailX[0]=x;
tailY[0]=y;
nTail=0;
break;
case '':
case 'q':
exit(EXIT_SUCCESS);
break;
default:break;
}
}
}
void Logic(){
if(nTail>0){//~ snake tail
for(int i=nTail-1;i>0;i--){//~ tail movement
tailX[i]=tailX[i-1];
tailY[i]=tailY[i-1];
tailC[i]=tailC[i-1];
if(tailX[i]==x&&tailY[i]==y){gameOver=true;return;}//~ on tail
}
tailX[0]=x;
tailY[0]=y;
if(pdir==dir){//~tail character
if(dir==LEFT||dir==RIGHT){tailC[0]='-';}
else if(dir==UP||dir==DOWN){tailC[0]='|';}
else{tailC[0]='+';}
}else{tailC[0]='+';}
}
switch(dir){//~ snake movement
case LEFT:x--;break;
case RIGHT:x++;break;
case UP:y--;break;
case DOWN:y++;break;
default:break;
}
if(portalWalls){//~ no-walls ~ loop-screen
if(x>=width){x=0;}
else if(x<0){x=width-1;}
if(y>=height){y=0;}
else if(y<0){y=height-1;}
}else if(x>=width||y>=height||x<0||y<0){Setup();gameOver=true;return;}//~ out of bounds
if(dir>0){//~ survive bonus
if(++scoreStep%100==0){score+=2;}
if(scoreStep>=1000){scoreStep=0;score+=2;}
}
if(x==fruitX&&y==fruitY){//~ collect fruit and delay decrease
score+=10;
if(delayTime>1e-20){delayTime*=.99;}
else{delayTime=0;}
nTail++;
if(nTail>1){
tailX[nTail-1]=tailX[nTail-2];
tailY[nTail-1]=tailY[nTail-2];
tailC[nTail-1]=tailC[nTail-2];
}else{
tailX[0]=x;
tailY[0]=y;
if(pdir==dir){//~tail character
if(dir==LEFT||dir==RIGHT){tailC[0]='-';}
else if(dir==UP||dir==DOWN){tailC[0]='|';}
else{tailC[0]='+';}
}else{tailC[0]='+';}
}
fruitX=(rand()%(width-2))+1;
fruitY=(rand()%(height-2))+1;
}
}
int toNum(char const*num){
int a=0;
for(int i=0,t;num[i]!='\0';i++){
t=num[i]-'0';
if(t>=0&&t<=9){a*=10;a+=t;}else{return -1;}
}
return a;
}
int main(int argc, char const *argv[]){
delayTimeOg=200;
for(int i=1;i<argc;i++){//~ run.exe -t 100 -p -b
if(argv[i][0]=='-'){
switch(argv[i][1]){
case 't':
delayTimeOg=toNum(argv[i+1]);
if(delayTimeOg<0){delayTimeOg=0;}
else if(delayTimeOg>60000){delayTimeOg=60000;}
break;
case 'p':portalWalls=true;break;
case 'b':_debugInfo=true;break;
default:break;
}
}
}
Setup();
while(!gameOver){
Draw();
if(delayTime>0){Sleep(delayTime);}
Input();
Logic();
if(gameOver){
if(!screenClear(true)){return EXIT_FAILURE;}
if(_debugInfo){//~ print info under game
std::cout
<<field
<<"\nidea:[https://youtu.be/E_-lMZDi7Uw] "
<<"\n[ESC/q] Exit [r] DelScore [p] Pause [wasd] Move [b] DebugInfo "
<<"\nscore: "<<score<<" "
<<"\n-------------------------------------------------------------------"
<<"\nscorestep: "<<scoreStep<<" "
<<"\ndelayTime: "<<delayTime<<" "
<<"\nnTail: "<<nTail<<" "
<<"\ndir: "<<dir<<" "
<<"\nX/Y: "<<x<<'/'<<y<<" "
<<"\nfruitX/Y: "<<fruitX<<'/'<<fruitY<<" "
<<"\nportalWalls: "<<portalWalls<<" "
<<"\n-------------------------------------------------------------------"
<<"\n. ## G a m e - O v e r ## again ? [y/n] ## "
<<std::endl;
}else{
std::cout
<<field
<<"\nidea:[https://youtu.be/E_-lMZDi7Uw] "
<<"\n[ESC/q] Exit [r] DelScore [p] Pause [wasd] Move [b] DebugInfo "
<<"\nscore: "<<score<<" "
<<"\n-------------------------------------------------------------------"
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. ## G a m e - O v e r ## again ? [y/n] ## "
<<"\n. "
<<"\n. "
<<"\n. "
<<"\n. "
<<std::endl;
}
int _repeat=_getch();
while(_repeat!='n'){
if(_repeat=='y'||_repeat=='r'){Setup();break;}
else if(_repeat==0x1b||_repeat=='q'){return EXIT_SUCCESS;}
else{_repeat=_getch();}
}
}
}
return EXIT_SUCCESS;
}