Compare commits

..

2 commits

Author SHA1 Message Date
ce62662dbb
progress 2024-11-28 23:07:53 +01:00
01180f1e49
.gitignore 2024-11-28 21:42:39 +01:00
3 changed files with 136 additions and 36 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.out

BIN
a.out

Binary file not shown.

171
ttt.cc
View file

@ -1,26 +1,17 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector>
#include <climits>
using namespace std; using namespace std;
void print_grid(const int dimension, vector<vector<int>> &grid) {
const int ROWS = 3; for (int y = 0; y < dimension; y++) {
const int COLUMNS = 3; for (int x = 0; x < 2 * dimension + 1; x++) {
/* Grid representing the tic-tac-toe field.
0 is empty,
1 is human,
-1 is computer.
*/
int grid[COLUMNS][ROWS];
void printGrid() {
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < 2 * COLUMNS + 1; x++) {
cout << '-'; cout << '-';
} }
cout << endl; cout << endl;
for (int x = 0; x < COLUMNS; x++) { for (int x = 0; x < dimension; x++) {
if (x == 0) { if (x == 0) {
cout << '|'; cout << '|';
} }
@ -34,8 +25,8 @@ void printGrid() {
cout << '|'; cout << '|';
} }
cout << endl; cout << endl;
if (y == COLUMNS - 1) { if (y == dimension - 1) {
for (int x = 0; x < 2 * COLUMNS + 1; x++) { for (int x = 0; x < 2 * dimension + 1; x++) {
cout << '-'; cout << '-';
} }
cout << endl; cout << endl;
@ -43,6 +34,104 @@ void printGrid() {
} }
} }
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;
}
bool parse_move(string move, int &x, int &y) { bool parse_move(string move, int &x, int &y) {
// split string on comma // split string on comma
@ -73,41 +162,51 @@ bool parse_move(string move, int &x, int &y) {
return false; return false;
} }
void player_move() { void player_move(const int dimension, vector<vector<int>> &grid) {
string move; string move;
int x; int x;
int y; int y;
while (true) { while (true) {
print_grid(dimension, grid);
cout << "Enter a valid move. (Format is 'x,y')" << endl; cout << "Enter a valid move. (Format is 'x,y')" << endl;
cin >> move; cin >> move;
if (!parse_move(move, x, y)) { if (!parse_move(move, x, y)) {
cout << "Couldn't parse move." << endl; cout << "Couldn't parse move." << endl;
continue; continue;
} }
if (x < 0 || y < 0 || x >= COLUMNS || y >= ROWS) { if (x < 0 || y < 0 || x >= dimension || y >= dimension) {
cout << "Coordinates out of bounds." << endl; cout << "Coordinates out of bounds." << endl;
continue; continue;
} }
break; break;
} }
grid[x][y] = '1'; grid[x][y] = 1;
} }
void computer_move() { void computer_move() {
} }
bool game_over() {
return false;
}
/* Play one game of tic tac toe, /* Play one game of tic tac toe,
/* returns 0 if the player won and 1 if the computer won. /* returns 0 if the player won and 1 if the computer won.
*/ */
int play_game() { 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);
}
}
// Query for starting player // Query for starting player
int startPlayer = -1; int startPlayer = -1;
while (startPlayer != 0 && startPlayer != 1) { while (startPlayer != 0 && startPlayer != 1) {
cout << "Which player should start? (0 - You, 1 - Program)"; cout << "Which player should start? (0 - You, 1 - Program)" << endl;
cin >> startPlayer; cin >> startPlayer;
} }
@ -117,10 +216,10 @@ int play_game() {
} }
while(true) { while(true) {
player_move(); player_move(dimension, grid);
if (game_over()) break; if (game_over(dimension, grid) != 0) break;
computer_move(); computer_move();
if (game_over()) break; if (game_over(dimension, grid) != 0) break;
} }
@ -128,12 +227,12 @@ int play_game() {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
cout << "Hello world" << endl; // for (int y = 0; y < DIMENSION; y++) {
for (int y = 0; y < ROWS; y++) { // for (int x = 0; x < DIMENSION; x++) {
for (int x = 0; x < COLUMNS; x++) { // grid[x][y] = 1;
grid[x][y] = 1; // }
} // }
}
printGrid(); play_game(3);
player_move();
} }