-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplix.c
438 lines (373 loc) · 10.2 KB
/
splix.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <stdlib.h>
#include <assert.h>
#include "raylib.h"
#include <stdio.h>
#define ROWS 30
#define COLUMNS 60
#define SQUARE_SIDE 20
#define STEPS_PER_SQUARE 10
enum direction{Still, Up, Down, Left, Right};
typedef enum direction direction;
enum input_mode{wasd, arrows};
typedef enum input_mode input_mode;
struct player {
int x;
int y;
int x_aligned;
int y_aligned;
int prev_x_aligned;
int prev_y_aligned;
direction head_direction;
direction next_direction;
int id;
Color color_main;
Color color_second;
Color color_head;
int x_upper;
int x_lower;
int y_upper;
int y_lower;
input_mode input;
};
typedef struct player player;
void arrow_input (direction *dir) {
if ( IsKeyDown(KEY_RIGHT) && *dir != Left ) *dir = Right;
if ( IsKeyDown(KEY_LEFT) && *dir != Right ) *dir = Left;
if ( IsKeyDown(KEY_UP) && *dir != Down ) *dir = Up;
if ( IsKeyDown(KEY_DOWN) && *dir != Up ) *dir = Down;
}
void wasd_input (direction *dir) {
if ( IsKeyDown(KEY_D) && *dir != Left ) *dir = Right;
if ( IsKeyDown(KEY_A) && *dir != Right ) *dir = Left;
if ( IsKeyDown(KEY_W) && *dir != Down ) *dir = Up;
if ( IsKeyDown(KEY_S) && *dir != Up ) *dir = Down;
}
// return 1 if soon outside border
// return 2 if soon in tail (own)
// negative other.id if soon in other's tail
int check_next_move(int **grid, const player p) {
direction dir = p.head_direction;
const int width = COLUMNS * SQUARE_SIDE;
const int height = ROWS * SQUARE_SIDE;
// index of tile that player/head is moving towards
int tile_x = p.x / SQUARE_SIDE;
int tile_y = p.y / SQUARE_SIDE;
switch (dir) {
case Left:
if ( p.x == 0 ) {
return p.id;
} else {
--tile_x;
}
break;
case Right:
if ( p.x + SQUARE_SIDE == width ) {
return p.id;
} else {
++tile_x;
}
break;
case Up:
if ( p.y == 0 ) {
return p.id;
} else {
--tile_y;
}
break;
case Down:
if ( p.y + SQUARE_SIDE == height ) {
return p.id;
} else {
++tile_y;
}
break;
case Still:
return 0;
}
// check if player is moving towards own or other's tail
if ( grid[tile_y][tile_x] < 0 ) {
if ( grid[tile_y][tile_x] == -p.id ) {
return p.id;
} else {
return -grid[tile_y][tile_x];
}
}
return 0;
}
// move player by step in pointing direction
void move_player(player *p, const int step) {
switch ((*p).head_direction) {
case Right:
(*p).x += step;
break;
case Left:
(*p).x -= step;
break;
case Up:
(*p).y -= step;
break;
case Down:
(*p).y += step;
break;
case Still:
break;
}
}
void draw(int **grid, player *players[], const int players_len) {
int tile; // tile value
Color clr;
BeginDrawing();
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLUMNS; ++col) {
tile = grid[row][col];
if (tile == 0) {
clr = RAYWHITE;
}
else if (tile < 0) {
clr = (*players[-tile-1]).color_second;
} else if (tile > 0) {
clr = (*players[tile-1]).color_main;
}
DrawRectangle(col * SQUARE_SIDE, row * SQUARE_SIDE, SQUARE_SIDE, SQUARE_SIDE, clr);
}
}
for (int i = 0; i<players_len; ++i) {
DrawRectangle(
(*players[i]).x,
(*players[i]).y,
SQUARE_SIDE,
SQUARE_SIDE,
(*players[i]).color_head
);
}
EndDrawing();
}
// 2d array, WIDTH * HEIGHT
int** make_grid(const int rows, const int columns) {
int** grid = malloc(rows * sizeof(int*));
for (int i = 0; i < rows; ++i) {
grid[i] = calloc(columns, sizeof(int));
}
return grid;
}
void free_grid(int** grid, const int rows) {
for (int i = 0; i < rows; ++i) {
free(grid[i]);
}
free(grid);
}
void flood_fill(int** grid, const int row, const int col, const int rows, const int columns, const int new, const int empty) {
if ( grid[row][col] != empty ) {
return;
} else {
grid[row][col] = new;
if ( row != 0 ) {
flood_fill(grid, row-1, col, rows, columns, new, empty);
} if (row != rows-1) {
flood_fill(grid, row+1, col, rows, columns, new, empty);
} if ( col != 0 ) {
flood_fill(grid, row, col-1, rows, columns, new, empty);
} if (col != columns-1) {
flood_fill(grid, row, col+1, rows, columns, new, empty);
}
}
}
void update_bounds(player *p) {
if ( (*p).x_upper < (*p).x_aligned ) {
(*p).x_upper = (*p).x_aligned;
} if ( (*p).x_lower > (*p).x_aligned ) {
(*p).x_lower = (*p).x_aligned;
} if ( (*p).y_upper < (*p).y_aligned ) {
(*p).y_upper = (*p).y_aligned;
} if ( (*p).y_lower > (*p).y_aligned ) {
(*p).y_lower = (*p).y_aligned;
}
}
void resize_bounds( int** grid, player *p ) {
int x_upper = -1;
int x_lower = COLUMNS+1;
int y_upper = -1;
int y_lower = ROWS+1;
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLUMNS; ++col) {
if ( grid[row][col] == (*p).id ) {
if ( x_upper < col ) {
x_upper = col;
} else if ( x_lower > col ) {
x_lower = col;
} else if ( y_upper < row ) {
y_upper = row;
} else if ( y_lower > row ) {
y_lower = row;
}
}
}
}
(*p).x_upper = x_upper;
(*p).x_lower = x_lower;
(*p).y_upper = y_upper;
(*p).y_lower = y_lower;
}
// replace all tiles = a -> b
void replace(int **grid, int a, int b, int rows, int columns) {
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < columns; ++col) {
if ( grid[row][col] == a ) {
grid[row][col] = b;
}
}
}
}
void area_capture(int** grid, const player p) {
/*
fill grid is size of the smallest box that contains all of the players
tiles + one row/column of "empty" tiles in every direction.
*/
int fill_grid_rows = p.y_upper - p.y_lower + 3;
int fill_grid_columns = p.x_upper - p.x_lower + 3;
// fill trail
replace(grid, -p.id, p.id, ROWS, COLUMNS);
int** fill_grid = make_grid(fill_grid_rows, fill_grid_columns);
/*
We only need to look at the tiles in the smallest box
that contains all of the players tiles.
This box is 2 rows / columns smaller than fill_grid.
This hopefully explains the test expressions in the for loops.
As fill_grid has a "marigin" of empty rows/columns,
the indexes are shifted by one.
This is why 1 is added to the grid-indexes when
accessing fill_grid.
*/
for ( int row = 0; row < fill_grid_rows-2; ++row ) {
for (int col = 0; col < fill_grid_columns-2; ++col) {
if ( grid[ row + p.y_lower ][ col + p.x_lower ] == p.id ) {
fill_grid[ row+1 ][ col+1 ] = 1;
} else {
fill_grid[ row+1 ][ col+1 ] = 0;
}
}
}
// flood fill the 0's with 2, starting in upper left corner
flood_fill(fill_grid, 0, 0, fill_grid_rows, fill_grid_columns, 2, 0);
/*
replace remaining 0's with p.id
the indexing is explained by comments in the nested loop above.
*/
for (int row = 0; row < fill_grid_rows-2; ++row) {
for (int col = 0; col < fill_grid_columns-2; ++col) {
if ( fill_grid[ row+1 ][ col+1 ] == 0 ) {
grid[ row + p.y_lower ][ col + p.x_lower ] = p.id;
}
}
}
free_grid(fill_grid, fill_grid_rows);
}
void spawn(int **grid, player *p) {
int x = rand() % (COLUMNS-1) + 2;
int y = rand() % (ROWS-1)+2;
grid[y-1][x-1] = (*p).id;
grid[y][x-1] = (*p).id;
grid[y+1][x-1] = (*p).id;
grid[y-1][x] = (*p).id;
grid[y][x] = (*p).id;
grid[y+1][x] = (*p).id;
grid[y-1][x+1] = (*p).id;
grid[y][x+1] = (*p).id;
grid[y+1][x+1] = (*p).id;
(*p).x = x * SQUARE_SIDE;
(*p).y = y * SQUARE_SIDE;
(*p).x_aligned = x;
(*p).y_aligned = y;
(*p).prev_x_aligned = x;
(*p).prev_y_aligned = y;
(*p).head_direction = Still;
(*p).next_direction = Still;
(*p).x_lower = x-1;
(*p).x_upper = x+1;
(*p).y_lower = y-1;
(*p).y_upper = y+1;
}
int main(void)
{
// dimensions
int head_step = SQUARE_SIDE / STEPS_PER_SQUARE;
assert(SQUARE_SIDE % head_step == 0);
const int screenWidth = COLUMNS * SQUARE_SIDE;
const int screenHeight = ROWS * SQUARE_SIDE;
// the grid on which the players exist
int** grid = make_grid(ROWS, COLUMNS);
// boiler stuffz
InitWindow(screenWidth, screenHeight, "Splix");
SetTargetFPS(60);
// player stuffs
player player_1;
player_1.color_main = MAROON;
player_1.color_second = PINK;
player_1.color_head = MAGENTA;
player_1.id = 1;
player_1.input = arrows;
spawn(grid, &player_1);
player player_2;
player_2.color_main = DARKGREEN;
player_2.color_second = GREEN;
player_2.color_head = LIME;
player_2.id = 2;
player_2.input = wasd;
spawn(grid, &player_2);
player *players[] = { &player_1, &player_2 };
int players_len = 2;
int move_status; // used when calling check_next_move
player *current; // used when looping through players
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
for ( int i = 0; i < players_len; ++i ) {
current = players[i];
switch ( (*current).input ) {
case arrows:
arrow_input(&(*current).next_direction);
break;
case wasd:
wasd_input(&(*current).next_direction);
break;
}
// only update direction and tile ownership when head is aligned with grid
if ( (*current).x % SQUARE_SIDE == 0 && (*current).y % SQUARE_SIDE == 0 ) {
(*current).x_aligned = (*current).x / SQUARE_SIDE;
(*current).y_aligned = (*current).y / SQUARE_SIDE;
(*current).head_direction = (*current).next_direction;
// check if player is outside or inside
if ( grid[(*current).y_aligned][(*current).x_aligned] != (*current).id ) {
// paint trail (-id)
grid[(*current).y_aligned][(*current).x_aligned] = -(*current).id;
update_bounds(current);
// check if previous tile was outside of home
// if so, capture area.
} else if ( grid[(*current).prev_y_aligned][(*current).prev_x_aligned] != (*current).id ) {
area_capture(grid, (*current) );
}
// if move_status is not zero, it is the id of the losing player
move_status = check_next_move(grid, (*current));
if ( move_status != 0 ) {
// clear player tiles
replace(grid, move_status, 0, ROWS, COLUMNS);
replace(grid, -move_status, 0, ROWS, COLUMNS);
//respawn player
spawn(grid, players[move_status-1]);
}
// update "previous position" to current
(*current).prev_y_aligned = (*current).y_aligned;
(*current).prev_x_aligned = (*current).x_aligned;
}
// advance player
move_player(players[i], head_step);
}
// draw everything
draw(grid, players, players_len);
}
exit_game:
CloseWindow(); // Close window and OpenGL context
free_grid(grid, ROWS);
return 0;
}
// TODO resize when area has been captured