diff --git a/Engine.cpp b/Engine.cpp index 60b879c..60a01bb 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -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 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 children = node->getChildren(); for (auto &c : children){ evaluateMMBranch(c, max); } - - //cout << "max: " << max << "\n"; + + if (node->getMMVal() > max) + max = node->getMMVal(); return max; } diff --git a/Engine.h b/Engine.h index ae314af..c5a1005 100644 --- a/Engine.h +++ b/Engine.h @@ -7,7 +7,7 @@ using namespace std; class Engine { Board* b; - + int alpha, beta; public: Engine();