From 934a28bec74cc300ff0a88e89cbd2752c731ca7c Mon Sep 17 00:00:00 2001 From: Brandon Jackson <1drummer@att.net> Date: Tue, 27 Oct 2015 21:51:19 -0500 Subject: [PATCH] Update Board.cpp --- Board.cpp | 746 +++++++++++++++++++++++++++--------------------------- 1 file changed, 366 insertions(+), 380 deletions(-) diff --git a/Board.cpp b/Board.cpp index 6d7b295..d13ccc2 100644 --- a/Board.cpp +++ b/Board.cpp @@ -5,39 +5,111 @@ using namespace std; Board::Board() { - for (int i = 0; i < 2; ++i) { + Piece* temp; + bool valid = false; + for (int i = 0; i < 2; ++i) { for (int j = 0; j < 8; ++j) { - boardArray[i][j] = 'X'; + temp = new Piece(i, j, 'X'); + xpieces.push_back(temp); + pieces.push_back(temp); } } - - for (int i = 2; i < 6; ++i) { + for (int i = 6; i < 8; ++i) { for (int j = 0; j < 8; ++j) { - boardArray[i][j] = '_'; - } - } - - - for (int i = 6; i <= 7; ++i) { - for (int j = 0; j < 8; ++j) { - boardArray[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; + bool valid = false; -moves Board::parse(string input) -{ + 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); + } +} + +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){ + if (pieces[i]->getX() == r && pieces[i]->getY() == c){ + return true; + } + } + + 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){ + if (pieces[i]->getX() == r && pieces[i]->getY() == c){ + return pieces[i]; + } + } + + return new Piece(); +} + +moves Board::parse(string input){ input = myToUpper(input); - cout<getX() == 7){ + cout<<"\n\n\nPlayer X wins!\n\n\n"<getX() == 0){ + cout<<"\n\n\nPlayer O wins!\n\n\n"<getX() << " " << pieces[i]->getY() << " " << pieces[i]->getType() << "\t"; + } + cout << "Debugging:\n\n"; + */ + cout << "; A B C D E F G H"<getType() == 'X') + cout << "|" << "X"; + else + cout << "|" << "O"; + else + cout << "|" << "_"; } + cout<<"|\n"; } - cout<<'\n'<getType() == 'X') + output += "|X"; + else + output += "|O"; + else + output += "|_"; + } + + output += "|\n"; + } + + output += "\n\nturn: "; + output += turn; + output += "\n"; + + return output; +} + +int Board::charToIntColumn(char input){ int kolumn; switch (input) @@ -146,8 +231,7 @@ int Board::charToIntColumn(char input) //converts column number to int return kolumn; } -char Board::intToCharColumn(int input) //converts column number to int -{ +char Board::intToCharColumn(int input){ char kolumn; switch (input) @@ -164,299 +248,221 @@ char Board::intToCharColumn(int input) //converts column number to int return kolumn; } + +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 = m.column; -void Board::move(string inputMove) -{ + cout << "INSIDE MOVE: " << row << " " << column << "\n\n"; - moves jugada = parse(inputMove); - - int row = 8 - (jugada.row); - int kolumn = charToIntColumn(jugada.column); - int temp = boardArray[row][kolumn]; - int reflector = 1; - - if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound!"< 8 || row < 0 || column > 8 || column < 0) { + cout<<"ERROR: index out of bound."<getX() << piece->getY() << "\n\n"; + if (piece->getType() != turn) { + cout<<"ERROR: attempting to move the wrong side's piece.\n"; } - else if (temp == '_') - { - cout<<"there's no piece in that spot!"<moveFwd(); + } + + 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 if (m.moveType == "RIGHT") { + //add error checking + //cout << "TESTING??\n\n"; + if(piece->getType() == 'O') { + row--; + column++; + } + 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(); + cout << piece->getX() << " " << piece->getY() << "\n\n"; + } + } + setValidTrue(); } - - - if (jugada.moveType == "FWD") - { - - if(boardArray[row+reflector][kolumn] != '_') - { - cout<<"you can't move that piece forward"< 8 || row < 0 || kolumn > 8 || kolumn < 0) - { - cout<<"ERROR: index out of bound!"<getType() != turn) { + cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n"; return false; } - int reflector = 1; - - if (pieceToMove == 'O') - { - reflector *= -1; - } - - else - { - if (moveType == "FWD") - { - - if (boardArray[r+reflector][c] == '_') return true; - else return false; - + else{ + int reflector = 1; + + if (piece->getType() == 'O') + reflector = -1; + + if (moveType == "FWD"){ + if (!isPiece(r + reflector, c)) + return true; + else + return false; } - else if (moveType == "RIGHT") - { - if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true; - else return false; + 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 (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true; - else return false; + 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; } } -vector Board::viewPossibleMoves() -{ +vector Board::viewPossibleMoves(){ + int r, c = -1; vector output; - for (int r = 0; r < 8; ++r) - { - for (int c = 0; c < 8; ++c) - { - if (boardArray[r][c] == turn) + if (turn == 'X'){ + for (int i = 0; i < xpieces.size(); ++i){ + r = xpieces[i]->getX(); + c = xpieces[i]->getY(); + if (isThisMovePossible(r, c, "FWD")) { - if (isThisMovePossible(r,c,"FWD")) - { - moves temp(8-r,intToCharColumn(c+1),"FWD"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"LEFT")) - { - moves temp(8-r,intToCharColumn(c+1),"LEFT"); - output.push_back(temp); - } - - if (isThisMovePossible(r,c,"RIGHT")) - { - moves temp(8-r,intToCharColumn(c+1),"RIGHT"); - output.push_back(temp); - } - + moves temp(8-r,c, "FWD"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"LEFT")) + { + moves temp(8-r,c, "LEFT"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"RIGHT")) + { + moves temp(8-r,c, "RIGHT"); + output.push_back(temp); + } + } + } + + else if (turn == '0') { + for (int i = 0; i < opieces.size(); ++i){ + r = opieces[i]->getX(); + c = opieces[i]->getY(); + if (isThisMovePossible(r, c, "FWD")) + { + moves temp(8-r,intToCharColumn(c+1), "FWD"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"LEFT")) + { + moves temp(8-r,intToCharColumn(c+1), "LEFT"); + output.push_back(temp); + } + + if (isThisMovePossible(r,c,"RIGHT")) + { + moves temp(8-r,intToCharColumn(c+1), "RIGHT"); + output.push_back(temp); } } } @@ -464,8 +470,7 @@ vector Board::viewPossibleMoves() return output; } -string Board::myToUpper(string input) -{ +string Board::myToUpper(string input){ string output; for (int i = 0 ; i < input.size(); ++i) @@ -480,26 +485,11 @@ string Board::myToUpper(string input) else output.push_back(input[i]); } - for (int i = 0; i < output.size(); ++i) - { - cout< input) -{ - cout<<"\n\nList of possible Moves:"< record; if (record.size() < 2) @@ -513,17 +503,18 @@ void Board::undo(Board& tablero) { 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)); } } + record.pop_back(); } } -void Board::interpret(string input, Board& tablero) //determines what kind of command its input is -{ +void Board::interpret(string input, Board& tablero){ vector record; input = myToUpper(input); + //cout << "MOVE: " << input << "\n\n"; if (input == "UNDO") { @@ -540,12 +531,10 @@ void Board::interpret(string input, Board& tablero) //determines what kind of co } cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<& inputVec, Board inputBoard) -{ +void Board::snapshot(vector& inputVec, Board inputBoard){ if (inputVec.size() == 10) { inputVec.erase(inputVec.begin()); @@ -556,29 +545,26 @@ void Board::snapshot(vector& inputVec, Board inputBoard) cout<<"QUEUE OVERFLOW!"< listOfMoves = viewPossibleMoves(); + else if (max == 'O'){ + return (opieces.size() - xpieces.size()); + } - //2) pick a movement - - srand(time(NULL)); - int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element - - //3) execute movement - - int temp = randomChoice; - - move(listOfMoves[randomChoice]); - - //cout<<"\n\nMove executed by AI: "<