tic-tac-toe-cpp/ttt.cc

238 lines
4.8 KiB
C++
Raw Normal View History

2024-11-27 17:10:19 +01:00
#include <iostream>
#include <string>
2024-11-28 23:07:53 +01:00
#include <vector>
#include <climits>
2024-11-27 17:10:19 +01:00
using namespace std;
2024-11-28 23:07:53 +01:00
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++) {
2024-11-27 17:10:19 +01:00
cout << '-';
}
cout << endl;
2024-11-28 23:07:53 +01:00
for (int x = 0; x < dimension; x++) {
2024-11-27 17:10:19 +01:00
if (x == 0) {
cout << '|';
}
if (grid[x][y] > 0) {
cout << 'X';
} else if (grid [x][y] < 0) {
cout << 'O';
} else {
cout << ' ';
}
cout << '|';
}
cout << endl;
2024-11-28 23:07:53 +01:00
if (y == dimension - 1) {
for (int x = 0; x < 2 * dimension + 1; x++) {
2024-11-27 17:10:19 +01:00
cout << '-';
}
cout << endl;
}
}
}
2024-11-28 23:07:53 +01:00
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;
else if (n == 1) return 1;
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;
}
}
}
void min(const int dimension, vector<vector<int>> &grid, int &x, int &y) {
int value = INT_MAX;
}
/*
Check if game is over.
returns:
-1 if bot won
0 if game is not over
1 if player won
2 if tie
*/
int game_over(const int dimension, vector<vector<int>> &grid) {
// check rows
for (int y = 0; y < dimension; y++) {
int sum = 0;
for (int x = 0; x < dimension; x++) {
sum += grid[x][y];
}
if (sum == dimension) {
return 1;
} else if (sum == -dimension) {
return -1;
}
sum = 0;
}
// check columns
for (int x = 0; x < dimension; x++) {
int sum = 0;
for (int y = 0; y < dimension; y++) {
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;
}
// check first diagonal
int sum = 0;
for (int n = 0; n < dimension; n++) {
sum += grid[n][n];
}
if (sum == dimension) {
cout << "player won" << endl;
return 1;
} else if (sum == -dimension) {
cout << "bot won" << endl;
return -1;
}
// check second diagonal
sum = 0;
for (int n = 1; n <= dimension; n++) {
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;
}
// check if grid is full
for (int y = 0; y < dimension; y++) {
for (int x = 0; x < dimension; x++) {
if (grid[x][y] == 0) return 0;
}
}
return 2;
}
2024-11-27 17:10:19 +01:00
bool parse_move(string move, int &x, int &y) {
// split string on comma
string x_str;
string y_str;
int pos = move.find(',');
// error if no comma found
if (pos == string::npos) {
return false;
}
// split on ','
x_str = move.substr(0, pos);
y_str = move.substr(pos + 1, move.length() - 1 - pos);
// convert to int
int x_local = stoi(x_str);
int y_local = stoi(y_str);
// check conversion and return expected result
if (x_local != -1 && y_local != -1) {
x = x_local;
y = y_local;
return true;
}
return false;
}
2024-11-28 23:07:53 +01:00
void player_move(const int dimension, vector<vector<int>> &grid) {
2024-11-27 17:10:19 +01:00
string move;
int x;
int y;
while (true) {
2024-11-28 23:07:53 +01:00
print_grid(dimension, grid);
2024-11-27 17:10:19 +01:00
cout << "Enter a valid move. (Format is 'x,y')" << endl;
cin >> move;
if (!parse_move(move, x, y)) {
cout << "Couldn't parse move." << endl;
continue;
}
2024-11-28 23:07:53 +01:00
if (x < 0 || y < 0 || x >= dimension || y >= dimension) {
2024-11-27 17:10:19 +01:00
cout << "Coordinates out of bounds." << endl;
continue;
}
break;
}
2024-11-28 23:07:53 +01:00
grid[x][y] = 1;
2024-11-27 17:10:19 +01:00
}
void computer_move() {
}
/* Play one game of tic tac toe,
/* returns 0 if the player won and 1 if the computer won.
*/
2024-11-28 23:07:53 +01:00
int play_game(const int dimension) {
/* Grid representing the tic-tac-toe field.
0 is empty,
1 is human,
-1 is computer.
*/
vector<vector<int>> grid;
for (int y = 0; y < dimension; y++) {
grid.push_back({});
for (int x = 0; x < dimension; x++) {
grid[y].push_back(0);
}
}
2024-11-27 17:10:19 +01:00
// Query for starting player
int startPlayer = -1;
while (startPlayer != 0 && startPlayer != 1) {
2024-11-28 23:07:53 +01:00
cout << "Which player should start? (0 - You, 1 - Program)" << endl;
2024-11-27 17:10:19 +01:00
cin >> startPlayer;
}
// Do move according to startPlayer
if (startPlayer == 1) {
computer_move();
}
while(true) {
2024-11-28 23:07:53 +01:00
player_move(dimension, grid);
if (game_over(dimension, grid) != 0) break;
2024-11-27 17:10:19 +01:00
computer_move();
2024-11-28 23:07:53 +01:00
if (game_over(dimension, grid) != 0) break;
2024-11-27 17:10:19 +01:00
}
return 0;
}
int main(int argc, char *argv[]) {
2024-11-28 23:07:53 +01:00
// for (int y = 0; y < DIMENSION; y++) {
// for (int x = 0; x < DIMENSION; x++) {
// grid[x][y] = 1;
// }
// }
play_game(3);
2024-11-27 17:10:19 +01:00
}