master AI commit, stable w/o alpha beta
This commit is contained in:
parent
01b8e2c06b
commit
1de453bce5
8 changed files with 162 additions and 68 deletions
102
Board.cpp
102
Board.cpp
|
@ -1,6 +1,7 @@
|
|||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
#include <vector>
|
||||
#include "Board.h"
|
||||
|
||||
|
@ -8,7 +9,7 @@ using namespace std;
|
|||
|
||||
Board::Board() {
|
||||
Piece* temp;
|
||||
bool valid = false;
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
temp = new Piece(i, j, 'X');
|
||||
|
@ -24,14 +25,15 @@ Board::Board() {
|
|||
pieces.push_back(temp);
|
||||
}
|
||||
}
|
||||
|
||||
valid, xtaken, otaken = false;
|
||||
}
|
||||
|
||||
Board::Board(const Board& b) {
|
||||
vector<Piece*> xp = b.getXPieces();
|
||||
vector<Piece*> op = b.getOPieces();
|
||||
vector<Piece*> xp = b.getTypePieces('X');
|
||||
vector<Piece*> op = b.getTypePieces('O');
|
||||
Piece* temp;
|
||||
//bool valid = false;
|
||||
char tempturn = b.getTurnPls();
|
||||
char tempturn = b.getTurn();
|
||||
turn = tempturn;
|
||||
|
||||
for (int i = 0; i < xp.size(); ++i) {
|
||||
|
@ -45,6 +47,8 @@ Board::Board(const Board& b) {
|
|||
opieces.push_back(temp);
|
||||
pieces.push_back(temp);
|
||||
}
|
||||
|
||||
valid, xtaken, otaken = false;
|
||||
}
|
||||
|
||||
//make this efficient!
|
||||
|
@ -76,6 +80,8 @@ void Board::isTaken(int r, int c) {
|
|||
for(int x = 0; x < opieces.size(); ++x) {
|
||||
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
|
||||
opieces.erase(opieces.begin() + x);
|
||||
cout << "otaken set to TRUE\n";
|
||||
otaken = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,6 +89,8 @@ void Board::isTaken(int r, int c) {
|
|||
for(int x = 0; x < xpieces.size(); ++x) {
|
||||
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
|
||||
xpieces.erase(xpieces.begin() + x);
|
||||
cout << "xtaken set to TRUE\n";
|
||||
xtaken = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -92,6 +100,30 @@ void Board::isTaken(int r, int c) {
|
|||
}
|
||||
}
|
||||
|
||||
bool Board::checkTaken(char c){
|
||||
if (c == 'X'){
|
||||
return xtaken;
|
||||
}
|
||||
|
||||
else if (c == 'O'){
|
||||
return otaken;
|
||||
}
|
||||
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
vector<Piece*> Board::getTypePieces(char type) const{
|
||||
if (type == 'X')
|
||||
return xpieces;
|
||||
else if (type == 'O')
|
||||
return opieces;
|
||||
else{
|
||||
cout << "Invalid type!\n";
|
||||
return pieces;
|
||||
}
|
||||
}
|
||||
|
||||
moves Board::parse(string input){
|
||||
input = myToUpper(input);
|
||||
|
||||
|
@ -138,20 +170,21 @@ bool Board::isGameOver(){
|
|||
return false;
|
||||
}
|
||||
|
||||
string Board::whoWon(){
|
||||
char Board::whoWon(){
|
||||
for (int i = 0; i < xpieces.size(); ++i){
|
||||
if (xpieces[i]->getX() == 7){
|
||||
return "Player X wins!";
|
||||
return 'X';
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < opieces.size(); ++i){
|
||||
if (opieces[i]->getX() == 0){
|
||||
return "Player O wins!";
|
||||
return 'O';
|
||||
}
|
||||
}
|
||||
|
||||
return "ERROR: function whoWon() called incorrectly.";
|
||||
cout << "ERROR: function whoWon() called incorrectly. Game is not over. \n";
|
||||
return 'a';
|
||||
}
|
||||
|
||||
void Board::changeTurns(){
|
||||
|
@ -159,6 +192,11 @@ void Board::changeTurns(){
|
|||
else turn = 'O';
|
||||
}
|
||||
|
||||
void Board::resetTaken(){
|
||||
xtaken = false;
|
||||
otaken = false;
|
||||
}
|
||||
|
||||
void Board::displayBoard(){
|
||||
cout << "; A B C D E F G H"<<endl;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
|
@ -248,7 +286,6 @@ char Board::intToCharColumn(int input){
|
|||
|
||||
void Board::move(string inputMove){
|
||||
moves m = parse(inputMove);
|
||||
//cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
|
||||
move(m);
|
||||
}
|
||||
|
||||
|
@ -484,8 +521,7 @@ string Board::myToUpper(string input){
|
|||
else output.push_back(input[i]);
|
||||
}
|
||||
|
||||
return output;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void Board::undo(Board& tablero){
|
||||
|
@ -538,21 +574,37 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
|
|||
inputVec.push_back(inputBoard);
|
||||
}
|
||||
|
||||
int Board::evaluate(char max){
|
||||
int val = 0;
|
||||
|
||||
if (max == 'X'){
|
||||
val += (xpieces.size() - opieces.size());
|
||||
}
|
||||
|
||||
else if (max == 'O'){
|
||||
val += (opieces.size() - xpieces.size());
|
||||
}
|
||||
|
||||
else {
|
||||
cout << "Error in evaluate: unidentified max, must be either 'X' or 'O'.\n";
|
||||
int Board::evaluate(char max, char min){
|
||||
vector<Piece*> maxPieces = getTypePieces(max);
|
||||
vector<Piece*> minPieces = getTypePieces(min);
|
||||
int reflector, val, x, y = 0;
|
||||
Piece* temp;
|
||||
|
||||
val += 2 * (maxPieces.size() - minPieces.size());
|
||||
|
||||
cout << (checkTaken(max)) << "\n";
|
||||
cout << (checkTaken(min)) << "\n";
|
||||
//check for taken conditions
|
||||
if (checkTaken(min)){
|
||||
cout << "adding 10 to val\n";
|
||||
val += 10;
|
||||
}
|
||||
|
||||
if (checkTaken(max)){
|
||||
cout << "subtracting 10 from val\n";
|
||||
val -= 10;
|
||||
}
|
||||
|
||||
//ultimate condition!
|
||||
if (isGameOver()){
|
||||
if (whoWon() == max)
|
||||
val = INT_MAX / 2;
|
||||
|
||||
else
|
||||
val = INT_MIN / 2;
|
||||
}
|
||||
|
||||
cout << "val: " << val << "\n";
|
||||
return val;
|
||||
}
|
||||
|
||||
|
|
15
Board.h
15
Board.h
|
@ -28,8 +28,8 @@ class Board {
|
|||
vector<Piece*> xpieces;
|
||||
vector<Piece*> opieces;
|
||||
vector<Piece*> pieces;
|
||||
bool valid, xtaken, otaken;
|
||||
char turn = 'O';
|
||||
bool valid = false;
|
||||
|
||||
public:
|
||||
Board();
|
||||
|
@ -40,14 +40,15 @@ public:
|
|||
bool isPiece(int r, int c);
|
||||
Piece* getPiece(int r, int c);
|
||||
void isTaken(int r, int c);
|
||||
vector<Piece*> getXPieces() const { return xpieces; }
|
||||
vector<Piece*> getOPieces() const { return opieces; }
|
||||
char getTurnPls() const { return turn; }
|
||||
bool checkTaken(char c);
|
||||
vector<Piece*> getPieces() const { return pieces; }
|
||||
vector<Piece*> getTypePieces(char type) const;
|
||||
char getTurn() const { return turn; }
|
||||
moves parse(string input);
|
||||
char getTurn() { return turn; }
|
||||
bool isGameOver();
|
||||
string whoWon();
|
||||
char whoWon();
|
||||
void changeTurns();
|
||||
void resetTaken();
|
||||
void displayBoard();
|
||||
string boardToString();
|
||||
int charToIntColumn(char input);
|
||||
|
@ -60,5 +61,5 @@ public:
|
|||
void undo(Board& tablero);
|
||||
void interpret(string input, Board& tablero);
|
||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||
int evaluate(char max);
|
||||
int evaluate(char max, char min);
|
||||
};
|
||||
|
|
96
Engine.cpp
96
Engine.cpp
|
@ -3,6 +3,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include "Engine.h"
|
||||
|
||||
Engine::Engine(){
|
||||
|
@ -14,13 +16,24 @@ void Engine::startGame(){
|
|||
cout<<"WELCOME\n";
|
||||
|
||||
cout<<"1. Play against AI?\n";
|
||||
cout<<"2. Play against a human?\n";
|
||||
cout<<"2. Watch AI vs AI?\n";
|
||||
cout<<"Enter choice: \n";
|
||||
|
||||
int choice = -1;
|
||||
cin >> choice;
|
||||
cout << "OK" << endl;
|
||||
|
||||
if (choice == 1)
|
||||
userGame();
|
||||
else if (choice == 2)
|
||||
AIGame();
|
||||
else {
|
||||
cout << "Please enter a valid choice.\n";
|
||||
startGame();
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::userGame(){
|
||||
string move;
|
||||
|
||||
bool gameOver = false;
|
||||
|
@ -39,6 +52,7 @@ void Engine::startGame(){
|
|||
|
||||
if(b->isValid()){
|
||||
b->changeTurns();
|
||||
b->resetTaken();
|
||||
b->setValidFalse();
|
||||
}
|
||||
}
|
||||
|
@ -55,36 +69,44 @@ void Engine::startGame(){
|
|||
}
|
||||
}
|
||||
|
||||
void Engine::easyAI(){
|
||||
vector<moves> listOfMoves = b->viewPossibleMoves();
|
||||
|
||||
srand(time(NULL));
|
||||
int randomChoice = rand() % (listOfMoves.size()-1) - 0;
|
||||
|
||||
b->move(listOfMoves[randomChoice]);
|
||||
b->changeTurns();
|
||||
void Engine::AIGame(){
|
||||
bool gameOver = false;
|
||||
|
||||
while (gameOver != true){
|
||||
gameOver = b->isGameOver();
|
||||
|
||||
while(b->getTurn() == 'O'){
|
||||
AI(3);
|
||||
}
|
||||
|
||||
b->displayBoard();
|
||||
sleep(1);
|
||||
|
||||
while(b->getTurn() == 'X' ){
|
||||
AI(3);
|
||||
}
|
||||
|
||||
gameOver = b->isGameOver();
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::AI(int depth){
|
||||
Board* state = new Board(*b);
|
||||
moves m;
|
||||
MNode* root = new MNode(*state, m, 0);
|
||||
createMMTree(root, depth);
|
||||
createMMTree(root, depth, 1);
|
||||
m = evaluateMMTree(root);
|
||||
|
||||
/*
|
||||
|
||||
printTree(0, root);
|
||||
cout << "\n";
|
||||
cout << "AI move: (" << m.row << ", " << m.column << "): " << m.moveType << "\n";
|
||||
*/
|
||||
|
||||
b->move(m);
|
||||
b->changeTurns();
|
||||
b->displayBoard();
|
||||
b->resetTaken();
|
||||
}
|
||||
|
||||
void Engine::createMMTree(MNode* node, int depth){
|
||||
void Engine::createMMTree(MNode* node, int depth, int alt){
|
||||
MNode* temp;
|
||||
char max, min;
|
||||
Board current = node->getState();
|
||||
vector<moves> listOfMoves = current.viewPossibleMoves();
|
||||
|
||||
|
@ -94,11 +116,29 @@ void Engine::createMMTree(MNode* node, int depth){
|
|||
current.getTurn() && current.isThisMovePossible(8 - listOfMoves[i].row,
|
||||
listOfMoves[i].column,
|
||||
listOfMoves[i].moveType)){
|
||||
current.resetTaken();
|
||||
current = current.move(listOfMoves[i]);
|
||||
max = current.getTurn();
|
||||
current.changeTurns();
|
||||
temp = new MNode(current, listOfMoves[i], current.evaluate('X'));
|
||||
min = current.getTurn();
|
||||
|
||||
if (current.checkTaken('X'))
|
||||
cout << "xtaken true\n";
|
||||
if (current.checkTaken('O'))
|
||||
cout << "otaken true\n";
|
||||
|
||||
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
|
||||
|
||||
if (alt == 1)
|
||||
temp->setType("max");
|
||||
else if (alt == 1)
|
||||
temp->setType("min");
|
||||
|
||||
current.resetTaken();
|
||||
node->addChild(temp);
|
||||
createMMTree(temp, --depth);
|
||||
|
||||
createMMTree(temp, --depth, alt * -1);
|
||||
|
||||
current.changeTurns();
|
||||
}
|
||||
}
|
||||
|
@ -116,25 +156,27 @@ moves Engine::evaluateMMTree(MNode* node){
|
|||
vector<MNode*> children = node->getChildren();
|
||||
|
||||
for (auto &c : children){
|
||||
val = evaluateMMBranch(c, 0);
|
||||
val = evaluateMMBranch(c, INT_MIN);
|
||||
if(val > max){
|
||||
temp = c;
|
||||
max = val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return temp->getMove();
|
||||
}
|
||||
|
||||
//return sum of eval values
|
||||
//pass in each child of root and 0
|
||||
int Engine::evaluateMMBranch(MNode* node, int sum){
|
||||
sum += node->getMMVal();
|
||||
int Engine::evaluateMMBranch(MNode* node, int max){
|
||||
if (node->getMMVal() > max)
|
||||
max = node->getMMVal();
|
||||
|
||||
vector<MNode*> children = node->getChildren();
|
||||
|
||||
for (auto &c : children){
|
||||
sum += evaluateMMBranch(c, sum);
|
||||
evaluateMMBranch(c, max);
|
||||
}
|
||||
|
||||
//cout << "max: " << max << "\n";
|
||||
|
||||
return sum;
|
||||
return max;
|
||||
}
|
5
Engine.h
5
Engine.h
|
@ -12,9 +12,10 @@ public:
|
|||
Engine();
|
||||
Board* getBoard() { return b; }
|
||||
void startGame();
|
||||
void easyAI();
|
||||
void userGame();
|
||||
void AIGame();
|
||||
void AI(int depth);
|
||||
void createMMTree(MNode* node, int depth);
|
||||
void createMMTree(MNode* node, int depth, int alt);
|
||||
moves evaluateMMTree(MNode* node);
|
||||
int evaluateMMBranch(MNode* node, int sum);
|
||||
};
|
||||
|
|
|
@ -33,7 +33,7 @@ bool MNode::hasChildren(){
|
|||
|
||||
void printTree(int depth, MNode* n){
|
||||
vector<MNode*> children;
|
||||
cout << "depth " << depth << " : " << n->getMMVal() << " | ";
|
||||
cout << "depth " << depth << " : " << n->getMMVal() << "\n";
|
||||
children = n->getChildren();
|
||||
|
||||
//print out root
|
||||
|
|
3
MNode.h
3
MNode.h
|
@ -11,6 +11,7 @@ class MNode {
|
|||
Board state;
|
||||
moves mvs;
|
||||
int minimax_val;
|
||||
string type; //min or max
|
||||
|
||||
public:
|
||||
MNode();
|
||||
|
@ -24,6 +25,8 @@ public:
|
|||
void setState(Board s) { state = s; }
|
||||
moves getMove() const { return mvs; }
|
||||
void setMove(moves m) { mvs = m; }
|
||||
string getType() { return type; }
|
||||
void setType(string t) { type = t; }
|
||||
bool hasChildren();
|
||||
};
|
||||
|
||||
|
|
|
@ -63,7 +63,3 @@ void Piece::moveRight(){
|
|||
else
|
||||
cout << "Error: trying to move an empty piece left.";
|
||||
}
|
||||
|
||||
void Piece::isTaken(){
|
||||
cout << getX() << " " << getY() << "\n\n";
|
||||
}
|
||||
|
|
3
Piece.h
3
Piece.h
|
@ -20,5 +20,4 @@ public:
|
|||
void setY(int c){ y = c; }
|
||||
char getType(){ return type; }
|
||||
void makeEmpty(){ type = '_'; }
|
||||
void isTaken();
|
||||
};
|
||||
};
|
||||
|
|
Reference in a new issue