diff --git a/Board.cpp b/Board.cpp index fbdf90c..27cfae6 100755 --- a/Board.cpp +++ b/Board.cpp @@ -5,21 +5,42 @@ using namespace std; Board::Board() { + Piece* temp; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 8; ++j) { - xpieces.push_back(new Piece(i, j, 'X')); - pieces.push_back(new Piece(i, j, 'X')); + temp = new Piece(i, j, 'X'); + xpieces.push_back(temp); + pieces.push_back(temp); } } for (int i = 6; i < 8; ++i) { for (int j = 0; j < 8; ++j) { - opieces.push_back(new Piece(i, j, 'O')); - pieces.push_back(new Piece(i, j, 'O')); + temp = new Piece(i, j, 'O'); + opieces.push_back(temp); + pieces.push_back(temp); } } } +Board::Board(const Board& b) { + vector xp = b.getXPieces(); + vector 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! bool Board::isPiece(int r, int c){ for (int i = 0; i < pieces.size(); ++i){ @@ -92,7 +113,7 @@ void Board::changeTurns(){ if (turn == 'O') turn = 'X'; else turn = 'O'; } - + void Board::displayBoard(){ /* cout << "Debugging:\n"; @@ -190,13 +211,13 @@ char Board::intToCharColumn(int input){ } void Board::move(string inputMove){ - moves jugada = parse(inputMove); - move(jugada); + moves m = parse(inputMove); + move(m); } -void Board::move(moves jugada){ - int row = 8 - (jugada.row); - int column = charToIntColumn(jugada.column); +void Board::move(moves m){ + int row = 8 - (m.row); + int column = charToIntColumn(m.column); //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"; } - else { + else { if (jugada.moveType == "FWD") { if(piece->getType() == 'O') { row--; @@ -234,9 +255,21 @@ void Board::move(moves jugada){ else { 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 if(isPiece(row--, column--)) { 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 if(isPiece(row--, column++)) { if(getPiece(row--, column++)->getType() != piece->getType()) { @@ -290,25 +323,25 @@ bool Board::isThisMovePossible(int r, int c, string moveType){ if (piece->getType() == 'O') reflector *= -1; - if (moveType == "FWD"){ - if (isPiece(r+reflector, c)) - return false; - else + if (moveType == "FWD"){ + if (!isPiece(r + reflector, c)) return true; + else + return false; } - else if (moveType == "RIGHT"){ - if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) - return false; - else + else if (moveType == "RIGHT"){ + if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) return true; + else + return false; } - else if (moveType == "LEFT"){ - if (isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0)) - return false; - else + else if (moveType == "LEFT"){ + if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0)) return true; + else + return false; } else return false; @@ -343,7 +376,7 @@ vector Board::viewPossibleMoves(){ } } - else { + else if (turn == 'X') { for (int i = 0; i < opieces.size(); ++i){ r = opieces[i]->getX(); c = opieces[i]->getY(); diff --git a/Board.h b/Board.h index 69bfe68..2c5a890 100755 --- a/Board.h +++ b/Board.h @@ -19,17 +19,18 @@ struct moves { }; class Board { - //vector> boardArray; vector xpieces; vector opieces; vector pieces; - //char boardArray [8][8]; char turn = 'O'; public: Board(); + Board(const Board& b); bool isPiece(int r, int c); Piece* getPiece(int r, int c); + vector getXPieces() const { return xpieces; } + vector getOPieces() const { return opieces; } moves parse(string input); char getTurn() { return turn; } bool isGameOver(); diff --git a/Engine.cpp b/Engine.cpp index 9e12925..2ed7a7f 100755 --- a/Engine.cpp +++ b/Engine.cpp @@ -45,7 +45,7 @@ void Engine::startGame(){ while(b->getTurn() == 'X' ) { - easyAI(); + AI(); } gameOver = b->isGameOver(); @@ -62,6 +62,7 @@ void Engine::easyAI() 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->changeTurns(); } @@ -69,12 +70,12 @@ void Engine::easyAI() void Engine::AI(){ cout << "----------------------BEGIN AI FUNCTION----------------------\n"; vector listOfMoves = b->viewPossibleMoves(); - Board temp = *b; + Board* temp = new Board(*b); //probably not needed, check later - if (temp.getTurn() != 'X'){ + if (temp->getTurn() != 'X'){ cout << "a changing of turns is needed. \n"; - temp.changeTurns(); + temp->changeTurns(); } //only doing 1 branch right now because testing @@ -82,7 +83,8 @@ void Engine::AI(){ minMax(temp, listOfMoves[i]); }*/ - //b->moveWOPrint(minMax(temp, listOfMoves[0], 0)); + b->move(minMax(temp, listOfMoves[0], 0)); + b->changeTurns(); //verification of correct turn if (b->getTurn() != 'O'){ @@ -93,30 +95,32 @@ void Engine::AI(){ cout << "----------------------END AI FUNCTION----------------------\n"; } -moves Engine::minMax(Board temp, moves m, int c){ - //testing purposes only - if (c > 5){ +moves Engine::minMax(Board* temp, moves m, int c){ + //testing purposes only, c = finite depth + if (c > 5){ return m; } - if (temp.isGameOver() == true){ + if (temp->isGameOver() == true){ cout << "END OF PATH REACHED\n"; return m; } 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"; - //temp.moveWOPrint(m); + temp->move(m); + temp->changeTurns(); } cout << "c: " << c << "\n\n"; - cout << "current turn: " << temp.getTurn() << "\n"; - vector listOfMoves = temp.viewPossibleMoves(); + cout << "current turn: " << temp->getTurn() << "\n"; + vector listOfMoves = temp->viewPossibleMoves(); for (int i = 0; i < listOfMoves.size(); ++i){ //return minMax(temp, listOfMoves[i]); } + temp->displayBoard(); //limited recursion return minMax(temp, listOfMoves[0], ++c); diff --git a/Engine.h b/Engine.h index 463d05c..97efbf8 100755 --- a/Engine.h +++ b/Engine.h @@ -12,5 +12,5 @@ public: void startGame(); void easyAI(); void AI(); - moves minMax(Board temp, moves m, int c); + moves minMax(Board* temp, moves m, int c); }; \ No newline at end of file diff --git a/test.cpp b/test.cpp index 2851e14..88d7f86 100755 --- a/test.cpp +++ b/test.cpp @@ -7,8 +7,7 @@ int main() { //board testing Board b; - cout << b.boardToString(); - + //engine testing Engine e; e.startGame();