-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
283 lines (233 loc) · 8.99 KB
/
main.cpp
File metadata and controls
283 lines (233 loc) · 8.99 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
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
276
277
278
279
280
281
282
283
/************************************************************
*** Title: sNaKe GaMe **************************************
*** Author: cold_summer aka Elizabeth Maslennikova **********
*** Date: 30/11/2020 ****************************************
*** Description: Save my farm from rodents invasion *********
************************************************************/
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <iostream>
const int VERTICAL = 25;
const int HORIZONTAL = VERTICAL*2;
int score = 2;
char board_array[VERTICAL][HORIZONTAL], users_input;
bool game_over;
typedef struct axis_points// START of struct
{
int x, y;
}points;// END of struct
points randomizer(points *p)// START of func
{// randomizes random x's and y's for both obstacles and mice
p->x = rand()%HORIZONTAL;
p->y = rand()%VERTICAL;
return *p;// pointer, 'coz in class obstacle will be implemented class mouse
}// END of func
class board // START of class
{
public:
board ()// START of constructor
{
for(int columns = 0; columns <= HORIZONTAL ; columns++)// fills the board with symbols
{
board_array[0][columns] = '-';
board_array[VERTICAL -1][columns] = '_';
}
for(int rows = 0; rows < VERTICAL; rows++)
board_array[rows][0] = board_array[VERTICAL -1][HORIZONTAL] = '|';
for(int rows = 1; rows <= VERTICAL - 2; rows++)
for(int columns = 1; columns <= HORIZONTAL - 1; columns++)
board_array[rows][columns]= ' ';
}// END of constructor
};// END of class
class obstacles// START of class
{
public:
points opoint[4];// stands for 'obstacle point'
void setNew_points()// START of method
{
for(int i = 0; i < 4; i++)// queue of arrays of datatype points 'οpoint' (obstacles' point)
opoint[i] = randomizer(opoint + i);// gives to each x and y random values
}// END of method
void random_types()// START of method
{// creates different types of obstacles (there are 3 types of them: '#','+' and '>')
int num_of_obstacles = 5;
do
{
for(int i = 0; i < 4; i++)// queue of arrays of datatype points 'opoint' (obstacles' point)
{
int random = rand()%3;// random type of obstacle for 'if operator'
for(int j = 0; j < 8; j++)// fills columns with 8 symbols: "########", "++++++++", ">>>>>>>>"
{
if(random == 0)
board_array[opoint[i].y][opoint[i].x + j] = '#';
if(random == 1)
board_array[opoint[i].y][opoint[i].x + j] = '+';
if(random == 2)
board_array[opoint[i].y][opoint[i].x + j] = '>';
}
}
num_of_obstacles--;
} while (num_of_obstacles > 0);// 4 obstacles must be created
}// END of method
};// END of class
class snake// START of class
{
public:
points spoint;// var of datatype struct points stands for 'snake's point'
points *tail = (points*)malloc(score*sizeof (points));// array of pointers, 'coz user's success is dynamical, 'score' is not constant
snake ()// START of constructor for initial snake's head values
{// for them to be in the middle of the board
spoint.x = HORIZONTAL/2;
spoint.y = VERTICAL/2;
}// END of constructor
void setHead()// START of a method to change the values of snake's head
{// in accordance with the user's input
switch(getch())
{
case 'W':
spoint.y--;
break;
case 'A':
spoint.x--;
break;
case 'D':
spoint.x++;
break;
case 'S':
spoint.y++;
break;
case 'X':// if player wants to quit the game
game_over = true;
break;
}
}// END of method
void setTail()// START of method
{
tail[0].y = spoint.y;
tail[0].x = spoint.x;
board_array[((tail)->y)][((tail)->x)] = 'o';
for(int i = score; i > 0; i--)
{
tail[i].y = tail[i - 1].y;
tail[i].x = tail[i - 1].x;
board_array[((tail + i)->y)][((tail + i)->x)] = 'o';// sets new points of tail in board array
if(i == score)
board_array[((tail + i)->y)][((tail + i)->x)] = ' ';
// clears old points of tail (for them not be printed again)
}
}// END of method
void check_borders()// START of method
{// checks if the snake has gone beyond the borders and returns it to opposite side
if(spoint.y < 0)
spoint.y = VERTICAL - 1;
if(spoint.y >= VERTICAL)
spoint.y = 0;
if(spoint.x < 0)
spoint.x = HORIZONTAL - 1;
if(spoint.x >= HORIZONTAL)
spoint.x = 0;
}// END of method
/*void collision_with_tail()// START of method
{// checks if the snake ate its tail
for(int i = score; i >= 0; i--)
if(board_array[spoint.y][spoint.x] == board_array[((tail + i)->y)][((tail + i)->x)])
game_over = true;
}// END of method
*/
void collision_with_obstacles(obstacles o)// START of method
{// checks if the snake hit obstacles
for(int i = 0; i < 4; i++)// 4 different RANDOM obstacles: '#', '>', '+'
for(int j = 0; j < 8; j++)// 8 symbols each one: "########", ">>>>>>>>", "++++++++"
if(board_array[spoint.y][spoint.x] == board_array[o.opoint[i].y][o.opoint[i].x + j])
game_over = true;
}// END of method
};// END of class
class mouse// START of class
{
public:
points mpoint;// stands for mouse's point
mouse()// START of constructor
{// sets random point for new mouse
mpoint = randomizer(&mpoint);
}// END of constructor
void setNew_mouse(snake s, obstacles o)// START of method
{
if(mpoint.y == s.spoint.y && mpoint.x == s.spoint.x)// checks if snake ate mouse
{
mpoint = randomizer(&mpoint);// sets random point for new mouse
for(int i = 0; i < 5; i++)// controls the mouse not be printed within borders' lines and obstacles
if((mpoint.y == VERTICAL && mpoint.x == HORIZONTAL) && (mpoint.y == o.opoint[i].y && mpoint.x == o.opoint[i].x))
mpoint = randomizer(&mpoint);// sets random point for new mouse
o.setNew_points();// sets random points for new obstacles
o.random_types();// sets random types for new obstacles
score++;// increases user's success
}
}// END of method
};// END of class
void draw(obstacles o,snake s, mouse m, board b)// START of func
{// draws the filled array board
board_array[s.spoint.y][s.spoint.x] = 'O';// sets new points of snake in board array
board_array[m.mpoint.y][m.mpoint.x] = '*';// sets new points of mouse in board array
for(int rows = 0; rows <= VERTICAL; rows++)
{
std::cout <<"\t\t\t\t";// for the board to be in the ~~middle
for(int columns = 0; columns <= HORIZONTAL; columns++)
std::cout << board_array[rows][columns];
std::cout << std:: endl;
}
// clears old points of snake (for them not be printed again)
board_array[s.spoint.y][s.spoint.x] = ' ';
// clears old points of mouse (for them not be printed again)
board_array[m.mpoint.y][m.mpoint.x] = ' ';
std::cout << std:: endl << "\t\t\t\t Press [X] if you want to quit ";
std::cout << std:: endl << "\t\t\t\t _ _ __";
std::cout << std:: endl << "\t\t\t\t (q\\_/p) {0O}";
std::cout << std:: endl << "\t\t\t\t |. .| \\__/";
std::cout << std:: endl << "\t\t\t\t \\ =\\,/= ~~~~~~~~~~~~~~~~~~~~~~~~~~~ /^/";
if(score <= 1)
std::cout << std:: endl << "\t\t\t\t )/ _ \\ |\\ | Your score is: " << score-2 << " mouse | ( ( ";
if(score > 1)
std::cout << std:: endl << "\t\t\t\t )/ _ \\ |\\ | Your score is: " << score-2 << " mouses | ( ( ";
std::cout << std:: endl << "\t\t\t\t (/\\):(/\\ )\\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ \\_\\_____";
std::cout << std:: endl << "\t\t\t\t \\_____/ |Oo\\ (_______)";
std::cout << std:: endl << "\t\t\t\t ^ ^ (_________()Oo";
}// END of func
int main()// START of MAIN
{
srand(time(NULL));
board b;// object of a class 'board' & its constructor
mouse m;// object of a class 'mouse' & its constructor
snake s;// object of a class 'snake' & its constructor
obstacles o;// object of a class 'obstacles' & its constructor
o.setNew_points();// sets new points for obstacles
o.random_types();// sets different types of random obstacles
for(int i = 0; i < 5; i++)// controls the mouse not be printed within borders' lines and obstacles
if((m.mpoint.y == VERTICAL && m.mpoint.x == HORIZONTAL) && (m.mpoint.y == o.opoint[i].y && m.mpoint.x == o.opoint[i].x))
m.mpoint = randomizer(&m.mpoint);// sets random point for new mouse
game_over = false;
while(!game_over)// START of snake-game
{
std::cout << std::endl;
draw(o,s,m,b);
s.setHead();
m.setNew_mouse(s,o);
s.setTail();
//s.collision_with_tail();
s.check_borders();
s.collision_with_obstacles(o);
}// END of while loop
for(int i = 0; i < score; i++)// returns borrowed memory to OS
free(s.tail + i);
if(game_over == true)
{
std::cout << std::endl << "You lost!\nPlay again?\n y/ n\n";
getch();
if(getch() == 'Y')
return main();
else
std::cout <<" Hope to see you soon!\nBye ~";
}
return 0;
}// END of MAIN