-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
193 lines (157 loc) · 5.54 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <vector>
#include <iostream>
#include <chrono>
#include <ncurses.h>
#include <set>
#include <algorithm>
#include "snake.h"
#include "point.h"
#include "berry.h"
#include "world.h"
std::set<Point> getAllCoordinates(const Point& worldStart, const Point& worldEnd)
{
auto allCoordinates = std::set<Point>{};
for (int x = worldStart.x + 1; x < worldEnd.x; x++)
{
for (int y = worldStart.y + 1; y < worldEnd.y; y++)
{
allCoordinates.insert(Point{x, y});
}
}
return allCoordinates;
}
std::vector<Point> getEmptyCoordinates(const std::set<Point>& allCoordinates, const std::vector<std::reference_wrapper<Entity>>& entities)
{
auto entityCoordinates = std::set<Point>{};
for (auto entity : entities)
{
auto coordinates = entity.get().getCoordinates();
entityCoordinates.insert(coordinates.begin(), coordinates.end());
}
std::vector<Point> emptyCoordinates;
std::set_difference(allCoordinates.begin(), allCoordinates.end(), entityCoordinates.begin(), entityCoordinates.end(), std::inserter(emptyCoordinates, emptyCoordinates.end()));
return emptyCoordinates;
}
int main()
{
int windowHeight = 0, windowWidth = 0;
auto window = initscr();
start_color();
noecho();
nodelay(window, TRUE);
curs_set(FALSE);
getmaxyx(stdscr, windowHeight, windowWidth);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(3, COLOR_WHITE, COLOR_BLACK);
init_pair(4, COLOR_WHITE, COLOR_RED);
int worldWidth = 20;
int worldHeight = 10;
if (windowWidth < worldWidth || windowHeight < worldHeight + 1) {
printf("Terminal window is too small to play the game.\n");
return 0;
}
int worldStartX = windowWidth / 2 - worldWidth / 2;
int worldStartY = windowHeight / 2 - worldWidth / 2;
int worldEndX = worldStartX + worldWidth;
int worldEndY = worldStartY + worldHeight;
const Point worldStart{ worldStartX, worldStartY };
const Point worldEnd{ worldEndX, worldEndY };
auto allCoordinates = getAllCoordinates(worldStart, worldEnd);
std::vector<std::reference_wrapper<Entity>> entities;
World world(worldStart, worldEnd);
entities.push_back(world);
Snake snake(worldStartX + 5, worldStartY + 5);
entities.push_back(snake);
auto berry = spawnBerry(getEmptyCoordinates(allCoordinates, entities));
entities.push_back(berry);
auto lastMove = std::chrono::high_resolution_clock::now();
bool gameOver = false;
int score = 0;
while (true)
{
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastMove).count();
// Handle input
switch (wgetch(window))
{
case 'w':
snake.setDirection(Direction::UP);
break;
case 's':
snake.setDirection(Direction::DOWN);
break;
case 'a':
snake.setDirection(Direction::LEFT);
break;
case 'd':
snake.setDirection(Direction::RIGHT);
break;
}
// Update
if (elapsedTime > 200 && !gameOver) {
snake.move();
lastMove = std::chrono::high_resolution_clock::now();
}
auto collision = snake.checkCollision(entities);
if (collision.has_value())
{
switch (collision.value().second)
{
case Material::WALL:
case Material::SNAKE_BODY:
gameOver = true;
break;
case Material::BERRY:
score += 1;
entities.erase(std::remove_if(entities.begin(), entities.end(), [collision](auto entity) {
return std::addressof(entity.get()) == std::addressof(collision.value().first);
}));
berry = spawnBerry(getEmptyCoordinates(allCoordinates, entities));
entities.push_back(berry);
snake.grow();
break;
case Material::SNAKE_HEAD:
break;
}
}
// Render
clear();
attron(COLOR_PAIR(3));
mvprintw(worldEndY + 1, worldEndX - worldWidth, "Score: %i", score);
for (auto entity : entities)
{
const char* character;
for (auto point : entity.get().getCoordinates())
{
switch (point.material)
{
case Material::SNAKE_HEAD:
attron(COLOR_PAIR(1));
character = "O";
break;
case Material::SNAKE_BODY:
attron(COLOR_PAIR(1));
character = "o";
break;
case Material::WALL:
attron(COLOR_PAIR(3));
character = "#";
break;
case Material::BERRY:
attron(COLOR_PAIR(2));
character = ".";
break;
}
mvprintw(point.y, point.x, character);
}
}
if (gameOver) {
attron(COLOR_PAIR(4));
mvprintw(worldEndY - worldHeight / 2, worldEndX - worldWidth / 2 - 5, "GAME OVER");
}
refresh();
}
endwin();
return 0;
}