Fix an off-by-one on the pawn 2D array indexes.
[Project_algorithmic_C.git] / src / main.c
CommitLineData
4ddf6f1a
JB
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <ncurses.h>
5
6#include "ui.h"
2e5c1894 7#include "othello.h"
54f1c58c 8#include "debug.h"
4ddf6f1a
JB
9
10int main() {
11 int row = 0, col = 0;
74e2b93b
JB
12 unsigned int round = 0;
13 unsigned int player = player_one; /* first player is black */
4ddf6f1a 14 bool exit_condition = false;
74e2b93b
JB
15 unsigned int nb_white = 0, nb_black = 0;
16
54f1c58c
JB
17 LIST_HEAD(shots_list);
18
74e2b93b
JB
19 char* player_msg;
20
21 unsigned int pawns[board_size][board_size] = {{}};
22 pawns[board_size][board_size] = init_pawns(pawns);
4ddf6f1a
JB
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);
54f1c58c 32 /* FIXME: fail if the screen size is too small */
74e2b93b
JB
33 //noecho();
34 echo();
4ddf6f1a
JB
35 curs_set(0);
36
74e2b93b
JB
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;
4ddf6f1a
JB
43
44 do {
74e2b93b
JB
45 print_board(board_center_y, board_center_x);
46 print_pawns(board_center_y, board_center_x, pawns);
47
54f1c58c 48 char* title_msg = "Jeu Othello";
74e2b93b 49 mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg);
4ddf6f1a 50
74e2b93b
JB
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 !";
4ddf6f1a 65 }
74e2b93b 66 mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg);
4ddf6f1a 67
74e2b93b
JB
68 int y;
69 char x;
70 bool input_ok = false;
71 do {
72 y = 0;
73 x = "";
54f1c58c
JB
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) {
74e2b93b
JB
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);
54f1c58c
JB
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);
74e2b93b
JB
84
85 round++; /* increment the round count */
86
87 refresh();
88
54f1c58c
JB
89 if (is_board_full(pawns) || round == 60) {
90 /* print the winnner and the restart possibility */
91 exit_condition = true;
92 }
93
74e2b93b
JB
94 } while (!exit_condition);
95
4ddf6f1a
JB
96 endwin();
97
98 exit(EXIT_SUCCESS);
99}