| 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 | |
| 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); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | coordinates_t set_coordinates(int y, int x, unsigned type) { |
| 28 | coordinates_t new_coordinates; |
| 29 | |
| 30 | new_coordinates.y = y; |
| 31 | new_coordinates.x = x; |
| 32 | new_coordinates.type = type; |
| 33 | return new_coordinates; |
| 34 | } |
| 35 | |
| 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 */ |
| 40 | } |
| 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 */ |
| 48 | } |
| 49 | } |
| 50 | return 1; /* error value for full array */ |
| 51 | } |