-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.c
187 lines (157 loc) · 5.09 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
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
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include "board.h"
/**
* Check if mine is on the current Tile.
* @return true if Tile has mine on or false otherwise
*/
bool is_mine_on(Board *board, int row, int column) {
assert(board != NULL);
return row >= 0 && row < board->row_count && column >= 0
&& column < board->column_count
&& board->tiles[row][column]->is_mine;
}
/**
* Count number of mines interacted with Tile.
* @return count of mines
*/
int count_neighbour_mines(Board *board, int row, int column) {
assert(board != NULL);
// iterates with interacting tiles and counts clue value
int count = 0;
for (int drow = -1; drow <= 1; drow++) {
for (int dcolumn = -1; dcolumn <= 1; dcolumn++) {
if (is_mine_on(board, row + drow, column + dcolumn)) {
count++;
}
}
}
return count;
}
/**
* Set values to tiles according to neighbour mines count.
* If Tile is a mine then value is set to -1.
*/
void set_tile_values(Board *board) {
assert(board != NULL);
for (int row = 0; row < board->row_count; row++) {
for (int column = 0; column < board->column_count; column++) {
if (board->tiles[row][column]->is_mine) {
board->tiles[row][column]->value = -1;
} else {
board->tiles[row][column]->value =
count_neighbour_mines(board, row, column);
}
}
}
}
/**
* When Game is won all mines are shown as marked.
*/
void mark_all_mines(Board *board) {
assert(board != NULL);
for (int row = 0; row < board->row_count; row++) {
for (int column = 0; column < board->column_count; column++) {
if (board->tiles[row][column]->tile_state == CLOSED) {
board->tiles[row][column]->tile_state = MARKED;
}
}
}
}
/**
* Generate random value from 0 to upper range of the border.
* @return random number in range 0 to upper_range param
*/
int generate_random_coordinates(int upper_range) {
return rand() % upper_range;
}
/**
* Generate random coordinates to row and column according to mine count value.
* Randomly sets mines to the Board pointer
*/
void set_mines_randomly(Board *board) {
assert(board != NULL);
int board_mine_count = 0;
srand(time(NULL));
while (board_mine_count != board->mine_count) {
int random_row = generate_random_coordinates(board->row_count);
int random_column = generate_random_coordinates(board->column_count);
if (board->tiles[random_row][random_column]->is_mine == false) {
board->tiles[random_row][random_column]->is_mine = true;
board_mine_count++;
}
}
}
/**
* Create and allocate pointer of the Board.
* @return pointer of the Board
*/
Board *create_board(int row_count, int column_count, int mine_count) {
Board *board = (Board *) calloc(1, sizeof(Board));
board->row_count = row_count;
board->column_count = column_count;
board->mine_count = mine_count;
for (int row = 0; row < board->row_count; row++) {
for (int column = 0; column < board->column_count; column++) {
board->tiles[row][column] = (Tile *) calloc(1, sizeof(Tile));
board->tiles[row][column]->tile_state = CLOSED;
board->tiles[row][column]->is_mine = false;
}
}
set_mines_randomly(board);
set_tile_values(board);
return board;
}
/**
* Free the Board and each Tile.
*/
void destroy_board(Board *board) {
assert(board != NULL);
for (int row = 0; row < board->row_count; row++) {
for (int column = 0; column < board->row_count; column++) {
free(board->tiles[row][column]);
}
}
free(board);
}
/**
* Check if Game is solved.
* @return false if Board consists of any Tile which is closed and has clue value, else true
*/
bool is_game_solved(Board *board) {
assert(board != NULL);
for (int row = 0; row < board->row_count; row++) {
for (int column = 0; column < board->column_count; column++) {
if (board->tiles[row][column]->tile_state == CLOSED
&& !board->tiles[row][column]->is_mine) {
return false;
}
}
}
mark_all_mines(board);
return true;
}
/**
* Check if input row and column are within correct range.
* @return true if input coordinates are within the range, false otherwise
*/
bool is_input_data_correct(Board *board, int input_row, int input_column) {
assert(board != NULL);
return input_row >= 0 && board->row_count > input_row
&& input_column >= 0 && board->column_count > input_column;
}
/**
* If Game is lost all mines are shown.
*/
void open_all_mines(Board *board) {
assert(board != NULL);
for (int row = 0; row < board->row_count; row++) {
for (int column = 0; column < board->column_count; column++) {
if (board->tiles[row][column]->tile_state == CLOSED
&& board->tiles[row][column]->is_mine) {
board->tiles[row][column]->tile_state = OPEN;
}
}
}
}