From: Jérôme Benoit Date: Thu, 16 Mar 2017 22:08:16 +0000 (+0100) Subject: TP 13 exo2: More routine for X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5df3071eec60ca43bc66e1266820de0572d4b629;p=TD_C.git TP 13 exo2: More routine for - handle the grid elements; - print the active elements. Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo2/README b/TP_13/exo2/README new file mode 100644 index 0000000..10090fe --- /dev/null +++ b/TP_13/exo2/README @@ -0,0 +1,10 @@ +Tic-tac-toe +----------- + +Touches correspondantes au case de la grille: + +a|z|e +-+-+- +q|s|d +-+-+- +w|x|c diff --git a/TP_13/exo2/lib/coordinates.c b/TP_13/exo2/lib/coordinates.c new file mode 100644 index 0000000..f9123e9 --- /dev/null +++ b/TP_13/exo2/lib/coordinates.c @@ -0,0 +1,51 @@ +/* + * ===================================================================================== + * + * Filename: coordinates.c + * + * Description: Data definition and functions to manipulate elements in the grid + * + * Version: 1.0 + * Created: 16/03/2017 19:05:02 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#include "coordinates.h" + +void init_coordinates(coordinates_t* coordinates_array) { + for (unsigned i = 0; i < MAX_COORDINATES; i++) { + coordinates_array[i] = set_coordinates(0, 0, 0); + } +} + +coordinates_t set_coordinates(int y, int x, unsigned type) { + coordinates_t new_coordinates; + + new_coordinates.y = y; + new_coordinates.x = x; + new_coordinates.type = type; + return new_coordinates; +} + +unsigned add_coordinates(coordinates_t new_coordinates, coordinates_t* coordinates_array) { + /* 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 */ + } + 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) { + return 2; /* error value for duplicates */ + } else if ((coordinates_array + i)->y == 0 && (coordinates_array + i)->x == 0) { + coordinates_array[i] = new_coordinates; + return 0; /* error value when everything if fine */ + } + } + return 1; /* error value for full array */ +} diff --git a/TP_13/exo2/lib/coordinates.h b/TP_13/exo2/lib/coordinates.h new file mode 100644 index 0000000..873d404 --- /dev/null +++ b/TP_13/exo2/lib/coordinates.h @@ -0,0 +1,35 @@ +/* + * ===================================================================================== + * + * Filename: coordinates.h + * + * Description: Header for data definition and functions to manipulate elements in the grid + * + * Version: 1.0 + * Created: 16/03/2017 19:06:16 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#ifndef COORDINATES_H +#define COORDINATES_H + +/* we only have nine elements in the grid */ +#define MAX_COORDINATES 9 + +typedef struct coordinates_s { + int y; + int x; + unsigned type; /* 0 = O, 1 = X */ +} coordinates_t; + +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); + +#endif /* COORDINATES_H */ diff --git a/TP_13/exo2/lib/display.c b/TP_13/exo2/lib/display.c index 22deb96..1e5def8 100644 --- a/TP_13/exo2/lib/display.c +++ b/TP_13/exo2/lib/display.c @@ -18,6 +18,8 @@ #include +#include "display.h" + /* in all print routine, y and x are the coordinates of the first character of the shape * which can be a space ' ' */ @@ -42,8 +44,8 @@ void print_board(int y, int x) { * - base_y + 3, base_x + 11 * - base_y + 6, base_x + 6 * - base_y + 6, base_x + 11 - * The added y value can be {0, 3, 6} - * The added x value can be {1, 6, 11} */ + * The added (y, x) couple values can be {0, 3, 6}x{1, 6, 11} + */ void print_x(int y, int x) { mvprintw(y, x, "\\/"); @@ -54,3 +56,38 @@ void print_o(int y, int x) { mvprintw(y, x, "/\\"); mvprintw(y+1, x, "\\/"); } + +/* y: 1 -> +0 x: 1 -> +1 + * 2 -> +3 2 -> +6 + * 3 -> +6 3 -> +11 */ +static int remap_y(int y) { + if (y == 1) { + return 0; + } else if (y == 2) { + return 3; + } else { + return 6; + } +} + +static int remap_x(int x) { + if (x == 1) { + return 1; + } else if (x == 2) { + return 6; + } else { + return 11; + } +} + +void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x) { + unsigned i = 0; + while ((coordinates_array + i)->y != 0 && (coordinates_array + i)->x != 0) { + if ((coordinates_array + i)->type == 0) { + print_o(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x)); + } else { + print_x(base_y + remap_y((coordinates_array + i)->y), base_x + remap_x((coordinates_array + i)->x)); + } + i++; + } +} diff --git a/TP_13/exo2/lib/display.h b/TP_13/exo2/lib/display.h index f0b1136..c3390fe 100644 --- a/TP_13/exo2/lib/display.h +++ b/TP_13/exo2/lib/display.h @@ -19,8 +19,11 @@ #ifndef DISPLAY_H #define DISPLAY_H +#include "coordinates.h" + void print_board(int y, int x); void print_x(int y, int x); void print_o(int y, int x); +void print_coordinates(coordinates_t coordinates_array[], int base_y, int base_x); #endif /* DISPLAY_H */ diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c index fcd3bcc..123ab08 100644 --- a/TP_13/exo2/src/main.c +++ b/TP_13/exo2/src/main.c @@ -1,32 +1,39 @@ #include #include +#include #include #include "display.h" +#include "coordinates.h" int main() { int row, col; + char* top_msg = ""; + char* back_msg = ""; 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); + /* 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; + 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); - 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); + print_coordinates(coordinates_array, base_y, base_x); refresh();