finished restructuring
This commit is contained in:
parent
d64552743a
commit
7859237ced
2 changed files with 439 additions and 793 deletions
105
Board.cpp
105
Board.cpp
|
@ -1,43 +1,28 @@
|
|||
class Board
|
||||
{
|
||||
char boardArray [8][8];
|
||||
char turn = 'O';
|
||||
#include "Board.h"
|
||||
|
||||
public:
|
||||
|
||||
Board()
|
||||
{
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
for (int j = 0; j < 8; ++j)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (int i = 6; i <= 7; ++i) {
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
boardArray[i][j] = 'O';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
moves parse(string input)
|
||||
moves Board::parse(string input)
|
||||
{
|
||||
|
||||
input = myToUpper(input);
|
||||
|
@ -72,12 +57,7 @@ class Board
|
|||
|
||||
}
|
||||
|
||||
char getTurn()
|
||||
{
|
||||
return turn;
|
||||
}
|
||||
|
||||
bool isGameOver()
|
||||
bool Board::isGameOver()
|
||||
{
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
|
@ -93,23 +73,16 @@ class Board
|
|||
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
// else return false; this line was making this function return false no matter what, apparently...
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void changeTurns()
|
||||
void Board::changeTurns()
|
||||
{
|
||||
if (turn == 'O') turn = 'X';
|
||||
else turn = 'O';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void displayBoard()
|
||||
void BoarddisplayBoard()
|
||||
{
|
||||
cout<<"\n\n";
|
||||
cout<<" A B C D E F G H"<<endl;
|
||||
|
@ -127,8 +100,7 @@ class Board
|
|||
cout<<"turn : "<<turn;
|
||||
}
|
||||
|
||||
|
||||
int charToIntColumn(char input) //converts column number to int
|
||||
int Board::charToIntColumn(char input) //converts column number to int
|
||||
{
|
||||
int kolumn;
|
||||
|
||||
|
@ -148,7 +120,7 @@ class Board
|
|||
}
|
||||
|
||||
|
||||
char intToCharColumn(int input) //converts column number to int
|
||||
char Board::intToCharColumn(int input) //converts column number to int
|
||||
{
|
||||
char kolumn;
|
||||
|
||||
|
@ -167,9 +139,7 @@ class Board
|
|||
return kolumn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void move(string inputMove)
|
||||
void Board::move(string inputMove)
|
||||
{
|
||||
|
||||
moves jugada = parse(inputMove);
|
||||
|
@ -283,7 +253,7 @@ class Board
|
|||
}
|
||||
|
||||
|
||||
void move(moves jugada)
|
||||
void Board::move(moves jugada)
|
||||
{
|
||||
|
||||
int row = 8 - (jugada.row);
|
||||
|
@ -392,9 +362,7 @@ class Board
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool isThisMovePossible(int r, int c, string moveType)
|
||||
bool Board::isThisMovePossible(int r, int c, string moveType)
|
||||
{
|
||||
char pieceToMove = boardArray[r][c];
|
||||
|
||||
|
@ -439,7 +407,7 @@ class Board
|
|||
|
||||
|
||||
|
||||
vector<moves> viewPossibleMoves()
|
||||
vector<moves> Board::viewPossibleMoves()
|
||||
{
|
||||
vector<moves> output;
|
||||
|
||||
|
@ -474,8 +442,41 @@ class Board
|
|||
return output;
|
||||
}
|
||||
|
||||
string Board::myToUpper(string input)
|
||||
{
|
||||
string output;
|
||||
|
||||
void easyAI()
|
||||
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::easyAI()
|
||||
{
|
||||
|
||||
//1) see all possible movements
|
||||
|
@ -496,5 +497,3 @@ class Board
|
|||
//cout<<"\n\nMove executed by AI: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
|
||||
|
||||
}
|
||||
|
||||
};
|
395
Board.h
395
Board.h
|
@ -2,389 +2,36 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
struct moves {
|
||||
int row;
|
||||
char column;
|
||||
string moveType;
|
||||
|
||||
moves(int linea, int columna, string m) {
|
||||
row = linea;
|
||||
column = columna;
|
||||
moveType = m;
|
||||
}
|
||||
};
|
||||
|
||||
class Board {
|
||||
char boardArray [8][8];
|
||||
char turn = 'O';
|
||||
|
||||
public:
|
||||
Board();
|
||||
|
||||
moves parse(string input);
|
||||
|
||||
char getTurn() { return turn; }
|
||||
|
||||
bool isGameOver();
|
||||
void changeTurns();
|
||||
void displayBoard();
|
||||
|
||||
|
||||
int 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 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 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 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 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> 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;
|
||||
}
|
||||
|
||||
|
||||
void 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 charToIntColumn(char input);
|
||||
char intToCharColumn(int input);
|
||||
void move(string inputMove);
|
||||
void move(moves jugada);
|
||||
bool isThisMovePossible(int r, int c, string moveType);
|
||||
vector<moves> viewPossibleMoves();
|
||||
string myToUpper(string input);
|
||||
void displayPossibleMoves(vector<moves> input);
|
||||
void easyAI();
|
||||
};
|
Reference in a new issue