-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.h
More file actions
354 lines (307 loc) · 9.95 KB
/
snake.h
File metadata and controls
354 lines (307 loc) · 9.95 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
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
#ifndef SNAKE_H
#define SNAKE_H
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib>
#if defined(__unix__) || defined(__APPLE__)
#include <termios.h>
#include <unistd.h>
#endif
#include <map>
#include <deque>
#include <algorithm>
#include <fstream>
#include <set>
#include <string>
#include <utility>
const int BOARD_SIZE = 10;
const int MAX_TOP_SCORES = 10;
const int BASE_DELAY_MS = 500;
const int MIN_DELAY_MS = 100;
const int DELAY_REDUCTION_MS = 50;
const int POISON_CHANCE = 3; // 1 in 3 chance for poison food
const char DIR_RIGHT = 'r';
const char DIR_LEFT = 'l';
const char DIR_UP = 'u';
const char DIR_DOWN = 'd';
const char PAUSE_KEY = 'x';
const char QUIT_KEY = 'q';
class SnakeGame {
private:
char direction;
bool paused;
std::multiset<int, std::greater<int>> topScores;
std::deque<std::pair<int, int>> snake;
std::pair<int, int> food;
std::pair<int, int> poisonFood;
int score;
void loadScores();
void saveScores();
void showTopScores();
void renderGame();
std::pair<int, int> getNextHead(const std::pair<int, int>& current, char dir);
void generateFood();
void generatePoisonFood();
bool isValidPosition(const std::pair<int, int>& pos);
void gameOver(const std::string& reason);
int calculateDelay();
public:
SnakeGame();
~SnakeGame();
// Game control methods
void startGame();
void handleInput(char input);
void updateGame();
void pauseGame();
void resumeGame();
bool isGameOver();
// Getters
int getScore() const { return score; }
bool isPaused() const { return paused; }
char getDirection() const { return direction; }
const std::deque<std::pair<int, int>>& getSnake() const { return snake; }
std::pair<int, int> getFood() const { return food; }
std::pair<int, int> getPoisonFood() const { return poisonFood; }
// Setters
void setDirection(char dir) { direction = dir; }
};
// Global game instance pointer for input -> game communication
extern SnakeGame* g_game;
// Utility functions
std::pair<int, int> get_next_head(const std::pair<int, int>& current, char direction);
void input_handler();
void game_play();
// SnakeGame class implementation
SnakeGame::SnakeGame() : direction(DIR_RIGHT), paused(false), score(0) {
loadScores();
snake.push_back(std::make_pair(0, 0));
generateFood();
poisonFood = std::make_pair(-1, -1);
}
SnakeGame::~SnakeGame() {
saveScores();
}
void SnakeGame::loadScores() {
std::ifstream infile("scores.txt");
if (!infile.is_open()) {
// File doesn't exist or can't be opened, start with empty scores
return;
}
int score;
while (infile >> score) {
if (score >= 0) { // Only accept non-negative scores
topScores.insert(score);
}
}
infile.close();
}
void SnakeGame::saveScores() {
std::ofstream outfile("scores.txt");
if (!outfile.is_open()) {
std::cerr << "Warning: Could not save scores to file." << std::endl;
return;
}
int count = 0;
for (int score : topScores) {
if (count++ == MAX_TOP_SCORES) break;
outfile << score << "\n";
}
outfile.close();
}
void SnakeGame::showTopScores() {
std::cout << "\n=== Top Scores ===\n";
int count = 0;
for (int s : topScores) {
std::cout << ++count << ". " << s << std::endl;
if (count == MAX_TOP_SCORES) break;
}
std::cout << "==================\n";
}
void SnakeGame::renderGame() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (i == food.first && j == food.second) {
std::cout << "🍎";
} else if (std::find(snake.begin(), snake.end(), std::make_pair(i, j)) != snake.end()) {
std::cout << "🐍";
} else if (i == poisonFood.first && j == poisonFood.second) {
std::cout << "💀";
} else {
std::cout << "⬜";
}
}
std::cout << std::endl;
}
}
std::pair<int, int> SnakeGame::getNextHead(const std::pair<int, int>& current, char dir) {
std::pair<int, int> next;
if (dir == DIR_RIGHT) {
next = std::make_pair(current.first, (current.second + 1) % BOARD_SIZE);
} else if (dir == DIR_LEFT) {
next = std::make_pair(current.first, current.second == 0 ? BOARD_SIZE - 1 : current.second - 1);
} else if (dir == DIR_DOWN) {
next = std::make_pair((current.first + 1) % BOARD_SIZE, current.second);
} else if (dir == DIR_UP) {
next = std::make_pair(current.first == 0 ? BOARD_SIZE - 1 : current.first - 1, current.second);
}
return next;
}
void SnakeGame::generateFood() {
do {
food = std::make_pair(rand() % BOARD_SIZE, rand() % BOARD_SIZE);
} while (std::find(snake.begin(), snake.end(), food) != snake.end());
}
void SnakeGame::generatePoisonFood() {
do {
poisonFood = std::make_pair(rand() % BOARD_SIZE, rand() % BOARD_SIZE);
} while (std::find(snake.begin(), snake.end(), poisonFood) != snake.end() &&
poisonFood == food);
}
bool SnakeGame::isValidPosition(const std::pair<int, int>& pos) {
return pos.first >= 0 && pos.first < BOARD_SIZE &&
pos.second >= 0 && pos.second < BOARD_SIZE;
}
static inline void clear_screen() {
#if defined(_WIN32)
system("cls");
#else
system("clear");
#endif
}
void SnakeGame::gameOver(const std::string& reason) {
clear_screen();
std::cout << "Game Over! " << reason << std::endl;
std::cout << "Final Score: " << score << " points\n";
topScores.insert(score);
saveScores();
showTopScores();
exit(0);
}
int SnakeGame::calculateDelay() {
int reduction = (snake.size() / 10) * DELAY_REDUCTION_MS;
return std::max(MIN_DELAY_MS, BASE_DELAY_MS - reduction);
}
void SnakeGame::startGame() {
clear_screen();
showTopScores();
}
void SnakeGame::handleInput(char input) {
std::map<char, char> keymap = {{'d', DIR_RIGHT}, {'a', DIR_LEFT}, {'w', DIR_UP}, {'s', DIR_DOWN}};
if (keymap.find(input) != keymap.end()) {
char newDirection = keymap[input];
// Prevent snake from moving backwards into itself
if (!(direction == DIR_RIGHT && newDirection == DIR_LEFT) &&
!(direction == DIR_LEFT && newDirection == DIR_RIGHT) &&
!(direction == DIR_UP && newDirection == DIR_DOWN) &&
!(direction == DIR_DOWN && newDirection == DIR_UP)) {
direction = newDirection;
}
} else if (input == PAUSE_KEY) {
paused = !paused;
} else if (input == QUIT_KEY) {
saveScores();
exit(0);
}
}
void SnakeGame::updateGame() {
if (paused) {
renderGame();
std::cout << "Game paused. Press x to continue" << std::endl;
std::cout << "Score: " << score << " points" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
return;
}
std::pair<int, int> head = getNextHead(snake.back(), direction);
score = snake.size() * 10;
if (std::find(snake.begin(), snake.end(), head) != snake.end()) {
gameOver("You hit yourself!");
return;
}
if (head == food) {
generateFood();
if (rand() % POISON_CHANCE == 0) {
generatePoisonFood();
} else {
poisonFood = std::make_pair(-1, -1);
}
snake.push_back(head);
} else if (head == poisonFood) {
gameOver("You ate poisonous food!");
return;
} else {
snake.push_back(head);
snake.pop_front();
}
renderGame();
std::cout << "length of snake: " << snake.size() << std::endl;
std::cout << "Score: " << score << " points" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(calculateDelay()));
}
void SnakeGame::pauseGame() {
paused = true;
}
void SnakeGame::resumeGame() {
paused = false;
}
bool SnakeGame::isGameOver() {
return false; // This would be set to true when game ends
}
// Utility functions (keeping original interface for compatibility)
std::pair<int, int> get_next_head(const std::pair<int, int>& current, char direction) {
std::pair<int, int> next;
if (direction == DIR_RIGHT) {
next = std::make_pair(current.first, (current.second + 1) % BOARD_SIZE);
} else if (direction == DIR_LEFT) {
next = std::make_pair(current.first, current.second == 0 ? BOARD_SIZE - 1 : current.second - 1);
} else if (direction == DIR_DOWN) {
next = std::make_pair((current.first + 1) % BOARD_SIZE, current.second);
} else if (direction == DIR_UP) {
next = std::make_pair(current.first == 0 ? BOARD_SIZE - 1 : current.first - 1, current.second);
}
return next;
}
void input_handler() {
#if defined(__unix__) || defined(__APPLE__)
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
std::map<char, char> keymap = {{'d', DIR_RIGHT}, {'a', DIR_LEFT}, {'w', DIR_UP}, {'s', DIR_DOWN}};
while (true) {
char input = getchar();
if (g_game && keymap.find(input) != keymap.end()) {
g_game->handleInput(input);
} else if (g_game && input == PAUSE_KEY) {
g_game->handleInput(input);
} else if (input == QUIT_KEY) {
exit(0);
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#else
std::map<char, char> keymap = {{'d', DIR_RIGHT}, {'a', DIR_LEFT}, {'w', DIR_UP}, {'s', DIR_DOWN}};
while (true) {
int c = std::cin.get();
if (c == EOF) break;
char input = static_cast<char>(c);
if (g_game) {
g_game->handleInput(input);
}
if (input == QUIT_KEY) exit(0);
}
#endif
}
void game_play() {
if (!g_game) {
g_game = new SnakeGame();
}
g_game->startGame();
while (true) {
std::cout << "\033[H";
g_game->updateGame();
}
}
#endif