TP_13 exo2: Finish the implementation of a basic tic-tac-toe game.
[TD_C.git] / TP_13 / exo2 / src / main.c
CommitLineData
7515f897
JB
1#include <stdlib.h>
2#include <stdio.h>
5df3071e 3#include <string.h>
7515f897
JB
4#include <ncurses.h>
5
6#include "display.h"
5df3071e 7#include "coordinates.h"
7515f897
JB
8
9int main() {
2737ed7c 10 int row, col, errno = 0, round = 0, player = 0, key_pressed;
811d4abe
JB
11 bool winning_condition = false;
12 bool loop_exit_condition = false;
04b0afb5 13 const int str_max_length = 255;
811d4abe 14 /* FIXME: make a strings handling library */
04b0afb5
JB
15 char* top_msg = malloc(str_max_length * sizeof(char));
16 char* back_msg = malloc(str_max_length * sizeof(char));
811d4abe 17 char* exit_msg = malloc(str_max_length * sizeof(char));
7515f897
JB
18
19 initscr();
5df3071e 20 getmaxyx(stdscr, row, col);
7515f897
JB
21 noecho();
22 curs_set(0);
23
5df3071e
JB
24 /* array of the active coordinates in the entered order */
25 coordinates_t coordinates_array[MAX_COORDINATES];
26 init_coordinates(coordinates_array);
2737ed7c 27 coordinates_t new_coordinates = {0, 0, 0};
5df3071e 28
7515f897 29 /* center base coordinates for the board */
5df3071e
JB
30 int base_y = row/2 - 4;
31 int base_x = col/2 - 7;
7515f897
JB
32
33 print_board(base_y, base_x);
5df3071e 34
2737ed7c 35 do {
04b0afb5
JB
36 if (errno == 0) round++;
37
38 if (round % 2 == 0) {
2737ed7c
JB
39 player = 1;
40 top_msg = "Joueur 2 joue";
04b0afb5
JB
41 } else {
42 player = 0;
43 top_msg = "Joueur 1 joue";
2737ed7c 44 }
7515f897 45
2737ed7c 46 mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg);
7515f897 47
2737ed7c
JB
48 print_coordinates(coordinates_array, base_y, base_x);
49
811d4abe
JB
50 /* FIXME: group the winning case code blocks */
51 if (!winning_condition) {
52 /* getch() is blocking */
53 key_pressed = getch();
54 switch (key_pressed) {
55 case 'a':
56 new_coordinates = set_coordinates(1, 1, player);
57 break;
58 case 'z':
59 new_coordinates = set_coordinates(1, 2, player);
60 break;
61 case 'e':
62 new_coordinates = set_coordinates(1, 3, player);
63 break;
64 case 'q':
65 new_coordinates = set_coordinates(2, 1, player);
66 break;
67 case 's':
68 new_coordinates = set_coordinates(2, 2, player);
69 break;
70 case 'd':
71 new_coordinates = set_coordinates(2, 3, player);
72 break;
73 case 'w':
74 new_coordinates = set_coordinates(3, 1, player);
75 break;
76 case 'x':
77 new_coordinates = set_coordinates(3, 2, player);
78 break;
79 case 'c':
80 new_coordinates = set_coordinates(3, 3, player);
81 break;
82 default:
83 /* set invalid coordinates */
84 new_coordinates = set_coordinates(0, 0, player);
85 break;
86 }
87 errno = add_coordinates(new_coordinates, coordinates_array, round);
88 winning_condition = chk_win_conditions(coordinates_array, round);
2737ed7c
JB
89 }
90
811d4abe
JB
91 if (winning_condition) {
92 if (player == 0) {
93 back_msg = "Joureur 1 gagne !";
94 } else {
95 back_msg = "Joureur 2 gagne !";
96 }
97 }
98
99 if (!winning_condition) {
100 if (errno == 2) {
101 back_msg = "Choisir une case vide";
102 } else if (errno == 3) {
103 back_msg = "Coordonnees invalides";
104 } else if (errno == 1) {
105 back_msg = "Tableau rempli sans gagnant: egalite";
106 } else if (errno == 4) {
107 back_msg = "Erreur inconnue";
108 } else if (errno == 0) {
109 /* FIXME: properly zero the string */
110 back_msg = "";
111 }
2737ed7c
JB
112 }
113
04b0afb5
JB
114 mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg);
115
811d4abe
JB
116 if (winning_condition || errno == 1) {
117 /* print the updated coordinates before exiting */
118 print_coordinates(coordinates_array, base_y, base_x);
119 exit_msg = "Pressez une touche pour sortir";
120 mvprintw(base_y + 12, (base_x + 7 - strlen(exit_msg)/2), exit_msg);
121 loop_exit_condition = true;
122 }
123
04b0afb5
JB
124 refresh();
125
811d4abe
JB
126 } while (!loop_exit_condition);
127
128 /* getch() is blocking */
129 getch();
130
131 endwin();
2737ed7c 132
04b0afb5
JB
133 if (!top_msg)
134 free(top_msg);
135 if (!back_msg)
136 free(back_msg);
811d4abe
JB
137 if (!exit_msg)
138 free(exit_msg);
7515f897
JB
139
140 exit(EXIT_SUCCESS);
141}