| 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, min_x = 42; |
| 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 | LIST_HEAD(shots_list); |
| 19 | |
| 20 | char* title_msg = "Jeu Othello"; |
| 21 | char* score_white_msg = "Pions blancs: %d"; |
| 22 | char* score_black_msg = "Pions noirs: %d"; |
| 23 | char* player_msg = "Joueur %d joue !"; |
| 24 | char* winner_msg = "Joueur %d gagne !"; |
| 25 | char* draw_msg = "Egalite !"; |
| 26 | |
| 27 | unsigned int pawns[board_size][board_size] = {{}}; |
| 28 | pawns[board_size][board_size] = init_pawns(pawns); |
| 29 | |
| 30 | initscr(); |
| 31 | if (has_colors() == false) { |
| 32 | endwin(); |
| 33 | printf("Votre terminal ne supporte pas les couleurs\n"); |
| 34 | exit(EXIT_FAILURE); |
| 35 | } |
| 36 | start_color(); |
| 37 | getmaxyx(stdscr, row, col); |
| 38 | /* FIXME: fail if the screen size is too small */ |
| 39 | echo(); |
| 40 | curs_set(0); |
| 41 | |
| 42 | /* center */ |
| 43 | int center_y = row/2; |
| 44 | int center_x = col/2; |
| 45 | /* base coordinates to center the board */ |
| 46 | int board_center_y = center_y - 26/2; |
| 47 | int board_center_x = center_x - 42/2; |
| 48 | |
| 49 | do { |
| 50 | print_board(board_center_y, board_center_x); |
| 51 | print_pawns(board_center_y, board_center_x, pawns); |
| 52 | |
| 53 | mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg); |
| 54 | |
| 55 | player = current_player(round); |
| 56 | |
| 57 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg, player); |
| 58 | |
| 59 | nb_white = count_pawn_type(pawns, white); |
| 60 | nb_black = count_pawn_type(pawns, black); |
| 61 | |
| 62 | mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white); |
| 63 | mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black); |
| 64 | |
| 65 | int y; |
| 66 | char x; |
| 67 | bool input_ok = false; |
| 68 | do { |
| 69 | y = 0; |
| 70 | x = ""; |
| 71 | char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):"; |
| 72 | int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x); |
| 73 | if (is_valid_input(y, map_col_letter_to_int(x), pawns) && prmt_rt == 1) { |
| 74 | input_ok = true; |
| 75 | } |
| 76 | } while (!input_ok); |
| 77 | pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_int(x), player, pawns); |
| 78 | struct shots_list shot_current; |
| 79 | shot_current.pawn_array_member = &pawns[y-1][x-1]; |
| 80 | //list_add(shot_current.list, shots_list.list); |
| 81 | |
| 82 | round++; /* increment the round count */ |
| 83 | |
| 84 | refresh(); |
| 85 | |
| 86 | /* here are all the end of the game conditions */ |
| 87 | //if (is_board_full(pawns) || round == 60) { |
| 88 | if (is_board_full(pawns)) { |
| 89 | int winner = eval_winner(nb_white, nb_black); |
| 90 | if (winner != 0) { |
| 91 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(winner_msg)/2), winner_msg, winner); |
| 92 | } else { |
| 93 | mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); |
| 94 | } |
| 95 | /* print and implement restart possibility */ |
| 96 | exit_condition = true; |
| 97 | } |
| 98 | |
| 99 | } while (!exit_condition); |
| 100 | |
| 101 | endwin(); |
| 102 | |
| 103 | exit(EXIT_SUCCESS); |
| 104 | } |