minmax structure done

This commit is contained in:
Rebecca Schofield 2015-10-29 08:58:32 -05:00
parent da2a1aec2b
commit b1b340e6d2
7 changed files with 118 additions and 46 deletions

View file

@ -250,7 +250,7 @@ void Board::move(string inputMove){
move(m); move(m);
} }
void Board::move(moves m){ Board Board::move(moves m){
int row = 8 - (m.row); int row = 8 - (m.row);
int column = m.column; int column = m.column;
@ -258,7 +258,7 @@ void Board::move(moves m){
if (row > 8 || row < 0 || column > 8 || column < 0) { if (row > 8 || row < 0 || column > 8 || column < 0) {
cout<<"ERROR: index out of bound."<<endl; cout<<"ERROR: index out of bound."<<endl;
return; return *this;
} }
Piece* piece; Piece* piece;
@ -268,7 +268,7 @@ void Board::move(moves m){
else{ else{
cout<<"ERROR: attempting to move an invalid piece.\n"; cout<<"ERROR: attempting to move an invalid piece.\n";
return; return *this;
} }
//cout << piece->getX() << piece->getY() << "\n\n"; //cout << piece->getX() << piece->getY() << "\n\n";
if (piece->getType() != turn) { if (piece->getType() != turn) {
@ -277,9 +277,9 @@ void Board::move(moves m){
else { else {
if(isThisMovePossible(row, column, m.moveType)) if(isThisMovePossible(row, column, m.moveType))
{ {
if (m.moveType == "FWD") { if (m.moveType == "FWD") {
piece->moveFwd(); piece->moveFwd();
} }
else if (m.moveType == "LEFT") { else if (m.moveType == "LEFT") {
@ -334,6 +334,8 @@ void Board::move(moves m){
setValidFalse(); setValidFalse();
} }
} }
return *this;
} }
bool Board::isThisMovePossible(int r, int c, string moveType){ bool Board::isThisMovePossible(int r, int c, string moveType){
@ -355,16 +357,16 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
if (piece->getType() == 'O') if (piece->getType() == 'O')
reflector = -1; reflector = -1;
if (moveType == "FWD"){ if (moveType == "FWD"){
if (!isPiece(r + reflector, c)) if (!isPiece(r + reflector, c))
return true; return true;
else else
return false; return false;
} }
else if (moveType == "RIGHT"){ else if (moveType == "RIGHT"){
temp = getPiece(r + reflector, c+1); temp = getPiece(r + reflector, c+1);
if(c < 7) { if(c < 7) {
if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) { if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) {
//cout << "What.\n\n"; //cout << "What.\n\n";
return true; return true;
@ -379,14 +381,14 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
return false; return false;
} }
} }
else { else {
return false; return false;
} }
} }
else if (moveType == "LEFT"){ else if (moveType == "LEFT"){
temp = getPiece(r + reflector, c-1); temp = getPiece(r + reflector, c-1);
if(c > 0) { if(c > 0) {
if (!isPiece(r+reflector, c-1) && (r+reflector >= 0) && (r+reflector <= 7)) { if (!isPiece(r+reflector, c-1) && (r+reflector >= 0) && (r+reflector <= 7)) {
//cout << "What.\n\n"; //cout << "What.\n\n";
return true; return true;
@ -401,7 +403,7 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
return false; return false;
} }
} }
else { else {
return false; return false;
} }
} }
@ -549,4 +551,4 @@ int Board::evaluate(char max, char min){
return 0; return 0;
} }
} }

11
Board.h
View file

@ -10,6 +10,12 @@ struct moves {
int row; int row;
int column; int column;
string moveType; string moveType;
moves() {
row = -1;
column = -1;
moveType = "";
}
moves(int linea, int columna, string m) { moves(int linea, int columna, string m) {
row = linea; row = linea;
@ -47,8 +53,7 @@ public:
int charToIntColumn(char input); int charToIntColumn(char input);
char intToCharColumn(int input); char intToCharColumn(int input);
void move(string inputMove); void move(string inputMove);
void move(moves jugada); Board move(moves m);
void moveWOPrint(moves jugada);
bool isThisMovePossible(int r, int c, string moveType); bool isThisMovePossible(int r, int c, string moveType);
vector<moves> viewPossibleMoves(); vector<moves> viewPossibleMoves();
string myToUpper(string input); string myToUpper(string input);
@ -56,4 +61,4 @@ public:
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, char min);
}; };

View file

@ -43,7 +43,8 @@ void Engine::startGame(){
} }
while(b->getTurn() == 'X' ){ while(b->getTurn() == 'X' ){
AI(); easyAI();
//AI(3);
} }
gameOver = b->isGameOver(); gameOver = b->isGameOver();
@ -64,10 +65,9 @@ void Engine::easyAI(){
} }
void Engine::AI(int depth){ void Engine::AI(int depth){
vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = new Board(*b); Board* temp = new Board(*b);
moves empty;
//create node with current board state MNode* root = new MNode(*temp, empty, 0);
//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,33 +75,23 @@ 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();
//verification of correct turn
/*
if (b->getTurn() != 'O'){
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
}
*/
b->changeTurns();
b->displayBoard(); b->displayBoard();
} }
moves Engine::minMax(Board* temp, moves m, int c, int r){ moves Engine::minMax(MNode* node, int depth){
//testing purposes only, c = finite depth Board current = node->getState();
//cout << "c: " << c << "\n\n"; vector<moves> listOfMoves = current.viewPossibleMoves();
//cout << "current turn: " << temp->getTurn() << "\n"; /*
vector<moves> listOfMoves = temp->viewPossibleMoves(); if (current.isGameOver() == true){
if (temp->isGameOver() == true){
cout << "END OF PATH REACHED\n\n"; cout << "END OF PATH REACHED\n\n";
return m; return m;
} }
else { else {
if(temp->getPiece(8 - m.row, m.column)->getType() == temp->getTurn() && temp->isThisMovePossible(8 - m.row, m.column, m.moveType)){ if(current.getPiece(8 - m.row, m.column)->getType() == current.getTurn() && current.isThisMovePossible(8 - m.row, m.column, m.moveType)){
temp->move(m); temp->move(m);
temp->evaluate('X', 'O'); temp->evaluate('X', 'O');
temp->changeTurns(); temp->changeTurns();
@ -114,4 +104,5 @@ moves Engine::minMax(Board* temp, moves m, int c, int r){
minMax(temp, listOfMoves[0], 0, 0); minMax(temp, listOfMoves[0], 0, 0);
return m; return m;
} }
} */
}

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Board.h" #include "Board.h"
#include "MNode.h"
using namespace std; using namespace std;
@ -13,5 +14,5 @@ public:
void startGame(); void startGame();
void easyAI(); void easyAI();
void AI(int depth); void AI(int depth);
moves minMax(Board* temp, moves m, int c, int r); moves minMax(MNode* node, int depth);
}; };

43
MNode.cpp Executable file
View file

@ -0,0 +1,43 @@
#include <iostream>
#include <vector>
#include "MNode.h"
using namespace std;
MNode::MNode(){
children.clear();
minimax_val = -1;
}
MNode::MNode(Board s, moves m, int mmval){
state = s;
mvs = m;
minimax_val = mmval;
}
MNode::MNode(const MNode& n){
children = n.getChildren();
minimax_val = n.getMMVal();
}
void MNode::setMMVal(int mmval) {
minimax_val = mmval;
}
bool MNode::hasChildren(){
if (children.size() != 0)
return true;
else
return false;
}
void printTree(int depth, const MNode& n){
vector<MNode*> children;
cout << "depth " << depth << " :" << n.getMMVal() << " ";
children = n.getChildren();
//print out root
for (int i = 0; i < children.size(); ++i){
printTree(++depth, *children[i]);
}
}

30
MNode.h Executable file
View file

@ -0,0 +1,30 @@
#pragma once
#include <iostream>
#include <vector>
#include "Board.h"
using namespace std;
class MNode {
vector<MNode*> children;
Board state;
moves mvs;
int minimax_val;
public:
MNode();
MNode(Board s, moves m, int mmval);
MNode(const MNode& n);
vector<MNode*> getChildren() const { return children; }
void addChild(MNode* n) { children.push_back(n); }
int getMMVal() const { return minimax_val; }
void setMMVal(int mmval);
Board getState() { return state; }
void setState(Board s) { state = s; }
moves getMove() { return mvs; }
void setMove(moves m) { mvs = m; }
bool hasChildren();
};
void printTree(int depth, const MNode& n);

View file

@ -3,13 +3,13 @@
all: test server all: test server
server: Server.o server: Server.o
g++ -std=c++11 -o server Server.o Engine.o Piece.o Board.o g++ -std=c++11 -o server Server.o Engine.o Piece.o Board.o MNode.o
Server.o: Server.cpp Engine.cpp Board.cpp Piece.cpp Server.o: Server.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp
g++ -std=c++11 -c -g Server.cpp Engine.cpp Board.cpp Piece.cpp g++ -std=c++11 -c -g Server.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp
test: test.o test: test.o
g++ -std=c++11 -o test test.o Engine.o Piece.o Board.o g++ -std=c++11 -o test test.o Engine.o Piece.o Board.o MNode.o
test.o: test.cpp Engine.cpp Board.cpp Piece.cpp test.o: test.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp
g++ -std=c++11 -c -g test.cpp Engine.cpp Board.cpp Piece.cpp g++ -std=c++11 -c -g test.cpp Engine.cpp Board.cpp Piece.cpp MNode.cpp