X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo2%2Fsrc%2Fmain.c;h=d57be23271c7e3d812ee9fba647d722fd4e7a02f;hb=a177f80cdc31a9df8097f56a33ae1be7d24ef0af;hp=fcd3bcc04279d7d61d6f5b838e00794d10f6f7ad;hpb=7515f89753dd7d16c4f2a9b11003f67d8148eb09;p=TD_C.git diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index fcd3bcc..d57be23 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -1,38 +1,155 @@ #include #include +#include #include #include "display.h" +#include "coordinates.h" int main() { - int row, col; + 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); + getmaxyx(stdscr, row, col); noecho(); curs_set(0); + /* array of the active coordinates in the entered order */ + coordinates_t coordinates_array[MAX_COORDINATES]; + init_coordinates(coordinates_array); + coordinates_t new_coordinates = {0, 0, 0}; + /* center base coordinates for the board */ - const int base_y = row/2 - 4; - const int base_x = col/2 - 7; + int base_y = row/2 - 4; + int base_x = col/2 - 7; + + do { + 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); + + print_board(base_y, base_x); + + print_coordinates(coordinates_array, base_y, base_x); + + /* 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); + } - print_board(base_y, base_x); + if (errno == 0) { + round++; + } - print_x(base_y, base_x + 1); - print_o(base_y, base_x + 6); - print_o(base_y, base_x + 11); - print_o(base_y + 3, base_x + 1); - print_o(base_y + 6, base_x + 1); - print_o(base_y + 3, base_x + 6); - print_x(base_y + 3, base_x + 11); - print_x(base_y + 6, base_x + 6); - print_x(base_y + 6, base_x + 11); - - refresh(); + if (winning_condition) { + if (player == 0) { + back_msg = "Joueur 1 gagne !"; + } else { + back_msg = "Joueur 2 gagne !"; + } + } - while (getch() != 'q'); + 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 (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; + reinit_coordinates(coordinates_array); + winning_condition = false; + loop_exit_condition = false; + clear(); + } + } + } + + refresh(); + + } 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); }