X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fmain.c;h=ce7c8a6b98a13d1f0636c8f0130fb34a701fd9ba;hb=cca9463e74e618b99f79d6b2c29b9b3f4f5a1511;hp=195bdbe1ef51644bf83d737bb6d42c148cadcfa3;hpb=74e2b93b658575fa792ada51c3bf1cdc3cfde247;p=Project_algorithmic_C.git diff --git a/src/main.c b/src/main.c index 195bdbe..ce7c8a6 100644 --- a/src/main.c +++ b/src/main.c @@ -5,28 +5,54 @@ #include "ui.h" #include "othello.h" +#include "debug.h" int main() { + int min_y = 26 + 6, min_x = 42 + 14 + 15 + 4; int row = 0, col = 0; unsigned int round = 0; unsigned int player = player_one; /* first player is black */ bool exit_condition = false; unsigned int nb_white = 0, nb_black = 0; - char* player_msg; + char* title_msg = "Jeu Othello"; + char* score_msg = "Pions %s: %d"; + char* invalid_move_msg = "Coup invalide"; + char* player_msg = "Joueur %d (%s) joue !"; + char* winner_msg = "Joueur %d (%s) gagne !"; + char* draw_msg = "Egalite !"; + char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; + + /* linked list of the history shots */ + //struct shots_history_list_s shots_history; + //INIT_LIST_HEAD(&shots_history.list); + /* linked list of black playable shots */ + struct shots_list_s black_playable_shots; + INIT_LIST_HEAD(&black_playable_shots.list); + /* linked list of white playable shots */ + struct shots_list_s white_playable_shots; + INIT_LIST_HEAD(&white_playable_shots.list); - unsigned int pawns[board_size][board_size] = {{}}; - pawns[board_size][board_size] = init_pawns(pawns); + unsigned int pawns[board_size][board_size] = { + {0, 0}, + {0, 0} + }; + init_pawns(pawns); initscr(); if (has_colors() == false) { endwin(); - printf("Votre terminal ne supporte pas les couleurs\n"); + printf("Votre terminal ne supporte pas les couleurs.\n"); exit(EXIT_FAILURE); } start_color(); getmaxyx(stdscr, row, col); - //noecho(); + if (row < min_y || col < min_x) { + endwin(); + printf("Votre terminal est trop petit pour afficher ce jeu.\n"); + printf("Merci d'agrandir la fenetre de votre terminal.\n"); + exit(EXIT_FAILURE); + } echo(); curs_set(0); @@ -41,45 +67,75 @@ int main() { print_board(board_center_y, board_center_x); print_pawns(board_center_y, board_center_x, pawns); - char* title_msg = "Jeu othello"; mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg); - nb_white = count_pawn_type(pawns, white); - nb_black = count_pawn_type(pawns, black); - - char* score_white_msg = "Pions blancs: %d"; - mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white); - char* score_black_msg = "Pions noirs: %d"; - mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black); - player = current_player(round); - + if (player == player_one) { - player_msg = "Joueur un (noir) joue !"; + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "noir")/2, player_msg, player, "noir"); } else { - player_msg = "Joueur deux (blanc) joue !"; + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "blanc")/2, player_msg, player, "blanc"); } - mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg); + + nb_white = count_pawns_type(pawns, white); + nb_black = count_pawns_type(pawns, black); + + mvprintw(center_y, center_x - 42/2 - snprintf(NULL, 0, score_msg, "noirs", nb_black) - 2, score_msg, "noirs", nb_black); + mvprintw(center_y, center_x + 42/2 + 2, score_msg, "blancs", nb_white); + + //display_array(1, 1, pawns); int y; - char x; + char x_char; bool input_ok = false; + unsigned int nb_pawns_reversed = 0; do { y = 0; - x = ""; - char* prompt_msg = "Prochain pion ? - ligne colonne (chiffre lettre):"; - prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x); - /* FIXME: separate the tests to permit to print explicit error messages */ - if (((y > 0 && y < board_size + 1) || \ - (map_col_letter_to_int(x) > 0 && map_col_letter_to_int(x) < board_size + 1)) \ - && is_box_type(y, x, pawns, empty)) { + x_char = ""; + char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):"; + int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, (center_x - strlen(prompt_msg)/2), prompt_msg, &y, &x_char); + int x = map_col_letter_to_index(x_char); + nb_pawns_reversed = valid_shot(y, x, player, pawns); + if (nb_pawns_reversed > 0 && prmt_rt == 1) { input_ok = true; + clear(); + } else { + mvprintw(center_y + 26/2 + 4, (center_x - strlen(invalid_move_msg)/2), invalid_move_msg); } } while (!input_ok); - pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_int(x), player, pawns); round++; /* increment the round count */ + /* here are all the end of the game conditions */ + if (is_board_full(pawns)) { + print_board(board_center_y, board_center_x); + /* print the updated pawns before exiting */ + print_pawns(board_center_y, board_center_x, pawns); + unsigned int winner = eval_winner(nb_white, nb_black); + if (winner != 0) { + if (winner == player_one) { + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir")/2, winner_msg, winner, "noir"); + } else { + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc")/2, winner_msg, winner, "blanc"); + } + } else { + mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); + } + mvprintw(center_y + 26/2 + 1, (center_x - strlen(exit_msg)/2), exit_msg); + exit_condition = true; + /* getch() is blocking */ + int key_exit = getch(); + if (key_exit == 'r') { + round = 0; + player = player_one; + nb_white = nb_black = 0; + /* FIXME: do not seem properly reset the pawns 2D array */ + init_pawns(pawns); + exit_condition = false; + clear(); + } + } + refresh(); } while (!exit_condition);