Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************
- *
- * board.cpp
- * Board Class
- *
- *
- ***************************************************/
- #include "board.h"
- #include <iostream>
- #include <vector>
- #include <climits>
- using namespace std;
- board::board()
- {
- for (int i = 0; i < 3; ++i)
- for (int j = 0; j < 3; ++j)
- gameBoard[i][j] = '*';
- computerTurn = false; //humans go first
- firstMoveMade = false;
- }
- board::board(const board & other)
- {
- for (int i = 0; i < 3; ++i)
- for (int j = 0; j < 3; ++j)
- gameBoard[i][j] = other.gameBoard[i][j];
- computerTurn = other.computerTurn;
- firstMoveMade = other.firstMoveMade;
- }
- board & board::operator=(const board & rhs)
- {
- if (this == &rhs) return *this;
- for(int i = 0; i < 10; ++i)
- for (int j = 0; j < 10; ++j)
- gameBoard[i][j] = rhs.gameBoard[i][j];
- computerTurn = rhs.computerTurn;
- firstMoveMade = rhs.firstMoveMade;
- return *this;
- }
- char board::getPosition(int x, int y)
- {
- if (x < 3 && x >= 0 && y < 3 && y >= 0) // boundary check
- return gameBoard[x][y];
- else return ' ';
- }
- void board::setPosition(int x, int y)
- {
- if (x < 3 && x >= 0 && y < 3 && y >= 0) // boundary check
- {
- if (!computerTurn)
- gameBoard[x][y] = 'X';
- else gameBoard[x][y] = 'O';
- }
- if (!firstMoveMade) firstMoveMade = true; // first move has been made
- computerTurn = !computerTurn; // someone just made a move
- }
- void board::makeComputerMove()
- {
- drawboard();
- getBestMove();
- drawboard();
- }
- void board::getBestMove()
- {
- board returnBoard;
- miniMax(INT_MIN + 1, INT_MAX -1, returnBoard);
- *this = returnBoard;
- }
- int board::miniMax(int alpha, int beta, board childWithMaximum)
- {
- if (checkDone())
- return boardScore();
- vector<board> children = getChildren();
- for (int i = 0; i < 9; ++i)
- {
- if(children.empty()) break;
- board curr = children.back();
- if (curr.firstMoveMade) // not an empty board
- {
- board dummyBoard;
- int score = curr.miniMax(alpha, beta, dummyBoard);
- if (computerTurn && (beta > score))
- {
- beta = score;
- childWithMaximum = *this;
- if (alpha >= beta) break;
- }
- else if (alpha < score)
- {
- alpha = score;
- childWithMaximum = *this;
- if (alpha >= beta) break;
- }
- }
- }
- return computerTurn? alpha : beta;
- }
- vector<board> board::getChildren()
- {
- vector<board> children;
- for (int i = 0; i < 3; ++i)
- { for (int j = 0; j < 3; ++j)
- {
- if (getPosition(i, j) == '*') //move not made here
- {
- board moveMade(*this);
- moveMade.setPosition(i, j);
- children.push_back(moveMade);
- }
- }
- }
- return children;
- }
- int board::boardScore()
- {
- return rowScore(gameBoard[0][0], gameBoard[0][1], gameBoard[0][2]) + rowScore(gameBoard[1][0], gameBoard[1][1], gameBoard[1][2])
- + rowScore(gameBoard[2][0], gameBoard[2][1], gameBoard[2][2]) + rowScore(gameBoard[0][0], gameBoard[1][0], gameBoard[2][0])
- + rowScore(gameBoard[0][1], gameBoard[1][1], gameBoard[2][1])+ rowScore(gameBoard[0][2], gameBoard[1][2], gameBoard[2][2])
- + rowScore(gameBoard[0][0], gameBoard[1][1], gameBoard[2][2])+ rowScore(gameBoard[0][2], gameBoard[1][1], gameBoard[2][0]);
- }
- int board::rowScore(char one, char two, char three)
- {
- char row[3] = {one, two, three};
- int numX = 0;
- int numO = 0;
- for (int i = 0; i < 3; ++i)
- {
- if (row[i] == 'X') numX++;
- if (row[i] == 'O') numO++;
- }
- if (numX == numO) return 0;
- else if (numO == 3) return 100; // three in a row
- else if (numO == 2) return 10; // two in a row
- else if (numO == 1) return 1; // one in a row
- else if (numX == 3) return -100;
- else if (numX == 2) return -10;
- else if (numX == 1) return -1;
- else return 0;
- }
- void board::makeHumanMove()
- {
- int x;
- int y;
- while(1)
- {
- cout << "Please enter the x - coordinate of the square you wish to mark (0 - 2)" << endl;
- cin >> x;
- cout << "Please enter the y - coordinate of the square you wish to mark (0 - 2)" << endl;
- cin >> y;
- if (x < 3 && x >= 0 && y < 3 && y >= 0 && gameBoard[x][y] != 'X' && gameBoard[x][y] != 'O')
- {
- setPosition(x, y);
- drawboard();
- break;
- }
- else
- {
- drawboard();
- cout << "Please enter valid coordinates." << endl;
- continue;
- }
- }
- return;
- }
- int board::checkWin()
- {
- if (checkDone() && computerTurn)
- {
- return 1;
- if (!computerTurn) return -1;
- return 0;
- }
- return -100; // testing only
- }
- bool board::checkDone()
- {
- if (!firstMoveMade) return false;
- // check for win
- else if (areEqual(gameBoard[0][0], gameBoard[0][1], gameBoard[0][2])
- || areEqual(gameBoard[1][0], gameBoard[1][1], gameBoard[1][2])
- || areEqual(gameBoard[2][0], gameBoard[2][1], gameBoard[2][2])
- || areEqual(gameBoard[0][0], gameBoard[1][0], gameBoard[2][0])
- || areEqual(gameBoard[0][1], gameBoard[1][1], gameBoard[2][1])
- || areEqual(gameBoard[0][2], gameBoard[1][2], gameBoard[2][2])
- || areEqual(gameBoard[0][0], gameBoard[1][1], gameBoard[2][2])
- || areEqual(gameBoard[0][2], gameBoard[1][1], gameBoard[2][0]))
- return true;
- for (int i = 0; i < 3; ++i)
- for (int j = 0; j < 3; ++j)
- if (gameBoard[i][j] == '*') return false; // no win statements are true and there is an empty
- // square, so game must be in progress
- return true; // must be a tie
- }
- bool board::areEqual(char one, char two, char three)
- {
- return ((two == one && three == one) && one != '*');
- }
- void board::drawboard()
- {
- cout << "\033[2J\033[1;1H" << endl; // clears the screen
- cout << "\n\n TIC-TAC-TOE ";
- cout << "\n\n You are X \n\n";
- if (!computerTurn) cout << "\n\n YOUR TURN \n\n";
- else cout << "\n\n COMPUTER TURN \n\n";
- cout << "\n";
- cout << " 0 1 2 " << endl;
- cout << " ________________" << endl;
- cout << " | | | |" << endl;
- cout << "0 | " << gameBoard[0][0] << " | " << gameBoard[0][1] << " | " << gameBoard[0][2] << " |" << endl;
- cout << " |-----|-----|----|" << endl;
- cout << "1 | " << gameBoard[1][0] << " | " << gameBoard[1][1] << " | " << gameBoard[1][2] << " |" << endl;
- cout << " |-----|-----|----|" << endl;
- cout << "2 | " << gameBoard[2][0] << " | " << gameBoard[2][1] << " | " << gameBoard[2][2] << " |" << endl;
- cout << " |_____|_____|____|\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement