Updated to work between server and client
This commit is contained in:
parent
df39863877
commit
87033ca041
1 changed files with 584 additions and 0 deletions
584
Board.cpp
Normal file
584
Board.cpp
Normal file
|
@ -0,0 +1,584 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Board.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Board::Board() {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
boardArray[i][j] = 'X';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int i = 2; i < 6; ++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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
moves Board::parse(string input)
|
||||
{
|
||||
|
||||
input = myToUpper(input);
|
||||
|
||||
cout<<input<<endl;
|
||||
|
||||
int temp1;
|
||||
char temp2;
|
||||
string temp3;
|
||||
|
||||
temp2 = input[0];
|
||||
temp1 = input[1] - '0';
|
||||
|
||||
if (input[3] == 'L')
|
||||
{
|
||||
temp3 = "LEFT";
|
||||
}
|
||||
|
||||
else if (input[3] == 'R')
|
||||
{
|
||||
temp3 = "RIGHT";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
temp3 = "FWD";
|
||||
}
|
||||
|
||||
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;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (boardArray[7][i] == 'X')
|
||||
{
|
||||
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
cout<<"|\n";
|
||||
}
|
||||
cout<<'\n'<<endl;
|
||||
cout<<"turn : "<<turn;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int Board::charToIntColumn(char input) //converts column number to int
|
||||
{
|
||||
int kolumn;
|
||||
|
||||
switch (input)
|
||||
{
|
||||
case 'A': kolumn = 0; break;
|
||||
case 'B': kolumn = 1; break;
|
||||
case 'C': kolumn = 2; break;
|
||||
case 'D': kolumn = 3; break;
|
||||
case 'E': kolumn = 4; break;
|
||||
case 'F': kolumn = 5; break;
|
||||
case 'G': kolumn = 6; break;
|
||||
case 'H': kolumn = 7; break;
|
||||
}
|
||||
|
||||
return kolumn;
|
||||
}
|
||||
|
||||
char Board::intToCharColumn(int input) //converts column number to int
|
||||
{
|
||||
char kolumn;
|
||||
|
||||
switch (input)
|
||||
{
|
||||
case 1: kolumn = 'A'; break;
|
||||
case 2: kolumn = 'B'; break;
|
||||
case 3: kolumn = 'C'; break;
|
||||
case 4: kolumn = 'D'; break;
|
||||
case 5: kolumn = 'E'; break;
|
||||
case 6: kolumn = 'F'; break;
|
||||
case 7: kolumn = 'G'; break;
|
||||
case 8: kolumn = 'H'; break;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
else 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;
|
||||
}
|
||||
|
||||
else 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
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 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 == "LEFT")
|
||||
{
|
||||
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
|
||||
vector<moves> Board::viewPossibleMoves()
|
||||
{
|
||||
vector<moves> output;
|
||||
|
||||
for (int r = 0; r < 8; ++r)
|
||||
{
|
||||
for (int c = 0; c < 8; ++c)
|
||||
{
|
||||
if (boardArray[r][c] == turn)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);// = 'Q';//(char) numeric;
|
||||
}
|
||||
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)
|
||||
{
|
||||
vector<Board> record;
|
||||
|
||||
if (record.size() < 2)
|
||||
{
|
||||
cout<<"nothing to undo"<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (int r = 0; r < 8; ++r)
|
||||
{
|
||||
for (int k = 0; k < 8; ++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
|
||||
{
|
||||
vector<Board> record;
|
||||
input = myToUpper(input);
|
||||
|
||||
if (input == "UNDO")
|
||||
{
|
||||
undo(tablero);
|
||||
}
|
||||
|
||||
else if (input == "DISPLAYRECORD") //for debugging purposes
|
||||
{
|
||||
cout<<"record: "<<endl;
|
||||
cout<<record.size();
|
||||
for (int i = 0; i < record.size(); ++i)
|
||||
{
|
||||
record[i].displayBoard();
|
||||
}
|
||||
cout<<"---------------------------------------------------END DISPLAY RECORD------------------------"<<endl;
|
||||
}
|
||||
|
||||
else tablero.move(input);
|
||||
}
|
||||
|
||||
void Board::snapshot(vector<Board>& inputVec, Board inputBoard)
|
||||
{
|
||||
if (inputVec.size() == 10)
|
||||
{
|
||||
inputVec.erase(inputVec.begin());
|
||||
}
|
||||
|
||||
else if (inputVec.size() > 10)
|
||||
{
|
||||
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
|
||||
|
||||
}
|
Reference in a new issue