-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminakes.cpp
218 lines (207 loc) · 5.04 KB
/
Terminakes.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
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
//# WRITTEN BY THITI MAHAWANNAKIT
//# VERSION 1.00
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
// Static variable
const int width = 20, height = 20;
//---------------
// Enumeration declaration
enum headingDirection {
STOP,
UP,
DOWN,
LEFT,
RIGHT
};
enum class typeOfWallCreation {
TOP,
BOTTOM
};
//------------------------
// Entity
struct Snake {
double xPos, yPos;
int tailX[100], tailY[100], numberOfTails=0;
headingDirection hDir;
Snake() {
xPos = width / 2;
yPos = height / 2;
hDir = STOP;
}
void READER_Movement()
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
hDir = UP;
break;
case 'a':
hDir = LEFT;
break;
case 's':
hDir = DOWN;
break;
case 'd':
hDir = RIGHT;
break;
}
}
}
};
struct Food {
int xPos, yPos;
void RandomPosition() {
xPos = rand() % width; // random x position of the food
yPos = rand() % height;// random y position of the food
}
};
//--------------------------
// functions
static void ClearTerminalScreen() {
system("cls");
}
// ---------------------
struct GAME_UTILS {
int score = 0;
bool isGameOver;
Snake ENTITY_snake;
Food ENTITY_food;
GAME_UTILS() {
isGameOver = false;
debug_enabled = false;
}
void Setup() {
isGameOver = false;
ENTITY_snake.hDir = STOP;
// Setting position of the snake to be centered of the generated box
ENTITY_snake.xPos = width / 2;
ENTITY_snake.yPos = height / 2;
ENTITY_food.RandomPosition();
score = 0;
//------------------------------------------------------------------
}
void Draw() {
ClearTerminalScreen();
WALL_Draw(typeOfWallCreation::TOP);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0) cout << "#"; // printing the left wall
if (i == ENTITY_snake.yPos && j == ENTITY_snake.xPos) cout << "O"; //printing the snake itself
else if (i == ENTITY_food.yPos && j == ENTITY_food.xPos) cout << "+"; // printing the food itself
else {
bool isPrinted = false;
for (int k = 0; k < ENTITY_snake.numberOfTails; k++)
{
if (ENTITY_snake.tailX[k] == j && ENTITY_snake.tailY[k] == i)
{
cout << "o";
isPrinted = true;
}
}
if (!isPrinted)
cout << " "; // showing empty space if nothings met the condition
}
if (j == width - 1) cout << "#"; // printing the right wall
}
cout << endl;
}
WALL_Draw(typeOfWallCreation::BOTTOM);
cout << "Score: " << score << endl; // showing the score at the bottom of the box
}
void HANDLER_Movement() {
ENTITY_snake.READER_Movement();
}
void TICK_Event() {
int prevX = ENTITY_snake.tailX[0];
int prevY = ENTITY_snake.tailY[0];
int prev2X, prev2Y;
ENTITY_snake.tailX[0] = ENTITY_snake.xPos;
ENTITY_snake.tailY[0] = ENTITY_snake.yPos;
for (int i = 1; i < ENTITY_snake.numberOfTails; i++)
{
prev2X = ENTITY_snake.tailX[i];
prev2Y = ENTITY_snake.tailY[i];
ENTITY_snake.tailX[i] = prevX;
ENTITY_snake.tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (ENTITY_snake.hDir)
{
case LEFT:
ENTITY_snake.xPos--;
break;
case RIGHT:
ENTITY_snake.xPos++;
break;
case UP:
ENTITY_snake.yPos--;
break;
case DOWN:
ENTITY_snake.yPos++;
break;
default:
break;
}
// Portal edge
if (ENTITY_snake.xPos >= width) ENTITY_snake.xPos = 0; else if (ENTITY_snake.xPos < 0) ENTITY_snake.xPos = width - 1;
if (ENTITY_snake.yPos >= height) ENTITY_snake.yPos = 0; else if (ENTITY_snake.yPos < 0) ENTITY_snake.yPos = height - 1;
//-----------
// Check if hitting it owns tails
for (int i = 0; i < ENTITY_snake.numberOfTails; i++)
if (ENTITY_snake.tailX[i] == ENTITY_snake.xPos && ENTITY_snake.tailY[i] == ENTITY_snake.yPos)
isGameOver = true;
// --------------------------------
if (debug_enabled) {
cout << "DEBUG MODE: ON [INSPECT X,Y AXIS OF THE ENTITYS]" << endl;
cout << "snake_x: " << ENTITY_snake.xPos << ", snake_y: " << ENTITY_snake.yPos << endl;
cout << "food_x: " << ENTITY_food.xPos << ", food_y: " << ENTITY_food.yPos << endl;
}
if (ENTITY_snake.xPos == ENTITY_food.xPos && ENTITY_snake.yPos == ENTITY_food.yPos)
{
score += 10;
ENTITY_food.RandomPosition();
ENTITY_snake.numberOfTails++;
}
}
void SetDebugMode(bool isToggle) {
debug_enabled = isToggle;
}
private:
bool debug_enabled;
// In order to draw a special thing on separate top/bottom wall [Could be implement later if needed => but stay the same for now]
void WALL_Draw(typeOfWallCreation type) {
switch (type)
{
case typeOfWallCreation::TOP:
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
break;
case typeOfWallCreation::BOTTOM:
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
break;
default:
break;
}
}
};
int main() {
//Creating game controller
GAME_UTILS controller;
controller.SetDebugMode(true);
//------------------------
controller.Setup();
while (!controller.isGameOver) {
controller.Draw();
controller.HANDLER_Movement();
controller.TICK_Event();
}
return 0;
}