barebones AI done

This commit is contained in:
Rebecca Schofield 2015-11-02 17:20:50 -06:00
parent c3cf43fc00
commit 0b5b8be258
2 changed files with 64 additions and 31 deletions

View file

@ -10,6 +10,8 @@
Engine::Engine(){
Board* brd = new Board();
b = brd;
alpha = INT_MIN;
beta = INT_MAX;
}
void Engine::startGame(){
@ -64,7 +66,6 @@ void Engine::userGame(){
gameOver = b->isGameOver();
b->setValidFalse();
b->snapshot(record, *b);
}
b->displayBoard();
@ -111,42 +112,76 @@ void Engine::AI(int depth){
void Engine::createMMTree(MNode* node, int depth, int alt){
MNode* temp;
char max, min;
bool cond = true;
Board current = node->getState();
vector<moves> listOfMoves = current.viewPossibleMoves();
if (node->getType() == "max"){
if (node->getMMVal() <= alpha){
cond = false;
}
}
else if (node->getType() == "min"){
if (node->getMMVal() >= beta){
cond = false;
}
}
if (depth >= 0){
for (int i = 0; i < listOfMoves.size(); ++i){
if(current.getPiece(8 - listOfMoves[i].row, listOfMoves[i].column)->getType() ==
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();
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, alt * -1);
current.changeTurns();
if (cond){
current.resetTaken();
current = current.move(listOfMoves[i]);
max = current.getTurn();
current.changeTurns();
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, alt * -1);
current.changeTurns();
}
}
}
}
else {
if (node->getType() == "max"){
if (node->getMMVal() > alpha){
alpha = node->getMMVal();
}
}
else if (node->getType() == "min"){
if (node->getMMVal() < beta){
beta = node->getMMVal();
}
}
}
//cout << "alpha: " << alpha << "\n";
//cout << "beta: " << beta << "\n";
return;
}
@ -171,16 +206,14 @@ moves Engine::evaluateMMTree(MNode* node){
}
int Engine::evaluateMMBranch(MNode* node, int max){
if (node->getMMVal() > max)
max = node->getMMVal();
vector<MNode*> children = node->getChildren();
for (auto &c : children){
evaluateMMBranch(c, max);
}
//cout << "max: " << max << "\n";
if (node->getMMVal() > max)
max = node->getMMVal();
return max;
}

View file

@ -7,7 +7,7 @@ using namespace std;
class Engine {
Board* b;
int alpha, beta;
public:
Engine();