structure done- unstable
This commit is contained in:
parent
b1b340e6d2
commit
5b9fc9393b
5 changed files with 82 additions and 16 deletions
54
Engine.cpp
54
Engine.cpp
|
@ -43,8 +43,8 @@ void Engine::startGame(){
|
||||||
}
|
}
|
||||||
|
|
||||||
while(b->getTurn() == 'X' ){
|
while(b->getTurn() == 'X' ){
|
||||||
easyAI();
|
//easyAI();
|
||||||
//AI(3);
|
AI(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
gameOver = b->isGameOver();
|
gameOver = b->isGameOver();
|
||||||
|
@ -68,6 +68,8 @@ void Engine::AI(int depth){
|
||||||
Board* temp = new Board(*b);
|
Board* temp = new Board(*b);
|
||||||
moves empty;
|
moves empty;
|
||||||
MNode* root = new MNode(*temp, empty, 0);
|
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
|
//only doing 1 branch right now because testing
|
||||||
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
/*for (int i = 0; i < listOfMoves.size(); ++i){
|
||||||
|
@ -75,15 +77,29 @@ void Engine::AI(int depth){
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
//remove this soon
|
//remove this soon
|
||||||
//b->move(minMax(temp, listOfMoves[0], 0, 0));
|
//b->move(minMax(temp, listOfMoves[0], 0, 0;
|
||||||
|
|
||||||
b->changeTurns();
|
vector<moves> listOfMoves = b->viewPossibleMoves();
|
||||||
|
b->move(listOfMoves[0]);
|
||||||
|
|
||||||
|
b->changeTurns();
|
||||||
b->displayBoard();
|
b->displayBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
moves Engine::minMax(MNode* node, int depth){
|
MNode* Engine::createMMTree(MNode* node, int depth){
|
||||||
Board current = node->getState();
|
Board current = node->getState();
|
||||||
vector<moves> listOfMoves = current.viewPossibleMoves();
|
vector<moves> listOfMoves = current.viewPossibleMoves();
|
||||||
|
vector<MNode*> 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){
|
if (current.isGameOver() == true){
|
||||||
cout << "END OF PATH REACHED\n\n";
|
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<MNode*> 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<MNode*> children = node->getChildren();
|
||||||
|
|
||||||
|
for (auto &c : children){
|
||||||
|
sum += evaluateMMBranch(c, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
*/
|
3
Engine.h
3
Engine.h
|
@ -14,5 +14,6 @@ public:
|
||||||
void startGame();
|
void startGame();
|
||||||
void easyAI();
|
void easyAI();
|
||||||
void AI(int depth);
|
void AI(int depth);
|
||||||
moves minMax(MNode* node, int depth);
|
MNode* createMMTree(MNode* node, int depth);
|
||||||
|
moves evaluateMMTree(MNode* node);
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,13 +31,13 @@ bool MNode::hasChildren(){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printTree(int depth, const MNode& n){
|
void printTree(int depth, MNode* n){
|
||||||
vector<MNode*> children;
|
vector<MNode*> children;
|
||||||
cout << "depth " << depth << " :" << n.getMMVal() << " ";
|
cout << "depth " << depth << " : " << n->getMMVal() << " | ";
|
||||||
children = n.getChildren();
|
children = n->getChildren();
|
||||||
|
|
||||||
//print out root
|
//print out root
|
||||||
for (int i = 0; i < children.size(); ++i){
|
for (int i = 0; i < children.size(); ++i){
|
||||||
printTree(++depth, *children[i]);
|
printTree(++depth, children[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
6
MNode.h
6
MNode.h
|
@ -20,11 +20,11 @@ public:
|
||||||
void addChild(MNode* n) { children.push_back(n); }
|
void addChild(MNode* n) { children.push_back(n); }
|
||||||
int getMMVal() const { return minimax_val; }
|
int getMMVal() const { return minimax_val; }
|
||||||
void setMMVal(int mmval);
|
void setMMVal(int mmval);
|
||||||
Board getState() { return state; }
|
Board getState() const { return state; }
|
||||||
void setState(Board s) { state = s; }
|
void setState(Board s) { state = s; }
|
||||||
moves getMove() { return mvs; }
|
moves getMove() const { return mvs; }
|
||||||
void setMove(moves m) { mvs = m; }
|
void setMove(moves m) { mvs = m; }
|
||||||
bool hasChildren();
|
bool hasChildren();
|
||||||
};
|
};
|
||||||
|
|
||||||
void printTree(int depth, const MNode& n);
|
void printTree(int depth, MNode* n);
|
27
test.cpp
27
test.cpp
|
@ -4,7 +4,30 @@ using namespace std;
|
||||||
|
|
||||||
int main()
|
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;
|
//Board b;
|
||||||
Engine e;
|
//Engine e;
|
||||||
e.startGame();
|
//e.startGame();
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue