Merge branch 'beccadev' into alex

This commit is contained in:
Alexander Huddleston 2015-10-29 09:41:45 -05:00
commit 2b011140b8
8 changed files with 189 additions and 171 deletions

153
Board.cpp
View file

@ -45,18 +45,6 @@ Board::Board(const Board& b) {
} }
} }
void Board::setValidFalse() {
valid = false;
}
void Board::setValidTrue() {
valid = true;
}
bool Board::isValid() {
return valid;
}
//make this efficient! //make this efficient!
bool Board::isPiece(int r, int c){ bool Board::isPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){ for (int i = 0; i < pieces.size(); ++i){
@ -68,31 +56,6 @@ bool Board::isPiece(int r, int c){
return false; return false;
} }
void Board::isTaken(int r, int c) {
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
if(pieces[i]->getType() == 'O') {
for(int x = 0; x < opieces.size(); ++x) {
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
opieces.erase(opieces.begin() + x);
//break;
}
}
}
else {
for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x);
//break;
}
}
}
pieces.erase(pieces.begin() + i);
break;
}
}
}
//make this efficient! //make this efficient!
Piece* Board::getPiece(int r, int c){ Piece* Board::getPiece(int r, int c){
for (int i = 0; i < pieces.size(); ++i){ for (int i = 0; i < pieces.size(); ++i){
@ -104,6 +67,29 @@ Piece* Board::getPiece(int r, int c){
return new Piece(); return new Piece();
} }
void Board::isTaken(int r, int c) {
for (int i = 0; i < pieces.size(); ++i){
if (pieces[i]->getX() == r && pieces[i]->getY() == c){
if(pieces[i]->getType() == 'O') {
for(int x = 0; x < opieces.size(); ++x) {
if (opieces[x]->getX() == r && opieces[x]->getY() == c) {
opieces.erase(opieces.begin() + x);
}
}
}
else {
for(int x = 0; x < xpieces.size(); ++x) {
if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) {
xpieces.erase(xpieces.begin() + x);
}
}
}
pieces.erase(pieces.begin() + i);
break;
}
}
}
moves Board::parse(string input){ moves Board::parse(string input){
input = myToUpper(input); input = myToUpper(input);
@ -137,17 +123,33 @@ moves Board::parse(string input){
bool Board::isGameOver(){ bool Board::isGameOver(){
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){
cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl;
return true; return true;
} }
} }
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){
cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl;
return true; return true;
} }
} }
return false;
}
string Board::whoWon(){
for (int i = 0; i < xpieces.size(); ++i){
if (xpieces[i]->getX() == 7){
return "Player X wins!";
}
}
for (int i = 0; i < opieces.size(); ++i){
if (opieces[i]->getX() == 0){
return "Player O wins!";
}
}
return "ERROR: function whoWon() called incorrectly.";
} }
void Board::changeTurns(){ void Board::changeTurns(){
@ -156,15 +158,6 @@ void Board::changeTurns(){
} }
void Board::displayBoard(){ void Board::displayBoard(){
/*
cout << "Debugging:\n";
for (int i = 0; i < pieces.size(); ++i){
if(i%8 == 0)
cout << "\n";
cout << pieces[i]->getX() << " " << pieces[i]->getY() << " " << pieces[i]->getType() << "\t";
}
cout << "Debugging:\n\n";
*/
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) {
int label = 8 - i; int label = 8 - i;
@ -253,11 +246,11 @@ 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"; //cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
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;
@ -265,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;
@ -275,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) {
@ -284,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") {
@ -341,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){
@ -362,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;
@ -386,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;
@ -408,7 +403,7 @@ bool Board::isThisMovePossible(int r, int c, string moveType){
return false; return false;
} }
} }
else { else {
return false; return false;
} }
} }
@ -446,7 +441,6 @@ vector<moves> Board::viewPossibleMoves(){
} }
else if (turn == 'O') { else if (turn == 'O') {
//cout << "\nTESTING\n\n";
for (int i = 0; i < opieces.size(); ++i){ for (int i = 0; i < opieces.size(); ++i){
r = opieces[i]->getX(); r = opieces[i]->getX();
c = opieces[i]->getY(); c = opieces[i]->getY();
@ -483,7 +477,7 @@ string Board::myToUpper(string input){
if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122))
{ {
numeric = input[i] - 32; numeric = input[i] - 32;
output.push_back((char)numeric);// = 'Q';//(char) numeric; output.push_back((char)numeric);
} }
else output.push_back(input[i]); else output.push_back(input[i]);
} }
@ -495,17 +489,13 @@ string Board::myToUpper(string input){
void Board::undo(Board& tablero){ void Board::undo(Board& tablero){
vector<Board> record; vector<Board> record;
if (record.size() < 2) if (record.size() < 2){
{
cout<<"nothing to undo"<<endl; cout<<"nothing to undo"<<endl;
} }
else else{
{ for (int r = 0; r < 8; ++r){
for (int r = 0; r < 8; ++r) for (int k = 0; k < 8; ++k){
{
for (int k = 0; k < 8; ++k)
{
//tablero.modifyAt(r,k,(record[record.size()-2]).elementAt(r,k)); //tablero.modifyAt(r,k,(record[record.size()-2]).elementAt(r,k));
} }
} }
@ -517,34 +507,29 @@ void Board::undo(Board& tablero){
void Board::interpret(string input, Board& tablero){ void Board::interpret(string input, Board& tablero){
vector<Board> record; vector<Board> record;
input = myToUpper(input); input = myToUpper(input);
//cout << "MOVE: " << input << "\n\n";
if (input == "UNDO") if (input == "UNDO"){
{
undo(tablero); undo(tablero);
} }
else if (input == "DISPLAYRECORD") //for debugging purposes else if (input == "DISPLAYRECORD"){
{
cout<<"record: "<<endl; cout<<"record: "<<endl;
cout<<record.size(); cout<<record.size();
for (int i = 0; i < record.size(); ++i)
{ for (int i = 0; i < record.size(); ++i){
record[i].displayBoard(); record[i].displayBoard();
} }
cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl;
} }
else tablero.move(input); else tablero.move(input);
} }
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){ void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
if (inputVec.size() == 10) if (inputVec.size() == 10){
{
inputVec.erase(inputVec.begin()); inputVec.erase(inputVec.begin());
} }
else if (inputVec.size() > 10) else if (inputVec.size() > 10){
{
cout<<"QUEUE OVERFLOW!"<<endl; cout<<"QUEUE OVERFLOW!"<<endl;
} }
@ -562,8 +547,8 @@ int Board::evaluate(char max, char min){
} }
else { else {
cout << "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 0;
} }
} }

20
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;
@ -28,26 +34,26 @@ class Board {
public: public:
Board(); Board();
Board(const Board& b); Board(const Board& b);
void setValidFalse(); void setValidFalse(){ valid = false; }
void setValidTrue(); void setValidTrue(){ valid = true; }
bool isValid(); bool isValid(){ return valid; }
bool isPiece(int r, int c); bool isPiece(int r, int c);
void isTaken(int r, int c);
Piece* getPiece(int r, int c); Piece* getPiece(int r, int c);
void isTaken(int r, int c);
vector<Piece*> getXPieces() const { return xpieces; } vector<Piece*> getXPieces() const { return xpieces; }
vector<Piece*> getOPieces() const { return opieces; } vector<Piece*> getOPieces() const { return opieces; }
char getTurnPls() const { return turn; } char getTurnPls() const { return turn; }
moves parse(string input); moves parse(string input);
char getTurn() { return turn; } char getTurn() { return turn; }
bool isGameOver(); bool isGameOver();
string whoWon();
void changeTurns(); void changeTurns();
void displayBoard(); void displayBoard();
string boardToString(); string boardToString();
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);
@ -55,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

@ -26,28 +26,25 @@ void Engine::startGame(){
vector<Board> record; vector<Board> record;
b->snapshot(record, *b); b->snapshot(record, *b);
while (gameOver != true) while (gameOver != true){
{
gameOver = b->isGameOver(); gameOver = b->isGameOver();
while(b->getTurn() == 'O' && !b->isValid()) while(b->getTurn() == 'O' && !b->isValid()){
{
b->displayBoard(); b->displayBoard();
cout<<"\nEnter command: "; cout<<"\nEnter command: ";
cin>>move; cin>>move;
cout << "\n"; cout << "\n";
b->interpret(move, *b); b->interpret(move, *b);
if(b->isValid()) {
if(b->isValid()){
b->changeTurns(); b->changeTurns();
b->setValidFalse(); b->setValidFalse();
} }
} }
//if(b->isValid()) cout << b->getTurn(); while(b->getTurn() == 'X' ){
easyAI();
while(b->getTurn() == 'X' ) //AI(3);
{
AI();
} }
gameOver = b->isGameOver(); gameOver = b->isGameOver();
@ -57,88 +54,55 @@ void Engine::startGame(){
} }
} }
void Engine::easyAI() void Engine::easyAI(){
{
vector<moves> listOfMoves = b->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
/*
for(int x = 0; x < listOfMoves.size(); ++x) {
cout << listOfMoves[x].row << " " << listOfMoves[x].column << " " << listOfMoves[x].moveType << "\n\n";
}
*/
srand(time(NULL)); srand(time(NULL));
int randomChoice = rand() % (listOfMoves.size()-1) - 0; int randomChoice = rand() % (listOfMoves.size()-1) - 0;
//int temp = randomChoice;
cout << "easy AI move: " << listOfMoves[randomChoice].row << listOfMoves[randomChoice].column << listOfMoves[randomChoice].moveType << "\n";
b->move(listOfMoves[randomChoice]); b->move(listOfMoves[randomChoice]);
b->changeTurns(); b->changeTurns();
} }
void Engine::AI(){ void Engine::AI(int depth){
//cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves();
Board* temp = new Board(*b); Board* temp = new Board(*b);
moves empty;
MNode* root = new MNode(*temp, empty, 0);
//probably not needed, check later
/*
if (b->getTurn() != 'X'){
cout << "a changing of turns is needed. \n";
b->changeTurns();
}
*/
//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){
minMax(b, listOfMoves[i]); minMax(b, listOfMoves[i]);
}*/ }*/
b->move(minMax(temp, listOfMoves[0], 0, 0)); //remove this soon
b->changeTurns(); //b->move(minMax(temp, listOfMoves[0], 0, 0));
//verification of correct turn b->changeTurns();
/*
if (b->getTurn() != 'O'){
cout << "ERROR in Engine::AI: b is on the wrong turn. \n";
}
*/
b->displayBoard(); b->displayBoard();
//cout << "----------------------END AI FUNCTION----------------------\n";
} }
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();
//cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
/* /*
if (c > 5){ if (current.isGameOver() == true){
return m; cout << "END OF PATH REACHED\n\n";
}
*/
if (temp->isGameOver() == true){
//cout << "END OF PATH REACHED\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)){
//cout << "piece has been moved in minMax\n";
temp->move(m); temp->move(m);
temp->evaluate('X', 'O'); temp->evaluate('X', 'O');
temp->changeTurns(); temp->changeTurns();
//temp->displayBoard();
} }
else { else {
//cout << m.row << " " << m.column << "\n\n";
m = minMax(temp, listOfMoves[++r], c, r); m = minMax(temp, listOfMoves[++r], c, r);
} }
//temp->displayBoard();
vector<moves> listOfMoves = temp->viewPossibleMoves(); vector<moves> listOfMoves = temp->viewPossibleMoves();
//cout << "listOfMoves size: " << listOfMoves.size() << "\n\n";
minMax(temp, listOfMoves[0], 0, 0); minMax(temp, listOfMoves[0], 0, 0);
//testing
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;
@ -9,9 +10,9 @@ class Engine {
public: public:
Engine(); Engine();
Board* getBoard() { return b; }
void startGame(); void startGame();
void easyAI(); void easyAI();
void AI(); void AI(int depth);
Board* getBoard() { return b; } moves minMax(MNode* node, int depth);
moves minMax(Board* temp, moves m, int c, int r); };
};

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

View file

@ -6,16 +6,5 @@ int main()
{ {
//Board b; //Board b;
Engine e; Engine e;
/*
>>>>>>> master
int pn = 0;
cout << "Welcome to Breakthrough server launcher, please enter a host port number: \n";
cin >> pn;
cin.clear();
cin.ignore(10000,'\n');
<<<<<<< HEAD
e.startGame(pn);
=======
*/
e.startGame(); e.startGame();
} }