Update Board.cpp
This commit is contained in:
parent
fc418150a9
commit
934a28bec7
1 changed files with 366 additions and 380 deletions
686
Board.cpp
686
Board.cpp
|
@ -5,39 +5,111 @@
|
|||
using namespace std;
|
||||
|
||||
Board::Board() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
moves Board::parse(string input)
|
||||
{
|
||||
Board::Board(const Board& b) {
|
||||
vector<Piece*> xp = b.getXPieces();
|
||||
vector<Piece*> 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');
|
||||
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<<input<<endl;
|
||||
|
||||
int temp1;
|
||||
char temp2;
|
||||
int temp2;
|
||||
string temp3;
|
||||
|
||||
temp2 = input[0];
|
||||
temp2 = input[0] - 'A';
|
||||
temp1 = input[1] - '0';
|
||||
|
||||
if (input[3] == 'L')
|
||||
|
@ -58,77 +130,90 @@ moves Board::parse(string input)
|
|||
moves output(temp1, temp2, temp3);
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
bool Board::isGameOver()
|
||||
{
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
|
||||
if (boardArray[0][i] == 'O')
|
||||
{
|
||||
cout<<"\n\n\nplayer O wins!\n\n\n"<<endl;
|
||||
bool Board::isGameOver(){
|
||||
for (int i = 0; i < xpieces.size(); ++i){
|
||||
if (xpieces[i]->getX() == 7){
|
||||
cout<<"\n\n\nPlayer X wins!\n\n\n"<<endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (boardArray[7][i] == 'X')
|
||||
{
|
||||
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl;
|
||||
for (int i = 0; i < opieces.size(); ++i){
|
||||
if (opieces[i]->getX() == 0){
|
||||
cout<<"\n\n\nPlayer O wins!\n\n\n"<<endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Board::changeTurns()
|
||||
{
|
||||
void Board::changeTurns(){
|
||||
if (turn == 'O') turn = 'X';
|
||||
else turn = 'O';
|
||||
}
|
||||
|
||||
void Board::displayBoard()
|
||||
{
|
||||
cout<<"\n\n";
|
||||
cout<<"; A B C D E F G H"<<endl;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
int mango = 8 - i;
|
||||
cout<<"; "<<mango<<" ";
|
||||
for (int j = 0; j < 8; ++j)
|
||||
{
|
||||
cout<<"|"<<boardArray[i][j];
|
||||
void Board::displayBoard(){
|
||||
/*
|
||||
cout << "Debugging:\n";
|
||||
for (int i = 0; i < pieces.size(); ++i){
|
||||
if(i%8 == 0)
|
||||
cout << "\n";
|
||||
cout << pieces[i]->getX() << " " << pieces[i]->getY() << " " << pieces[i]->getType() << "\t";
|
||||
}
|
||||
cout << "Debugging:\n\n";
|
||||
*/
|
||||
cout << "; A B C D E F G H"<<endl;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
int label = 8 - i;
|
||||
cout<<"; "<<label<<" ";
|
||||
for (int j = 0; j < 8; ++j){
|
||||
if (isPiece(i, j))
|
||||
if (getPiece(i, j)->getType() == 'X')
|
||||
cout << "|" << "X";
|
||||
else
|
||||
cout << "|" << "O";
|
||||
else
|
||||
cout << "|" << "_";
|
||||
}
|
||||
|
||||
cout<<"|\n";
|
||||
}
|
||||
|
||||
cout<<'\n'<<endl;
|
||||
cout<<"turn : "<<turn;
|
||||
cout<<"turn: "<<turn << "\n";
|
||||
}
|
||||
|
||||
string Board::boardToString()
|
||||
{
|
||||
string temp;
|
||||
temp.append("\n");
|
||||
temp.append("\n");
|
||||
temp.append("; A B C D E F G H\n");
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
int mango = 8 - i;
|
||||
temp.append("; " + to_string(mango) + " ");
|
||||
for (int j = 0; j < 8; ++j)
|
||||
{
|
||||
temp.append("|");
|
||||
temp.push_back(boardArray[i][j]);
|
||||
}
|
||||
temp.append("|\n");
|
||||
}
|
||||
temp.append("\n");
|
||||
temp.append("turn : " + turn);
|
||||
|
||||
return temp;
|
||||
string Board::boardToString(){
|
||||
string output = "";
|
||||
output += "; A B C D E F G H\n";
|
||||
int label;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
label = 8 - i;
|
||||
output += "; ";
|
||||
output += '0' + label;
|
||||
output += " ";
|
||||
for (int j = 0; j < 8; ++j){
|
||||
if (isPiece(i, j))
|
||||
if (getPiece(i, j)->getType() == 'X')
|
||||
output += "|X";
|
||||
else
|
||||
output += "|O";
|
||||
else
|
||||
output += "|_";
|
||||
}
|
||||
|
||||
int Board::charToIntColumn(char input) //converts column number to int
|
||||
{
|
||||
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)
|
||||
|
@ -165,280 +249,204 @@ char Board::intToCharColumn(int input) //converts column number to int
|
|||
return kolumn;
|
||||
}
|
||||
|
||||
void Board::move(string inputMove)
|
||||
{
|
||||
|
||||
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!"<<endl;
|
||||
void Board::move(string inputMove){
|
||||
moves m = parse(inputMove);
|
||||
cout << "MOVE: " << m.row << " " << m.column << " " << m.moveType << "\n\n";
|
||||
move(m);
|
||||
}
|
||||
|
||||
else if (temp != turn)
|
||||
{
|
||||
cout<<"you can't move that piece at this turn!"<<endl;
|
||||
void Board::move(moves m){
|
||||
int row = 8 - (m.row);
|
||||
int column = m.column;
|
||||
|
||||
cout << "INSIDE MOVE: " << row << " " << column << "\n\n";
|
||||
|
||||
if (row > 8 || row < 0 || column > 8 || column < 0) {
|
||||
cout<<"ERROR: index out of bound."<<endl;
|
||||
return;
|
||||
}
|
||||
|
||||
else if (temp == '_')
|
||||
{
|
||||
cout<<"there's no piece in that spot!"<<endl;
|
||||
Piece* piece;
|
||||
//cout << "TEST" << row << " " << column << "\n\n";
|
||||
if (isPiece(row, column))
|
||||
piece = getPiece(row, column);
|
||||
|
||||
else{
|
||||
cout<<"ERROR: attempting to move an invalid piece.\n";
|
||||
return;
|
||||
}
|
||||
//cout << piece->getX() << piece->getY() << "\n\n";
|
||||
if (piece->getType() != turn) {
|
||||
cout<<"ERROR: attempting to move the wrong side's piece.\n";
|
||||
}
|
||||
|
||||
else if (temp == 'X' || temp == 'O')
|
||||
else {
|
||||
if(isThisMovePossible(row, column, m.moveType))
|
||||
{
|
||||
|
||||
if (temp == 'O')
|
||||
{
|
||||
reflector *= -1;
|
||||
if (m.moveType == "FWD") {
|
||||
piece->moveFwd();
|
||||
}
|
||||
|
||||
|
||||
if (jugada.moveType == "FWD")
|
||||
{
|
||||
|
||||
if(boardArray[row+reflector][kolumn] != '_')
|
||||
{
|
||||
cout<<"you can't move that piece forward"<<endl;
|
||||
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();
|
||||
}
|
||||
else
|
||||
{
|
||||
boardArray[row][kolumn] = '_';
|
||||
boardArray[row+reflector][kolumn] = temp;
|
||||
|
||||
changeTurns();
|
||||
displayBoard();
|
||||
|
||||
cout << "Invalid move.\n\n";
|
||||
setValidFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (jugada.moveType == "LEFT")
|
||||
{
|
||||
if (kolumn == 0)
|
||||
{
|
||||
cout<<"Destination Spot out of range!"<<endl;
|
||||
}
|
||||
|
||||
else if (boardArray[row+reflector][kolumn-1] == temp)
|
||||
{
|
||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||
}
|
||||
|
||||
bool Board::isThisMovePossible(int r, int c, string moveType){
|
||||
Piece* piece;
|
||||
Piece* temp;
|
||||
if (isPiece(r, c))
|
||||
piece = getPiece(r, c);
|
||||
else
|
||||
{
|
||||
boardArray[row][kolumn] = '_';
|
||||
boardArray[row+reflector][kolumn-1] = temp;
|
||||
return false;
|
||||
|
||||
changeTurns();
|
||||
displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
else if (jugada.moveType == "RIGHT")
|
||||
{
|
||||
if (kolumn == 7)
|
||||
{
|
||||
cout<<"Destination Spot out of range!"<<endl;
|
||||
}
|
||||
|
||||
else if (boardArray[row+reflector][kolumn+1] == temp)
|
||||
{
|
||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
boardArray[row][kolumn] = '_';
|
||||
boardArray[row+reflector][kolumn+1] = temp;
|
||||
|
||||
changeTurns();
|
||||
displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"Unrecognized movetype!"<<endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"Invalid piece!"<<endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Board::move(moves jugada)
|
||||
{
|
||||
|
||||
int row = 8 - (jugada.row);
|
||||
int kolumn = charToIntColumn(jugada.column);
|
||||
|
||||
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
||||
{
|
||||
cout<<"ERROR: index out of bound!"<<endl;
|
||||
}
|
||||
|
||||
int temp = boardArray[row][kolumn];
|
||||
if (temp != turn)
|
||||
{
|
||||
cout<<"you can't move that piece at this turn!"<<endl;
|
||||
}
|
||||
|
||||
else if (temp == '_')
|
||||
{
|
||||
cout<<"there's no piece in that spot!"<<endl;
|
||||
}
|
||||
|
||||
int reflector = 1;
|
||||
|
||||
if (temp == 'X' || temp == 'O')
|
||||
{
|
||||
|
||||
if (temp == 'O')
|
||||
{
|
||||
reflector *= -1;
|
||||
}
|
||||
|
||||
|
||||
if (jugada.moveType == "FWD")
|
||||
{
|
||||
|
||||
if(boardArray[row+reflector][kolumn] != '_')
|
||||
{
|
||||
cout<<"you can't move that piece forward"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
boardArray[row][kolumn] = '_';
|
||||
boardArray[row+reflector][kolumn] = temp;
|
||||
|
||||
changeTurns();
|
||||
displayBoard();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
else if (jugada.moveType == "LEFT")
|
||||
{
|
||||
if (kolumn == 0)
|
||||
{
|
||||
cout<<"Destination Spot out of range!"<<endl;
|
||||
}
|
||||
|
||||
else if (boardArray[row+reflector][kolumn-1] == temp)
|
||||
{
|
||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
boardArray[row][kolumn] = '_';
|
||||
boardArray[row+reflector][kolumn-1] = temp;
|
||||
|
||||
changeTurns();
|
||||
displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
else if (jugada.moveType == "RIGHT")
|
||||
{
|
||||
if (kolumn == 7)
|
||||
{
|
||||
cout<<"Destination Spot out of range!"<<endl;
|
||||
}
|
||||
|
||||
else if (boardArray[row+reflector][kolumn+1] == temp)
|
||||
{
|
||||
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
boardArray[row][kolumn] = '_';
|
||||
boardArray[row+reflector][kolumn+1] = temp;
|
||||
|
||||
changeTurns();
|
||||
displayBoard();
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"Unrecognized movetype!"<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"Invalid piece!"<<endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool Board::isThisMovePossible(int r, int c, string moveType)
|
||||
{
|
||||
char pieceToMove = boardArray[r][c];
|
||||
|
||||
if (pieceToMove != turn) //trying to move invalid piece
|
||||
{
|
||||
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 (pieceToMove == 'O')
|
||||
{
|
||||
reflector *= -1;
|
||||
}
|
||||
if (piece->getType() == 'O')
|
||||
reflector = -1;
|
||||
|
||||
if (moveType == "FWD"){
|
||||
if (!isPiece(r + reflector, c))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
if (moveType == "FWD")
|
||||
{
|
||||
|
||||
if (boardArray[r+reflector][c] == '_') return true;
|
||||
else return false;
|
||||
|
||||
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<moves> Board::viewPossibleMoves()
|
||||
{
|
||||
vector<moves> Board::viewPossibleMoves(){
|
||||
int r, c = -1;
|
||||
vector<moves> output;
|
||||
|
||||
for (int r = 0; r < 8; ++r)
|
||||
if (turn == 'X'){
|
||||
for (int i = 0; i < xpieces.size(); ++i){
|
||||
r = xpieces[i]->getX();
|
||||
c = xpieces[i]->getY();
|
||||
if (isThisMovePossible(r, c, "FWD"))
|
||||
{
|
||||
for (int c = 0; c < 8; ++c)
|
||||
moves temp(8-r,c, "FWD");
|
||||
output.push_back(temp);
|
||||
}
|
||||
|
||||
if (isThisMovePossible(r,c,"LEFT"))
|
||||
{
|
||||
if (boardArray[r][c] == turn)
|
||||
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");
|
||||
|
@ -456,16 +464,13 @@ vector<moves> Board::viewPossibleMoves()
|
|||
moves temp(8-r,intToCharColumn(c+1), "RIGHT");
|
||||
output.push_back(temp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<<output[i]<<endl;
|
||||
}
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
void Board::displayPossibleMoves(vector<moves> input)
|
||||
{
|
||||
cout<<"\n\nList of possible Moves:"<<endl;
|
||||
for (int i = 0; i < input.size(); ++i)
|
||||
{
|
||||
cout<<"possible move: "<<input[i].row<<" "<<input[i].column<<" "<<input[i].moveType<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Board::undo(Board& tablero)
|
||||
{
|
||||
void Board::undo(Board& tablero){
|
||||
vector<Board> 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<Board> 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------------------------"<<endl;
|
||||
}
|
||||
|
||||
else tablero.move(input);
|
||||
}
|
||||
|
||||
void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
|
||||
{
|
||||
void Board::snapshot(vector<Board>& inputVec, Board inputBoard){
|
||||
if (inputVec.size() == 10)
|
||||
{
|
||||
inputVec.erase(inputVec.begin());
|
||||
|
@ -556,29 +545,26 @@ void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
|
|||
cout<<"QUEUE OVERFLOW!"<<endl;
|
||||
}
|
||||
|
||||
|
||||
inputVec.push_back(inputBoard);
|
||||
}
|
||||
|
||||
//move this to its own file
|
||||
void Board::easyAI()
|
||||
{
|
||||
|
||||
//1) see all possible movements
|
||||
|
||||
vector<moves> listOfMoves = viewPossibleMoves();
|
||||
|
||||
//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: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
|
||||
|
||||
int Board::evaluate(char max, char min){
|
||||
//right now just evaluating number of pieces
|
||||
if (max == 'X'){
|
||||
return (xpieces.size() - opieces.size());
|
||||
}
|
||||
|
||||
else if (max == 'O'){
|
||||
return (opieces.size() - xpieces.size());
|
||||
}
|
||||
|
||||
else {
|
||||
cout << "Unidentified max, must be either X or O.\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Reference in a new issue