-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fruit.cpp
174 lines (134 loc) · 3.65 KB
/
Fruit.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
#include "Fruit.h"
/*----------------------------------------------------------------------------------------------*/
/* Function controls Fruit movement
* Generates a random direction and sets the Fruit
* Location based on that direction UP up 'd' right etc..
*/
void Fruit:: move(char board[terminal_Size_Y][terminal_Size_X]) {
Delete(board);
char directions[] = { UP,DOWN , LEFT , RIGHT };
int randIndex = rand() % 4; // Generate a number between 0 to 3
char dir = directions[randIndex];
if (checkLegalMove(dir, board)) {
if (dir == UP)
{
setY(getY() - 1);
}
else if (dir == DOWN) {
setY(getY() + 1);
}
else if (dir == RIGHT) {
setX(getX() + 1);
}
else if (dir == LEFT) {
setX(getX() - 1);
}
this->direction = dir;
steps--;
}
draw();
if (steps == 0) {
Delete(board);
setAlive(DEAD, board);
}
}
/*----------------------------------------------------------------------------------------------*/
/* Function takes current coordinates of Fruit and direction and returns if it's valid
*/
bool Fruit::checkLegalMove(char direction, char board[terminal_Size_Y][terminal_Size_X]) {
int newX = this->getX();
int newY = this->getY();
if (direction == UP)
{
newY--;
}
else if (direction == DOWN)
{
newY++;
}
else if (direction == RIGHT)
{
newX++;
}
else if (direction == LEFT)
{
newX--;
}
if (board[newY][newX] == ' ' || board[newY][newX] == FOOD)
return true;
else
return false;
}
/*----------------------------------------------------------------------------------------------*/
/* Function draws the Fruit on the screen
* goes to the Fruit's coordinates and draws it
*/
void Fruit::draw() {
gotoxy(getX(), getY());
cout << value;
}
/*----------------------------------------------------------------------------------------------*/
/* Hides the Fruit from the User
*/
void Fruit::Delete(char board[][terminal_Size_X], bool silentMode) {
if (!silentMode) {
setTextColor(Color::LIGHTGREY);
gotoxy(getX(), getY());
cout << board[getY()][getX()];
}
}
/*----------------------------------------------------------------------------------------------*
* GETTERS:
*----------------------------------------------------------------------------------------------*/
/* Value GETTER
*/
int Fruit:: getValue() const {
return value;
}
/*----------------------------------------------------------------------------------------------*/
/* Char Direction GETTER
*/
char Fruit::getDirection() const {
return direction;
}
/*----------------------------------------------------------------------------------------------*/
/* Int steps GETTER
*/
int Fruit::getSteps() const {
return steps;
}
/*----------------------------------------------------------------------------------------------*/
/* Boolean alive GETTER
*/
bool Fruit::getAlive() const {
return alive;
}
/*----------------------------------------------------------------------------------------------*
* SETTERS:
*----------------------------------------------------------------------------------------------*/
/* Value SETTER
*/
void Fruit::setValue() {
/* initialize random seed: */
srand(time(NULL));
value = rand() % 5 + 5; // Generate a number between 5 to 9
}
/*----------------------------------------------------------------------------------------------*/
/* steps SETTER
*/
void Fruit::setSteps(int newSteps) {
steps = newSteps;
}
/*----------------------------------------------------------------------------------------------*/
/* Alive SETTER
*/
void Fruit::setAlive(bool newState,char board[][terminal_Size_X], bool silentMode) {
alive = newState;
if (alive) {
setValue();
steps = Max_Fruit_Steps;
}
else {
this->Delete(board, silentMode);
}
}