-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
218 lines (200 loc) · 4.67 KB
/
snake.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
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <algorithm>
#include <chrono>
#include <deque>
#include <iostream>
#include <random>
#include <thread>
using namespace std;
// height of the area
const int SC_HEIGHT = 10;
// width of the area
const int SC_WIDTH = 15;
// amount of attempts to spawn an apple
// until program crashes with winning message
const int MAX_SPAWN_COUNT = 10;
const int SLEEP_TIMEOUT_MS = 150;
const char* clearscreen = "\033[2J\033[1;1H";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
enum class Direction { up, right, down, left };
Direction direction = Direction::right;
// initial fruit coords
int fruitI = rng() % SC_HEIGHT;
int fruitJ = rng() % SC_WIDTH;
// IO functions to catch key press without enter
void enable_raw_mode() {
termios term;
tcgetattr(0, &term);
term.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &term);
}
void disable_raw_mode() {
termios term;
tcgetattr(0, &term);
term.c_lflag |= ICANON | ECHO;
tcsetattr(0, TCSANOW, &term);
}
bool kbhit() {
int byteswaiting;
ioctl(0, FIONREAD, &byteswaiting);
return byteswaiting > 0;
}
// initial snake
deque<pair<int, int>> snake = {{2, 2}};
bool isOverlaping(int i, int j) {
for (pair<int, int>& p : snake) {
if (p.first == i && p.second == j) {
return true;
}
}
return false;
}
void gameOver(string reason, bool clearScreen) {
disable_raw_mode();
tcflush(0, TCIFLUSH);
cout << '\n';
if (clearScreen) {
cout << clearscreen;
}
cout << reason << endl;
exit(0);
}
void logic() {
int next_i = 0;
int next_j = 0;
switch (direction) {
case Direction::up:
next_i = snake.back().first - 1;
next_j = snake.back().second;
break;
case Direction::left:
next_i = snake.back().first;
next_j = snake.back().second - 1;
break;
case Direction::down:
next_i = snake.back().first + 1;
next_j = snake.back().second;
break;
case Direction::right:
next_i = snake.back().first;
next_j = snake.back().second + 1;
break;
}
if (next_i != fruitI || next_j != fruitJ) {
snake.pop_front();
} else {
int count_spawn = 0;
do {
count_spawn++;
fruitI = rng() % SC_HEIGHT;
fruitJ = rng() % SC_WIDTH;
} while (isOverlaping(fruitI, fruitJ) && count_spawn < MAX_SPAWN_COUNT);
if (count_spawn == MAX_SPAWN_COUNT) {
gameOver("Nowhere to spawn fruits! You win, I guess..", false);
}
}
if (next_i < 0) {
next_i = SC_HEIGHT - 1;
} else if (next_i >= SC_HEIGHT) {
next_i = 0;
}
if (next_j < 0) {
next_j = SC_WIDTH - 1;
}
if (next_j >= SC_WIDTH) {
next_j = 0;
}
if (isOverlaping(next_i, next_j)) {
gameOver("Game over! You crashed into yourself!", false);
}
snake.push_back({next_i, next_j});
}
void draw() {
for (int i = 0; i < SC_WIDTH + 2; ++i) {
cout << '#';
}
cout << '\n';
bool headPrinted = false;
for (int i = 0; i < SC_HEIGHT; ++i) {
cout << '#';
for (int j = 0; j < SC_WIDTH; ++j) {
// Encountered an apple
if (i == fruitI && j == fruitJ) {
cout << 'A';
continue;
}
bool foundSnakesPart = false;
for (pair<int, int>& p : snake) {
if (p.first == i && p.second == j) {
foundSnakesPart = true;
if (p == snake.back()) {
// snake's head
cout << 'O';
headPrinted = true;
continue;
}
// snake's tail
cout << '*';
}
}
if (!foundSnakesPart) cout << ' ';
}
cout << '#';
cout << '\n';
}
for (int i = 0; i < SC_WIDTH + 2; ++i) {
cout << '#';
}
cout.flush();
if (kbhit()) {
// change direction
char c;
cin >> c;
if (c == 27) {
// arrow keys
char _;
cin >> _ >> c;
}
switch (c) {
case 65:
case 'w':
if (direction == Direction::down) break;
direction = Direction::up;
break;
case 68:
case 'a':
if (direction == Direction::right) break;
direction = Direction::left;
break;
case 66:
case 's':
if (direction == Direction::up) break;
direction = Direction::down;
break;
case 67:
case 'd':
if (direction == Direction::left) break;
direction = Direction::right;
break;
}
}
logic();
}
void loop() {
cout << clearscreen;
draw();
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMEOUT_MS));
}
void handle_exit(sig_atomic_t s) {
gameOver("CTRL-C was detected. Shutting down!", true);
}
int main() {
signal(SIGINT, handle_exit);
enable_raw_mode();
while (true) {
loop();
}
return 0;
}