Merge branch 'beccadev'

This commit is contained in:
Rebecca Schofield 2015-11-02 16:21:38 -06:00
commit 63c2b52b5a
9 changed files with 219 additions and 88 deletions

100
Board.cpp
View file

@ -1,4 +1,7 @@
#include <ctime>
#include <cstdlib>
#include <iostream> #include <iostream>
#include <limits.h>
#include <vector> #include <vector>
#include "Board.h" #include "Board.h"
@ -6,7 +9,7 @@ using namespace std;
Board::Board() { Board::Board() {
Piece* temp; Piece* temp;
bool valid = false;
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 8; ++j) { for (int j = 0; j < 8; ++j) {
temp = new Piece(i, j, 'X'); temp = new Piece(i, j, 'X');
@ -22,14 +25,15 @@ Board::Board() {
pieces.push_back(temp); pieces.push_back(temp);
} }
} }
valid, xtaken, otaken = false;
} }
Board::Board(const Board& b) { Board::Board(const Board& b) {
vector<Piece*> xp = b.getXPieces(); vector<Piece*> xp = b.getTypePieces('X');
vector<Piece*> op = b.getOPieces(); vector<Piece*> op = b.getTypePieces('O');
Piece* temp; Piece* temp;
//bool valid = false; char tempturn = b.getTurn();
char tempturn = b.getTurnPls();
turn = tempturn; turn = tempturn;
for (int i = 0; i < xp.size(); ++i) { for (int i = 0; i < xp.size(); ++i) {
@ -43,6 +47,8 @@ Board::Board(const Board& b) {
opieces.push_back(temp); opieces.push_back(temp);
pieces.push_back(temp); pieces.push_back(temp);
} }
valid, xtaken, otaken = false;
} }
//make this efficient! //make this efficient!
@ -74,6 +80,8 @@ void Board::isTaken(int r, int c) {
for(int x = 0; x < opieces.size(); ++x) { for(int x = 0; x < opieces.size(); ++x) {
if (opieces[x]->getX() == r && opieces[x]->getY() == c) { if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
opieces.erase(opieces.begin() + x); opieces.erase(opieces.begin() + x);
cout << "otaken set to TRUE\n";
otaken = true;
} }
} }
} }
@ -81,6 +89,8 @@ void Board::isTaken(int r, int c) {
for(int x = 0; x < xpieces.size(); ++x) { for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) { if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x); xpieces.erase(xpieces.begin() + x);
cout << "xtaken set to TRUE\n";
xtaken = true;
} }
} }
} }
@ -90,6 +100,30 @@ void Board::isTaken(int r, int c) {
} }
} }
bool Board::checkTaken(char c){
if (c == 'X'){
return xtaken;
}
else if (c == 'O'){
return otaken;
}
else
return false;
}
vector<Piece*> Board::getTypePieces(char type) const{
if (type == 'X')
return xpieces;
else if (type == 'O')
return opieces;
else{
cout << "Invalid type!\n";
return pieces;
}
}
moves Board::parse(string input){ moves Board::parse(string input){
input = myToUpper(input); input = myToUpper(input);
@ -136,20 +170,21 @@ bool Board::isGameOver(){
return false; return false;
} }
string Board::whoWon(){ char Board::whoWon(){
for (int i = 0; i < xpieces.size(); ++i){ for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){ if (xpieces[i]->getX() == 7){
return "Player X wins!"; return 'X';
} }
} }
for (int i = 0; i < opieces.size(); ++i){ for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){ if (opieces[i]->getX() == 0){
return "Player O wins!"; return 'O';
} }
} }
return "ERROR: function whoWon() called incorrectly."; cout << "ERROR: function whoWon() called incorrectly. Game is not over. \n";
return 'a';
} }
void Board::changeTurns(){ void Board::changeTurns(){
@ -157,6 +192,11 @@ void Board::changeTurns(){
else turn = 'O'; else turn = 'O';
} }
void Board::resetTaken(){
xtaken = false;
otaken = false;
}
void Board::displayBoard(){ void Board::displayBoard(){
cout << "; A B C D E F G H"<<endl; cout << "; A B C D E F G H"<<endl;
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
@ -246,7 +286,6 @@ char Board::intToCharColumn(int input){
void Board::move(string inputMove){ void Board::move(string inputMove){
moves m = parse(inputMove); moves m = parse(inputMove);
//cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
move(m); move(m);
} }
@ -482,8 +521,7 @@ string Board::myToUpper(string input){
else output.push_back(input[i]); else output.push_back(input[i]);
} }
return output; return output;
} }
void Board::undo(Board& tablero){ void Board::undo(Board& tablero){
@ -537,18 +575,36 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
} }
int Board::evaluate(char max, char min){ int Board::evaluate(char max, char min){
//right now just evaluating number of pieces vector<Piece*> maxPieces = getTypePieces(max);
if (max == 'X'){ vector<Piece*> minPieces = getTypePieces(min);
return (xpieces.size() - opieces.size()); int reflector, val, x, y = 0;
Piece* temp;
val += 2 * (maxPieces.size() - minPieces.size());
cout << (checkTaken(max)) << "\n";
cout << (checkTaken(min)) << "\n";
//check for taken conditions
if (checkTaken(min)){
cout << "adding 10 to val\n";
val += 10;
} }
else if (max == 'O'){ if (checkTaken(max)){
return (opieces.size() - xpieces.size()); cout << "subtracting 10 from val\n";
val -= 10;
} }
else { //ultimate condition!
cout << "Error in evaluate: unidentified max, must be either 'X' or 'O'.\n"; if (isGameOver()){
return 0; if (whoWon() == max)
val = INT_MAX / 2;
else
val = INT_MIN / 2;
} }
cout << "val: " << val << "\n";
return val;
} }

13
Board.h
View file

@ -28,8 +28,8 @@ class Board {
vector<Piece*> xpieces; vector<Piece*> xpieces;
vector<Piece*> opieces; vector<Piece*> opieces;
vector<Piece*> pieces; vector<Piece*> pieces;
bool valid, xtaken, otaken;
char turn = 'O'; char turn = 'O';
bool valid = false;
public: public:
Board(); Board();
@ -40,14 +40,15 @@ public:
bool isPiece(int r, int c); bool isPiece(int r, int c);
Piece* getPiece(int r, int c); Piece* getPiece(int r, int c);
void isTaken(int r, int c); void isTaken(int r, int c);
vector<Piece*> getXPieces() const { return xpieces; } bool checkTaken(char c);
vector<Piece*> getOPieces() const { return opieces; } vector<Piece*> getPieces() const { return pieces; }
char getTurnPls() const { return turn; } vector<Piece*> getTypePieces(char type) const;
char getTurn() const { return turn; }
moves parse(string input); moves parse(string input);
char getTurn() { return turn; }
bool isGameOver(); bool isGameOver();
string whoWon(); char whoWon();
void changeTurns(); void changeTurns();
void resetTaken();
void displayBoard(); void displayBoard();
string boardToString(); string boardToString();
int charToIntColumn(char input); int charToIntColumn(char input);

View file

@ -2,6 +2,9 @@
#include <vector> #include <vector>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include "Engine.h" #include "Engine.h"
Engine::Engine(){ Engine::Engine(){
@ -13,13 +16,24 @@ void Engine::startGame(){
cout<<"WELCOME\n"; cout<<"WELCOME\n";
cout<<"1. Play against AI?\n"; cout<<"1. Play against AI?\n";
cout<<"2. Play against a human?\n"; cout<<"2. Watch AI vs AI?\n";
cout<<"Enter choice: \n"; cout<<"Enter choice: \n";
int choice = -1; int choice = -1;
cin >> choice; cin >> choice;
cout << "OK" << endl; cout << "OK" << endl;
if (choice == 1)
userGame();
else if (choice == 2)
AIGame();
else {
cout << "Please enter a valid choice.\n";
startGame();
}
}
void Engine::userGame(){
string move; string move;
bool gameOver = false; bool gameOver = false;
@ -38,13 +52,14 @@ void Engine::startGame(){
if(b->isValid()){ if(b->isValid()){
b->changeTurns(); b->changeTurns();
b->resetTaken();
b->setValidFalse(); b->setValidFalse();
} }
} }
while(b->getTurn() == 'X' ){ while(b->getTurn() == 'X' ){
easyAI(); //easyAI();
//AI(3); AI(3);
} }
gameOver = b->isGameOver(); gameOver = b->isGameOver();
@ -58,55 +73,114 @@ void Engine::startGame(){
cout << "Game over. " + s + " wins!\n\n"; cout << "Game over. " + s + " wins!\n\n";
} }
void Engine::easyAI(){ void Engine::AIGame(){
vector<moves> listOfMoves = b->viewPossibleMoves(); bool gameOver = false;
srand(time(NULL)); while (gameOver != true){
int randomChoice = rand() % (listOfMoves.size()-1) - 0; gameOver = b->isGameOver();
b->move(listOfMoves[randomChoice]); while(b->getTurn() == 'O'){
b->changeTurns(); AI(3);
}
b->displayBoard();
sleep(1);
while(b->getTurn() == 'X' ){
AI(3);
}
gameOver = b->isGameOver();
}
} }
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);
createMMTree(root, depth, 1);
m = evaluateMMTree(root);
//only doing 1 branch right now because testing printTree(0, root);
/*for (int i = 0; i < listOfMoves.size(); ++i){
minMax(b, listOfMoves[i]); b->move(m);
}*/ b->changeTurns();
b->resetTaken();
//remove this soon
//b->move(minMax(temp, listOfMoves[0], 0, 0));
b->changeTurns();
b->displayBoard();
} }
moves Engine::minMax(MNode* node, int depth){ void Engine::createMMTree(MNode* node, int depth, int alt){
MNode* temp;
char max, min;
Board current = node->getState(); Board current = node->getState();
vector<moves> listOfMoves = current.viewPossibleMoves(); vector<moves> listOfMoves = current.viewPossibleMoves();
/*
if (current.isGameOver() == true){ if (depth >= 0){
cout << "END OF PATH REACHED\n\n"; for (int i = 0; i < listOfMoves.size(); ++i){
return m; 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.resetTaken();
current = current.move(listOfMoves[i]);
max = current.getTurn();
current.changeTurns();
min = current.getTurn();
if (current.checkTaken('X'))
cout << "xtaken true\n";
if (current.checkTaken('O'))
cout << "otaken true\n";
temp = new MNode(current, listOfMoves[i], current.evaluate(max, min));
if (alt == 1)
temp->setType("max");
else if (alt == 1)
temp->setType("min");
current.resetTaken();
node->addChild(temp);
createMMTree(temp, --depth, alt * -1);
current.changeTurns();
}
}
} }
else { return;
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){
//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){
val = evaluateMMBranch(c, INT_MIN);
if(val > max){
temp = c;
max = val;
}
}
return temp->getMove();
}
int Engine::evaluateMMBranch(MNode* node, int max){
if (node->getMMVal() > max)
max = node->getMMVal();
vector<MNode*> children = node->getChildren();
for (auto &c : children){
evaluateMMBranch(c, max);
}
//cout << "max: " << max << "\n";
return max;
}

View file

@ -12,7 +12,10 @@ public:
Engine(); Engine();
Board* getBoard() { return b; } Board* getBoard() { return b; }
void startGame(); void startGame();
void easyAI(); void userGame();
void AIGame();
void AI(int depth); void AI(int depth);
moves minMax(MNode* node, int depth); void createMMTree(MNode* node, int depth, int alt);
moves evaluateMMTree(MNode* node);
int evaluateMMBranch(MNode* node, int sum);
}; };

View file

@ -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() << "\n";
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]);
} }
} }

View file

@ -11,6 +11,7 @@ class MNode {
Board state; Board state;
moves mvs; moves mvs;
int minimax_val; int minimax_val;
string type; //min or max
public: public:
MNode(); MNode();
@ -20,11 +21,13 @@ 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; }
string getType() { return type; }
void setType(string t) { type = t; }
bool hasChildren(); bool hasChildren();
}; };
void printTree(int depth, const MNode& n); void printTree(int depth, MNode* n);

View file

@ -63,7 +63,3 @@ void Piece::moveRight(){
else else
cout << "Error: trying to move an empty piece left."; cout << "Error: trying to move an empty piece left.";
} }
void Piece::isTaken(){
cout << getX() << " " << getY() << "\n\n";
}

View file

@ -20,5 +20,4 @@ public:
void setY(int c){ y = c; } void setY(int c){ y = c; }
char getType(){ return type; } char getType(){ return type; }
void makeEmpty(){ type = '_'; } void makeEmpty(){ type = '_'; }
void isTaken(); };
};

View file

@ -4,7 +4,6 @@ using namespace std;
int main() int main()
{ {
//Board b;
Engine e; Engine e;
e.startGame(); e.startGame();
} }