Repository navigation
Expand file tree
/
Copy pathsnake.cpp
More file actions
265 lines (227 loc) · 7.02 KB
/
Copy pathsnake.cpp
File metadata and controls
265 lines (227 loc) · 7.02 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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#endif
using namespace std;
enum Directions { STOP = 0, LEFT, RIGHT, UP, DOWN };
class SnakeGame {
private:
bool gameOver;
int difficultyDelay;
const int width = 80; // Reduced slightly for better terminal view stability
const int height = 25;
int headX, headY;
int fruitX, fruitY;
int score;
vector<int> tailX;
vector<int> tailY;
Directions dir;
void HideCursor() {
#ifdef _WIN32
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(consoleHandle, &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(consoleHandle, &cursorInfo);
#else
cout << "\033[?25l";
#endif
}
void ShowCursor() {
#ifdef _WIN32
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(consoleHandle, &cursorInfo);
cursorInfo.bVisible = true;
SetConsoleCursorInfo(consoleHandle, &cursorInfo);
#else
cout << "\033[?25h";
#endif
}
void SpawnFruit() {
bool onSnake;
do {
onSnake = false;
fruitX = rand() % width;
fruitY = rand() % height;
if (fruitX == headX && fruitY == headY) {
onSnake = true;
}
for (size_t i = 0; i < tailX.size(); i++) {
if (tailX[i] == fruitX && tailY[i] == fruitY) {
onSnake = true;
break;
}
}
} while (onSnake);
}
public:
SnakeGame() {
srand(time(nullptr));
gameOver = false;
difficultyDelay = 40;
dir = STOP;
headX = width / 2;
headY = height / 2;
score = 0;
}
~SnakeGame() {
ShowCursor();
}
void SelectDifficulty() {
int choice;
cout << "Choose Difficulty :\n";
cout << "1. Easy\n";
cout << "2. Medium\n";
cout << "3. Hard\n";
cout << "Choice (1-3): ";
cin >> choice;
while (choice < 1 || choice > 3) {
cout << "Invalid! Choose Difficulty (1-Easy, 2-Medium, 3-Hard): ";
cin >> choice;
}
switch (choice) {
case 1: difficultyDelay = 80; break;
case 2: difficultyDelay = 45; break;
case 3: difficultyDelay = 20; break;
}
HideCursor();
SpawnFruit();
}
void Draw() {
#ifdef _WIN32
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
#else
cout << "\033[H"; // Fast ANSI cursor home positioning
#endif
// Top Border
for (int i = 0; i < width + 2; i++) cout << "\033[0;106m \033[0m";
cout << "\n";
// Map Grid
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0) cout << "\033[0;106m \033[0m"; // Left wall
if (i == headY && j == headX) {
cout << "\033[0;105m \033[0m"; // Snake Head
} else if (j == fruitX && i == fruitY) {
cout << "\033[0;101m \033[0m"; // Fruit
} else {
bool isTail = false;
for (size_t k = 0; k < tailX.size(); k++) {
if (tailX[k] == j && tailY[k] == i) {
cout << "\033[0;102m \033[0m"; // Snake Tail
isTail = true;
break;
}
}
if (!isTail) cout << " ";
}
if (j == width - 1) cout << "\033[0;106m \033[0m"; // Right wall
}
cout << "\n";
}
// Bottom Border
for (int i = 0; i < width + 2; i++) cout << "\033[0;106m \033[0m";
cout << "\n";
cout << "Score : " << score << " \n";
}
void Input() {
#ifdef _WIN32
if (_kbhit()) {
switch (_getch()) {
case 'a': if (dir != RIGHT) dir = LEFT; break;
case 'd': if (dir != LEFT) dir = RIGHT; break;
case 'w': if (dir != DOWN) dir = UP; break;
case 's': if (dir != UP) dir = DOWN; break;
case 27: gameOver = true; break;
}
}
#else
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
struct timeval timeout = {0, 0};
if (select(1, &readfds, nullptr, nullptr, &timeout) > 0) {
char ch = getchar();
switch (ch) {
case 'a': if (dir != RIGHT) dir = LEFT; break;
case 'd': if (dir != LEFT) dir = RIGHT; break;
case 'w': if (dir != DOWN) dir = UP; break;
case 's': if (dir != UP) dir = DOWN; break;
case 27: gameOver = true; break;
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#endif
}
void Logic() {
if (dir == STOP) return;
// Shift Tail Positions
if (!tailX.empty()) {
tailX.insert(tailX.begin(), headX);
tailY.insert(tailY.begin(), headY);
tailX.pop_back();
tailY.pop_back();
}
// Move Head
switch (dir) {
case LEFT: headX--; break;
case RIGHT: headX++; break;
case UP: headY--; break;
case DOWN: headY++; break;
default: break;
}
// Collision: Wall
if (headX >= width || headX < 0 || headY >= height || headY < 0) {
gameOver = true;
}
// Collision: Self
for (size_t i = 0; i < tailX.size(); i++) {
if (tailX[i] == headX && tailY[i] == headY) {
gameOver = true;
break;
}
}
// Collision: Fruit
if (headX == fruitX && headY == fruitY) {
score += 10;
// Grow tail by adding last head position to the vector tracking list
tailX.push_back(headX);
tailY.push_back(headY);
SpawnFruit();
}
}
bool IsGameOver() const { return gameOver; }
int GetDelay() const { return difficultyDelay; }
};
int main() {
std::ios::sync_with_stdio(false);
std::cout.tie(nullptr);
SnakeGame game;
game.SelectDifficulty();
while (!game.IsGameOver()) {
game.Draw();
game.Input();
game.Logic();
#ifdef _WIN32
Sleep(game.GetDelay());
#else
usleep(game.GetDelay() * 1000);
#endif
}
cout << "\nGame Over! Final Score: " << endl;
return 0;
}