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 unsigned add_coordinates(coordinates_t new_coordinates
, coordinates_t
* coordinates_array
) {
37 /* valid coordinates are in the [1-3] range */
38 if (new_coordinates
.y
< 1 || new_coordinates
.y
> 3 || new_coordinates
.x
< 1 || new_coordinates
.x
> 3) {
39 return 3; /* error value for invalid coordinates */
41 for (unsigned i
= 0; i
< MAX_COORDINATES
; i
++) {
42 /* check if already entered */
43 if (new_coordinates
.y
== (coordinates_array
+ i
)->y
&& new_coordinates
.x
== (coordinates_array
+ i
)->x
) {
44 return 2; /* error value for duplicates */
45 } else if ((coordinates_array
+ i
)->y
== 0 && (coordinates_array
+ i
)->x
== 0) {
46 coordinates_array
[i
] = new_coordinates
;
47 return 0; /* error value when everything if fine */
50 return 1; /* error value for full array */