X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo2%2Fsrc%2Fmain.c;h=854ddb9c1e52d9dd6a90622eda0b11120080e55b;hb=9c2b41f4a3539f1087e5560d8ceb301978f8a55b;hp=0606beba47b507e5f49dbf755df029a907a27075;hpb=2737ed7c252e5f624b8f98cbe28eb05450c9420e;p=TD_C.git diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index 0606beb..854ddb9 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -7,9 +7,14 @@ #include "coordinates.h" int main() { - int row, col, errno = 0, round = 0, player = 0, key_pressed; - char* top_msg = ""; - char* back_msg = ""; + int row, col, errno = 0, round = 0, player = 0, key_pressed = 0; + bool winning_condition = false; + bool loop_exit_condition = false; + const int str_max_length = 50; + /* FIXME: make a strings handling library */ + char* top_msg = malloc(str_max_length * sizeof(char)); + char* back_msg = malloc(str_max_length * sizeof(char)); + char* exit_msg = malloc(str_max_length * sizeof(char)); initscr(); getmaxyx(stdscr, row, col); @@ -17,81 +22,143 @@ int main() { curs_set(0); /* array of the active coordinates in the entered order */ - coordinates_t coordinates_array[MAX_COORDINATES]; - init_coordinates(coordinates_array); + coordinates_t coordinates_array[MAX_COORDINATES] = { + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0} + }; coordinates_t new_coordinates = {0, 0, 0}; /* center base coordinates for the board */ int base_y = row/2 - 4; int base_x = col/2 - 7; - print_board(base_y, base_x); - do { - if (round % 2 == 0 || round == 0) { - player = 0; - top_msg = "Joueur 1 joue"; - } else { + 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_board(base_y, base_x); print_coordinates(coordinates_array, base_y, base_x); - key_pressed = getch(); - switch (key_pressed) { - case 'a': - new_coordinates = set_coordinates(1, 1, player); - break; - case 'z': - new_coordinates = set_coordinates(1, 2, player); - break; - case 'e': - new_coordinates = set_coordinates(1, 3, player); - break; - case 'q': - new_coordinates = set_coordinates(2, 1, player); - break; - case 's': - new_coordinates = set_coordinates(2, 2, player); - break; - case 'd': - new_coordinates = set_coordinates(2, 3, player); - break; - case 'w': - new_coordinates = set_coordinates(3, 1, player); - break; - case 'x': - new_coordinates = set_coordinates(3, 2, player); - break; - case 'c': - new_coordinates = set_coordinates(3, 3, player); - break; - default: - continue; - break; + /* FIXME: group the winning case code blocks */ + if (!winning_condition) { + /* getch() is blocking */ + key_pressed = getch(); + switch (key_pressed) { + case 'a': + new_coordinates = set_coordinates(1, 1, player); + break; + case 'z': + new_coordinates = set_coordinates(1, 2, player); + break; + case 'e': + new_coordinates = set_coordinates(1, 3, player); + break; + case 'q': + new_coordinates = set_coordinates(2, 1, player); + break; + case 's': + new_coordinates = set_coordinates(2, 2, player); + break; + case 'd': + new_coordinates = set_coordinates(2, 3, player); + break; + case 'w': + new_coordinates = set_coordinates(3, 1, player); + break; + case 'x': + new_coordinates = set_coordinates(3, 2, player); + break; + case 'c': + new_coordinates = set_coordinates(3, 3, player); + break; + default: + /* set invalid coordinates */ + new_coordinates = set_coordinates(0, 0, player); + break; + } + errno = add_coordinates(new_coordinates, coordinates_array, round); + winning_condition = chk_win_conditions(coordinates_array, round); } - errno = add_coordinates(new_coordinates, coordinates_array); + if (errno == 0) { + round++; + } + + if (winning_condition) { + if (player == 0) { + back_msg = "Joueur 1 gagne !"; + } else { + back_msg = "Joueur 2 gagne !"; + } + } + + if (!winning_condition) { + if (errno == 2) { + back_msg = "Choisir une case vide"; + } else if (errno == 3) { + back_msg = "Coordonnees invalides"; + } 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 to avoid the clear() */ + back_msg = ""; + clear(); + } + } + + mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); - if (errno == 2) { - back_msg = "Choisir une case vide"; - continue; - } else if (errno == 3) { - back_msg = "Coordonnees invalides"; - continue; - } else if (errno == 0) { - back_msg = ""; + if (winning_condition || errno == 1) { + /* print the updated coordinates before exiting */ + print_coordinates(coordinates_array, base_y, base_x); + exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; + mvprintw(base_y + 12, (base_x + 7 - strlen(exit_msg)/2), exit_msg); + loop_exit_condition = true; + if (loop_exit_condition) { + int key_exit = 0; + /* getch() is blocking */ + key_exit = getch(); + if (key_exit == 'r') { + round = 0; + player = 0; + errno = 0; + zero_coordinates(coordinates_array); + winning_condition = false; + loop_exit_condition = false; + clear(); + } + } } - round++; + refresh(); - } while (errno == 0 || errno == 2); + } while (!loop_exit_condition); endwin(); + if (!top_msg) + free(top_msg); + if (!back_msg) + free(back_msg); + if (!exit_msg) + free(exit_msg); + exit(EXIT_SUCCESS); }