#include #include #include #include #include #include "Board.h" using namespace std; Board::Board() { Piece* temp; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 8; ++j) { 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) { temp = new Piece(i, j, 'O'); opieces.push_back(temp); pieces.push_back(temp); } } valid, xtaken, otaken = false; } Board::Board(const Board& b) { vector xp = b.getTypePieces('X'); vector op = b.getTypePieces('O'); Piece* temp; char tempturn = b.getTurn(); turn = tempturn; 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); } valid, xtaken, otaken = false; } //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; } //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(); } 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); otaken = true; } } } else { for(int x = 0; x < xpieces.size(); ++x) { if (xpieces[x]->getX() == r && xpieces[x]->getY() == c) { xpieces.erase(xpieces.begin() + x); xtaken = true; } } } pieces.erase(pieces.begin() + i); break; } } } bool Board::checkTaken(char c){ if (c == 'X'){ return xtaken; } else if (c == 'O'){ return otaken; } else return false; } vector Board::getTypePieces(char type) const{ if (type == 'X') return xpieces; else if (type == 'O') return opieces; else{ cout << "Invalid type!\n"; return pieces; } } bool Board::isGameOver(){ for (int i = 0; i < xpieces.size(); ++i){ if (xpieces[i]->getX() == 7){ return true; } } for (int i = 0; i < opieces.size(); ++i){ if (opieces[i]->getX() == 0){ return true; } } return false; } char Board::whoWon(){ for (int i = 0; i < xpieces.size(); ++i){ if (xpieces[i]->getX() == 7){ return 'X'; } } for (int i = 0; i < opieces.size(); ++i){ if (opieces[i]->getX() == 0){ return 'O'; } } cout << "ERROR: function whoWon() called incorrectly. Game is not over. \n"; return 'a'; } void Board::changeTurns(){ if (turn == 'O') turn = 'X'; else turn = 'O'; } void Board::resetTaken(){ xtaken = false; otaken = false; } void Board::displayBoard(){ 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; } Board Board::move(moves m){ int row = m.row; int column = m.column; Piece* piece; if (isPiece(row, column)) piece = getPiece(row, column); else{ cout<<"ERROR: attempting to move an invalid piece.\n"; return *this; } if (piece->getType() != turn) { cout<<"ERROR: attempting to move the wrong side's piece.\n"; return *this; } else { if(isThisMovePossible(row, column, m.moveType)) { record.push_back(*this); if (m.moveType == "FWD") { piece->moveFwd(); } else if (m.moveType == "LEFT") { 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") { 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 { piece->moveRight(); } } setValidTrue(); } else { cout << "Invalid move.\n\n"; setValidFalse(); } } return *this; } bool Board::isThisMovePossible(int r, int c, string moveType){ Piece* piece; Piece* temp; if (isPiece(r, c)) piece = getPiece(r, c); else return false; if (piece->getType() != turn) { cout << "Error in Board::isThisMovePossible: trying to move a piece outside your turn.\n"; 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"){ temp = getPiece(r + reflector, c+1); if(c < 7) { if (!isPiece(r+reflector, c+1) && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7)) { return true; } else if(temp->getType() != piece->getType()) { char a = temp->getType(); char b = piece->getType(); return true; } else { return false; } } 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)) { return true; } else if(temp->getType() != piece->getType()) { char a = temp->getType(); char b = piece->getType(); return true; } else { return false; } } else { return false; } } else return false; } } vector Board::viewPossibleMoves(){ int r, c = -1; vector output; if (turn == 'X'){ for (int i = 0; i < xpieces.size(); ++i){ r = xpieces[i]->getX(); c = xpieces[i]->getY(); if (isThisMovePossible(r, c, "FWD")) { moves temp(r,c, "FWD"); output.push_back(temp); } if (isThisMovePossible(r,c,"LEFT")) { moves temp(r,c, "LEFT"); output.push_back(temp); } if (isThisMovePossible(r,c,"RIGHT")) { moves temp(r,c, "RIGHT"); output.push_back(temp); } } } else if (turn == 'O') { for (int i = 0; i < opieces.size(); ++i){ r = opieces[i]->getX(); c = opieces[i]->getY(); if (isThisMovePossible(r, c, "FWD")) { moves temp(r,c, "FWD"); output.push_back(temp); } if (isThisMovePossible(r,c,"LEFT")) { moves temp(r,c, "LEFT"); output.push_back(temp); } if (isThisMovePossible(r,c,"RIGHT")) { moves temp(r,c, "RIGHT"); output.push_back(temp); } } } return output; } string Board::myToUpper(string input){ string output; for (int i = 0 ; i < input.size(); ++i) { int numeric; if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122)) { numeric = input[i] - 32; output.push_back((char)numeric); } else output.push_back(input[i]); } return output; } Board* Board::undo(){ //assuming this will only be called by a player, against an AI if (record.size() == 0){ cout<< "ERROR: there is nothing to undo"; return this; } else{ Board* temp = new Board(record[record.size()-2]); return temp; } } void Board::snapshot(vector& inputVec, Board inputBoard){ if (inputVec.size() == 10){ inputVec.erase(inputVec.begin()); } else if (inputVec.size() > 10){ cout<<"QUEUE OVERFLOW!"< maxPieces = getTypePieces(max); vector minPieces = getTypePieces(min); int reflector, val, x, y = 0; Piece* temp; val += 2 * (maxPieces.size() - minPieces.size()); //check for taken conditions if (checkTaken(min)){ val = val + 10; } if (checkTaken(max)){ val = val - 10; } //ultimate condition! if (isGameOver()){ if (whoWon() == max) val = val + 10; else val = val - 10; } return val; }