From: Jérôme Benoit Date: Fri, 17 Mar 2017 20:42:01 +0000 (+0100) Subject: TP 13 exo2: preliminary loop for a full tic-tac-toe game for 2 players X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2737ed7c252e5f624b8f98cbe28eb05450c9420e;p=TD_C.git TP 13 exo2: preliminary loop for a full tic-tac-toe game for 2 players Still a little bit buggy. Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index 123ab08..0606beb 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -7,7 +7,7 @@ #include "coordinates.h" int main() { - int row, col; + int row, col, errno = 0, round = 0, player = 0, key_pressed; char* top_msg = ""; char* back_msg = ""; @@ -19,25 +19,77 @@ int main() { /* 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 */ int base_y = row/2 - 4; int base_x = col/2 - 7; - if (!top_msg) mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); print_board(base_y, base_x); - if (!back_msg) mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); - int errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array); - errno = add_coordinates(set_coordinates(1, 3, 0), coordinates_array); - errno = add_coordinates(set_coordinates(2, 3, 1), coordinates_array); - errno = add_coordinates(set_coordinates(1, 1, 0), coordinates_array); + do { + if (round % 2 == 0 || round == 0) { + player = 0; + top_msg = "Joueur 1 joue"; + } else { + player = 1; + top_msg = "Joueur 2 joue"; + } - print_coordinates(coordinates_array, base_y, base_x); - - refresh(); + 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); - while (getch() != 'q'); + 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; + } + + errno = add_coordinates(new_coordinates, coordinates_array); + + 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 = ""; + } + + round++; + + } while (errno == 0 || errno == 2); endwin();