compiles, but not correct
This commit is contained in:
parent
5b9fc9393b
commit
5f4c6c2539
5 changed files with 58 additions and 80 deletions
17
Board.cpp
17
Board.cpp
|
@ -1,3 +1,5 @@
|
|||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Board.h"
|
||||
|
@ -536,19 +538,24 @@ void Board::snapshot(vector<Board>& 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;
|
||||
}
|
||||
|
||||
|
|
2
Board.h
2
Board.h
|
@ -60,5 +60,5 @@ public:
|
|||
void undo(Board& tablero);
|
||||
void interpret(string input, Board& tablero);
|
||||
void snapshot(vector<Board>& inputVec, Board inputBoard);
|
||||
int evaluate(char max, char min);
|
||||
int evaluate(char max);
|
||||
};
|
||||
|
|
84
Engine.cpp
84
Engine.cpp
|
@ -2,6 +2,7 @@
|
|||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#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<moves> 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<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));
|
||||
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<moves> 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<MNode*> 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<MNode*> children = node->getChildren();
|
||||
|
||||
|
@ -147,4 +140,3 @@ int Engine::evaluateMMBranch(Node* node, int sum){
|
|||
|
||||
return sum;
|
||||
}
|
||||
*/
|
3
Engine.h
3
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);
|
||||
};
|
||||
|
|
28
test.cpp
28
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
|
||||
}
|
||||
|
|
Reference in a new issue