2 * =====================================================================================
4 * Filename: coordinates.c
6 * Description: Data definition and functions to manipulate elements in the grid
9 * Created: 16/03/2017 19:05:02
13 * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
14 * Organization: Piment Noir
16 * =====================================================================================
19 #include "coordinates.h"
21 void init_coordinates(coordinates_t
* coordinates_array
) {
22 for (unsigned i
= 0; i
< MAX_COORDINATES
; i
++) {
23 coordinates_array
[i
] = set_coordinates(0, 0, 0);
27 coordinates_t
set_coordinates(int y
, int x
, unsigned type
) {
28 coordinates_t new_coordinates
;
30 new_coordinates
.y
= y
;
31 new_coordinates
.x
= x
;
32 new_coordinates
.type
= type
;
33 return new_coordinates
;
36 /* the function do a lot of sanity checks before adding new board elements,
37 * hence the loop. moving the checks in the main loop is also possible */
38 unsigned add_coordinates(coordinates_t new_coordinates
, coordinates_t
* coordinates_array
, unsigned round
) {
39 /* valid coordinates are in the [1-3] range */
40 if (new_coordinates
.y
< 1 || new_coordinates
.y
> 3 || new_coordinates
.x
< 1 || new_coordinates
.x
> 3) {
41 return 3; /* error value for invalid coordinates */
42 } else if (round
== MAX_COORDINATES
+ 1) {
43 return 1; /* error value for full array */
46 for (unsigned i
= 0; i
< MAX_COORDINATES
; i
++) {
47 /* check if already entered */
48 if (new_coordinates
.y
== (coordinates_array
+ i
)->y
&& new_coordinates
.x
== (coordinates_array
+ i
)->x
) {
49 return 2; /* error value for duplicates */
50 } else if ((coordinates_array
+ i
)->y
== 0 && (coordinates_array
+ i
)->x
== 0) {
51 coordinates_array
[i
] = new_coordinates
;
52 return 0; /* error value when everything if fine */
55 return 4; /* error value for unknown error case - should never happen - */
58 bool chk_win_conditions(coordinates_t
* coordinates_array
) {
59 for (unsigned i
= 0; i
< MAX_COORDINATES
; i
++) {