Compare commits
2 commits
0ba6fe5be7
...
ce62662dbb
Author | SHA1 | Date | |
---|---|---|---|
ce62662dbb | |||
01180f1e49 |
3 changed files with 136 additions and 36 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.out
|
BIN
a.out
BIN
a.out
Binary file not shown.
171
ttt.cc
171
ttt.cc
|
@ -1,26 +1,17 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <climits>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
const int ROWS = 3;
|
||||
const int COLUMNS = 3;
|
||||
|
||||
/* 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++) {
|
||||
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++) {
|
||||
cout << '-';
|
||||
}
|
||||
cout << endl;
|
||||
for (int x = 0; x < COLUMNS; x++) {
|
||||
for (int x = 0; x < dimension; x++) {
|
||||
if (x == 0) {
|
||||
cout << '|';
|
||||
}
|
||||
|
@ -34,8 +25,8 @@ void printGrid() {
|
|||
cout << '|';
|
||||
}
|
||||
cout << endl;
|
||||
if (y == COLUMNS - 1) {
|
||||
for (int x = 0; x < 2 * COLUMNS + 1; x++) {
|
||||
if (y == dimension - 1) {
|
||||
for (int x = 0; x < 2 * dimension + 1; x++) {
|
||||
cout << '-';
|
||||
}
|
||||
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) {
|
||||
// split string on comma
|
||||
|
@ -73,41 +162,51 @@ bool parse_move(string move, int &x, int &y) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void player_move() {
|
||||
void player_move(const int dimension, vector<vector<int>> &grid) {
|
||||
string move;
|
||||
int x;
|
||||
int y;
|
||||
while (true) {
|
||||
print_grid(dimension, grid);
|
||||
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;
|
||||
}
|
||||
if (x < 0 || y < 0 || x >= COLUMNS || y >= ROWS) {
|
||||
if (x < 0 || y < 0 || x >= dimension || y >= dimension) {
|
||||
cout << "Coordinates out of bounds." << endl;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
grid[x][y] = '1';
|
||||
grid[x][y] = 1;
|
||||
}
|
||||
|
||||
void computer_move() {
|
||||
}
|
||||
|
||||
bool game_over() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Play one game of tic tac toe,
|
||||
/* 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
|
||||
int 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;
|
||||
}
|
||||
|
||||
|
@ -117,10 +216,10 @@ int play_game() {
|
|||
}
|
||||
|
||||
while(true) {
|
||||
player_move();
|
||||
if (game_over()) break;
|
||||
player_move(dimension, grid);
|
||||
if (game_over(dimension, grid) != 0) break;
|
||||
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[]) {
|
||||
cout << "Hello world" << endl;
|
||||
for (int y = 0; y < ROWS; y++) {
|
||||
for (int x = 0; x < COLUMNS; x++) {
|
||||
grid[x][y] = 1;
|
||||
}
|
||||
}
|
||||
printGrid();
|
||||
player_move();
|
||||
// for (int y = 0; y < DIMENSION; y++) {
|
||||
// for (int x = 0; x < DIMENSION; x++) {
|
||||
// grid[x][y] = 1;
|
||||
// }
|
||||
// }
|
||||
|
||||
play_game(3);
|
||||
|
||||
}
|
Loading…
Reference in a new issue