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
266
267
268
269
270
271
272
273
274
275
|
#include "terminal-game-utils.hpp"
#include <chrono>
#include <cstdint>
#include <future>
#include <iostream>
#include <ostream>
#include <poll.h>
#include <queue>
#include <string>
#include <termios.h>
#include <thread>
#include <tuple>
#include <unistd.h>
struct termios orig_termios;
class Snake {
private:
struct mSnakeUnit {
int x;
int y;
std::string appearance;
};
const std::map<char, std::string> HEAD_ARROWS = {
{'l', "◁"}, {'d', "▽"}, {'u', "△"}, {'r', "▷"}};
const std::string SNAKE_BODY = "═║╔╗╚╝";
bool mSnakeAteApple = false;
char mSnakeDirection = 'r';
bool mRunning = true;
int mDelay = 500;
int mWidth, mHeight, mStartingSpeed;
double mAcceleration;
mSnakeUnit mLastDequeued;
std::deque<mSnakeUnit> mSnake;
std::queue<tgu::GameKey> mKeyQueue;
std::string mRepeat(int times, std::string str) {
std::string returned;
for (int i = 0; i < times; i++)
returned += str;
return returned;
}
std::uint8_t mGetDirectionByte(mSnakeUnit pos, mSnakeUnit goal) {
if (pos.x > goal.x)
return 0b0001;
if (pos.x < goal.x)
return 0b1000;
if (pos.y > goal.y)
return 0b0100;
if (pos.y < goal.y)
return 0b0010;
return 0b0000;
}
std::string mGetTurningCharacter(mSnakeUnit Before, mSnakeUnit Current,
mSnakeUnit After) {
std::uint8_t BeforeDirection = mGetDirectionByte(Current, Before);
std::uint8_t AfterDirection = mGetDirectionByte(Current, After);
std::uint8_t CharacterBytes = BeforeDirection + AfterDirection;
if (CharacterBytes == 0b1001)
return "═";
if (CharacterBytes == 0b0110)
return "║";
if (CharacterBytes == 0b1010)
return "╔";
if (CharacterBytes == 0b0011)
return "╗";
if (CharacterBytes == 0b1100)
return "╚";
if (CharacterBytes == 0b0101)
return "╝";
return "";
}
void mPrintBoardOutlines() {
tgu::MoveCursor(0, 0);
std::string horizontalLine = mRepeat(mWidth * 2 - 1, "═");
std::string horizontalSpaces = std::string(mWidth * 2 - 1, ' ');
std::cout << "╔" << horizontalLine + "╗\n\r";
for (int i = 0; i < mHeight; i++) {
std::cout << "║" + horizontalSpaces << "║\n\r";
}
std::cout << "╚" << horizontalLine + "╝\n\r";
}
char mFlipDirection(char c) {
if (c == 'l')
return 'r';
if (c == 'r')
return 'l';
if (c == 'd')
return 'u';
if (c == 'u')
return 'd';
return ' ';
}
void mSetSnakeDirection() {
tgu::GameKey popped = mKeyQueue.front();
mKeyQueue.pop();
char PotentialSnakeDirection;
if (popped == tgu::GameKey::W || popped == tgu::GameKey::UPARROW)
PotentialSnakeDirection = 'u';
else if (popped == tgu::GameKey::A || popped == tgu::GameKey::LEFTARROW)
PotentialSnakeDirection = 'l';
else if (popped == tgu::GameKey::S || popped == tgu::GameKey::DOWNARROW)
PotentialSnakeDirection = 'd';
else if (popped == tgu::GameKey::D ||
popped == tgu::GameKey::RIGHTARROW) {
PotentialSnakeDirection = 'r';
} else
return;
if (PotentialSnakeDirection == mFlipDirection(mSnakeDirection))
return;
else
mSnakeDirection = PotentialSnakeDirection;
}
std::tuple<int, int> mGetDirectionOffset(char c) {
tgu::MoveCursor(30, 30);
std::cout << c;
if (c == 'l')
return std::tuple<int, int>(-1, 0);
if (c == 'd')
return std::tuple<int, int>(0, 1);
if (c == 'u')
return std::tuple<int, int>(0, -1);
if (c == 'r') {
return std::tuple<int, int>(1, 0);
}
std::cout << "Some ting wong. Direction is " << +c;
return std::tuple<int, int>(0, 0);
}
void mSnakeStep() {
if (!mSnakeAteApple) {
mLastDequeued = mSnake.front();
mSnake.pop_front();
}
std::tuple<int, int> Offset = mGetDirectionOffset(mSnakeDirection);
int PotentialX = mSnake.back().x + std::get<0>(Offset);
int PotentialY = mSnake.back().y + std::get<1>(Offset);
std::cout << "Potentials: " << PotentialX << ", " << PotentialY;
std::cout << "Actual: " << mSnake.back().x << ", " << mSnake.back().y;
if (mSnake[mSnake.size() - 2].x != PotentialX ||
mSnake[mSnake.size() - 2].y != PotentialY) {
mSnake.push_back(
{PotentialX, PotentialY, HEAD_ARROWS.at(mSnakeDirection)});
mSnake[mSnake.size() - 2].appearance = mGetTurningCharacter(
mSnake[mSnake.size() - 3], mSnake[mSnake.size() - 2],
mSnake[mSnake.size() - 1]);
} else {
mRunning = false;
std::cout << "Stop the game. Snake somehow ate itself????";
}
tgu::MoveCursor(mHeight + 2, 0);
std::cout << "Brother. " << mSnake.back().x << ", " << mSnake.back().y;
}
void mGameTick() {
if (mKeyQueue.size() != 0) {
mSetSnakeDirection();
}
mSnakeStep();
}
void mInputLoop() {
tgu::GameKey key;
while (mRunning) {
key = tgu::GetKeyPress();
if (mKeyQueue.size() <= 2)
mKeyQueue.push(key);
}
}
bool mHorizontalConnectors(mSnakeUnit, mSnakeUnit) { return false; }
void mDrawSnake() {
for (int i = 0; i < mSnake.size(); i++) {
mSnakeUnit &s = mSnake[i];
if (s.appearance == "═") {
tgu::MoveCursor(s.y + 2, (s.x * 2) + 1);
std::cout << mRepeat(3, s.appearance);
} else if (i != 0 && mHorizontalConnectors(s, mSnake[i - 1])) {
std::cout << "";
} else {
tgu::MoveCursor(s.y + 2, (s.x * 2) + 2);
std::cout << s.appearance;
}
}
tgu::MoveCursor(mHeight + 2, 0);
std::cout << "Brother. " << mSnake.back().x << ", " << mSnake.back().y;
tgu::MoveCursor(mLastDequeued.y + 2, (mLastDequeued.x * 2) + 1);
std::cout << " ";
std::cout << std::flush;
}
void mGameLoop() {
while (mRunning) {
std::this_thread::sleep_for(std::chrono::milliseconds(mDelay));
mGameTick();
mDrawSnake();
}
}
public:
struct GameSettings {
int width = 14;
int height = 14;
int startingSpeed = 3;
double acceleration = 0.3;
};
Snake(const GameSettings &settings)
: mWidth(settings.width), mHeight(settings.height),
mStartingSpeed(settings.startingSpeed),
mAcceleration(settings.acceleration) {
if (mWidth < 5)
mWidth = 5;
if (mHeight < 5)
mHeight = 5;
}
void StartGame() {
tgu::EnableRawMode();
tgu::MakeCursorInvisible();
tgu::EnterGameBuffer();
mPrintBoardOutlines();
mSnake.push_back({0, 2, "═"});
mSnake.push_back({1, 2, "═"});
mSnake.push_back({2, 2, "═"});
mSnake.push_back({3, 2, "⇒"});
mSnakeDirection = 'r';
auto InputLoopResponse =
std::async(std::launch::async, [this] { mInputLoop(); });
mGameLoop();
tgu::LeaveGameBuffer();
}
};
int main() {
Snake hi({});
hi.StartGame();
}
|