-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.ino
More file actions
260 lines (201 loc) · 6.93 KB
/
game.ino
File metadata and controls
260 lines (201 loc) · 6.93 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
#include "testlib.h"
#include "game.h"
#include <Arduino.h>
#define CS_B 9
#define CS_A 10
/*
A maneira com que este array está ordenado, permite que as verificações das combinações sejam
feitas de maneira linear
*/
const static struct move neighbour_pos[] = {
{ 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 },
{ 0, -1 }, { -1, -1 }, { -1, 0 }, { -1, 1 }
};
/*
Função que verifica se uma coluna não está cheia, se ela não está, retorna o índice da primeira
linha não vazia da coluna
*/
static int col_is_not_full(struct game *game, int column)
{
for (int i = 0; i < GAME_DIM; i++) {
if (game->board[column][i] == NONE)
return i;
}
return -1;
}
/*
Verifica se uma posição está vazia ou não
*/
static inline bool is_nonempty_pos(struct game *game, struct move *mv)
{
if ((mv->x < 0 || mv->y < 0) || (mv->x >= GAME_DIM || mv->y >= GAME_DIM))
return false;
return game->board[mv->y][mv->x] != NONE;
}
/*
Função que verifica se o jogador da rodada venceu o jogo
*/
int check_win(struct game *game, struct move *mv, uint8_t same_neighbours[8])
{
if (game->board[mv->y][mv->x] == NONE)
return false;
for (int i = 0; i < 8; i++) {
struct move chk_pos = { mv->x, mv->y };
for (int j = 0; j < 4; j++) {
chk_pos.x += neighbour_pos[i].x;
chk_pos.y += neighbour_pos[i].y;
if (is_nonempty_pos(game, &chk_pos) &&
game->board[mv->y][mv->x] == game->board[chk_pos.y][chk_pos.x])
same_neighbours[i]++;
else
break;
}
}
for (int i = 0; i < 4; i++) {
if (same_neighbours[i] + same_neighbours[i + 4] >= 3)
return i;
}
return -1;
}
void show_winning_row(struct game *game, struct move *mv, int dir, uint8_t same_neighbours[8])
{
int best_dir = same_neighbours[dir] > same_neighbours[dir + 4] ? dir : dir + 4;
struct move initial_pos = {
mv->x + neighbour_pos[best_dir].x * same_neighbours[best_dir],
mv->y + neighbour_pos[best_dir].y * same_neighbours[best_dir]
};
for (int total = same_neighbours[best_dir] + same_neighbours[(best_dir + 4) % 8]; total >= 3; total--) {
struct move cur_pos;
for (int i = 0; i < 6; i++) {
cur_pos = initial_pos;
for (int j = 0; j < 4; j++) {
game->board[cur_pos.y][cur_pos.x] = i % 2 == 0 ? BOTH : NONE;
cur_pos.x += neighbour_pos[(best_dir + 4) % 8].x;
cur_pos.y += neighbour_pos[(best_dir + 4) % 8].y;
}
int tmp = game->curr_player;
game->curr_player = 0;
update_board(CS_A, game);
game->curr_player = 1;
update_board(CS_B, game);
game->curr_player = tmp;
delay(200);
}
cur_pos = initial_pos;
for (int i = 0; i < 4; i++) {
game->board[cur_pos.y][cur_pos.x] = game->curr_player == 0 ? RED_CHECKER : YLW_CHECKER;
cur_pos.x += neighbour_pos[(best_dir + 4) % 8].x;
cur_pos.y += neighbour_pos[(best_dir + 4) % 8].y;
}
initial_pos.x += neighbour_pos[(best_dir + 4) % 8].x;
initial_pos.y += neighbour_pos[(best_dir + 4) % 8].y;
}
int tmp = game->curr_player;
game->curr_player = 0;
update_board(CS_A, game);
game->curr_player = 1;
update_board(CS_B, game);
game->curr_player = tmp;
}
/*
Função que imprime um ponteiro no display de leds
*/
void print_arrow(int chip_select, int player, uint8_t ind, struct game *game) {
// A verificação se a coluna está cheia deve ser feita em outra função
byte buffer = 0;
enum position pos;
if(player == 0) {
pos = RED_CHECKER;
}
else {
pos = YLW_CHECKER;
}
for (int i = 0; i < GAME_DIM; i++) {
buffer <<= 1;
buffer |= game->board[ind][i] == pos;
}
buffer |= B00000001;
sendData(chip_select, ind+1, buffer);
}
/*
Função que procura a próxima coluna livre para inserção
A variável dir pode ser + ou - 1, indicando para qual direção queremos ir (+: direita; -: esquerda)
*/
int find_next_valid_index(struct game *game, int start_ind, int dir) {
for (int i = (start_ind + dir + GAME_DIM) % GAME_DIM; i != start_ind; i = (i + dir + GAME_DIM) % GAME_DIM) {
if (game->last_position[i] != 7)
return i;
}
return -1;
}
/*
Função que interpreta os inputs dos jogadores e registra o movimento feito por eles
*/
struct move* make_move(uint8_t chip_select, struct game *game) {
static struct move mv = {0};
uint8_t start_column;
uint8_t end_column;
int ind, tmp;
for (int j = 0; j < GAME_DIM; j++) {
if (game->last_position[j] != GAME_DIM-1) {
start_column = j;
break;
}
}
for (int j = GAME_DIM-1; j >= 0; j--) {
if (game->last_position[j] != GAME_DIM-1) {
end_column = j;
break;
}
}
ind = start_column;
uint8_t right_key = game->curr_player == 0 ? RIGHT_PLAYER_A : RIGHT_PLAYER_B;
uint8_t left_key = game->curr_player == 0 ? LEFT_PLAYER_A : LEFT_PLAYER_B;
uint8_t enter_key = game->curr_player == 0 ? ENTER_PLAYER_A : ENTER_PLAYER_B;
uint8_t last_enter = LOW;
uint8_t curr_enter = LOW;
uint8_t last_left = LOW;
uint8_t curr_left = LOW;
uint8_t last_right = LOW;
uint8_t curr_right = LOW;
unsigned long start_millis = millis();
unsigned long curr_millis = start_millis;
while((curr_enter = digitalRead(enter_key)) != HIGH) {
if ((curr_left = digitalRead(left_key)) == HIGH && last_left == LOW) {
ind = find_next_valid_index(game, ind, -1);
}
last_left = curr_left;
if ((curr_right = digitalRead(right_key)) == HIGH && last_right == LOW) {
ind = find_next_valid_index(game, ind, 1);
}
last_right = curr_right;
delay(10);
enum position pos = game->curr_player == 0 ? RED_CHECKER : YLW_CHECKER;
update_board(chip_select, game);
curr_millis = millis();
if (curr_millis - start_millis <= 500) {
print_arrow(chip_select, game->curr_player, ind, game);
}
else if (curr_millis - start_millis >= 1000) {
start_millis = curr_millis;
}
}
mv.y = ind;
mv.x = game->last_position[ind]+1;
game->last_position[ind]++;
return &mv;
}
/*
Atualiza o estado de somente uma matriz de led selecionada pelo campo chip_select
*/
void update_board(uint8_t chip_select, struct game *game) {
enum position pos = game->curr_player == 0 ? RED_CHECKER : YLW_CHECKER;
for (int j = 0; j < GAME_DIM; j++) {
uint8_t buffer = 0;
for (int i = 0; i < GAME_DIM; i++) {
buffer <<= 1;
buffer |= (game->board[j][i] == pos) || (game->board[j][i] == BOTH);
}
sendData(chip_select, j+1, buffer);
}
}