Fix an off-by-one on the pawn 2D array indexes.
[Project_algorithmic_C.git] / src / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <ncurses.h>
5
6 #include "ui.h"
7 #include "othello.h"
8 #include "debug.h"
9
10 int main() {
11 int row = 0, col = 0;
12 unsigned int round = 0;
13 unsigned int player = player_one; /* first player is black */
14 bool exit_condition = false;
15 unsigned int nb_white = 0, nb_black = 0;
16
17 LIST_HEAD(shots_list);
18
19 char* player_msg;
20
21 unsigned int pawns[board_size][board_size] = {{}};
22 pawns[board_size][board_size] = init_pawns(pawns);
23
24 initscr();
25 if (has_colors() == false) {
26 endwin();
27 printf("Votre terminal ne supporte pas les couleurs\n");
28 exit(EXIT_FAILURE);
29 }
30 start_color();
31 getmaxyx(stdscr, row, col);
32 /* FIXME: fail if the screen size is too small */
33 //noecho();
34 echo();
35 curs_set(0);
36
37 /* center */
38 int center_y = row/2;
39 int center_x = col/2;
40 /* base coordinates to center the board */
41 int board_center_y = center_y - 26/2;
42 int board_center_x = center_x - 42/2;
43
44 do {
45 print_board(board_center_y, board_center_x);
46 print_pawns(board_center_y, board_center_x, pawns);
47
48 char* title_msg = "Jeu Othello";
49 mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg);
50
51 nb_white = count_pawn_type(pawns, white);
52 nb_black = count_pawn_type(pawns, black);
53
54 char* score_white_msg = "Pions blancs: %d";
55 mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white);
56 char* score_black_msg = "Pions noirs: %d";
57 mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black);
58
59 player = current_player(round);
60
61 if (player == player_one) {
62 player_msg = "Joueur un (noir) joue !";
63 } else {
64 player_msg = "Joueur deux (blanc) joue !";
65 }
66 mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg);
67
68 int y;
69 char x;
70 bool input_ok = false;
71 do {
72 y = 0;
73 x = "";
74 char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):";
75 int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x);
76 if (is_valid_input(y, map_col_letter_to_int(x), pawns) && prmt_rt == 1) {
77 input_ok = true;
78 }
79 } while (!input_ok);
80 pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_int(x), player, pawns);
81 struct shots_list shot_current;
82 shot_current.pawn_array_member = &pawns[y-1][x-1];
83 //list_add(shot_current.list, shots_list.list);
84
85 round++; /* increment the round count */
86
87 refresh();
88
89 if (is_board_full(pawns) || round == 60) {
90 /* print the winnner and the restart possibility */
91 exit_condition = true;
92 }
93
94 } while (!exit_condition);
95
96 endwin();
97
98 exit(EXIT_SUCCESS);
99 }