-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
198 lines (192 loc) · 5.69 KB
/
code.cpp
File metadata and controls
198 lines (192 loc) · 5.69 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
195
196
197
198
#include <Windows.h>
#include <algorithm>
#include <cassert>
#include <conio.h>
#include <iostream>
#include <vector>
using namespace std;
struct Point {
int x;
int y;
Point(int x1, int y1) : x(x1), y(y1) {}
bool operator==(const Point &rhs) { return ((rhs.x == x) && (rhs.y == y)); }
};
bool is_point_in_snake(const vector<Point> &snake, Point &point, int n) {
for (int k = 0; k < n; k++)
if (point == snake[k])
return true;
return false;
}
void case_d(vector<Point> &snake, Point &point, char &direction,
char old_direction, bool &is_on_wrong_button, bool &game_over) {
if ((point.y == snake[snake.size() - 2].y) &&
(snake[snake.size() - 2].x < point.x) ||
(point.y != snake[snake.size() - 2].y))
point.x += 1;
else
is_on_wrong_button = true;
}
void case_a(vector<Point> &snake, Point &point, char &direction,
char old_direction, bool &is_on_wrong_button, bool &game_over) {
if ((point.y == snake[snake.size() - 2].y) &&
(snake[snake.size() - 2].x > point.x) ||
(point.y != snake[snake.size() - 2].y))
point.x -= 1;
else
is_on_wrong_button = true;
}
void case_w(vector<Point> &snake, Point &point, char &direction,
char old_direction, bool &is_on_wrong_button, bool &game_over) {
if ((point.x == snake[snake.size() - 2].x) &&
(snake[snake.size() - 2].y < point.y) ||
(point.x != snake[snake.size() - 2].x))
point.y += 1;
else
is_on_wrong_button = true;
}
void case_s(vector<Point> &snake, Point &point, char &direction,
char old_direction, bool &is_on_wrong_button, bool &game_over) {
if ((point.x == snake[snake.size() - 2].x) &&
(snake[snake.size() - 2].y > point.y) ||
(point.x != snake[snake.size() - 2].x))
point.y -= 1;
else
is_on_wrong_button = true;
}
press_button(vector<Point> &snake, Point &point, char &direction,
char old_direction, bool &is_on_wrong_button, bool &game_over) {
switch (direction) {
case 'd':
case_d(snake, point, direction, old_direction, is_on_wrong_button,
game_over);
break;
case 'a':
case_a(snake, point, direction, old_direction, is_on_wrong_button,
game_over);
break;
case 'w':
case_w(snake, point, direction, old_direction, is_on_wrong_button,
game_over);
break;
case 's':
case_s(snake, point, direction, old_direction, is_on_wrong_button,
game_over);
break;
default:
is_on_wrong_button = true;
}
}
int move_snake(vector<Point> &snake, Point &point, char &direction,
char old_direction, bool &game_over, bool **food, size_t &count,
size_t &quantity_food) {
bool is_on_wrong_button = false;
press_button(snake, point, direction, old_direction, is_on_wrong_button,
game_over);
if (game_over)
return 0;
if (is_on_wrong_button) {
direction = old_direction;
move_snake(snake, point, direction, direction, game_over, food, count,
quantity_food);
} else {
snake.push_back(point);
if (food[point.y][point.x]) { // assert(false);
food[point.y][point.x] = false;
quantity_food--;
count++;
} else
snake.erase(snake.begin());
}
if ((point.x == 0) || (point.y == 0) || (point.x == 14) || (point.y == 14) ||
is_point_in_snake(snake, point, snake.size() - 3)) {
game_over = true;
return 0;
}
}
void draw_game(const vector<Point> &snake, bool **food, size_t &quantity_food) {
int size = 15;
quantity_food = 0;
int number_of_last_symbol = size - 1;
for (int j = 0; j < size; j++) {
if ((j != 0) && (j != number_of_last_symbol))
for (int i = 0; i < size; i++) {
Point p(i, number_of_last_symbol - j);
if ((i == 0) || (i == number_of_last_symbol))
cout << '|';
else {
if (is_point_in_snake(snake, p, snake.size()))
cout << 'O';
else if (food[number_of_last_symbol - j][i]) {
cout << '*';
quantity_food++;
} else
cout << ' ';
}
}
else
for (int i = 0; i < size; i++)
if (i % 2 == 0)
cout << '+';
else
cout << '-';
cout << endl;
}
}
void give_food(bool **food, size_t quantity_food, size_t size_food) {
for (int i = 0; i < size_food; i++)
for (int j = 0; j < size_food; j++)
food[i][j] = false;
for (int i = 0; i < quantity_food; i++)
food[rand() % size_food][rand() % size_food] = true;
}
resize_snack(vector<Point> &snake, int n) {
for (int i = 0; i < n; i++)
snake.erase(snake.begin());
}
int main() {
size_t size_food = 15;
size_t count = 0;
size_t quantity_food = 5;
bool **food = new bool *[size_food];
for (int i = 0; i < size_food; i++)
food[i] = new bool[size_food];
for (int i = 0; i < size_food; i++)
for (int j = 0; j < size_food; j++)
food[i][j] = false;
bool game_over = false;
char direction = 'd';
vector<Point> snake;
Point point(2, 2);
snake.push_back(point);
for (int j = 0; j < 6; j++) {
point.x = 2 + j + 1;
snake.push_back(point);
}
Point point1 = snake.back();
give_food(food, quantity_food, size_food);
int t = 1;
while (true) {
char old_direction = direction;
draw_game(snake, food, quantity_food);
if (quantity_food == 0)
give_food(food, 5, size_food);
if (count == t * 10) {
t++;
resize_snack(snake, 10);
}
Sleep(1000);
system("cls");
if (kbhit()) {
direction = getch();
}
move_snake(snake, point1, direction, old_direction, game_over, food, count,
quantity_food);
if (game_over)
break;
}
system("cls");
cout << "Game over" << endl;
cout << count;
Sleep(5000);
return 0;
}