Fix an off-by-one on the pawn 2D array indexes.
[Project_algorithmic_C.git] / lib / othello.c
CommitLineData
4ddf6f1a
JB
1/*
2 * =====================================================================================
3 *
4 * Filename: othello.c
5 *
6 * Description: Handle the othello board content
7 *
8 * Version: 1.0
9 * Created: 25/04/2017 15:16:08
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
14 * Organization: Piment Noir
15 *
16 * =====================================================================================
17 */
18
74e2b93b
JB
19#include <stdlib.h>
20#include <stdbool.h>
4ddf6f1a 21
74e2b93b
JB
22#include "othello.h"
23
24unsigned int current_player(unsigned int round_count) {
25
26 if (round_count % 2 != 0) {
27 return player_two;
28 } else {
29 return player_one;
30 }
31}
32
33/* for consitency with ncurses, the board coordinates are in the following order:
54f1c58c 34 * O-x-->
74e2b93b
JB
35 * |
36 * y
37 * |
54f1c58c
JB
38 * v
39 * The origin O has (1, 1) coordinates */
74e2b93b
JB
40
41unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]) {
42
54f1c58c 43 return pawn_array[y-1][x-1];
74e2b93b
JB
44}
45
46bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size], unsigned int type) {
47
48 if (type > 2) {
49 return NULL;
50 }
51 if (get_box_value(y, x, pawn_array) == type) {
52 return true;
53 } else {
54 return false;
55 }
56}
57
58/* helper function to set a value != empty at the (y, x) in the pawns array */
59int** set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]) {
60
61 if (is_box_type(y, x, pawn_array, empty)) {
54f1c58c 62 pawn_array[y-1][x-1] = type;
74e2b93b
JB
63 return pawn_array;
64 } else {
65 return NULL;
66 }
67}
68
69static int** zero_pawns(unsigned int pawn_array[board_size][board_size]) {
54f1c58c
JB
70
71 for (unsigned int i = 1; i <= board_size; i++) {
72 for (unsigned int j = 1; j <= board_size; j++) {
74e2b93b
JB
73 pawn_array = set_pawn(i, j, empty, pawn_array);
74 }
75 }
76 return pawn_array;
77}
78
79/* set the pawns in the start position */
80int** init_pawns(unsigned int pawn_array[board_size][board_size]) {
81
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 pawn_array;
88}
89
90unsigned int count_pawn_type(unsigned int pawn_array[board_size][board_size], unsigned int type) {
91 unsigned int count = 0;
92
93 if (type > 2) {
94 return -1;
95 }
54f1c58c
JB
96 for (unsigned int i = 1; i <= board_size; i++) {
97 for (unsigned int j = 1; j <= board_size; j++) {
98 if (get_box_value(i, j, pawn_array) == type) {
74e2b93b
JB
99 count++;
100 }
101 }
102 }
103 return count;
104}
54f1c58c
JB
105
106bool is_valid_input(int y, int x, unsigned int pawn_array[board_size][board_size]) {
107
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)) {
112 return true;
113 } else {
114 return false;
115 }
116}
117
118bool is_board_full(unsigned int pawn_array[board_size][board_size]) {
119
120 for (unsigned int i = 1; i <= board_size; i++) {
121 for (unsigned int j = 1; j <= board_size; j++) {
122 if (is_box_type(i, j, pawn_array , empty)) {
123 return false;
124 }
125 }
126 }
127 return true;
128}
129
130void status_pawn(int y, int x, unsigned int pawn_array[board_size][board_size]) {
131
132}
133
134bool is_legal_box(int y, int x, int player, unsigned int pawn_array[board_size][board_size]) {
135
136}
137
138bool reverse_box(unsigned int pawn_array[board_size][board_size]) {
139
140}