| 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 | /* 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 */ |
| 44 | } |
| 45 | |
| 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 */ |
| 53 | } |
| 54 | } |
| 55 | return 4; /* error value for unknown error case - should never happen - */ |
| 56 | } |
| 57 | |
| 58 | bool chk_win_conditions(coordinates_t* coordinates_array) { |
| 59 | for (unsigned i = 0; i < MAX_COORDINATES; i++) { |
| 60 | |
| 61 | } |
| 62 | return false; |
| 63 | } |