-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (148 loc) · 3.92 KB
/
main.cpp
File metadata and controls
170 lines (148 loc) · 3.92 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <ncurses.h>
struct Coordinate
{
int x;
int y;
};
Coordinate operator+(const Coordinate a, const Coordinate b)
{
return {a.x + b.x, a.y + b.y};
}
bool operator==(const Coordinate a, const Coordinate b)
{
return a.x == b.x && a.y == b.y;
}
void print(const Coordinate c, char ch = 'x')
{
mvaddch(c.y, c.x, ch);
}
enum class Direction
{
UP = 0,
LEFT = 1,
DOWN = 2,
RIGHT = 3,
};
// ncurses initialization / destruction functions
class Scoped_Screen
{
public:
Scoped_Screen();
~Scoped_Screen();
};
Scoped_Screen::Scoped_Screen()
{
initscr();
cbreak();
noecho();
clear();
keypad(stdscr, TRUE);
curs_set(0);
timeout(75);
refresh();
}
Scoped_Screen::~Scoped_Screen()
{
nocbreak();
endwin();
}
bool opposite(Direction a, Direction b)
{
switch (a)
{
case (Direction::UP):
return b == Direction::DOWN;
case (Direction::LEFT):
return b == Direction::RIGHT;
case (Direction::DOWN):
return b == Direction::UP;
case (Direction::RIGHT):
return b == Direction::LEFT;
default:
return false;
}
}
int main(int argc, char *argv[])
{
Scoped_Screen s;
int height;
int width;
getmaxyx(stdscr, height, width);
std::vector<Coordinate> snek(1, {1, 1});
Direction snek_direction = Direction::RIGHT;
// adjust based on given direction
Coordinate adjust[4] = {
{0, -1}, // UP
{-1, 0}, // LEFT
{0, 1}, // DOWN
{1, 0} // RIGHT
};
Coordinate ball{1 + rand() % (width - 1), 1 + rand() % (height - 1)};
int score{1};
int c{'\0'};
do
{
// print stuff
clear();
print(ball, 'O');
std::for_each(snek.begin(), snek.end(), [](auto c) { print(c); });
mvprintw(0, 0, "SNEK - score: %i", score);
// display some information about terminal height/width and snake position
// mvprintw(0, 30, "max height: %i, max width: %i current: (%i, %i)", height, width, snek.front().x, snek.front().y);
refresh();
// get new snake direction based on keyboard input
Direction new_direction = snek_direction;
c = getch();
switch (c)
{
case KEY_UP:
new_direction = Direction::UP;
break;
case KEY_LEFT:
new_direction = Direction::LEFT;
break;
case KEY_DOWN:
new_direction = Direction::DOWN;
break;
case KEY_RIGHT:
new_direction = Direction::RIGHT;
break;
}
// new snek direction should not be backwards into itself
if (!opposite(snek_direction, new_direction) || snek.size() == 1)
snek_direction = new_direction;
// move the snek
snek.back() = snek.front() + adjust[(int)snek_direction];
std::rotate(snek.rbegin(), snek.rbegin() + 1, snek.rend());
// check snek boundaries
std::for_each(snek.begin(), snek.end(), [&](const Coordinate &i) {
if (i.x >= width || i.x < 0)
c = 'q';
if (i.y >= height || i.y < 1)
c = 'q';
});
// check snek self collisions
if (std::find(snek.begin() + 1, snek.end(), snek.front()) != snek.end())
c = 'q';
// check ball collision
if (std::find(snek.begin(), snek.end(), ball) != snek.end())
{
// increment score, generate new ball, add section to snek
++score;
do
{
ball = {rand() % width, rand() % height};
} while (std::find(snek.begin(), snek.end(), ball) != snek.end());
snek.push_back(snek.back());
}
} while (c != 'q');
clear();
mvprintw(height/2, width/2 - 5, "FINAL SCORE");
mvprintw(height/2 - 1, width/2 - 1, "%i", score);
timeout(-1);
getch();
return 0;
}