Code cleanup and fix the logic behind the player constants number
[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() {
a8b54576 11 int min_y = 26, min_x = 42;
4ddf6f1a 12 int row = 0, col = 0;
74e2b93b
JB
13 unsigned int round = 0;
14 unsigned int player = player_one; /* first player is black */
4ddf6f1a 15 bool exit_condition = false;
74e2b93b
JB
16 unsigned int nb_white = 0, nb_black = 0;
17
54f1c58c
JB
18 LIST_HEAD(shots_list);
19
a8b54576
JB
20 char* title_msg = "Jeu Othello";
21 char* score_white_msg = "Pions blancs: %d";
22 char* score_black_msg = "Pions noirs: %d";
23 char* player_msg = "Joueur %d joue !";
24 char* winner_msg = "Joueur %d gagne !";
25 char* draw_msg = "Egalite !";
74e2b93b
JB
26
27 unsigned int pawns[board_size][board_size] = {{}};
28 pawns[board_size][board_size] = init_pawns(pawns);
4ddf6f1a
JB
29
30 initscr();
31 if (has_colors() == false) {
32 endwin();
33 printf("Votre terminal ne supporte pas les couleurs\n");
34 exit(EXIT_FAILURE);
35 }
36 start_color();
37 getmaxyx(stdscr, row, col);
54f1c58c 38 /* FIXME: fail if the screen size is too small */
74e2b93b 39 echo();
4ddf6f1a
JB
40 curs_set(0);
41
74e2b93b
JB
42 /* center */
43 int center_y = row/2;
44 int center_x = col/2;
45 /* base coordinates to center the board */
46 int board_center_y = center_y - 26/2;
47 int board_center_x = center_x - 42/2;
4ddf6f1a
JB
48
49 do {
74e2b93b
JB
50 print_board(board_center_y, board_center_x);
51 print_pawns(board_center_y, board_center_x, pawns);
52
74e2b93b 53 mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg);
4ddf6f1a 54
a8b54576
JB
55 player = current_player(round);
56
57 mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg, player);
58
74e2b93b
JB
59 nb_white = count_pawn_type(pawns, white);
60 nb_black = count_pawn_type(pawns, black);
61
74e2b93b 62 mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white);
74e2b93b
JB
63 mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black);
64
74e2b93b
JB
65 int y;
66 char x;
67 bool input_ok = false;
68 do {
69 y = 0;
70 x = "";
54f1c58c
JB
71 char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):";
72 int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x);
73 if (is_valid_input(y, map_col_letter_to_int(x), pawns) && prmt_rt == 1) {
74e2b93b
JB
74 input_ok = true;
75 }
76 } while (!input_ok);
77 pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_int(x), player, pawns);
54f1c58c
JB
78 struct shots_list shot_current;
79 shot_current.pawn_array_member = &pawns[y-1][x-1];
80 //list_add(shot_current.list, shots_list.list);
74e2b93b
JB
81
82 round++; /* increment the round count */
83
84 refresh();
85
a8b54576
JB
86 /* here are all the end of the game conditions */
87 //if (is_board_full(pawns) || round == 60) {
88 if (is_board_full(pawns)) {
89 int winner = eval_winner(nb_white, nb_black);
90 if (winner != 0) {
91 mvprintw(center_y - 26/2 - 2, (center_x - strlen(winner_msg)/2), winner_msg, winner);
92 } else {
93 mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg);
94 }
95 /* print and implement restart possibility */
54f1c58c
JB
96 exit_condition = true;
97 }
98
74e2b93b
JB
99 } while (!exit_condition);
100
4ddf6f1a
JB
101 endwin();
102
103 exit(EXIT_SUCCESS);
104}