diff --git a/Board.cpp b/Board.cpp index 27cfae6..7c89ecd 100755 --- a/Board.cpp +++ b/Board.cpp @@ -6,6 +6,7 @@ using namespace std; Board::Board() { Piece* temp; + bool valid = false; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 8; ++j) { temp = new Piece(i, j, 'X'); @@ -27,6 +28,7 @@ Board::Board(const Board& b) { vector xp = b.getXPieces(); vector op = b.getOPieces(); Piece* temp; + bool valid = false; for (int i = 0; i < xp.size(); ++i) { temp = new Piece(xp[i]->getX(), xp[i]->getY(), 'X'); @@ -41,6 +43,18 @@ Board::Board(const Board& b) { } } +void Board::setValidFalse() { + valid = false; +} + +void Board::setValidTrue() { + valid = true; +} + +bool Board::isValid() { + return valid; +} + //make this efficient! bool Board::isPiece(int r, int c){ for (int i = 0; i < pieces.size(); ++i){ @@ -52,6 +66,31 @@ bool Board::isPiece(int r, int c){ 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! Piece* Board::getPiece(int r, int c){ for (int i = 0; i < pieces.size(); ++i){ @@ -67,10 +106,10 @@ moves Board::parse(string input){ input = myToUpper(input); int temp1; - char temp2; + int temp2; string temp3; - temp2 = input[0]; + temp2 = input[0] - 'A'; temp1 = input[1] - '0'; if (input[3] == 'L') @@ -212,14 +251,15 @@ char Board::intToCharColumn(int input){ void Board::move(string inputMove){ moves m = parse(inputMove); + cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n"; move(m); } void Board::move(moves m){ int row = 8 - (m.row); - int column = charToIntColumn(m.column); + int column = m.column; - //cout << row << " " << column << "\n\n"; + cout << "INSIDE MOVE: " << row << " " << column << "\n\n"; if (row > 8 || row < 0 || column > 8 || column < 0) { cout<<"ERROR: index out of bound."<getType() == 'O') { - row--; + else { + if(isThisMovePossible(row, column, m.moveType)) + { + if (m.moveType == "FWD") { + piece->moveFwd(); } - else { - row++; - } - if(isPiece(row, column)) { - cout << "Invalid move, there is a piece there.\n"; - return; - } - 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 (m.moveType == "LEFT") { - //add error checking - if(isPiece(row--, column--)) { - if(getPiece(row--, column--)->getType() != piece->getType()) { - getPiece(row--, column--)->isTaken(); + + else if (m.moveType == "LEFT") { + //add error checking + if(piece->getType() == 'O') { + row--; + column--; + } + else { + row++; + column--; + } + if(isPiece(row, column)) { + if(getPiece(row, column)->getType() != piece->getType()) { + isTaken(row, column); + piece->moveLeft(); + } + } + else { piece->moveLeft(); } - else { - cout << "Invalid move, there is a piece there.\n"; - return; + } + + else if (m.moveType == "RIGHT") { + //add error checking + //cout << "TESTING??\n\n"; + if(piece->getType() == 'O') { + row--; + column++; } - } - else { - piece->moveLeft(); - } - } - - else if (m.moveType == "RIGHT") { - //add error checking - if(isPiece(row--, column++)) { - if(getPiece(row--, column++)->getType() != piece->getType()) { - getPiece(row--, column++)->isTaken(); + else { + row++; + column++; + } + if(isPiece(row, column)) { + if(getPiece(row, column)->getType() != piece->getType()) { + isTaken(row, column); + piece->moveRight(); + } + } + else { + cout << piece->getX() << " " << piece->getY() << "\n\n"; piece->moveRight(); - } - else { - cout << "Invalid move, there is a piece there.\n"; - return; + cout << piece->getX() << " " << piece->getY() << "\n\n"; } } - else { - piece->moveRight(); - } + setValidTrue(); + } + else + { + cout << "Invalid move.\n\n"; + setValidFalse(); } } } bool Board::isThisMovePossible(int r, int c, string moveType){ Piece* piece; + Piece* temp; if (isPiece(r, c)) piece = getPiece(r, c); else @@ -321,27 +358,57 @@ bool Board::isThisMovePossible(int r, int c, string moveType){ int reflector = 1; if (piece->getType() == 'O') - reflector *= -1; + reflector = -1; if (moveType == "FWD"){ if (!isPiece(r + reflector, c)) - return true; - else + 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 true; - else + else if (moveType == "RIGHT"){ + temp = getPiece(r + reflector, c+1); + if(c < 7) { + if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) { + //cout << "What.\n\n"; + return true; + } + else if(temp->getType() != piece->getType()) { + char a = temp->getType(); + char b = piece->getType(); + cout << a << " " << b << "\n\n"; + return true; + } + else { + return false; + } + } + else { return false; + } } - else if (moveType == "LEFT"){ - if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0)) - return true; - else + else if (moveType == "LEFT"){ + temp = getPiece(r + reflector, c-1); + if(c > 0) { + if (!isPiece(r+reflector, c-1) && (r+reflector >= 0) && (r+reflector <= 7)) { + //cout << "What.\n\n"; + return true; + } + else if(temp->getType() != piece->getType()) { + char a = temp->getType(); + char b = piece->getType(); + cout << a << " " << b << "\n\n"; + return true; + } + else { + return false; + } + } + else { return false; + } } else return false; @@ -358,25 +425,25 @@ vector Board::viewPossibleMoves(){ c = xpieces[i]->getY(); if (isThisMovePossible(r, c, "FWD")) { - moves temp(8-r,intToCharColumn(c+1), "FWD"); + moves temp(8-r,c, "FWD"); output.push_back(temp); } if (isThisMovePossible(r,c,"LEFT")) { - moves temp(8-r,intToCharColumn(c+1), "LEFT"); + moves temp(8-r,c, "LEFT"); output.push_back(temp); } if (isThisMovePossible(r,c,"RIGHT")) { - moves temp(8-r,intToCharColumn(c+1), "RIGHT"); + moves temp(8-r,c, "RIGHT"); output.push_back(temp); } } } - else if (turn == 'X') { + else if (turn == '0') { for (int i = 0; i < opieces.size(); ++i){ r = opieces[i]->getX(); c = opieces[i]->getY(); @@ -447,6 +514,7 @@ void Board::undo(Board& tablero){ void Board::interpret(string input, Board& tablero){ vector record; input = myToUpper(input); + //cout << "MOVE: " << input << "\n\n"; if (input == "UNDO") { @@ -463,7 +531,6 @@ void Board::interpret(string input, Board& tablero){ } cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"< opieces; vector pieces; char turn = 'O'; + bool valid = false; public: Board(); Board(const Board& b); + void setValidFalse(); + void setValidTrue(); + bool isValid(); bool isPiece(int r, int c); + void isTaken(int r, int c); Piece* getPiece(int r, int c); vector getXPieces() const { return xpieces; } vector getOPieces() const { return opieces; } diff --git a/Engine.cpp b/Engine.cpp index 2ed7a7f..96f0867 100755 --- a/Engine.cpp +++ b/Engine.cpp @@ -31,24 +31,28 @@ void Engine::startGame(){ { gameOver = b->isGameOver(); - while(b->getTurn() == 'O' ) + while(b->getTurn() == 'O' && !b->isValid()) { b->displayBoard(); cout<<"\nEnter command: "; cin>>move; cout << "\n"; b->interpret(move, *b); - b->changeTurns(); + if(b->isValid()) { + b->changeTurns(); + b->setValidFalse(); + } } - cout << "TEST\n\n"; + if(b->isValid()) cout << b->getTurn(); while(b->getTurn() == 'X' ) { - AI(); + easyAI(); } gameOver = b->isGameOver(); + b->setValidFalse(); b->snapshot(record, *b); } @@ -57,7 +61,11 @@ void Engine::startGame(){ void Engine::easyAI() { vector 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)); int randomChoice = rand() % (listOfMoves.size()-1) - 0; @@ -70,61 +78,64 @@ void Engine::easyAI() void Engine::AI(){ cout << "----------------------BEGIN AI FUNCTION----------------------\n"; vector listOfMoves = b->viewPossibleMoves(); - Board* temp = new Board(*b); + //Board* b = new Board(*b); //probably not needed, check later - if (temp->getTurn() != 'X'){ + /* + if (b->getTurn() != 'X'){ cout << "a changing of turns is needed. \n"; - temp->changeTurns(); + b->changeTurns(); } - + */ //only doing 1 branch right now because testing /*for (int i = 0; i < listOfMoves.size(); ++i){ - minMax(temp, listOfMoves[i]); + minMax(b, listOfMoves[i]); }*/ - b->move(minMax(temp, listOfMoves[0], 0)); + b->move(minMax(listOfMoves[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->displayBoard(); cout << "----------------------END AI FUNCTION----------------------\n"; } -moves Engine::minMax(Board* temp, moves m, int c){ +moves Engine::minMax(moves m, int c){ //testing purposes only, c = finite depth + /* if (c > 5){ return m; } - if (temp->isGameOver() == true){ + if (b->isGameOver() == true){ cout << "END OF PATH REACHED\n"; return m; } else { - if(temp->isThisMovePossible(8 - m.row, temp->charToIntColumn(m.column), m.moveType)){ + if(b->isThisMovePossible(8 - m.row, b->charToIntColumn(m.column), m.moveType)){ cout << "piece has been moved in minMax\n"; - temp->move(m); - temp->changeTurns(); + b->move(m); + b->changeTurns(); } cout << "c: " << c << "\n\n"; - cout << "current turn: " << temp->getTurn() << "\n"; - vector listOfMoves = temp->viewPossibleMoves(); + cout << "current turn: " << b->getTurn() << "\n"; + vector listOfMoves = b->viewPossibleMoves(); for (int i = 0; i < listOfMoves.size(); ++i){ - //return minMax(temp, listOfMoves[i]); + //return minMax(b, listOfMoves[i]); } - temp->displayBoard(); + b->displayBoard(); //limited recursion - return minMax(temp, listOfMoves[0], ++c); + return minMax(b, listOfMoves[0], ++c); //testing return m; - } + }*/ } \ No newline at end of file diff --git a/Engine.h b/Engine.h index 97efbf8..d1b3809 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(moves m, int c); }; \ No newline at end of file diff --git a/Piece.cpp b/Piece.cpp index a223740..265180a 100755 --- a/Piece.cpp +++ b/Piece.cpp @@ -42,7 +42,7 @@ void Piece::moveLeft(){ else if (type == 'O'){ x--; - y++; + y--; } else @@ -57,7 +57,7 @@ void Piece::moveRight(){ else if (type == 'O'){ x--; - y--; + y++; } else