-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
170 lines (139 loc) · 4.34 KB
/
main.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
#include <iostream>
#include <cstdlib>
#include <conio.h>
bool isGameOver = false;
using namespace std;
class Food {
public:
int x, y;
int consumed;
};
class SnakePart {
public:
int x, y;
};
class Snake {
public:
static const int SnakeLength = 256;
int length = 0;
SnakePart body[SnakeLength];
};
class Game {
protected:
static const int rows = 35;
static const int columns = 35;
static const int foods = 50;
char board[rows][columns];
Food food[foods];
Snake snake;
public:
Game() {
fillBoard();
}
void fillBoard() {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
if(i == 0 || i == rows - 1 || j == 0 || j == columns - 1) {
board[i][j] = '#';
}
else {
board[i][j] = ' ';
}
}
}
}
void printBoard() {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
void DrawSnake() {
for(int i = 1; i < snake.length; i++) {
board[snake.body[i].y][snake.body[i].x] = '*';
}
board[snake.body[0].y][snake.body[0].x] = '@';
}
void moveSnake(int dx, int dy) {
for(int i = snake.length-1; i > 0; i-- ) {
snake.body[i] = snake.body[i-1];
}
snake.body[0].x += dx;
snake.body[0].y += dy;
}
void HandleInput() {
char ch = getch();
switch(ch) {
case 'w': moveSnake(0, -1); break;
case 's': moveSnake(0, 1); break;
case 'a': moveSnake(-1, 0); break;
case 'd': moveSnake(1, 0); break;
case 'q': isGameOver = true; break;
}
}
void clearScreen() {
system("cls");
}
void GameRules() {
for(int i = 0; i < foods; i++) {
if(!food[i].consumed) {
if(food[i].x == snake.body[0].x && food[i].y == snake.body[0].y) {
food[i].consumed = 1;
snake.length++;
cout << "Food eaten! Snake length: " << snake.length << endl;
}
}
}
for(int i = 1; i < snake.length; i++) {
if(snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y) {
isGameOver = true;
cout << "Game Over, Snake bit itself";
cout << "Score: " << snake.length << endl;
}
}
if(snake.body[0].x == 0 || snake.body[0].x == columns - 1
|| snake.body[0].y == 0 || snake.body[0].y == rows - 1) {
isGameOver = true;
cout << "Game Over, Snake hit the wall." << endl;
cout << "Score: " << snake.length;
}
}
void FoodSetUp() {
for(int i = 0; i < foods; i++) {
food[i].x = 1 + rand() % (columns - 2);
food[i].y = 1 + rand() % (rows - 2);
food[i].consumed = 0;
}
}
void SnakeSetUp() {
snake.length = 1;
snake.body[0].x = 1 + rand() % (columns - 2);
snake.body[0].y = 1 + rand() % (rows - 2);
cout << "Initial Snake Position: (" << snake.body[0].x << ", " << snake.body[0].y << ")" << endl;
}
void DrawFood() {
for(int i = 0; i < foods; i++) {
if(!food[i].consumed) {
board[food[i].y][food[i].x] = '+';
}
}
}
};
int main() {
Game game;
srand(time(0));
game.SnakeSetUp();
game.FoodSetUp();
while(!isGameOver) {
game.fillBoard();
game.DrawFood();
game.DrawSnake();
game.clearScreen();
game.printBoard();
game.GameRules();
if(!isGameOver) game.HandleInput();
}
return 0;
}