From 5f4c6c25391ff269f7d79ea9d7f0a3e93b973d22 Mon Sep 17 00:00:00 2001 From: Rebecca Schofield Date: Thu, 29 Oct 2015 13:54:16 -0500 Subject: [PATCH] compiles, but not correct --- Board.cpp | 17 +++++++---- Board.h | 2 +- Engine.cpp | 88 +++++++++++++++++++++++++----------------------------- Engine.h | 3 +- test.cpp | 28 ++--------------- 5 files changed, 58 insertions(+), 80 deletions(-) diff --git a/Board.cpp b/Board.cpp index 6750ab2..072fc95 100644 --- a/Board.cpp +++ b/Board.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include "Board.h" @@ -536,19 +538,24 @@ void Board::snapshot(vector& inputVec, Board inputBoard){ inputVec.push_back(inputBoard); } -int Board::evaluate(char max, char min){ - //right now just evaluating number of pieces +int Board::evaluate(char max){ + int val = 0; + + srand(time(NULL)); + val = rand() % 100 + 1; + /* if (max == 'X'){ - return (xpieces.size() - opieces.size()); + val += (xpieces.size() - opieces.size()); } else if (max == 'O'){ - return (opieces.size() - xpieces.size()); + val += (opieces.size() - xpieces.size()); } else { cout << "Error in evaluate: unidentified max, must be either 'X' or 'O'.\n"; - return 0; } + */ + return val; } diff --git a/Board.h b/Board.h index 7af5bcf..cc01a6a 100644 --- a/Board.h +++ b/Board.h @@ -60,5 +60,5 @@ public: void undo(Board& tablero); void interpret(string input, Board& tablero); void snapshot(vector& inputVec, Board inputBoard); - int evaluate(char max, char min); + int evaluate(char max); }; diff --git a/Engine.cpp b/Engine.cpp index 947b583..5430dbb 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "Engine.h" Engine::Engine(){ @@ -65,19 +66,16 @@ void Engine::easyAI(){ } 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){ - minMax(b, listOfMoves[i]); - }*/ - - //remove this soon - //b->move(minMax(temp, listOfMoves[0], 0, 0; + Board* state = new Board(*b); + moves m; + MNode* root = new MNode(*state, m, 0); + createMMTree(root, depth); + m = evaluateMMTree(root); + cout << "\n"; + /* + printTree(0, root); + cout << "\n"; + */ vector listOfMoves = b->viewPossibleMoves(); b->move(listOfMoves[0]); @@ -86,58 +84,53 @@ void Engine::AI(int depth){ b->displayBoard(); } -MNode* Engine::createMMTree(MNode* node, int depth){ +void Engine::createMMTree(MNode* node, int depth){ + MNode* temp; 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)); + 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 = current.move(listOfMoves[i]); + current.changeTurns(); + temp = new MNode(current, listOfMoves[i], current.evaluate('X')); + node->addChild(temp); + createMMTree(temp, --depth); + current.changeTurns(); + } } } - else return node; - - /* - if (current.isGameOver() == true){ - cout << "END OF PATH REACHED\n\n"; - return m; - } - - else { - if(current.getPiece(8 - m.row, m.column)->getType() == current.getTurn() && current.isThisMovePossible(8 - m.row, m.column, m.moveType)){ - temp->move(m); - temp->evaluate('X', 'O'); - temp->changeTurns(); - } - else { - m = minMax(temp, listOfMoves[++r], c, r); - } - - vector listOfMoves = temp->viewPossibleMoves(); - minMax(temp, listOfMoves[0], 0, 0); - return m; - } - */ + return; } moves Engine::evaluateMMTree(MNode* node){ //returns the move from children that maximizes evaluate() + MNode* temp; moves m; + int max = INT_MIN; + int val; vector children = node->getChildren(); for (auto &c : children){ - cout << "c: " << c->getMMVal(); + val = evaluateMMBranch(c, 0); + + if(val > max){ + temp = c; + max = val; + } } - return m; + return temp->getMove(); } -/* + //return sum of eval values //pass in each child of root and 0 -int Engine::evaluateMMBranch(Node* node, int sum){ +int Engine::evaluateMMBranch(MNode* node, int sum){ sum += node->getMMVal(); vector children = node->getChildren(); @@ -146,5 +139,4 @@ int Engine::evaluateMMBranch(Node* node, int sum){ } return sum; -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/Engine.h b/Engine.h index 104284b..b915383 100644 --- a/Engine.h +++ b/Engine.h @@ -14,6 +14,7 @@ public: void startGame(); void easyAI(); void AI(int depth); - MNode* createMMTree(MNode* node, int depth); + void createMMTree(MNode* node, int depth); moves evaluateMMTree(MNode* node); + int evaluateMMBranch(MNode* node, int sum); }; diff --git a/test.cpp b/test.cpp index ec8eb3e..302979c 100644 --- a/test.cpp +++ b/test.cpp @@ -4,30 +4,8 @@ using namespace std; int main() { - MNode* a = new MNode(); - MNode* b = new MNode(); - MNode* c = new MNode(); - MNode* d = new MNode(); + Engine e; + e.startGame(); - 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(); + //COMPILES BUT EVERYTHING IS PRINTING FIRST MOVE }