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