1115e92d82563ac0e795ba8d1d633c81d48f2004
2 * =====================================================================================
6 * Description: Handle the othello board content
9 * Created: 25/04/2017 15:16:08
13 * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
14 * Organization: Piment Noir
16 * =====================================================================================
24 unsigned int current_player(unsigned int round_count
) {
26 if (round_count
% 2 != 0) {
33 /* for consitency with ncurses, the board coordinates are in the following order:
39 * The origin O has (1, 1) coordinates */
41 unsigned int get_box_value(int y
, int x
, unsigned int pawn_array
[board_size
][board_size
]) {
43 return pawn_array
[y
-1][x
-1];
46 bool is_box_type(int y
, int x
, unsigned int pawn_array
[board_size
][board_size
], unsigned int type
) {
51 if (get_box_value(y
, x
, pawn_array
) == type
) {
58 /* helper function to set a value != empty at the (y, x) coordinates in the pawns array */
59 int** set_pawn(int y
, int x
, unsigned int type
, unsigned int pawn_array
[board_size
][board_size
]) {
61 if (is_box_type(y
, x
, pawn_array
, empty
)) {
62 pawn_array
[y
-1][x
-1] = type
;
63 return (int**)pawn_array
;
69 static int** zero_pawns(unsigned int pawn_array
[board_size
][board_size
]) {
71 for (unsigned int i
= 1; i
<= board_size
; i
++) {
72 for (unsigned int j
= 1; j
<= board_size
; j
++) {
73 pawn_array
= set_pawn(i
, j
, empty
, pawn_array
);
76 return (int**)pawn_array
;
79 /* set the pawns in the start position */
80 int** init_pawns(unsigned int pawn_array
[board_size
][board_size
]) {
82 pawn_array
= zero_pawns(pawn_array
);
83 pawn_array
= set_pawn(5, 4, black
, pawn_array
);
84 pawn_array
= set_pawn(4, 5, black
, pawn_array
);
85 pawn_array
= set_pawn(4, 4, white
, pawn_array
);
86 pawn_array
= set_pawn(5, 5, white
, pawn_array
);
87 return (int**)pawn_array
;
90 unsigned int count_pawn_type(unsigned int pawn_array
[board_size
][board_size
], unsigned int type
) {
91 unsigned int count
= 0;
96 for (unsigned int i
= 1; i
<= board_size
; i
++) {
97 for (unsigned int j
= 1; j
<= board_size
; j
++) {
98 if (is_box_type(i
, j
, pawn_array
, type
)) {
106 bool is_valid_input(int y
, int x
, unsigned int pawn_array
[board_size
][board_size
]) {
108 /* FIXME: separate the tests to permit to print explicit error messages */
109 if ((y
> 0 && y
< board_size
+ 1) && \
110 (x
> 0 && x
< board_size
+ 1) && \
111 is_box_type(y
, x
, pawn_array
, empty
)) {
118 bool is_board_full(unsigned int pawn_array
[board_size
][board_size
]) {
120 /* an alternate method is to test the round count vs. 60 */
121 for (unsigned int i
= 1; i
<= board_size
; i
++) {
122 for (unsigned int j
= 1; j
<= board_size
; j
++) {
123 if (is_box_type(i
, j
, pawn_array
, empty
)) {
131 unsigned int eval_winner(unsigned int nb_white
, unsigned int nb_black
) {
133 if (nb_white
> nb_black
) {
135 } else if (nb_white
< nb_black
) {
142 void direction_to_coordinates(unsigned int direction
, int* start_y
, int* start_x
) {
144 if (direction
== north
) {
145 *start_y
= *start_y
- 1;
146 } else if (direction
== north_east
) {
147 *start_y
= *start_y
- 1;
148 *start_x
= *start_x
+ 1;
149 } else if (direction
== east
) {
150 *start_x
= *start_x
+ 1;
151 } else if (direction
== south_east
) {
152 *start_y
= *start_y
+ 1;
153 *start_x
= *start_x
+ 1;
154 } else if (direction
== south
) {
155 *start_y
= *start_y
+ 1;
156 } else if (direction
== south_west
) {
157 *start_y
= *start_y
+ 1;
158 *start_x
= *start_x
- 1;
159 } else if (direction
== west
) {
160 *start_x
= *start_x
- 1;
161 } else if (direction
== north_west
) {
162 *start_y
= *start_y
- 1;
163 *start_x
= *start_x
- 1;
167 bool explore(int y
, int x
, int direction
) {