TP 13 exo2: Code cleanups
[TD_C.git] / TP_13 / exo2 / lib / coordinates.c
CommitLineData
5df3071e
JB
1/*
2 * =====================================================================================
3 *
4 * Filename: coordinates.c
5 *
6 * Description: Data definition and functions to manipulate elements in the grid
7 *
8 * Version: 1.0
9 * Created: 16/03/2017 19:05:02
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
19#include "coordinates.h"
20
21void init_coordinates(coordinates_t* coordinates_array) {
811d4abe 22
5df3071e
JB
23 for (unsigned i = 0; i < MAX_COORDINATES; i++) {
24 coordinates_array[i] = set_coordinates(0, 0, 0);
25 }
26}
27
28coordinates_t set_coordinates(int y, int x, unsigned type) {
29 coordinates_t new_coordinates;
30
31 new_coordinates.y = y;
32 new_coordinates.x = x;
33 new_coordinates.type = type;
34 return new_coordinates;
35}
36
811d4abe
JB
37/* FIXME: Does it worth doing a coordinates_t get_coordinates(int y, int x, unsigned type); helper function? */
38/* Or a int get_coordinates_x(int y, int x, unsigned type); helper function? */
39
04b0afb5
JB
40/* the function do a lot of sanity checks before adding new board elements,
41 * hence the loop. moving the checks in the main loop is also possible */
42unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array, unsigned round) {
811d4abe 43
5df3071e
JB
44 /* valid coordinates are in the [1-3] range */
45 if (new_coordinates.y < 1 || new_coordinates.y > 3 || new_coordinates.x < 1 || new_coordinates.x > 3) {
46 return 3; /* error value for invalid coordinates */
811d4abe
JB
47 } else if (round == MAX_COORDINATES) {
48 /* round is off-by-one */
49 coordinates_array[MAX_COORDINATES - 1] = new_coordinates;
04b0afb5 50 return 1; /* error value for full array */
5df3071e
JB
51 }
52 for (unsigned i = 0; i < MAX_COORDINATES; i++) {
53 /* check if already entered */
54 if (new_coordinates.y == (coordinates_array + i)->y && new_coordinates.x == (coordinates_array + i)->x) {
55 return 2; /* error value for duplicates */
56 } else if ((coordinates_array + i)->y == 0 && (coordinates_array + i)->x == 0) {
57 coordinates_array[i] = new_coordinates;
58 return 0; /* error value when everything if fine */
59 }
60 }
04b0afb5
JB
61 return 4; /* error value for unknown error case - should never happen - */
62}
63
811d4abe
JB
64static bool chk_line(coordinates_t* coordinates_array, int line_number, unsigned round) {
65 unsigned nb_o_align = 0;
66 unsigned nb_x_align = 0;
67
68 for (unsigned i = 0; i < round; i++) {
69 /* check if they are all the same */
70 if ((coordinates_array + i)->y == line_number && (coordinates_array + i)->type == 0) {
71 nb_o_align++;
72 }
73 if ((coordinates_array + i)->y == line_number && (coordinates_array + i)->type == 1) {
74 nb_x_align++;
75 }
76 }
77 if (nb_o_align == 3 || nb_x_align == 3) {
78 return true;
79 }
80 return false;
81}
82
83static bool chk_column(coordinates_t* coordinates_array, int column_number, unsigned round) {
84 unsigned nb_o_align = 0;
85 unsigned nb_x_align = 0;
04b0afb5 86
811d4abe
JB
87 for (unsigned i = 0; i < round; i++) {
88 /* check if they are all the same */
89 if ((coordinates_array + i)->x == column_number && (coordinates_array + i)->type == 0) {
90 nb_o_align++;
91 }
92 if ((coordinates_array + i)->x == column_number && (coordinates_array + i)->type == 1) {
93 nb_x_align++;
94 }
95 }
96 /* one column must be full of the same type */
97 if (nb_o_align == 3 || nb_x_align == 3) {
98 return true;
99 }
100 return false;
101}
102
103static bool chk_diagonals(coordinates_t* coordinates_array, unsigned round) {
104 unsigned nb_o_diag_one = 0, nb_o_diag_two = 0;
105 unsigned nb_x_diag_one = 0, nb_x_diag_two = 0;
106
107 for (unsigned i = 0; i < round; i++) {
108 /* dumb count of each elements type in the two diagonals */
109 for (int y_x_diag = 1; y_x_diag < 4; y_x_diag++) {
110 if ((coordinates_array + i)->y == y_x_diag && (coordinates_array + i)->x == y_x_diag && (coordinates_array + i)->type == 0) {
111 nb_o_diag_one++;
112 }
113 if ((coordinates_array + i)->y == y_x_diag && (coordinates_array + i)->x == (4 - y_x_diag) && (coordinates_array + i)->type == 0) {
114 nb_o_diag_two++;
115 }
116 if ((coordinates_array + i)->y == y_x_diag && (coordinates_array + i)->x == y_x_diag && (coordinates_array + i)->type == 1) {
117 nb_x_diag_one++;
118 }
119 if ((coordinates_array + i)->y == y_x_diag && (coordinates_array + i)->x == (4 - y_x_diag) && (coordinates_array + i)->type == 1) {
120 nb_x_diag_two++;
121 }
122 }
123 if (nb_o_diag_one == 3 || nb_o_diag_two == 3 || nb_x_diag_one == 3 || nb_x_diag_two == 3) {
124 return true;
125 }
126 }
127 return false;
128}
129
130bool chk_win_conditions(coordinates_t* coordinates_array, unsigned round) {
131
a7ebbad7
JB
132 /* winning conditions begin at round = 5 */
133 if (round > 4) {
811d4abe
JB
134 if (chk_diagonals(coordinates_array, round)) {
135 return true;
136 }
137 for (unsigned i = 1; i < 4; i++) {
138 if (chk_line(coordinates_array, i, round) || \
139 chk_column(coordinates_array, i, round)) {
140 return true;
141 }
142 }
04b0afb5
JB
143 }
144 return false;
5df3071e 145}