From 04b0afb5d5c81f1d98e98b9a6e532b1d3c868cc4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 19 Mar 2017 17:38:11 +0100 Subject: [PATCH] TP 13 exo2: Implement the missing bits to do a full game. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Still missing the win condition check. Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/io.c | 10 ++++++++- TP_13/exo1/lib/io.h | 1 + TP_13/exo1/lib/utils.c | 8 -------- TP_13/exo1/lib/utils.h | 2 -- TP_13/exo2/lib/coordinates.c | 16 +++++++++++++-- TP_13/exo2/lib/coordinates.h | 5 ++++- TP_13/exo2/src/main.c | 40 ++++++++++++++++++++++++------------ 7 files changed, 55 insertions(+), 27 deletions(-) diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index e5a3000..69b13d7 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -1,9 +1,9 @@ +#include #include #include #include "io.h" #include "array.h" -#include "utils.h" int prompt_value(const char* msg, int* result) { puts(msg); @@ -108,6 +108,14 @@ void do_resize(int array[], unsigned* old_size) { *old_size = new_size; } +void handle_prompt_error(int errno) { + if (errno != 0) { + printf("\nMerci de saisir un nombre entier, exiting\n"); + /* it's somewhat violent but better than looping forever */ + exit(EXIT_FAILURE); + } +} + void display_array(int array[], unsigned size) { if (array != NULL) { printf("\n--array begin--\n"); diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h index 24de1b1..6dfc77a 100644 --- a/TP_13/exo1/lib/io.h +++ b/TP_13/exo1/lib/io.h @@ -3,6 +3,7 @@ int prompt_value(const char* msg, int* result); int* prompt_array(int array[], unsigned* size); +void handle_prompt_error(int errno); void display_choice_menu(); diff --git a/TP_13/exo1/lib/utils.c b/TP_13/exo1/lib/utils.c index 5139bae..1f0b140 100644 --- a/TP_13/exo1/lib/utils.c +++ b/TP_13/exo1/lib/utils.c @@ -13,11 +13,3 @@ void swap_ptr(void* v1, void* v2) { v1 = v2; v2 = tmp; } - -void handle_prompt_error(int errno) { - if (errno != 0) { - printf("\nMerci de saisir un nombre entier, exiting\n"); - /* it's somewhat violent but better than looping forever */ - exit(EXIT_FAILURE); - } -} diff --git a/TP_13/exo1/lib/utils.h b/TP_13/exo1/lib/utils.h index fe3fa08..03f1226 100644 --- a/TP_13/exo1/lib/utils.h +++ b/TP_13/exo1/lib/utils.h @@ -6,6 +6,4 @@ void swap_int(int* v1, int* v2); void swap_ptr(void* v1, void* v2); -void handle_prompt_error(int errno); - #endif /* UTILS_H */ diff --git a/TP_13/exo2/lib/coordinates.c b/TP_13/exo2/lib/coordinates.c index f9123e9..a9d8159 100644 --- a/TP_13/exo2/lib/coordinates.c +++ b/TP_13/exo2/lib/coordinates.c @@ -33,11 +33,16 @@ coordinates_t set_coordinates(int y, int x, unsigned type) { return new_coordinates; } -unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array) { +/* 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 */ +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 + 1) { + return 1; /* error value for full array */ } + 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) { @@ -47,5 +52,12 @@ unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinat return 0; /* error value when everything if fine */ } } - return 1; /* error value for full array */ + return 4; /* error value for unknown error case - should never happen - */ +} + +bool chk_win_conditions(coordinates_t* coordinates_array) { + for (unsigned i = 0; i < MAX_COORDINATES; i++) { + + } + return false; } diff --git a/TP_13/exo2/lib/coordinates.h b/TP_13/exo2/lib/coordinates.h index 873d404..3a17ade 100644 --- a/TP_13/exo2/lib/coordinates.h +++ b/TP_13/exo2/lib/coordinates.h @@ -19,6 +19,8 @@ #ifndef COORDINATES_H #define COORDINATES_H +#include + /* we only have nine elements in the grid */ #define MAX_COORDINATES 9 @@ -30,6 +32,7 @@ typedef struct coordinates_s { void init_coordinates(coordinates_t* coordinates_array); coordinates_t set_coordinates(int y, int x, unsigned type); -unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array); +unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array, unsigned round); +bool chk_win_conditions(coordinates_t* coordinates_array); #endif /* COORDINATES_H */ diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index 0606beb..390bc40 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -8,8 +8,9 @@ int main() { int row, col, errno = 0, round = 0, player = 0, key_pressed; - char* top_msg = ""; - char* back_msg = ""; + const int str_max_length = 255; + char* top_msg = malloc(str_max_length * sizeof(char)); + char* back_msg = malloc(str_max_length * sizeof(char)); initscr(); getmaxyx(stdscr, row, col); @@ -28,19 +29,21 @@ int main() { print_board(base_y, base_x); do { - if (round % 2 == 0 || round == 0) { - player = 0; - top_msg = "Joueur 1 joue"; - } else { + if (errno == 0) round++; + + if (round % 2 == 0) { player = 1; top_msg = "Joueur 2 joue"; + } else { + player = 0; + top_msg = "Joueur 1 joue"; } mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); - mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); print_coordinates(coordinates_array, base_y, base_x); + /* getch() is blocking */ key_pressed = getch(); switch (key_pressed) { case 'a': @@ -71,25 +74,36 @@ int main() { new_coordinates = set_coordinates(3, 3, player); break; default: - continue; + /* set invalid coordinates */ + new_coordinates = set_coordinates(0, 0, player); break; } - errno = add_coordinates(new_coordinates, coordinates_array); + errno = add_coordinates(new_coordinates, coordinates_array, round); if (errno == 2) { back_msg = "Choisir une case vide"; - continue; } else if (errno == 3) { back_msg = "Coordonnees invalides"; - continue; + } else if (errno == 1) { + back_msg = "Tableau rempli sans gagnant: egalite"; + } else if (errno == 4) { + back_msg = "Erreur inconnue"; } else if (errno == 0) { + /* FIXME: properly zero the string */ back_msg = ""; } - round++; + mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); + + refresh(); + + } while (errno != 1); - } while (errno == 0 || errno == 2); + if (!top_msg) + free(top_msg); + if (!back_msg) + free(back_msg); endwin(); -- 2.34.1