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