diff --git a/puissance4.c b/puissance4.c index a40a32be2546c649a48065ce3ad50fce895069ea..fb4712f5c7a07384c2ee3630af4258a33324ab5b 100644 --- a/puissance4.c +++ b/puissance4.c @@ -15,6 +15,8 @@ int** create_board(int rows, int cols); int destroy_board(int rows, int** board); bool is_valid_game_mode(int game_mode); bool is_valid_board_size(int rows, int cols); +bool is_column_full(int col, int rows, int** board); +int select_random_column(int number_of_columns, int number_of_rows, int** board); enum STATE { @@ -29,9 +31,9 @@ struct coordinate { }; enum GAME_MODE { - PLAYER_VS_PLAYER, - PLAYER_VS_SMART_AI, - PLAYER_VS_DUMMY_AI, + PLAYER_VS_RANDOM_AI = 1, + PLAYER_VS_SMART_AI = 2, + PLAYER_VS_PLAYER = 3 }; @@ -50,6 +52,7 @@ int main(int argc, char const* argv[]) { int game_mode = atoi(argv[1]); int rows = atoi(argv[2]); int cols = atoi(argv[3]); + srand(AI_SEED); int** board = create_board(rows, cols); if (board == NULL) { @@ -61,35 +64,40 @@ int main(int argc, char const* argv[]) { printf("Board size is %dx%d (rows x col)", rows, cols); print_board(rows, cols, board); - int col, player; + int player_selected_column, player; struct coordinate coordinate; while (true) { - printf("Column number? (starts at 1):"); + if (game_mode == PLAYER_VS_PLAYER || player % 2 == 0) { + printf("\nColumn number? (starts at 1):"); + scanf("%d", &player_selected_column); + } else { + player_selected_column = select_random_column(cols, rows, board); + } - scanf("%d", &col); - if (col < 1 || col > cols) { + if (player_selected_column < 1 || player_selected_column > cols) { continue; } - coordinate = add_token(col, player % 2, rows, board); - if (game_mode == 1) { - /* code */ - } - + coordinate = add_token(player_selected_column, player % 2, rows, board); + print_board(rows, cols, board); if (does_player_win(coordinate.x, coordinate.y, rows, cols, board)) { - printf("Player %s won!\n", player % 2 == 0 ? "one" : "two"); - print_board(rows, cols, board); + if (game_mode == PLAYER_VS_PLAYER) { + printf("\nPlayer %s won!\n", player % 2 == 0 ? "one" : "two"); + } else { + printf("\n%s won!\n", player % 2 == 0 ? "Player one" : "Computer"); + } + break; } - print_board(rows, cols, board); if (player + 1 == rows * cols) { - printf("It is a draw.\n"); + printf("\nIt is a draw.\n"); break; } player++; + } if (destroy_board(rows, board) == EXIT_FAILURE) { fprintf(stderr, "Error: Unable to free memory for the board.\n"); @@ -106,10 +114,22 @@ bool is_valid_board_size(int rows, int cols) { return rows >= MIN_ROW && cols >= MIN_COLUMN; } -int select_random_column(int number_of_columns) { - srand(AI_SEED); - int rand_num = rand() % number_of_columns; - return rand() % number_of_columns + 1; +int select_random_column(int number_of_columns, int number_of_rows, int** board) { + int robot_selected_column = -999; + do { + robot_selected_column = rand() % number_of_columns + 1; + } while (is_column_full(robot_selected_column, number_of_rows, board)); + + return robot_selected_column; +} + +bool is_column_full(int col, int rows, int** board) { + for (int i = 0; i < rows; i++) { + if (board[i][col - 1] == EMPTY) { + return false; + } + } + return true; } struct coordinate add_token(int col, int player, int rows, int** board) { @@ -273,5 +293,4 @@ void print_board(int rows, int cols, int** board) { for (int i = 1; i < cols + 1; i++) { printf(" %d", i); } - printf("\n"); }