#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include <ncurses.h>
#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);
+ if (has_colors() == false) {
+ endwin();
+ printf("Votre terminal ne supporte pas les couleurs.\n");
+ exit(EXIT_FAILURE);
+ }
+ start_color();
+ getmaxyx(stdscr, row, col);
noecho();
curs_set(0);
+ /* array of the active coordinates in the entered order */
+ 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 */
- 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;
+ zero_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);
}