Commit | Line | Data |
---|---|---|
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" |
4ddf6f1a JB |
8 | |
9 | int main() { | |
10 | int row = 0, col = 0; | |
74e2b93b JB |
11 | unsigned int round = 0; |
12 | unsigned int player = player_one; /* first player is black */ | |
4ddf6f1a | 13 | bool exit_condition = false; |
74e2b93b JB |
14 | unsigned int nb_white = 0, nb_black = 0; |
15 | ||
16 | char* player_msg; | |
17 | ||
18 | unsigned int pawns[board_size][board_size] = {{}}; | |
19 | pawns[board_size][board_size] = init_pawns(pawns); | |
4ddf6f1a JB |
20 | |
21 | initscr(); | |
22 | if (has_colors() == false) { | |
23 | endwin(); | |
24 | printf("Votre terminal ne supporte pas les couleurs\n"); | |
25 | exit(EXIT_FAILURE); | |
26 | } | |
27 | start_color(); | |
28 | getmaxyx(stdscr, row, col); | |
74e2b93b JB |
29 | //noecho(); |
30 | echo(); | |
4ddf6f1a JB |
31 | curs_set(0); |
32 | ||
74e2b93b JB |
33 | /* center */ |
34 | int center_y = row/2; | |
35 | int center_x = col/2; | |
36 | /* base coordinates to center the board */ | |
37 | int board_center_y = center_y - 26/2; | |
38 | int board_center_x = center_x - 42/2; | |
4ddf6f1a JB |
39 | |
40 | do { | |
74e2b93b JB |
41 | print_board(board_center_y, board_center_x); |
42 | print_pawns(board_center_y, board_center_x, pawns); | |
43 | ||
44 | char* title_msg = "Jeu othello"; | |
45 | mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg); | |
4ddf6f1a | 46 | |
74e2b93b JB |
47 | nb_white = count_pawn_type(pawns, white); |
48 | nb_black = count_pawn_type(pawns, black); | |
49 | ||
50 | char* score_white_msg = "Pions blancs: %d"; | |
51 | mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white); | |
52 | char* score_black_msg = "Pions noirs: %d"; | |
53 | mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black); | |
54 | ||
55 | player = current_player(round); | |
56 | ||
57 | if (player == player_one) { | |
58 | player_msg = "Joueur un (noir) joue !"; | |
59 | } else { | |
60 | player_msg = "Joueur deux (blanc) joue !"; | |
4ddf6f1a | 61 | } |
74e2b93b | 62 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg); |
4ddf6f1a | 63 | |
74e2b93b JB |
64 | int y; |
65 | char x; | |
66 | bool input_ok = false; | |
67 | do { | |
68 | y = 0; | |
69 | x = ""; | |
70 | char* prompt_msg = "Prochain pion ? - ligne colonne (chiffre lettre):"; | |
71 | prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x); | |
72 | /* FIXME: separate the tests to permit to print explicit error messages */ | |
73 | if (((y > 0 && y < board_size + 1) || \ | |
74 | (map_col_letter_to_int(x) > 0 && map_col_letter_to_int(x) < board_size + 1)) \ | |
75 | && is_box_type(y, x, pawns, empty)) { | |
76 | input_ok = true; | |
77 | } | |
78 | } while (!input_ok); | |
79 | pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_int(x), player, pawns); | |
80 | ||
81 | round++; /* increment the round count */ | |
82 | ||
83 | refresh(); | |
84 | ||
85 | } while (!exit_condition); | |
86 | ||
4ddf6f1a JB |
87 | endwin(); |
88 | ||
89 | exit(EXIT_SUCCESS); | |
90 | } |