-
Notifications
You must be signed in to change notification settings - Fork 0
/
draughts.c
408 lines (319 loc) · 9.64 KB
/
draughts.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Draughts Game for La-Fortuna Board provided by the University of Southampton
Project for COMP2215 2018-2019 designed by Marcin Wiech
Project structure inspired by github user MKLOL based on GNU General Public License v2.0
*/
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "ruota.h"
#include "rios.h"
#include <avr/interrupt.h>
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
#define BOARD_SIZE 240
#define BOARD_SQUARE_SIZE 30
#define BLACK1 0x18C4
#define BLACK2 0x1001
#define SELECT_GREEN 0x76AD
#define SUPER_RED 0xD041
#define SUPER_BLUE 0x325D
#define BORDER_COLOUR 0x4228
#define true 1
#define false 0
typedef int8_t bool;
typedef struct {
int8_t x, y;
} position;
typedef struct {
position pos;
bool player;
bool moved;
} token;
token tokens[24] = {
{{1, 0}, 0, 0},
{{3, 0}, 0, 0},
{{5, 0}, 0, 0},
{{7, 0}, 0, 0},
{{0, 1}, 0, 0},
{{2, 1}, 0, 0},
{{4, 1}, 0, 0},
{{6, 1}, 0, 0},
{{1, 2}, 0, 0},
{{3, 2}, 0, 0},
{{5, 2}, 0, 0},
{{7, 2}, 0, 0},
{{0, 7}, 1, 0},
{{2, 7}, 1, 0},
{{4, 7}, 1, 0},
{{6, 7}, 1, 0},
{{1, 6}, 1, 0},
{{3, 6}, 1, 0},
{{5, 6}, 1, 0},
{{7, 6}, 1, 0},
{{0, 5}, 1, 0},
{{2, 5}, 1, 0},
{{4, 5}, 1, 0},
{{6, 5}, 1, 0}
};
void os_init(void);
void draw_start_board(void);
void draw_square(int8_t, int8_t);
void draw_token(int8_t, int8_t, int8_t);
void draw_token_at_position(token *);
void perform_action(void);
token *get_token_at_position(int8_t, int8_t);
bool is_valid_move(void);
bool can_move(void);
void move_cursor(int8_t, int8_t);
int check_switches(int);
// false is blue player and true is red player
bool turn = false;
token *selected_token = NULL;
token *pawn_replaced = NULL;
position previous_selected_position = {-1, -1};
position current_cursor_position = {0, 0};
void main(void) {
os_init();
os_add_task(check_switches, 100, 1);
draw_start_board();
sei();
for (;;) {
}
}
void os_init(void) {
/* 8MHz clock, no prescaling (DS, p. 48) */
CLKPR = (1 << CLKPCE);
CLKPR = 0;
init_lcd();
os_init_scheduler();
os_init_ruota();
}
/*
* Drawing functions
*/
void draw_start_board() {
clear_screen();
int i;
int j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
draw_square(i, j);
}
}
}
void draw_square(int8_t x, int8_t y) {
uint16_t colour;
if (current_cursor_position.x == x && current_cursor_position.y == y) {
colour = SELECT_GREEN;
} else {
colour = BORDER_COLOUR;
}
rectangle rect = {
x * BOARD_SQUARE_SIZE+40,
x * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE+40,
y * BOARD_SQUARE_SIZE,
y * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE,
};
fill_rectangle(rect, colour);
if(!(current_cursor_position.x == x && current_cursor_position.y == y)){
rectangle rectf_fill = {
x * BOARD_SQUARE_SIZE+1+40,
x * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-1+40,
y * BOARD_SQUARE_SIZE+1,
y * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-1,
};
if ((x + y) % 2 == 0) {
colour = BLACK1;
} else {
colour = BLACK2;
}
fill_rectangle(rectf_fill, colour);
}
token *token_at = get_token_at_position(x, y);
if (token_at != NULL) {
draw_token_at_position(token_at);
}
}
void draw_token(int8_t x, int8_t y, bool player_val) {
uint16_t colour, inner_colour;
if (player_val == 0) {
colour = SUPER_BLUE;
inner_colour = 0x10AF;
} else {
colour = SUPER_RED;
inner_colour = 0x78C2;
}
rectangle rectf_fill = {
x * BOARD_SQUARE_SIZE+7+40,
x * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-7+40,
y * BOARD_SQUARE_SIZE+7,
y * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-7,
};
fill_rectangle(rectf_fill, colour);
rectangle rectf_fill1 = {
x * BOARD_SQUARE_SIZE+8+40,
x * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-8+40,
y * BOARD_SQUARE_SIZE+8,
y * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-8,
};
fill_rectangle(rectf_fill1, inner_colour);
rectangle rectf_fill2 = {
x * BOARD_SQUARE_SIZE+10+40,
x * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-10+40,
y * BOARD_SQUARE_SIZE+10,
y * BOARD_SQUARE_SIZE + BOARD_SQUARE_SIZE-10,
};
fill_rectangle(rectf_fill2, colour);
}
void draw_token_at_position(token *token) {
draw_token(token->pos.x, token->pos.y, token->player);
}
/*
* Functions for the game
*/
/*
* When clicking the center button
*/
void perform_action() {
token *token_at;
if (selected_token == NULL) {
token_at = get_token_at_position(current_cursor_position.x, current_cursor_position.y);
if (token_at != NULL && token_at->player == turn) {
previous_selected_position.x = current_cursor_position.x;
previous_selected_position.y = current_cursor_position.y;
token_at->pos.x = -1;
token_at->pos.y = -1;
selected_token = token_at;
}
} else {
int8_t x_taken = -2, y_taken = -2;
if (is_valid_move() && can_move()) {
token_at = get_token_at_position(current_cursor_position.x, current_cursor_position.y);
if (token_at != NULL && token_at->player != selected_token->player) {
x_taken = token_at->pos.x;
y_taken = token_at->pos.y;
if((previous_selected_position.x < current_cursor_position.x)){
selected_token->pos.x = x_taken+1;
}else{
selected_token->pos.x = current_cursor_position.x-1;
}
if(previous_selected_position.y < current_cursor_position.y){
selected_token->pos.y = current_cursor_position.y+1;
}
else{
selected_token->pos.y = y_taken-1;
}
token_at->pos.x = -2;
token_at->pos.y = -2;
turn = 1 - turn;
}else{
selected_token->pos.x = current_cursor_position.x;
selected_token->pos.y = current_cursor_position.y;
}
turn = 1 - turn;
} else {
selected_token->pos.x = previous_selected_position.x;
selected_token->pos.y = previous_selected_position.y;
}
draw_square(selected_token->pos.x, selected_token->pos.y);
selected_token = NULL;
previous_selected_position.x = -1;
previous_selected_position.y = -1;
}
draw_square(current_cursor_position.x, current_cursor_position.y);
}
token *get_token_at_position(int8_t x, int8_t y) {
int8_t i;
for (i = 0; i < 24; i++) {
if (tokens[i].pos.x == x && tokens[i].pos.y == y) {
return & tokens[i];
}
}
return NULL;
}
bool is_valid_move() {
if (selected_token->pos.x == -1 && selected_token->pos.y == -1) {
if (abs(previous_selected_position.x - current_cursor_position.x) <= 1) {
if (selected_token->player == 0
&& (abs(previous_selected_position.y - current_cursor_position.y)
&& abs(previous_selected_position.x - current_cursor_position.x) == 1)) {
return true;
} else if (selected_token->player == 1
&& (abs(previous_selected_position.x - current_cursor_position.x)
&& abs(previous_selected_position.x - current_cursor_position.x) == 1)) {
return true;
}
}
}
return false;
}
bool can_move() {
token *token_at;
int8_t new_x, new_y;
int8_t x = current_cursor_position.x, y = current_cursor_position.y;
token_at = get_token_at_position(x, y);
if(token_at == NULL && x >= 0 && x < 8 && y >=0 && y < 8){
return true;
}
if (token_at->player == selected_token->player) {
return false;
}
if (token_at->player != selected_token->player) {
if((previous_selected_position.x < x)){
new_x = x+1;
}else{
new_x = x-1;
}
if(previous_selected_position.y < y){
new_y = y+1;
}
else{
new_y = y-1;
}
if(new_y < 0 || new_y > 7 || new_x < 0 || new_x > 7 || get_token_at_position(new_x, new_y) != NULL){
return false;
}
}
return true;
}
/*
* Functions for handling the movement of a token
*/
void move_cursor(int8_t x, int8_t y) {
bool changed = false;
position previous_pos = {
current_cursor_position.x,
current_cursor_position.y
};
if (previous_pos.x + x >= 0 && previous_pos.x + x < 8) {
current_cursor_position.x = previous_pos.x + x;
changed = true;
}
if (previous_pos.y + y >= 0 && previous_pos.y + y < 8) {
current_cursor_position.y = previous_pos.y + y;
changed = true;
}
if (changed) {
draw_square(previous_pos.x, previous_pos.y);
draw_square(current_cursor_position.x, current_cursor_position.y);
}
}
int check_switches(int state) {
if (get_switch_press(_BV(SWN))) {
move_cursor(0, -1);
}
if (get_switch_press(_BV(SWE))) {
move_cursor(1, 0);
}
if (get_switch_press(_BV(SWS))) {
move_cursor(0, 1);
}
if (get_switch_press(_BV(SWW))) {
move_cursor(-1, 0);
}
if (get_switch_press(_BV(SWC))) {
perform_action();
}
return state;
}