Skip to content

Commit

Permalink
Big Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Broderick-Westrope committed Apr 16, 2021
1 parent b3e401c commit 4b2b704
Show file tree
Hide file tree
Showing 19 changed files with 492 additions and 639 deletions.
516 changes: 162 additions & 354 deletions Board.h → Board/Board.cpp

Large diffs are not rendered by default.

156 changes: 156 additions & 0 deletions Board/Board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#ifndef BOARD_H
#define BOARD_H

class Board
{
private:
int **grid;
int boardSize;
int turn;
vector <Cell> emptyCells{};

public:
explicit Board(int bs)
{
boardSize = bs;
grid = new int *[boardSize];
for (int i = 0; i < boardSize; i++)
grid[i] = new int[boardSize];

for (int i = 0; i < boardSize; i++)
for (int j = 0; j < boardSize; j++)
{
grid[i][j] = 0;
}
turn = 1;
}

virtual ~Board()
{

for (int i = 0; i < boardSize; i++)
delete[] grid[i];

delete[] grid;
}

Board(Board &cboard)
{
boardSize = cboard.GetBoardSize();

grid = new int *[boardSize];
for (int i = 0; i < boardSize; i++)
grid[i] = new int[boardSize];

for (int i = 0; i < boardSize; i++)
for (int j = 0; j < boardSize; j++)
grid[i][j] = 0;

for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
grid[i][j] = cboard.GridValue(i, j);
}
}

turn = cboard.GetTurn();
emptyCells = cboard.emptyCells;
}

//SECTION - Spots
void addCells();

void PrintCells(vector <Cell> cells);

vector <Cell> GetEmpty()
{
return emptyCells;
}

int EmptySize()
{
return emptyCells.size();
}

void RemoveEmptyCell(int x, int y);


//SECTION - Board
int GetBoardSize()
{
return boardSize;
}

void PrintBoard();

bool isBoardFull();


//SECTION - Grid
int GridValue(int x, int y)
{
return grid[x][y];
}

int **GetGrid()
{
return grid;
}


//SECTION - Neighbours
stack <Cell> CheckNeighbours(int target, int x, int y);

bool isInVector(vector <Cell> v, Cell e);

bool isInVector(vector <PathCell> v, PathCell e);

void PrintNeighbours(stack <Cell> s);


//SECTION - Moving
bool AddTestMove(int playerIndex, int x, int y);

bool AddMove(int playerIndex, int x, int y);

bool IsValidInput(int x, int y);


//SECTION - Win Checking
int Evaluation(int player, int opponent);

bool CheckForWin(int playerType);

bool CheckLine(int playerType);

bool DepthFirstSearch(int playerType);


//SECTION - Other
int MoveNumber()
{ return (((boardSize * boardSize) - EmptySize()) + 1); }

bool SetTurn(int playerType)
{
turn = playerType;
return true;
}

bool CanWin()
{
if ((EmptySize() + (boardSize * 2 - 1)) <= (boardSize * boardSize))
return true;
return false;
}

void printCoord(int x, int y, bool el);

int GetTurn()
{
return turn;
}

};

#endif //BOARD_H
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ project(DSA_Hex_Game2)

set(CMAKE_CXX_STANDARD 14)

add_executable(DSA_Hex_Game2 main.cpp Cell.h AStar.h)
add_executable(DSA_Hex_Game2 main.cpp Cell.h Players/AStar.h Board/Board.h)

SET(CMAKE_BUILD_TYPE Debug)
14 changes: 5 additions & 9 deletions Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

struct Cell
{
int x, y; //coordinates for the cell
int x, y;

Cell(int _x, int _y) : x(_x), y(_y)
{}
};

struct Move
{
int x, y; //coordinates for the move
double v; //Cell value
int x, y;
double v;

Move(int _x, int _y, double _v) : x(_x), y(_y), v(_v)
{}
Expand All @@ -36,13 +36,9 @@ class PathCell

bool Compare(PathCell other)
{
// printf("\nC 1\n");
if (x == other.x && y == other.y && parent == other.parent && GetValue() == other.GetValue())
{
// printf("\nC 2\n");
return true;
}
// printf("\nC 3\n");

return false;
}

Expand All @@ -59,7 +55,7 @@ class ASCell //For use in AStar
double f, g, h;
ASCell *parent;
public:
int x, y; //coordinates for the move
int x, y;

void SetValues(double _h, double _g)
{
Expand Down
14 changes: 7 additions & 7 deletions HexGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ class HexGame
player[1] = p2;
}

int play(bool showAll, int startPlayer);
int Play(bool showAll, int startPlayer);
};

int HexGame::play(bool showAll, int startPlayer)
int HexGame::Play(bool showAll, int startPlayer)
{
if (!showAll)
system("CLS");

bool won = false; //By default, no one has won
board->PrintBoard(); //Print the game board to the screen
board->addCells(); //Initialise the list of free spots
board->setTurn(startPlayer);
board->SetTurn(startPlayer);

//Loop until someone has won or the board has been filled
while (!won && !board->isBoardFull())
{
int playerType = board->getTurn(); //Determine who's turn it is
int playerIndex = (playerType == player[0]->getType()) ? 0 : 1;
int playerType = board->GetTurn(); //Determine who's turn it is
int playerIndex = (playerType == player[0]->GetType()) ? 0 : 1;
int x = -1;
int y = -1;

Expand All @@ -49,7 +49,7 @@ int HexGame::play(bool showAll, int startPlayer)
system("CLS");

//Display the move that was made
cout << player[playerIndex]->getPlayerSymbol() << " played ";
cout << player[playerIndex]->GetSymbol() << " played ";
board->printCoord(x + 1, y + 1, false);

//Add the move to our board
Expand All @@ -60,7 +60,7 @@ int HexGame::play(bool showAll, int startPlayer)
won = board->CheckForWin(playerType); //Check to see if this player has just won. If so, end the game and tell the players
if (won)
{
cout << player[playerIndex]->getPlayerName() << " player wins!" << endl;
cout << player[playerIndex]->GetName() << " player wins!" << endl;
return playerType;
}
}
Expand Down
Loading

0 comments on commit 4b2b704

Please sign in to comment.