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" |
54f1c58c | 8 | #include "debug.h" |
4ddf6f1a JB |
9 | |
10 | int 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 | 19 | char* score_msg = "Pions %s: %d"; |
cca9463e | 20 | char* invalid_move_msg = "Coup invalide"; |
45ce2fe3 JB |
21 | char* player_msg = "Joueur %d (%s) joue !"; |
22 | char* winner_msg = "Joueur %d (%s) gagne !"; | |
a8b54576 | 23 | char* draw_msg = "Egalite !"; |
cca9463e | 24 | char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; |
a80646b7 JB |
25 | |
26 | /* linked list of the history shots */ | |
27 | //struct shots_history_list_s shots_history; | |
28 | //INIT_LIST_HEAD(&shots_history.list); | |
29 | /* linked list of black playable shots */ | |
30 | struct shots_list_s black_playable_shots; | |
31 | INIT_LIST_HEAD(&black_playable_shots.list); | |
32 | /* linked list of white playable shots */ | |
33 | struct shots_list_s white_playable_shots; | |
34 | INIT_LIST_HEAD(&white_playable_shots.list); | |
35 | ||
36 | unsigned int pawns[board_size][board_size] = { | |
37 | {0, 0}, | |
38 | {0, 0} | |
39 | }; | |
40 | init_pawns(pawns); | |
4ddf6f1a JB |
41 | |
42 | initscr(); | |
43 | if (has_colors() == false) { | |
44 | endwin(); | |
45ce2fe3 | 45 | printf("Votre terminal ne supporte pas les couleurs.\n"); |
4ddf6f1a JB |
46 | exit(EXIT_FAILURE); |
47 | } | |
48 | start_color(); | |
49 | getmaxyx(stdscr, row, col); | |
45ce2fe3 JB |
50 | if (row < min_y || col < min_x) { |
51 | endwin(); | |
a80646b7 | 52 | printf("Votre terminal est trop petit pour afficher ce jeu.\n"); |
45ce2fe3 JB |
53 | printf("Merci d'agrandir la fenetre de votre terminal.\n"); |
54 | exit(EXIT_FAILURE); | |
55 | } | |
74e2b93b | 56 | echo(); |
4ddf6f1a JB |
57 | curs_set(0); |
58 | ||
74e2b93b JB |
59 | /* center */ |
60 | int center_y = row/2; | |
61 | int center_x = col/2; | |
62 | /* base coordinates to center the board */ | |
63 | int board_center_y = center_y - 26/2; | |
64 | int board_center_x = center_x - 42/2; | |
4ddf6f1a JB |
65 | |
66 | do { | |
74e2b93b JB |
67 | print_board(board_center_y, board_center_x); |
68 | print_pawns(board_center_y, board_center_x, pawns); | |
69 | ||
74e2b93b | 70 | mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg); |
4ddf6f1a | 71 | |
a8b54576 JB |
72 | player = current_player(round); |
73 | ||
45ce2fe3 JB |
74 | if (player == player_one) { |
75 | mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "noir")/2, player_msg, player, "noir"); | |
76 | } else { | |
77 | mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "blanc")/2, player_msg, player, "blanc"); | |
78 | } | |
a8b54576 | 79 | |
a80646b7 JB |
80 | nb_white = count_pawns_type(pawns, white); |
81 | nb_black = count_pawns_type(pawns, black); | |
74e2b93b | 82 | |
45ce2fe3 JB |
83 | mvprintw(center_y, center_x - 42/2 - snprintf(NULL, 0, score_msg, "noirs", nb_black) - 2, score_msg, "noirs", nb_black); |
84 | mvprintw(center_y, center_x + 42/2 + 2, score_msg, "blancs", nb_white); | |
a80646b7 | 85 | |
cca9463e | 86 | //display_array(1, 1, pawns); |
74e2b93b | 87 | |
74e2b93b | 88 | int y; |
a80646b7 | 89 | char x_char; |
74e2b93b | 90 | bool input_ok = false; |
cca9463e | 91 | unsigned int nb_pawns_reversed = 0; |
74e2b93b JB |
92 | do { |
93 | y = 0; | |
a80646b7 | 94 | x_char = ""; |
54f1c58c | 95 | char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):"; |
cca9463e | 96 | int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, (center_x - strlen(prompt_msg)/2), prompt_msg, &y, &x_char); |
a80646b7 | 97 | int x = map_col_letter_to_index(x_char); |
cca9463e JB |
98 | nb_pawns_reversed = valid_shot(y, x, player, pawns); |
99 | if (nb_pawns_reversed > 0 && prmt_rt == 1) { | |
74e2b93b | 100 | input_ok = true; |
cca9463e JB |
101 | clear(); |
102 | } else { | |
103 | mvprintw(center_y + 26/2 + 4, (center_x - strlen(invalid_move_msg)/2), invalid_move_msg); | |
74e2b93b JB |
104 | } |
105 | } while (!input_ok); | |
74e2b93b JB |
106 | |
107 | round++; /* increment the round count */ | |
108 | ||
a8b54576 | 109 | /* here are all the end of the game conditions */ |
a8b54576 | 110 | if (is_board_full(pawns)) { |
cca9463e JB |
111 | print_board(board_center_y, board_center_x); |
112 | /* print the updated pawns before exiting */ | |
113 | print_pawns(board_center_y, board_center_x, pawns); | |
a80646b7 | 114 | unsigned int winner = eval_winner(nb_white, nb_black); |
a8b54576 | 115 | if (winner != 0) { |
45ce2fe3 | 116 | if (winner == player_one) { |
cca9463e | 117 | mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir")/2, winner_msg, winner, "noir"); |
45ce2fe3 | 118 | } else { |
cca9463e | 119 | mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc")/2, winner_msg, winner, "blanc"); |
45ce2fe3 | 120 | } |
a8b54576 JB |
121 | } else { |
122 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); | |
123 | } | |
cca9463e | 124 | mvprintw(center_y + 26/2 + 1, (center_x - strlen(exit_msg)/2), exit_msg); |
54f1c58c | 125 | exit_condition = true; |
cca9463e JB |
126 | /* getch() is blocking */ |
127 | int key_exit = getch(); | |
128 | if (key_exit == 'r') { | |
129 | round = 0; | |
130 | player = player_one; | |
131 | nb_white = nb_black = 0; | |
132 | /* FIXME: do not seem properly reset the pawns 2D array */ | |
133 | init_pawns(pawns); | |
134 | exit_condition = false; | |
135 | clear(); | |
136 | } | |
54f1c58c | 137 | } |
a80646b7 JB |
138 | |
139 | refresh(); | |
54f1c58c | 140 | |
74e2b93b JB |
141 | } while (!exit_condition); |
142 | ||
4ddf6f1a JB |
143 | endwin(); |
144 | ||
145 | exit(EXIT_SUCCESS); | |
146 | } |