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