From 5f055f644ccf17618b1e721fd3baeec5ccd13572 Mon Sep 17 00:00:00 2001 From: Leon Vatthauer Date: Thu, 28 Nov 2024 23:24:05 +0100 Subject: [PATCH] done implementing minmax, only needs some better printing now --- .vscode/settings.json | 3 +- ttt.cc | 73 +++++++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9d08b58..d7912ee 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "iostream": "cpp" + "iostream": "cpp", + "ostream": "cpp" } } \ No newline at end of file diff --git a/ttt.cc b/ttt.cc index 2afdd29..9e1a05c 100644 --- a/ttt.cc +++ b/ttt.cc @@ -5,6 +5,10 @@ using namespace std; +int game_over(const int dimension, vector> &grid); +int min(const int dimension, vector> &grid, int &x, int &y); +int max(const int dimension, vector> &grid, int &x, int &y); + void print_grid(const int dimension, vector> &grid) { for (int y = 0; y < dimension; y++) { for (int x = 0; x < 2 * dimension + 1; x++) { @@ -34,10 +38,6 @@ void print_grid(const int dimension, vector> &grid) { } } -int *minimax(const int dimension, vector> &grid) { - return 0; -} - int max(const int dimension, vector> &grid, int &x, int &y) { int n = game_over(dimension, grid); if (n == 2) return 0; @@ -45,18 +45,53 @@ int max(const int dimension, vector> &grid, int &x, int &y) { else if (n == -1) return -1; int value = INT_MIN; - for (int y = 0; y < dimension; y++) { - for (int x = 0; x < dimension; x++) { - if (grid[y][x] != 0) continue; - grid[y][x] = 1; - + for (int y_ = 0; y_ < dimension; y_++) { + for (int x_ = 0; x_ < dimension; x_++) { + if (grid[y_][x_] != 0) continue; + int old_x = x; + int old_y = y; + grid[y_][x_] = 1; + int next_val = min(dimension, grid, x, y); + if (next_val > value) { + value = next_val; + x = x_; + y = y_; + } else { + x = old_x; + y = old_y; + } + grid[y_][x_] = 0; } } + return value; } -void min(const int dimension, vector> &grid, int &x, int &y) { - int value = INT_MAX; +int min(const int dimension, vector> &grid, int &x, int &y) { + int n = game_over(dimension, grid); + if (n == 2) return 0; + else if (n == 1) return 1; + else if (n == -1) return -1; + int value = INT_MAX; + for (int y_ = 0; y_ < dimension; y_++) { + for (int x_ = 0; x_ < dimension; x_++) { + if (grid[y_][x_] != 0) continue; + int old_x = x; + int old_y = y; + grid[y_][x_] = -1; + int next_val = max(dimension, grid, x, y); + if (next_val < value) { + value = next_val; + x = x_; + y = y_; + } else { + x = old_x; + y = old_y; + } + grid[y_][x_] = 0; + } + } + return value; } /* @@ -89,10 +124,8 @@ int game_over(const int dimension, vector> &grid) { sum += grid[x][y]; } if (sum == dimension) { - cout << "player won" << endl; return 1; } else if (sum == -dimension) { - cout << "bot won" << endl; return -1; } sum = 0; @@ -104,10 +137,8 @@ int game_over(const int dimension, vector> &grid) { sum += grid[n][n]; } if (sum == dimension) { - cout << "player won" << endl; return 1; } else if (sum == -dimension) { - cout << "bot won" << endl; return -1; } @@ -117,10 +148,8 @@ int game_over(const int dimension, vector> &grid) { sum += grid[dimension - n][n - 1]; } if (sum == dimension) { - cout << "player won" << endl; return 1; } else if (sum == -dimension) { - cout << "bot won" << endl; return -1; } @@ -183,7 +212,11 @@ void player_move(const int dimension, vector> &grid) { grid[x][y] = 1; } -void computer_move() { +void computer_move(const int dimension, vector> &grid) { + int x; + int y; + min(dimension, grid, x, y); + grid[y][x] = -1; } @@ -212,13 +245,13 @@ int play_game(const int dimension) { // Do move according to startPlayer if (startPlayer == 1) { - computer_move(); + computer_move(dimension, grid); } while(true) { player_move(dimension, grid); if (game_over(dimension, grid) != 0) break; - computer_move(); + computer_move(dimension, grid); if (game_over(dimension, grid) != 0) break; }