compiles, but not correct

This commit is contained in:
Rebecca Schofield 2015-10-29 13:54:16 -05:00
parent 5b9fc9393b
commit 5f4c6c2539
5 changed files with 58 additions and 80 deletions

View file

@ -1,3 +1,5 @@
#include <ctime>
#include <cstdlib>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "Board.h" #include "Board.h"
@ -536,19 +538,24 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
inputVec.push_back(inputBoard); inputVec.push_back(inputBoard);
} }
int Board::evaluate(char max, char min){ int Board::evaluate(char max){
//right now just evaluating number of pieces int val = 0;
srand(time(NULL));
val = rand() % 100 + 1;
/*
if (max == 'X'){ if (max == 'X'){
return (xpieces.size() - opieces.size()); val += (xpieces.size() - opieces.size());
} }
else if (max == 'O'){ else if (max == 'O'){
return (opieces.size() - xpieces.size()); val += (opieces.size() - xpieces.size());
} }
else { else {
cout << "Error in evaluate: unidentified max, must be either 'X' or 'O'.\n"; cout << "Error in evaluate: unidentified max, must be either 'X' or 'O'.\n";
return 0;
} }
*/
return val;
} }

View file

@ -60,5 +60,5 @@ public:
void undo(Board& tablero); void undo(Board& tablero);
void interpret(string input, Board& tablero); void interpret(string input, Board& tablero);
void snapshot(vector<Board>& inputVec, Board inputBoard); void snapshot(vector<Board>& inputVec, Board inputBoard);
int evaluate(char max, char min); int evaluate(char max);
}; };

View file

@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <limits.h>
#include "Engine.h" #include "Engine.h"
Engine::Engine(){ Engine::Engine(){
@ -65,19 +66,16 @@ void Engine::easyAI(){
} }
void Engine::AI(int depth){ void Engine::AI(int depth){
Board* temp = new Board(*b); Board* state = new Board(*b);
moves empty; moves m;
MNode* root = new MNode(*temp, empty, 0); MNode* root = new MNode(*state, m, 0);
root = createMMTree(root, depth); createMMTree(root, depth);
//printTree(0, root); CURRENTLY PRODUCES SEG FAULT m = evaluateMMTree(root);
cout << "\n";
//only doing 1 branch right now because testing /*
/*for (int i = 0; i < listOfMoves.size(); ++i){ printTree(0, root);
minMax(b, listOfMoves[i]); cout << "\n";
}*/ */
//remove this soon
//b->move(minMax(temp, listOfMoves[0], 0, 0;
vector<moves> listOfMoves = b->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
b->move(listOfMoves[0]); b->move(listOfMoves[0]);
@ -86,58 +84,53 @@ void Engine::AI(int depth){
b->displayBoard(); b->displayBoard();
} }
MNode* Engine::createMMTree(MNode* node, int depth){ void Engine::createMMTree(MNode* node, int depth){
MNode* temp;
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){ if (depth >= 0){
for (int i = 0; i < children.size(); ++i){ for (int i = 0; i < listOfMoves.size(); ++i){
cout << "i: " << i << "\n"; if(current.getPiece(8 - listOfMoves[i].row, listOfMoves[i].column)->getType() ==
node->addChild(createMMTree(children[i], --depth)); 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; return;
/*
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<moves> listOfMoves = temp->viewPossibleMoves();
minMax(temp, listOfMoves[0], 0, 0);
return m;
}
*/
} }
moves Engine::evaluateMMTree(MNode* node){ moves Engine::evaluateMMTree(MNode* node){
//returns the move from children that maximizes evaluate() //returns the move from children that maximizes evaluate()
MNode* temp;
moves m; moves m;
int max = INT_MIN;
int val;
vector<MNode*> children = node->getChildren(); vector<MNode*> children = node->getChildren();
for (auto &c : children){ 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 //return sum of eval values
//pass in each child of root and 0 //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(); sum += node->getMMVal();
vector<MNode*> children = node->getChildren(); vector<MNode*> children = node->getChildren();
@ -147,4 +140,3 @@ int Engine::evaluateMMBranch(Node* node, int sum){
return sum; return sum;
} }
*/

View file

@ -14,6 +14,7 @@ public:
void startGame(); void startGame();
void easyAI(); void easyAI();
void AI(int depth); void AI(int depth);
MNode* createMMTree(MNode* node, int depth); void createMMTree(MNode* node, int depth);
moves evaluateMMTree(MNode* node); moves evaluateMMTree(MNode* node);
int evaluateMMBranch(MNode* node, int sum);
}; };

View file

@ -4,30 +4,8 @@ using namespace std;
int main() int main()
{ {
MNode* a = new MNode(); Engine e;
MNode* b = new MNode(); e.startGame();
MNode* c = new MNode();
MNode* d = new MNode();
a->setMMVal(1); //COMPILES BUT EVERYTHING IS PRINTING FIRST MOVE
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();
} }