+/*
+ * =====================================================================================
+ *
+ * Filename: coordinates.c
+ *
+ * Description: Data definition and functions to manipulate elements in the grid
+ *
+ * Version: 1.0
+ * Created: 16/03/2017 19:05:02
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
+ * Organization: Piment Noir
+ *
+ * =====================================================================================
+ */
+
+#include "coordinates.h"
+
+void init_coordinates(coordinates_t* coordinates_array) {
+ for (unsigned i = 0; i < MAX_COORDINATES; i++) {
+ coordinates_array[i] = set_coordinates(0, 0, 0);
+ }
+}
+
+coordinates_t set_coordinates(int y, int x, unsigned type) {
+ coordinates_t new_coordinates;
+
+ new_coordinates.y = y;
+ new_coordinates.x = x;
+ new_coordinates.type = type;
+ return new_coordinates;
+}
+
+unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array) {
+ /* valid coordinates are in the [1-3] range */
+ if (new_coordinates.y < 1 || new_coordinates.y > 3 || new_coordinates.x < 1 || new_coordinates.x > 3) {
+ return 3; /* error value for invalid coordinates */
+ }
+ for (unsigned i = 0; i < MAX_COORDINATES; i++) {
+ /* check if already entered */
+ if (new_coordinates.y == (coordinates_array + i)->y && new_coordinates.x == (coordinates_array + i)->x) {
+ return 2; /* error value for duplicates */
+ } else if ((coordinates_array + i)->y == 0 && (coordinates_array + i)->x == 0) {
+ coordinates_array[i] = new_coordinates;
+ return 0; /* error value when everything if fine */
+ }
+ }
+ return 1; /* error value for full array */
+}