-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
99 lines (96 loc) · 2.28 KB
/
snake.cpp
File metadata and controls
99 lines (96 loc) · 2.28 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
#include <iostream>
#include <conio.h>
#include <time.h>
#include <windows.h>
using namespace std;
int main()
{
int score;
char retry='y';
cout <<"Press Any Key to Start"<<endl;
getch();
while (retry=='y' || retry == 'Y')
{
srand(time(NULL));
score=0;
int pion=(rand()-1)%18+1;
int poin=(rand()-1)%18+1;
int x[1000];
int y[1000];
x[0]=10;
y[0]=10;
x[1]=9;
y[1]=10;
x[2]=8;
y[2]=10;
int ular=3 ;
string a[20][20];
char ctr='d';
while (x[0]!=0 && x[0]!=19 && y[0]!=0 && y[0]!=19)
{
for (int i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
if (i==0 || j==0 || i==19 || j==19)
a[i][j]="* ";
else
a[i][j]=" ";
}
}
a[poin][pion]="o ";
for(int s=ular;s>0;s--)
{
x[s]=x[s-1];
y[s]=y[s-1];
}
for (int r=0;r<=ular;r++)
{
a[y[r]][x[r]]="* ";
}
if (kbhit())
{
ctr=getch();
}
if(ctr=='w')
y[0]--;
if(ctr=='s')
y[0]++;
if(ctr=='a')
x[0]--;
if(ctr=='d')
x[0]++;
for(int m=0;m<20;m++)
{
for (int n=0;n<20;n++)
{
cout << a[m][n];
}
cout <<endl;
}
if (a[0][0]==a[poin][pion])
{
ular++;
poin=rand()%19;
if (poin%19==0)
poin++;
pion=rand()%19;
if (pion%19==0)
pion++;
score=score+1;
}
for (int gh=1;gh<ular;gh++)
{
if (x[0]==x[gh] && y[0]==y[gh] )
{
x[0]=0;
}
}
Sleep(25);
system("cls");
}
cout << "Score = " << score<<endl;
cout << "Start again? (y/n)"; cin >>retry;
}
return 0;
}