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

View file

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