-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
150 lines (137 loc) · 3.01 KB
/
Board.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
#include "Board.h"
Board::Board()
{
Score = 0;
Food = { 0, 0 };
cnsl = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < Width; i++)
{
B[0][i] = 1;
B[Height - 1][i] = 1;
}
for (int i = 0; i < Height; i++)
{
B[i][0] = 1;
B[i][Width - 1] = 1;
}
s.head = { Height / 2, Width / 2 };
s.ntail = 0;
last = 0;
GenerateFood();
Print();
}
void Board::GenerateFood()
{
srand(time(NULL));
int x, y;
//food can neither be on snake's body nor the wall
do
{
x = 0, y = 0;
while (x <= 0 || x >= Width - 1) x = rand() % Width;
while (y <= 0 || y >= Height - 1) y = rand() % Height;
} while (B[y][x] != 0);
B[y][x] = 4;
Food = { y, x };
}
void Board::Clear()
{
for (int i = 1; i < Height - 1; i++)
{
for (int j = 1; j < Width - 1; j++)
{
B[i][j] = 0;
}
}
B[Food.first][Food.second] = FOOD;
}
void Board::Print()
{
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
switch (B[i][j])
{
case WALL:
cout << '#';
break;
case SNK:
cout << 'o';
break;
case EMP:
cout << ' ';
break;
case HEAD:
cout << 'O';
break;
case FOOD:
cout << '$';
break;
}
}
cout << endl;
}
}
void Board::Update()
{
//seting console cursor to the beginning to overwrite the old board
SetConsoleCursorPosition(cnsl, { 0, 0 });
//printing
Print();
cout << "Score : " << Score << endl;
}
bool Board::Move(int Key)
{
//Checking if input is an arrow key
//Checking if snake is capable of moving in the input direction or not
//if not move the same latest move
if (((Key != LEFT && Key != UP && Key != RIGHT && Key != DOWN) ||
((Key == UP && last == DOWN) || (Key == DOWN && last == UP) || (Key == RIGHT && last == LEFT) || (Key == LEFT && last == RIGHT))))
{
Key = last;
}
//moving
switch (Key)
{
case UP:
s.history.push_back(s.head);
s.head = { s.head.first + -1, s.head.second + 0 };
break;
case DOWN:
s.history.push_back(s.head);
s.head = { s.head.first + 1, s.head.second + 0 };
break;
case LEFT:
s.history.push_back(s.head);
s.head = { s.head.first + 0, s.head.second + -1 };
break;
case RIGHT:
s.history.push_back(s.head);
s.head = { s.head.first + 0, s.head.second + 1 };
break;
default:
break;
}
//storing the new move
last = Key;
//if snake has found the food increase score and tail length
if (B[s.head.first][s.head.second] == FOOD) s.ntail++, Score += 10;
//if snake has gone into a wall or its body game over
if (B[s.head.first][s.head.second] == SNK || B[s.head.first][s.head.second] == WALL)
return true; //game over
//clearing the board to print a new one
Clear();
//Determine the new head location
B[s.head.first][s.head.second] = HEAD;
//Determining the tails location (if any)
for (int i = (int)s.history.size() - 1; i >= (int)s.history.size() - s.ntail; i--)
B[s.history[i].first][s.history[i].second] = SNK;
//if food is already eaten Generate a new one
if (B[Food.first][Food.second] != FOOD)
GenerateFood();
//now update to print
Update();
//game is still not over
return false;
}