-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
88 lines (79 loc) · 2.16 KB
/
board.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
#include "board.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
// -------------------------
// --- BOARD INITIALIZER ---
// -------------------------
board *initBoard(int n_rows, int n_cols)
// Initialize board with empty cells
{
board *pboard;
cell emptyCell;
pboard = malloc(sizeof(board));
pboard -> rows = n_rows;
pboard -> cols = n_cols;
pboard -> grid = malloc(n_rows * sizeof(cell *));
for (size_t i = 0; i < n_rows; i++) {
pboard -> grid[i] = malloc(n_cols * sizeof(cell));
for (size_t j = 0; j < n_cols; j++) {
emptyCell.row = i;
emptyCell.col = j;
emptyCell.island = -1; // -1 stands for no island
emptyCell.hint = 0;
pboard -> grid[i][j] = emptyCell;
}
}
return pboard;
}
// --------------------------
// --- BOARD MANIPULATION ---
// --------------------------
bool isInBoard(board *pboard, int row, int col)
{
int n_rows = pboard -> rows;
int n_cols = pboard -> cols;
if (row >= 0 && row < n_rows && col >= 0 && col < n_cols)
{
return true;
}
return false;
}
cellType getStatus(board *pboard, int row, int col)
// Returns true is a cell is flooded
{
cell c = pboard -> grid[row][col];
return c.type;
}
cellList *getOrthogonalCells(board *pboard, int row, int col){
assert(isInBoard(pboard, row, col));
cellList *list = initCellList();
int ort_row, ort_col;
int cases[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (int i = 0; i < 4; i++)
{
ort_row = row + cases[i][0];
ort_col = col + cases[i][1];
if (isInBoard(pboard, ort_row, ort_col))
{
appendToCellList(list, &(pboard -> grid[ort_row][ort_col]));
}
}
return list;
}
// ---------------
// --- DISPLAY ---
// ---------------
void printBoard(board *pboard)
// Prints board to terminal
{
cell *pc;
for (size_t i = 0; i < (pboard -> rows); i++) {
for (size_t j = 0; j < (pboard -> cols); j++) {
printCell(&(pboard -> grid[i][j]));
}
printf("\n");
}
}