-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.c
77 lines (64 loc) · 1.77 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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "board.h"
/* Board.c - All the related functions to build the game board, create a copy of the original board, rlease the board from the memory, get a value from a specific
cell on the board and set a specific cell to a value */
board* board_create(int rows, int cols) {
int* temp;
int i, m;
board* retval = (board*)malloc(sizeof(board));
if (retval == NULL) {
printf("ERROR : Not enough memory");
exit(1);
}
retval->rows = rows;
retval->cols = cols;
retval->grid = (int**) malloc(rows * sizeof(int*));
if (retval->grid == NULL) {
printf("ERROR : Not enough memory");
exit(1);
}
temp = (int*) malloc(rows * cols * sizeof(int));
if (temp == NULL) {
printf("ERROR : Not enough memory");
exit(1);
}
i = 0;
for (; i < rows; i++) {
retval->grid[i] = temp + (i * cols);
}
m = 0;
for (; m < rows; m++) {
int n = 0;
for (; n < cols; n++) {
retval->grid[m][n] = BOARD_EMPTY_CELL;
}
}
return retval;
}
board* board_copy(board* src) {
int i, j;
board* retval = board_create(src->rows, src->cols);
for (i=0; i < retval->rows; i++)
for (j=0; j < retval->cols; j++)
retval->grid[i][j] = src->grid[i][j];
return retval;
}
void board_release(board* game_board) {
free(game_board->grid[0]);
free(game_board->grid);
free(game_board);
}
int board_get(board* game_board, int i, int j) {
assert(game_board);
assert(game_board->grid);
assert(game_board->grid[i]);
return game_board->grid[i][j];
}
void board_set(board* game_board, int i, int j, int value) {
assert(game_board);
assert(game_board->grid);
assert(game_board->grid[i]);
game_board->grid[i][j] = value;
}