diff --git a/main.c b/main.c index 13e3d3e3861622001ddcf7a62129e6070f94d2a2..87eef38c84fec09802a5cb80fd8e2530f4c0c9c0 100644 --- a/main.c +++ b/main.c @@ -5,7 +5,6 @@ #include <stdio.h> #include <stdlib.h> -#include <time.h> #include "puissance.h" void play_against_random_ai(puissance game) { @@ -13,7 +12,7 @@ void play_against_random_ai(puissance game) { while(game.state == ONGOING) { if (game.current_player == PLAYER_ONE) { - printf("Waiting for player one (" PLAYER_ONE_STRING ")...\n"); + printf("\nColumn number? (starts at 1):"); do { scanf("%d", &selected_col_index); selected_col_index -= 1; @@ -29,14 +28,8 @@ void play_against_human(puissance game) { int selected_col_index = -1; while(game.state == ONGOING) { - if (game.current_player == PLAYER_ONE) { - printf("Waiting for player one (" PLAYER_ONE_STRING ")...\n"); - - } else { - printf("Waiting for player two (" PLAYER_TWO_STRING ")...\n"); - } - do { + printf("\nColumn number? (starts at 1):"); scanf("%d", &selected_col_index); selected_col_index -= 1; } while (manual_play(&game, selected_col_index) == false); @@ -45,7 +38,7 @@ void play_against_human(puissance game) { } int main(int argc, char *argv[]) { - srand(time(NULL)); + srand(0); puissance game; if (argc != 4) { @@ -58,6 +51,7 @@ int main(int argc, char *argv[]) { int nb_cols = atoi(argv[3]); game_init(&game, mode, nb_rows, nb_cols); + printf("Board size is %dx%d (rows x col)", nb_rows, nb_cols); print_game(game); switch (mode) { diff --git a/puissance.c b/puissance.c index cff3d57ee334893ef97c5b4ea683251d4eb4a2dc..e803c3ffeb93bedb439a104711d3424764b6b288 100644 --- a/puissance.c +++ b/puissance.c @@ -59,7 +59,6 @@ void print_bot(char *display, int col) { strcat(display, " "); strcat(display, i_has_string); } - strcat(display, "\n"); } void print_row_separator(char *display, int col) { @@ -76,6 +75,8 @@ void print_row_separator(char *display, int col) { void print_game(puissance p) { char display[1024]; + // Ensure that the display string is empty before doing anything + display[0] = '\0'; // Print the top of the game print_top(display, p.col); @@ -114,7 +115,7 @@ void print_game(puissance p) { display[0] = '\0'; if (p.state != ONGOING) { - display_game_result(p.state); + display_game_result(p); } } @@ -194,6 +195,20 @@ bool diagonal_parse(puissance *p, int x_r, int y_c, int starting_row, int starti bool four_aligned = true; int last_played_value = p->data[starting_row][starting_col]; + // Search the end position of the diagonal + do { + int x = -x_r + starting_row; + int y = -y_c + starting_col; + + if (x > 0 && x < p->row && y > 0 && y < p->col && p->data[x][y] == last_played_value) { + starting_row = x; + starting_col = y; + } else { + break; + } + } while(true); + + // Verify if we have the same value aligned enough times for (int i = 1; i <= NB_VERIFICATION_FOR_WIN; i++) { int r = x_r * i + starting_row; int c = y_c * i + starting_col; @@ -216,7 +231,6 @@ GameResult diagonal_game_check(puissance *p, int last_col_index_played) { return p->state; } - bool four_aligned = false; // Get the row index int last_row_index_played = get_available_row_index(p, last_col_index_played) + 1; @@ -240,6 +254,23 @@ GameResult diagonal_game_check(puissance *p, int last_col_index_played) { return ONGOING; } +GameResult verify_space_remaining(puissance *p) { + if (p->state != ONGOING) { + return p->state; + } + bool space_available = false; + for (int i = 0; i < p->col; i++) { + if (p->data[0][i] == EMPTY_CELL_VALUE) { + space_available = true; + break; + } + } + if (space_available == false) { + p->state = DRAW; + } + return p->state; +} + GameResult verify_game(puissance *p, int last_col_index_played) { // Vertical check vertical_game_check(p, last_col_index_played); @@ -247,22 +278,28 @@ GameResult verify_game(puissance *p, int last_col_index_played) { horizontal_game_check(p, last_col_index_played); // Diagonal check diagonal_game_check(p, last_col_index_played); + // Verify remaining space + verify_space_remaining(p); return p->state; } -GameResult display_game_result(GameResult gr) { - if (gr == PLAYER_ONE_WIN) { - printf("The player one (" PLAYER_ONE_STRING ") win !\n"); +GameResult display_game_result(puissance p) { + if (p.state == PLAYER_ONE_WIN) { + printf("\nPlayer one won!\n"); } - if (gr == PLAYER_TWO_WIN) { - printf("The player two (" PLAYER_TWO_STRING ") win !\n"); + if (p.state == PLAYER_TWO_WIN) { + if (p.mode == TWO_PLAYERS) { + printf("\nPlayer two won!\n"); + } else { + printf("\nComputer won!\n"); + } } - if (gr == DRAW) { - printf("It's a draw !\n"); + if (p.state == DRAW) { + printf("\nIt is a draw.\n"); } - return gr; + return p.state; } int get_available_row_index(puissance *p, int selected_col_index) { diff --git a/puissance.h b/puissance.h index a70f220882cbee3795f622ba4d3a025cea84f3aa..7baf727918cec34d47d7322c422b61b2230d576c 100644 --- a/puissance.h +++ b/puissance.h @@ -49,7 +49,7 @@ typedef struct _puissance { void game_init(puissance *p, GameMode mode, int row, int col); void print_game(puissance p); GameResult verify_game(puissance *p, int last_col_index_played); -GameResult display_game_result(GameResult gr); +GameResult display_game_result(puissance p); int get_available_row_index(puissance *p, int selected_col_index); bool manual_play(puissance *p, int selected_col_index); bool random_play(puissance *p);