Spell fix in comment.
[TD_C.git] / TP_13 / exo2 / lib / coordinates.c
index 0a2d5e028c406579b8da3c3509727463fee1710e..fc5cb4f3ea50f3d2f7dcce179a672759ae7b7027 100644 (file)
@@ -18,9 +18,7 @@
 
 #include "coordinates.h"
 
-#include <ncurses.h>
-
-void init_coordinates(coordinates_t* coordinates_array) {
+void zero_coordinates(coordinates_t* coordinates_array) {
 
     for (unsigned i = 0; i < MAX_COORDINATES; i++) {
         coordinates_array[i] = set_coordinates(0, 0, 0);
@@ -37,17 +35,18 @@ coordinates_t set_coordinates(int y, int x, unsigned type) {
 }
 
 /* FIXME: Does it worth doing a coordinates_t get_coordinates(int y, int x, unsigned type); helper function? */
-/* Or a int get_coordinates_x(int y, int x, unsigned type); helper function? */
+/* Or a int get_coordinates_x(coordinates_t coordinates); helper function? */
 
-/* the function do a lot of sanity checks before adding new board elements,
- * hence the loop. moving the checks in the main loop is also possible */
+/* this function do a lot of sanity checks before adding new board elements,
+ * hence the loop. moving the checks in the main loop is also possible.
+ * for now, just branch in the main loop given this function error value returned. */
 unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array, unsigned round) {
 
     /* 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 */
-    } else if (round == MAX_COORDINATES) {
-        /* round is off-by-one */
+    } else if (round == MAX_COORDINATES - 1) {
+        /* round is off-by-one vs. MAX_COORDINATES */
         coordinates_array[MAX_COORDINATES - 1] = new_coordinates;
         return 1; /* error value for full array */
     }
@@ -67,7 +66,7 @@ static bool chk_line(coordinates_t* coordinates_array, int line_number, unsigned
     unsigned nb_o_align = 0;
     unsigned nb_x_align = 0;
 
-    for (unsigned i = 0; i < round; i++) {
+    for (unsigned i = 0; i <= round; i++) {
         /* check if they are all the same */
         if ((coordinates_array + i)->y == line_number && (coordinates_array + i)->type == 0) {
             nb_o_align++;
@@ -86,7 +85,7 @@ static bool chk_column(coordinates_t* coordinates_array, int column_number, unsi
     unsigned nb_o_align = 0;
     unsigned nb_x_align = 0;
 
-    for (unsigned i = 0; i < round; i++) {
+    for (unsigned i = 0; i <= round; i++) {
         /* check if they are all the same */
         if ((coordinates_array + i)->x == column_number && (coordinates_array + i)->type == 0) {
             nb_o_align++;
@@ -106,7 +105,7 @@ static bool chk_diagonals(coordinates_t* coordinates_array, unsigned round) {
     unsigned nb_o_diag_one = 0, nb_o_diag_two = 0;
     unsigned nb_x_diag_one = 0, nb_x_diag_two = 0;
 
-    for (unsigned i = 0; i < round; i++) {
+    for (unsigned i = 0; i <= round; i++) {
         /* dumb count of each elements type in the two diagonals */
         for (int y_x_diag = 1; y_x_diag < 4; y_x_diag++) {
             if ((coordinates_array + i)->y == y_x_diag && (coordinates_array + i)->x == y_x_diag && (coordinates_array + i)->type == 0) {