2015-10-29 22:52:18 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2015-10-29 13:54:16 -05:00
|
|
|
#include <limits.h>
|
2015-11-02 16:19:42 -06:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2015-10-29 22:52:18 -05:00
|
|
|
#include "Engine.h"
|
2015-11-03 20:37:25 -06:00
|
|
|
#include "Parser.h"
|
2015-10-29 22:52:18 -05:00
|
|
|
|
|
|
|
Engine::Engine(){
|
|
|
|
Board* brd = new Board();
|
|
|
|
b = brd;
|
2015-11-02 17:20:50 -06:00
|
|
|
alpha = INT_MIN;
|
|
|
|
beta = INT_MAX;
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::startGame(){
|
|
|
|
cout<<"WELCOME\n";
|
|
|
|
|
|
|
|
cout<<"1. Play against AI?\n";
|
2015-11-02 16:19:42 -06:00
|
|
|
cout<<"2. Watch AI vs AI?\n";
|
2015-10-29 22:52:18 -05:00
|
|
|
cout<<"Enter choice: \n";
|
|
|
|
|
|
|
|
int choice = -1;
|
|
|
|
cin >> choice;
|
2015-11-02 17:27:45 -06:00
|
|
|
cout<<"Enter AI difficulty: \n";
|
|
|
|
int difficulty;
|
|
|
|
cin >> difficulty;
|
|
|
|
|
2015-10-29 22:52:18 -05:00
|
|
|
cout << "OK" << endl;
|
|
|
|
|
2015-11-02 16:19:42 -06:00
|
|
|
if (choice == 1)
|
2015-11-02 17:27:45 -06:00
|
|
|
userGame(difficulty);
|
2015-11-02 16:19:42 -06:00
|
|
|
else if (choice == 2)
|
2015-11-02 17:27:45 -06:00
|
|
|
AIGame(difficulty);
|
2015-11-02 16:19:42 -06:00
|
|
|
else {
|
|
|
|
cout << "Please enter a valid choice.\n";
|
|
|
|
startGame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:27:45 -06:00
|
|
|
void Engine::userGame(int difficulty){
|
2015-10-29 22:52:18 -05:00
|
|
|
string move;
|
|
|
|
|
|
|
|
bool gameOver = false;
|
|
|
|
vector<Board> record;
|
|
|
|
b->snapshot(record, *b);
|
2015-11-03 15:12:29 -06:00
|
|
|
cin.ignore();
|
|
|
|
cin.clear();
|
|
|
|
|
2015-10-29 22:52:18 -05:00
|
|
|
while (gameOver != true){
|
|
|
|
gameOver = b->isGameOver();
|
|
|
|
|
|
|
|
while(b->getTurn() == 'O' && !b->isValid()){
|
|
|
|
b->displayBoard();
|
|
|
|
cout<<"\nEnter command: ";
|
2015-11-03 15:12:29 -06:00
|
|
|
cin.clear();
|
|
|
|
getline(cin, move);
|
2015-10-29 22:52:18 -05:00
|
|
|
cout << "\n";
|
2015-11-03 15:12:29 -06:00
|
|
|
parse(move, *b);
|
2015-10-29 22:52:18 -05:00
|
|
|
|
|
|
|
if(b->isValid()){
|
|
|
|
b->changeTurns();
|
2015-11-02 16:19:42 -06:00
|
|
|
b->resetTaken();
|
2015-10-29 22:52:18 -05:00
|
|
|
b->setValidFalse();
|
|
|
|
}
|
2015-11-03 21:41:07 -06:00
|
|
|
b->displayBoard();
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while(b->getTurn() == 'X' ){
|
2015-11-02 17:27:45 -06:00
|
|
|
AI(difficulty);
|
2015-10-29 22:52:18 -05:00
|
|
|
|
2015-11-03 20:37:25 -06:00
|
|
|
gameOver = b->isGameOver();
|
|
|
|
b->setValidFalse();
|
|
|
|
b->snapshot(record, *b);
|
|
|
|
b->displayBoard();
|
|
|
|
}
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
2015-11-02 17:27:45 -06:00
|
|
|
|
2015-10-29 22:52:18 -05:00
|
|
|
string s = "";
|
|
|
|
s += b->getTurn();
|
|
|
|
cout << "Game over. " + s + " wins!\n\n";
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:27:45 -06:00
|
|
|
void Engine::AIGame(int difficulty){
|
2015-11-02 16:19:42 -06:00
|
|
|
bool gameOver = false;
|
|
|
|
|
|
|
|
while (gameOver != true){
|
|
|
|
gameOver = b->isGameOver();
|
|
|
|
|
|
|
|
while(b->getTurn() == 'O'){
|
2015-11-02 17:27:45 -06:00
|
|
|
AI(difficulty);
|
2015-11-02 16:19:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
b->displayBoard();
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
while(b->getTurn() == 'X' ){
|
2015-11-02 17:27:45 -06:00
|
|
|
AI(difficulty);
|
2015-11-02 16:19:42 -06:00
|
|
|
}
|
|
|
|
|
2015-11-03 20:37:25 -06:00
|
|
|
b->displayBoard();
|
|
|
|
sleep(1);
|
2015-11-02 16:19:42 -06:00
|
|
|
gameOver = b->isGameOver();
|
|
|
|
}
|
2015-11-02 17:27:45 -06:00
|
|
|
|
|
|
|
string s = "";
|
|
|
|
s += b->getTurn();
|
|
|
|
cout << "Game over. " + s + " wins!\n\n";
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::AI(int depth){
|
2015-11-03 21:41:07 -06:00
|
|
|
if (depth == -1)
|
|
|
|
depth = INT_MAX;
|
|
|
|
|
2015-10-29 13:54:16 -05:00
|
|
|
Board* state = new Board(*b);
|
|
|
|
moves m;
|
2015-11-03 21:41:07 -06:00
|
|
|
MNode* root = new MNode(*state, m, 0);
|
|
|
|
this->resetAB();
|
2015-11-02 16:19:42 -06:00
|
|
|
createMMTree(root, depth, 1);
|
2015-10-29 13:54:16 -05:00
|
|
|
m = evaluateMMTree(root);
|
2015-10-29 22:52:18 -05:00
|
|
|
|
2015-10-30 11:56:44 -05:00
|
|
|
b->move(m);
|
2015-10-29 13:06:15 -05:00
|
|
|
b->changeTurns();
|
2015-11-02 16:19:42 -06:00
|
|
|
b->resetTaken();
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:19:42 -06:00
|
|
|
void Engine::createMMTree(MNode* node, int depth, int alt){
|
2015-10-29 13:54:16 -05:00
|
|
|
MNode* temp;
|
2015-11-02 16:19:42 -06:00
|
|
|
char max, min;
|
2015-11-02 17:20:50 -06:00
|
|
|
bool cond = true;
|
2015-10-29 22:52:18 -05:00
|
|
|
Board current = node->getState();
|
|
|
|
vector<moves> listOfMoves = current.viewPossibleMoves();
|
2015-10-29 13:54:16 -05:00
|
|
|
|
2015-11-02 17:20:50 -06:00
|
|
|
if (node->getType() == "max"){
|
|
|
|
if (node->getMMVal() <= alpha){
|
|
|
|
cond = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (node->getType() == "min"){
|
|
|
|
if (node->getMMVal() >= beta){
|
|
|
|
cond = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:06:15 -05:00
|
|
|
if (depth >= 0){
|
2015-10-29 13:54:16 -05:00
|
|
|
for (int i = 0; i < listOfMoves.size(); ++i){
|
2015-11-03 15:12:29 -06:00
|
|
|
if(current.getPiece(listOfMoves[i].row, listOfMoves[i].column)->getType() ==
|
|
|
|
current.getTurn() && current.isThisMovePossible(listOfMoves[i].row,
|
2015-10-29 13:54:16 -05:00
|
|
|
listOfMoves[i].column,
|
|
|
|
listOfMoves[i].moveType)){
|
2015-11-02 17:20:50 -06:00
|
|
|
if (cond){
|
|
|
|
current.resetTaken();
|
|
|
|
current = current.move(listOfMoves[i]);
|
|
|
|
max = current.getTurn();
|
|
|
|
current.changeTurns();
|
|
|
|
min = current.getTurn();
|
|
|
|
|
|
|
|
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
|
|
|
|
|
|
|
|
if (alt == 1)
|
|
|
|
temp->setType("max");
|
2015-11-03 20:37:25 -06:00
|
|
|
else if (alt == -1)
|
2015-11-02 17:20:50 -06:00
|
|
|
temp->setType("min");
|
|
|
|
|
|
|
|
current.resetTaken();
|
|
|
|
node->addChild(temp);
|
|
|
|
|
2015-11-03 20:37:25 -06:00
|
|
|
alt = alt * -1;
|
|
|
|
createMMTree(temp, --depth, alt);
|
2015-11-02 17:20:50 -06:00
|
|
|
|
|
|
|
current.changeTurns();
|
|
|
|
}
|
2015-10-29 13:54:16 -05:00
|
|
|
}
|
2015-10-29 13:06:15 -05:00
|
|
|
}
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
2015-11-02 17:20:50 -06:00
|
|
|
|
|
|
|
else {
|
|
|
|
if (node->getType() == "max"){
|
|
|
|
if (node->getMMVal() > alpha){
|
|
|
|
alpha = node->getMMVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (node->getType() == "min"){
|
|
|
|
if (node->getMMVal() < beta){
|
|
|
|
beta = node->getMMVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:54:16 -05:00
|
|
|
return;
|
2015-10-29 08:58:32 -05:00
|
|
|
}
|
2015-10-29 13:06:15 -05:00
|
|
|
|
|
|
|
moves Engine::evaluateMMTree(MNode* node){
|
|
|
|
//returns the move from children that maximizes evaluate()
|
2015-10-29 13:54:16 -05:00
|
|
|
MNode* temp;
|
|
|
|
int max = INT_MIN;
|
2015-11-03 20:37:25 -06:00
|
|
|
int min = INT_MAX;
|
2015-10-29 13:54:16 -05:00
|
|
|
int val;
|
2015-10-29 13:06:15 -05:00
|
|
|
vector<MNode*> children = node->getChildren();
|
|
|
|
|
|
|
|
for (auto &c : children){
|
2015-11-03 20:37:25 -06:00
|
|
|
val = evaluateMMBranch(c, INT_MIN, INT_MAX);
|
2015-10-29 13:54:16 -05:00
|
|
|
if(val > max){
|
|
|
|
temp = c;
|
|
|
|
max = val;
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
2015-11-03 20:37:25 -06:00
|
|
|
|
|
|
|
if(val < min){
|
|
|
|
temp = c;
|
|
|
|
min = val;
|
|
|
|
}
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
2015-11-02 16:19:42 -06:00
|
|
|
|
2015-10-29 13:54:16 -05:00
|
|
|
return temp->getMove();
|
2015-10-29 22:52:18 -05:00
|
|
|
}
|
2015-10-29 13:54:16 -05:00
|
|
|
|
2015-11-03 20:37:25 -06:00
|
|
|
int Engine::evaluateMMBranch(MNode* node, int max, int min){
|
2015-10-29 13:06:15 -05:00
|
|
|
vector<MNode*> children = node->getChildren();
|
|
|
|
|
|
|
|
for (auto &c : children){
|
2015-11-03 20:37:25 -06:00
|
|
|
evaluateMMBranch(c, max, min);
|
2015-10-29 13:06:15 -05:00
|
|
|
}
|
2015-11-02 17:20:50 -06:00
|
|
|
|
|
|
|
if (node->getMMVal() > max)
|
|
|
|
max = node->getMMVal();
|
2015-10-29 13:06:15 -05:00
|
|
|
|
2015-11-03 20:37:25 -06:00
|
|
|
if (node->getMMVal() < min)
|
|
|
|
min = node->getMMVal();
|
|
|
|
|
2015-11-02 16:19:42 -06:00
|
|
|
return max;
|
2015-11-03 21:41:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::resetAB(){
|
|
|
|
alpha = INT_MIN;
|
|
|
|
beta = INT_MAX;
|
2015-11-03 20:37:25 -06:00
|
|
|
}
|