Add the basic code to make othello playable without any rule checks
[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:
34 * --x-->
35 * |
36 * y
37 * |
38 * v */
39
40unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]) {
41
42 return pawn_array[y][x];
43}
44
45bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size], unsigned int type) {
46
47 if (type > 2) {
48 return NULL;
49 }
50 if (get_box_value(y, x, pawn_array) == type) {
51 return true;
52 } else {
53 return false;
54 }
55}
56
57/* helper function to set a value != empty at the (y, x) in the pawns array */
58int** set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]) {
59
60 if (is_box_type(y, x, pawn_array, empty)) {
61 pawn_array[y][x] = type;
62 return pawn_array;
63 } else {
64 return NULL;
65 }
66}
67
68static int** zero_pawns(unsigned int pawn_array[board_size][board_size]) {
69 for (unsigned int i = 0; i < board_size; i++) {
70 for (unsigned int j = 0; j < board_size; j++) {
71 pawn_array = set_pawn(i, j, empty, pawn_array);
72 }
73 }
74 return pawn_array;
75}
76
77/* set the pawns in the start position */
78int** init_pawns(unsigned int pawn_array[board_size][board_size]) {
79
80 pawn_array = zero_pawns(pawn_array);
81 pawn_array = set_pawn(5, 4, black, pawn_array);
82 pawn_array = set_pawn(4, 5, black, pawn_array);
83 pawn_array = set_pawn(4, 4, white, pawn_array);
84 pawn_array = set_pawn(5, 5, white, pawn_array);
85 return pawn_array;
86}
87
88unsigned int count_pawn_type(unsigned int pawn_array[board_size][board_size], unsigned int type) {
89 unsigned int count = 0;
90
91 if (type > 2) {
92 return -1;
93 }
94 for (unsigned int i = 0; i < board_size; i++) {
95 for (unsigned int j = 0; j < board_size; j++) {
96 if (pawn_array[i][j] == type) {
97 count++;
98 }
99 }
100 }
101 return count;
102}