diff --git a/Engine.cpp b/Engine.cpp index 17079f0..947b583 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -43,8 +43,8 @@ void Engine::startGame(){ } while(b->getTurn() == 'X' ){ - easyAI(); - //AI(3); + //easyAI(); + AI(3); } gameOver = b->isGameOver(); @@ -68,6 +68,8 @@ void Engine::AI(int depth){ Board* temp = new Board(*b); moves empty; MNode* root = new MNode(*temp, empty, 0); + root = createMMTree(root, depth); + //printTree(0, root); CURRENTLY PRODUCES SEG FAULT //only doing 1 branch right now because testing /*for (int i = 0; i < listOfMoves.size(); ++i){ @@ -75,15 +77,29 @@ void Engine::AI(int depth){ }*/ //remove this soon - //b->move(minMax(temp, listOfMoves[0], 0, 0)); - - b->changeTurns(); + //b->move(minMax(temp, listOfMoves[0], 0, 0; + + vector listOfMoves = b->viewPossibleMoves(); + b->move(listOfMoves[0]); + + b->changeTurns(); b->displayBoard(); } -moves Engine::minMax(MNode* node, int depth){ +MNode* Engine::createMMTree(MNode* node, int depth){ Board current = node->getState(); vector listOfMoves = current.viewPossibleMoves(); + vector children = node->getChildren(); + + if (depth >= 0){ + for (int i = 0; i < children.size(); ++i){ + cout << "i: " << i << "\n"; + node->addChild(createMMTree(children[i], --depth)); + } + } + + else return node; + /* if (current.isGameOver() == true){ cout << "END OF PATH REACHED\n\n"; @@ -106,3 +122,29 @@ moves Engine::minMax(MNode* node, int depth){ } */ } + +moves Engine::evaluateMMTree(MNode* node){ + //returns the move from children that maximizes evaluate() + moves m; + vector children = node->getChildren(); + + for (auto &c : children){ + cout << "c: " << c->getMMVal(); + } + + return m; +} +/* +//return sum of eval values +//pass in each child of root and 0 +int Engine::evaluateMMBranch(Node* node, int sum){ + sum += node->getMMVal(); + vector children = node->getChildren(); + + for (auto &c : children){ + sum += evaluateMMBranch(c, sum); + } + + return sum; +} +*/ \ No newline at end of file diff --git a/Engine.h b/Engine.h index 89f3327..104284b 100644 --- a/Engine.h +++ b/Engine.h @@ -14,5 +14,6 @@ public: void startGame(); void easyAI(); void AI(int depth); - moves minMax(MNode* node, int depth); + MNode* createMMTree(MNode* node, int depth); + moves evaluateMMTree(MNode* node); }; diff --git a/MNode.cpp b/MNode.cpp index 8f4c410..ca4c548 100755 --- a/MNode.cpp +++ b/MNode.cpp @@ -31,13 +31,13 @@ bool MNode::hasChildren(){ return false; } -void printTree(int depth, const MNode& n){ +void printTree(int depth, MNode* n){ vector children; - cout << "depth " << depth << " :" << n.getMMVal() << " "; - children = n.getChildren(); + cout << "depth " << depth << " : " << n->getMMVal() << " | "; + children = n->getChildren(); //print out root for (int i = 0; i < children.size(); ++i){ - printTree(++depth, *children[i]); + printTree(++depth, children[i]); } } \ No newline at end of file diff --git a/MNode.h b/MNode.h index 1afe963..a35de3d 100755 --- a/MNode.h +++ b/MNode.h @@ -20,11 +20,11 @@ public: void addChild(MNode* n) { children.push_back(n); } int getMMVal() const { return minimax_val; } void setMMVal(int mmval); - Board getState() { return state; } + Board getState() const { return state; } void setState(Board s) { state = s; } - moves getMove() { return mvs; } + moves getMove() const { return mvs; } void setMove(moves m) { mvs = m; } bool hasChildren(); }; -void printTree(int depth, const MNode& n); \ No newline at end of file +void printTree(int depth, MNode* n); \ No newline at end of file diff --git a/test.cpp b/test.cpp index c26c406..ec8eb3e 100644 --- a/test.cpp +++ b/test.cpp @@ -4,7 +4,30 @@ using namespace std; int main() { + MNode* a = new MNode(); + MNode* b = new MNode(); + MNode* c = new MNode(); + MNode* d = new MNode(); + + a->setMMVal(1); + b->setMMVal(3); + c->setMMVal(87); + d->setMMVal(-1); + + a->addChild(b); + a->addChild(c); + b->addChild(d); + + printTree(0, a); + cout << "\n"; + printTree(0, b); + cout << "\n"; + printTree(0, c); + cout << "\n"; + printTree(0, d); + cout << "\n"; + //Board b; - Engine e; - e.startGame(); + //Engine e; + //e.startGame(); }