-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrid.c
105 lines (84 loc) · 1.74 KB
/
grid.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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "grid.h"
tile grid[width][width];
tile tile_null;
void grid_init(void)
{
unsigned char i;
unsigned char j;
for(i=0; i<width; i++) {
for(j=0; j<width; j++) {
tile *tile = &grid[i][j];
tile_init(tile, j, i, 0);
}
}
tile_init(&tile_null, 0, 0, 0);
}
void grid_draw(grid_draw_cell_cb draw_cell)
{
unsigned char i;
unsigned char j;
tile *tile;
for(i=0; i<width; i++) {
for(j=0; j<width; j++) {
tile = &grid[i][j];
if (tile_needs_drawing(tile)) {
draw_cell( tile );
tile_set_needs_drawing(tile, false);
}
}
}
}
tile *grid_random_available_cell(void)
{
tile *tile = NULL;
array tiles;
size_t length;
size_t random_index;
grid_available_cells(&tiles);
length = array_length(&tiles);
if ( length > 0 ) {
random_index = rand() % length;
tile = array_get(&tiles, random_index);
}
array_destroy(&tiles);
return tile;
}
void grid_available_cells(array *cells)
{
unsigned char i;
unsigned char j;
tile *tile;
unsigned char value;
array_init(cells, width*width);
for(i=0;i<width;i++) {
for(j=0;j<width;j++) {
tile = &grid[i][j];
value = tile_get_value(tile);
if (0 == value) {
array_add(cells, tile);
}
}
}
}
bool grid_cells_are_available(void)
{
bool available;
array cells;
grid_available_cells(&cells);
available = cells.length > 0;
array_destroy(&cells);
return available;
}
tile* grid_get(signed char x, signed char y)
{
if ( grid_within_bounds(x, y) ) {
return &grid[y][x];
}
return NULL;
}
bool grid_within_bounds(signed char x, signed char y) {
return x >= 0 && x < width && y >= 0 && y < width;
}