done implementing minmax, only needs some better printing now

This commit is contained in:
Leon Vatthauer 2024-11-28 23:24:05 +01:00
parent ce62662dbb
commit 5f055f644c
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
2 changed files with 55 additions and 21 deletions

View file

@ -1,5 +1,6 @@
{
"files.associations": {
"iostream": "cpp"
"iostream": "cpp",
"ostream": "cpp"
}
}

73
ttt.cc
View file

@ -5,6 +5,10 @@
using namespace std;
int game_over(const int dimension, vector<vector<int>> &grid);
int min(const int dimension, vector<vector<int>> &grid, int &x, int &y);
int max(const int dimension, vector<vector<int>> &grid, int &x, int &y);
void print_grid(const int dimension, vector<vector<int>> &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<vector<int>> &grid) {
}
}
int *minimax(const int dimension, vector<vector<int>> &grid) {
return 0;
}
int max(const int dimension, vector<vector<int>> &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<vector<int>> &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<vector<int>> &grid, int &x, int &y) {
int value = INT_MAX;
int min(const int dimension, vector<vector<int>> &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<vector<int>> &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<vector<int>> &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<vector<int>> &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<vector<int>> &grid) {
grid[x][y] = 1;
}
void computer_move() {
void computer_move(const int dimension, vector<vector<int>> &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;
}