| 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 | |
| 9 | int main() { |
| 10 | int row = 0, col = 0; |
| 11 | unsigned int round = 0; |
| 12 | unsigned int player = player_one; /* first player is black */ |
| 13 | bool exit_condition = false; |
| 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); |
| 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); |
| 29 | //noecho(); |
| 30 | echo(); |
| 31 | curs_set(0); |
| 32 | |
| 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; |
| 39 | |
| 40 | do { |
| 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); |
| 46 | |
| 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 !"; |
| 61 | } |
| 62 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg); |
| 63 | |
| 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 | |
| 87 | endwin(); |
| 88 | |
| 89 | exit(EXIT_SUCCESS); |
| 90 | } |