-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControl.cpp
176 lines (156 loc) · 4.44 KB
/
Control.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
#include "Control.h"
#include "Display.h"
#include <cstdio>
#define X_VELOCITY 1
#define Y_VELOCITY 1
location snakeSections[MAX_SNAKE_SIZE];
int snakeSize;
bool stateMachineActive;
static enum SnakeDirection {
up,
down,
left,
right,
} movementDirection;
static enum ControlState {
init_st,
init_snake_st,
move_snake_st,
check_snake_st,
wait_for_input_st,
game_over_st,
} currentState;
void Control_init() {
snakeSize = DEFAULT_SNAKE_SIZE;
movementDirection = right;
// Assign the snake it's initial positions
for (int initialSnakeSection = 0; initialSnakeSection < snakeSize;
++initialSnakeSection) {
// Default snake sections get assigned from the top right corner
// downward in such a way that the snake faces downward when the game
// starts
snakeSections[initialSnakeSection].x = snakeSize - initialSnakeSection;
snakeSections[initialSnakeSection].y = 1;
printf("(%d, %d)\n", snakeSections[initialSnakeSection].x,
snakeSections[initialSnakeSection].y);
}
stateMachineActive = true;
currentState = init_st;
}
void Control_debugStateMachine() {
static bool firstPass = true;
static enum ControlState previousState;
if (currentState != previousState || firstPass) {
firstPass = false;
previousState = currentState;
switch (currentState) {
case init_st:
printf("\tEntering Control Init State\n");
break;
case init_snake_st:
printf("\tEntering Init Snake State\n");
break;
case move_snake_st:
printf("\tEntering Control Move Snake St\n");
break;
case check_snake_st:
printf("\tEntering Control Check Snake St\n");
break;
case wait_for_input_st:
printf("\tEntering Control Wait For Input St\n");
break;
case game_over_st:
printf("\tEntering Control Game Over St\n");
break;
}
}
}
void Control_tickFunction() {
// Transitions of the state machine
switch (currentState) {
case init_st:
currentState = init_snake_st;
break;
case init_snake_st:
currentState = move_snake_st;
break;
case move_snake_st:
currentState = check_snake_st;
break;
case check_snake_st:
if (Control_detectWallCollision() || Control_detectTailCollision()) {
currentState = game_over_st;
} else {
Control_updateScreen();
currentState = wait_for_input_st;
}
break;
case wait_for_input_st:
currentState = move_snake_st;
break;
case game_over_st:
break;
}
Control_debugStateMachine();
// Actions of the state machine
switch (currentState) {
case init_st:
break;
case init_snake_st:
break;
case move_snake_st:
Control_updateSnake();
break;
case check_snake_st:
break;
case wait_for_input_st:
// wait for the user's input...
break;
case game_over_st:
stateMachineActive = false;
break;
}
}
bool Control_getStatusOfControlStateMachine() {
return stateMachineActive;
}
bool Control_detectWallCollision() {
for (int section = 0; section < snakeSize; ++section) {
if (snakeSections[section].x == 0 ||
snakeSections[section].x == DISPLAY_HEIGHT - 1 ||
snakeSections[section].y == 0 ||
snakeSections[section].y == DISPLAY_WIDTH - 1)
return true;
}
return false;
}
bool Control_detectTailCollision() {
return false;
}
void Control_updateSnake() {
for (int section = snakeSize; section > 1; --section) {
snakeSections[section - 1].x = snakeSections[section - 2].x;
snakeSections[section - 1].y = snakeSections[section - 2].y;
}
switch (movementDirection) {
case up:
snakeSections[0].x -= 1;
break;
case down:
snakeSections[0].x += 1;
break;
case left:
snakeSections[0].y -= 1;
break;
case right:
snakeSections[0].y += 1;
}
}
void Control_updateScreen() {
Display_clearArena();
for (int section = 0; section < snakeSize; ++section) {
Display_drawSnakeSection(snakeSections[section].x,
snakeSections[section].y, section);
}
Display_drawScreen();
}