-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnake.cpp
More file actions
221 lines (190 loc) · 5.97 KB
/
snake.cpp
File metadata and controls
221 lines (190 loc) · 5.97 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
#include <bits/stdc++.h>
#ifdef _WIN32
#include <conio.h> // For Windows
#include <windows.h> // For Windows specific functions
#else
#include <ncurses.h> // For Linux
#include <unistd.h> // For usleep in Linux
#endif
using namespace std;
#define MAX_LENGTH 1000
// Directions
const char DIR_UP = 'U';
const char DIR_DOWN = 'D';
const char DIR_LEFT = 'L';
const char DIR_RIGHT = 'R';
int consoleWidth, consoleHeight;
void initScreen() {
#ifdef _WIN32
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
#else
getmaxyx(stdscr, consoleHeight, consoleWidth); // ncurses function to get screen size
#endif
}
struct Point {
int xCoord;
int yCoord;
Point() {}
Point(int x, int y) : xCoord(x), yCoord(y) {}
};
class Snake {
int length;
char direction;
public:
Point body[MAX_LENGTH];
Snake(int x, int y) : length(1), direction(DIR_RIGHT) {
body[0] = Point(x, y);
}
int getLength() {
return length;
}
void changeDirection(char newDirection) {
if ((newDirection == DIR_UP && direction != DIR_DOWN) ||
(newDirection == DIR_DOWN && direction != DIR_UP) ||
(newDirection == DIR_LEFT && direction != DIR_RIGHT) ||
(newDirection == DIR_RIGHT && direction != DIR_LEFT)) {
direction = newDirection;
}
}
bool move(Point food) {
for (int i = length - 1; i > 0; --i) {
body[i] = body[i - 1];
}
switch (direction) {
case DIR_UP: body[0].yCoord--; break;
case DIR_DOWN: body[0].yCoord++; break;
case DIR_RIGHT: body[0].xCoord++; break;
case DIR_LEFT: body[0].xCoord--; break;
}
// Snake bites itself
for (int i = 1; i < length; ++i) {
if (body[0].xCoord == body[i].xCoord && body[0].yCoord == body[i].yCoord) {
return false;
}
}
// Snake eats food
if (food.xCoord == body[0].xCoord && food.yCoord == body[0].yCoord) {
body[length++] = Point(body[length - 1].xCoord, body[length - 1].yCoord);
}
return true;
}
};
class Board {
Snake *snake;
const char SNAKE_BODY = 'O';
Point food;
const char FOOD = 'o';
int score;
public:
Board() : score(0) {
spawnFood();
snake = new Snake(10, 10);
}
~Board() {
delete snake;
}
int getScore() {
return score;
}
void spawnFood() {
int x = rand() % consoleWidth;
int y = rand() % consoleHeight;
food = Point(x, y);
}
void displayCurrentScore() {
gotoxy(consoleWidth / 2, 0);
printw("Current Score : %d", score);
}
void gotoxy(int x, int y) {
#ifdef _WIN32
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
#else
move(y, x); // ncurses move cursor for Linux (note the order: y, x)
#endif
}
void clearScreen() {
#ifdef _WIN32
system("cls"); // Windows clear screen
#else
clear(); // ncurses clear screen for Linux
#endif
}
void draw() {
clearScreen();
for (int i = 0; i < snake->getLength(); ++i) {
gotoxy(snake->body[i].xCoord, snake->body[i].yCoord);
printw("%c", SNAKE_BODY);
}
gotoxy(food.xCoord, food.yCoord);
printw("%c", FOOD);
displayCurrentScore();
refresh();
}
bool update() {
bool isAlive = snake->move(food);
if (!isAlive) return false;
if (food.xCoord == snake->body[0].xCoord && food.yCoord == snake->body[0].yCoord) {
score++;
spawnFood();
}
return true;
}
void getInput() {
#ifdef _WIN32
if (kbhit()) {
int key = getch();
if (key == 'w' || key == 'W') snake->changeDirection(DIR_UP);
else if (key == 'a' || key == 'A') snake->changeDirection(DIR_LEFT);
else if (key == 's' || key == 'S') snake->changeDirection(DIR_DOWN);
else if (key == 'd' || key == 'D') snake->changeDirection(DIR_RIGHT);
}
#else
int key = getch();
if (key != ERR) {
if (key == 'w' || key == 'W' || key == KEY_UP) snake->changeDirection(DIR_UP);
else if (key == 'a' || key == 'A' || key == KEY_LEFT) snake->changeDirection(DIR_LEFT);
else if (key == 's' || key == 'S' || key == KEY_DOWN) snake->changeDirection(DIR_DOWN);
else if (key == 'd' || key == 'D' || key == KEY_RIGHT) snake->changeDirection(DIR_RIGHT);
}
#endif
}
};
int main() {
#ifndef _WIN32
initscr(); // Start ncurses mode
cbreak(); // Disable line buffering
noecho(); // Don't echo input
keypad(stdscr, TRUE); // Enable special keys
nodelay(stdscr, TRUE); // Non-blocking input
#endif
srand(time(0));
initScreen();
Board *board = new Board();
while (board->update()) {
board->getInput();
board->draw();
#ifdef _WIN32
Sleep(100); // Sleep for Windows
#else
usleep(100 * 1000); // usleep for Linux (microseconds)
#endif
}
#ifndef _WIN32
printw("Game over\n");
printw("Final score is: %d\n", board->getScore());
refresh(); // Refresh the screen to show the output
endwin(); // End ncurses mode
#else
cout << "Game over" << endl;
cout << "Final score is: " << board->getScore() << endl;
#endif
delete board;
return 0;
}