| 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 | unsigned int pawns[board_size][board_size] = {{}}; |
| 25 | pawns[board_size][board_size] = init_pawns(pawns); |
| 26 | |
| 27 | initscr(); |
| 28 | if (has_colors() == false) { |
| 29 | endwin(); |
| 30 | printf("Votre terminal ne supporte pas les couleurs.\n"); |
| 31 | exit(EXIT_FAILURE); |
| 32 | } |
| 33 | start_color(); |
| 34 | getmaxyx(stdscr, row, col); |
| 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 | } |
| 41 | /* FIXME: fail if the screen size is too small */ |
| 42 | echo(); |
| 43 | curs_set(0); |
| 44 | |
| 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; |
| 51 | |
| 52 | do { |
| 53 | print_board(board_center_y, board_center_x); |
| 54 | print_pawns(board_center_y, board_center_x, pawns); |
| 55 | |
| 56 | mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg); |
| 57 | |
| 58 | player = current_player(round); |
| 59 | |
| 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 | } |
| 65 | |
| 66 | nb_white = count_pawn_type(pawns, white); |
| 67 | nb_black = count_pawn_type(pawns, black); |
| 68 | |
| 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); |
| 71 | |
| 72 | int y; |
| 73 | char x; |
| 74 | bool input_ok = false; |
| 75 | do { |
| 76 | y = 0; |
| 77 | x = (char)""; |
| 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); |
| 80 | if (is_valid_input(y, map_col_letter_to_index(x), pawns) && prmt_rt == 1) { |
| 81 | input_ok = true; |
| 82 | } |
| 83 | } while (!input_ok); |
| 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); |
| 90 | |
| 91 | round++; /* increment the round count */ |
| 92 | |
| 93 | refresh(); |
| 94 | |
| 95 | /* here are all the end of the game conditions */ |
| 96 | //if (is_board_full(pawns) || (round == 60)) { |
| 97 | if (is_board_full(pawns)) { |
| 98 | int winner = eval_winner(nb_white, nb_black); |
| 99 | if (winner != 0) { |
| 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 | } |
| 105 | } else { |
| 106 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); |
| 107 | } |
| 108 | /* print and implement restart possibility */ |
| 109 | exit_condition = true; |
| 110 | } |
| 111 | |
| 112 | } while (!exit_condition); |
| 113 | |
| 114 | endwin(); |
| 115 | |
| 116 | exit(EXIT_SUCCESS); |
| 117 | } |