X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo2%2Flib%2Fcoordinates.c;h=883f11962521c4561d197b67da734490a003eed6;hb=a177f80cdc31a9df8097f56a33ae1be7d24ef0af;hp=624de14df11ad3715e57f19d505fced123191cdf;hpb=a7ebbad77d79b05e88e3dc07ab844770419205bd;p=TD_C.git diff --git a/TP_13/exo2/lib/coordinates.c b/TP_13/exo2/lib/coordinates.c index 624de14..883f119 100644 --- a/TP_13/exo2/lib/coordinates.c +++ b/TP_13/exo2/lib/coordinates.c @@ -18,8 +18,6 @@ #include "coordinates.h" -#include - void init_coordinates(coordinates_t* coordinates_array) { for (unsigned i = 0; i < MAX_COORDINATES; i++) { @@ -27,6 +25,11 @@ void init_coordinates(coordinates_t* coordinates_array) { } } +void reinit_coordinates(coordinates_t* coordinates_array) { + + init_coordinates(coordinates_array); +} + coordinates_t set_coordinates(int y, int x, unsigned type) { coordinates_t new_coordinates; @@ -46,8 +49,8 @@ unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinat /* 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 +70,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 +89,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 +109,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) { @@ -131,8 +134,8 @@ static bool chk_diagonals(coordinates_t* coordinates_array, unsigned round) { bool chk_win_conditions(coordinates_t* coordinates_array, unsigned round) { - /* winning conditions begin at round = 5 */ - if (round > 4) { + /* winning conditions begin at round = 4 */ + if (round > 3) { if (chk_diagonals(coordinates_array, round)) { return true; }