Skip to content
Snippets Groups Projects
Commit 28015cae authored by dario.genga's avatar dario.genga
Browse files

Add smart ai play

The program valid each tests, but it need some refactoring to improve
the code quality.
parent 74a4db72
Branches
No related tags found
No related merge requests found
......@@ -24,6 +24,23 @@ void play_against_random_ai(puissance game) {
}
}
void play_against_smart_ai(puissance game) {
int selected_col_index = -1;
while(game.state == ONGOING) {
if (game.current_player == PLAYER_ONE) {
printf("\nColumn number? (starts at 1):");
do {
scanf("%d", &selected_col_index);
selected_col_index -= 1;
} while (manual_play(&game, selected_col_index) == false);
} else {
smart_play(&game);
}
print_game(game);
}
}
void play_against_human(puissance game) {
int selected_col_index = -1;
......@@ -57,7 +74,9 @@ int main(int argc, char *argv[]) {
switch (mode) {
case RAND_AI:
play_against_random_ai(game);
break;
case SMART_AI:
play_against_smart_ai(game);
break;
case TWO_PLAYERS:
play_against_human(game);
......
......@@ -27,6 +27,21 @@ void game_init(puissance *p, GameMode mode, int row, int col) {
}
}
puissance game_copy(puissance *p) {
puissance copy;
game_init(&copy, p->mode, p->row, p->col);
copy.state = p->state;
copy.current_player = p->current_player;
for (int r = 0; r < copy.row; r++) {
for (int c = 0; c < copy.col; c++) {
copy.data[r][c] = p->data[r][c];
}
}
return copy;
}
void print_top(char *display, int col) {
// Print top
strcat(display, "\n┌");
......@@ -354,9 +369,39 @@ bool random_play(puissance *p) {
}
bool smart_play(puissance *p) {
bool valid_action = true;
bool move_validated = false;
// Search for a wining action
for (int i = 0; i < p->col; i++) {
puissance copy = game_copy(p);
return valid_action;
if (manual_play(&copy, i)) {
if (copy.state != ONGOING) {
move_validated = manual_play(p, i);
}
}
game_destroy(&copy);
if (move_validated) {
return move_validated;
}
}
// Try to prevent the user to win
for (int i = 0; i < p->col; i++) {
puissance copy = game_copy(p);
copy.current_player = PLAYER_ONE;
if (manual_play(&copy, i)) {
if (copy.state != ONGOING) {
move_validated = manual_play(p, i);
}
}
game_destroy(&copy);
if (move_validated) {
return move_validated;
}
}
// If nothing has been done, do something random
return random_play(p);
}
void game_destroy(puissance *p) {
......
......@@ -47,6 +47,7 @@ typedef struct _puissance {
} puissance;
void game_init(puissance *p, GameMode mode, int row, int col);
puissance game_copy(puissance *p);
void print_game(puissance p);
GameResult verify_game(puissance *p, int last_col_index_played);
GameResult display_game_result(puissance p);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment