This commit is contained in:
Alexander Huddleston 2015-10-27 14:55:48 -05:00
commit 9fe535b10c
5 changed files with 81 additions and 44 deletions

View file

@ -5,21 +5,42 @@
using namespace std; using namespace std;
Board::Board() { Board::Board() {
Piece* temp;
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) {
xpieces.push_back(new Piece(i, j, 'X')); temp = new Piece(i, j, 'X');
pieces.push_back(new Piece(i, j, 'X')); xpieces.push_back(temp);
pieces.push_back(temp);
} }
} }
for (int i = 6; i < 8; ++i) { for (int i = 6; i < 8; ++i) {
for (int j = 0; j < 8; ++j) { for (int j = 0; j < 8; ++j) {
opieces.push_back(new Piece(i, j, 'O')); temp = new Piece(i, j, 'O');
pieces.push_back(new Piece(i, j, 'O')); opieces.push_back(temp);
pieces.push_back(temp);
} }
} }
} }
Board::Board(const Board& b) {
vector<Piece*> xp = b.getXPieces();
vector<Piece*> op = b.getOPieces();
Piece* temp;
for (int i = 0; i < xp.size(); ++i) {
temp = new Piece(xp[i]->getX(), xp[i]->getY(), 'X');
xpieces.push_back(temp);
pieces.push_back(temp);
}
for (int i = 0; i < op.size(); ++i) {
temp = new Piece(op[i]->getX(), op[i]->getY(), 'O');
opieces.push_back(temp);
pieces.push_back(temp);
}
}
//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){
@ -92,7 +113,7 @@ void Board::changeTurns(){
if (turn == 'O') turn = 'X'; if (turn == 'O') turn = 'X';
else turn = 'O'; else turn = 'O';
} }
void Board::displayBoard(){ void Board::displayBoard(){
/* /*
cout << "Debugging:\n"; cout << "Debugging:\n";
@ -190,13 +211,13 @@ char Board::intToCharColumn(int input){
} }
void Board::move(string inputMove){ void Board::move(string inputMove){
moves jugada = parse(inputMove); moves m = parse(inputMove);
move(jugada); move(m);
} }
void Board::move(moves jugada){ void Board::move(moves m){
int row = 8 - (jugada.row); int row = 8 - (m.row);
int column = charToIntColumn(jugada.column); int column = charToIntColumn(m.column);
//cout << row << " " << column << "\n\n"; //cout << row << " " << column << "\n\n";
@ -219,7 +240,7 @@ void Board::move(moves jugada){
cout<<"ERROR: attempting to move the wrong side's piece.\n"; cout<<"ERROR: attempting to move the wrong side's piece.\n";
} }
else { else {
if (jugada.moveType == "FWD") { if (jugada.moveType == "FWD") {
if(piece->getType() == 'O') { if(piece->getType() == 'O') {
row--; row--;
@ -234,9 +255,21 @@ void Board::move(moves jugada){
else { else {
piece->moveFwd(); piece->moveFwd();
} }
/*
=======
if (!(isThisMovePossible(row, column, m.moveType))){
cout << "Unable to move: impossible move.\n";
//add a try again
return;
}
if (m.moveType == "FWD") {
piece->moveFwd();
>>>>>>> beccadev
*/
} }
else if (jugada.moveType == "LEFT") { else if (m.moveType == "LEFT") {
//add error checking //add error checking
if(isPiece(row--, column--)) { if(isPiece(row--, column--)) {
if(getPiece(row--, column--)->getType() != piece->getType()) { if(getPiece(row--, column--)->getType() != piece->getType()) {
@ -253,7 +286,7 @@ void Board::move(moves jugada){
} }
} }
else if (jugada.moveType == "RIGHT") { else if (m.moveType == "RIGHT") {
//add error checking //add error checking
if(isPiece(row--, column++)) { if(isPiece(row--, column++)) {
if(getPiece(row--, column++)->getType() != piece->getType()) { if(getPiece(row--, column++)->getType() != piece->getType()) {
@ -290,25 +323,25 @@ 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 false;
else
return true; return true;
else
return false;
} }
else if (moveType == "RIGHT"){ else if (moveType == "RIGHT"){
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))
return false;
else
return true; return true;
else
return false;
} }
else if (moveType == "LEFT"){ else if (moveType == "LEFT"){
if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0)) if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0))
return false;
else
return true; return true;
else
return false;
} }
else return false; else return false;
@ -343,7 +376,7 @@ vector<moves> Board::viewPossibleMoves(){
} }
} }
else { else if (turn == 'X') {
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();

View file

@ -19,17 +19,18 @@ struct moves {
}; };
class Board { class Board {
//vector<vector<char>> boardArray;
vector<Piece*> xpieces; vector<Piece*> xpieces;
vector<Piece*> opieces; vector<Piece*> opieces;
vector<Piece*> pieces; vector<Piece*> pieces;
//char boardArray [8][8];
char turn = 'O'; char turn = 'O';
public: public:
Board(); Board();
Board(const Board& b);
bool isPiece(int r, int c); bool isPiece(int r, int c);
Piece* getPiece(int r, int c); Piece* getPiece(int r, int c);
vector<Piece*> getXPieces() const { return xpieces; }
vector<Piece*> getOPieces() const { return opieces; }
moves parse(string input); moves parse(string input);
char getTurn() { return turn; } char getTurn() { return turn; }
bool isGameOver(); bool isGameOver();

View file

@ -45,7 +45,7 @@ void Engine::startGame(){
while(b->getTurn() == 'X' ) while(b->getTurn() == 'X' )
{ {
easyAI(); AI();
} }
gameOver = b->isGameOver(); gameOver = b->isGameOver();
@ -62,6 +62,7 @@ void Engine::easyAI()
int randomChoice = rand() % (listOfMoves.size()-1) - 0; int randomChoice = rand() % (listOfMoves.size()-1) - 0;
int temp = randomChoice; 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();
} }
@ -69,12 +70,12 @@ void Engine::easyAI()
void Engine::AI(){ void Engine::AI(){
cout << "----------------------BEGIN AI FUNCTION----------------------\n"; cout << "----------------------BEGIN AI FUNCTION----------------------\n";
vector<moves> listOfMoves = b->viewPossibleMoves(); vector<moves> listOfMoves = b->viewPossibleMoves();
Board temp = *b; Board* temp = new Board(*b);
//probably not needed, check later //probably not needed, check later
if (temp.getTurn() != 'X'){ if (temp->getTurn() != 'X'){
cout << "a changing of turns is needed. \n"; cout << "a changing of turns is needed. \n";
temp.changeTurns(); temp->changeTurns();
} }
//only doing 1 branch right now because testing //only doing 1 branch right now because testing
@ -82,7 +83,8 @@ void Engine::AI(){
minMax(temp, listOfMoves[i]); minMax(temp, listOfMoves[i]);
}*/ }*/
//b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); b->move(minMax(temp, listOfMoves[0], 0));
b->changeTurns();
//verification of correct turn //verification of correct turn
if (b->getTurn() != 'O'){ if (b->getTurn() != 'O'){
@ -93,30 +95,32 @@ void Engine::AI(){
cout << "----------------------END AI FUNCTION----------------------\n"; cout << "----------------------END AI FUNCTION----------------------\n";
} }
moves Engine::minMax(Board temp, moves m, int c){ moves Engine::minMax(Board* temp, moves m, int c){
//testing purposes only //testing purposes only, c = finite depth
if (c > 5){ if (c > 5){
return m; return m;
} }
if (temp.isGameOver() == true){ if (temp->isGameOver() == true){
cout << "END OF PATH REACHED\n"; cout << "END OF PATH REACHED\n";
return m; return m;
} }
else { else {
if(temp.isThisMovePossible(8 - m.row, temp.charToIntColumn(m.column), m.moveType)){ if(temp->isThisMovePossible(8 - m.row, temp->charToIntColumn(m.column), m.moveType)){
cout << "piece has been moved in minMax\n"; cout << "piece has been moved in minMax\n";
//temp.moveWOPrint(m); temp->move(m);
temp->changeTurns();
} }
cout << "c: " << c << "\n\n"; cout << "c: " << c << "\n\n";
cout << "current turn: " << temp.getTurn() << "\n"; cout << "current turn: " << temp->getTurn() << "\n";
vector<moves> listOfMoves = temp.viewPossibleMoves(); vector<moves> listOfMoves = temp->viewPossibleMoves();
for (int i = 0; i < listOfMoves.size(); ++i){ for (int i = 0; i < listOfMoves.size(); ++i){
//return minMax(temp, listOfMoves[i]); //return minMax(temp, listOfMoves[i]);
} }
temp->displayBoard();
//limited recursion //limited recursion
return minMax(temp, listOfMoves[0], ++c); return minMax(temp, listOfMoves[0], ++c);

View file

@ -12,5 +12,5 @@ public:
void startGame(); void startGame();
void easyAI(); void easyAI();
void AI(); void AI();
moves minMax(Board temp, moves m, int c); moves minMax(Board* temp, moves m, int c);
}; };

View file

@ -7,8 +7,7 @@ int main()
{ {
//board testing //board testing
Board b; Board b;
cout << b.boardToString();
//engine testing //engine testing
Engine e; Engine e;
e.startGame(); e.startGame();