15Slide
Grid15::GridManager Class Reference

Manages storage, initialization, movement of tiles, and saving/loading of a grid. DONT USE: IT'S DECPRECATED. More...

#include <GridManager.h>

Public Member Functions

 GridManager ()
 Creates a new grid, initialized to GridManager::GOAL_GRID. More...
 
 GridManager (const std::string &saveFile)
 Creates a new grid from a file. May throw exceptions from GridManager::load. More...
 
 GridManager (const std::array< std::array< std::uint8_t, 4 >, 4 > &newGrid)
 Creates a new grid from an existing grid. May throw exceptions from GridManager::setGrid. More...
 
 GridManager (const std::uint8_t newGrid[4][4])
 Creates a new grid from an existing grid. May throw exceptions from GridManager::setGrid. More...
 
void swapTile (const std::uint8_t tileX, const std::uint8_t tileY)
 Swaps the tile at the given coordinates with the no tile. More...
 
void swapTile (const std::uint8_t tileNum)
 Swaps the tile given with the no tile. More...
 
std::uint8_t getTile (const std::uint8_t tileX, const std::uint8_t tileY)
 Gets the tile at the given coordinates. More...
 
std::uint8_t getX (const std::uint8_t tileNum)
 Gets the x coordinate of the given tile. More...
 
std::uint8_t getY (const std::uint8_t tileNum)
 Gets the y coordinate of the given tile. More...
 
bool validMove (const std::int64_t tileNum)
 Checks if the tile movement will be valid. More...
 
bool validMove (const std::int64_t tileX, const std::int64_t tileY)
 Checks if the tile movement will be valid. More...
 
bool hasWon ()
 Checks if the grid is won. More...
 
void setGrid (const std::array< std::array< std::uint8_t, 4 >, 4 > &newGrid)
 Sets GridManager::gameGrid. More...
 
void setGrid (const std::uint8_t newGrid[4][4])
 Sets GridManager::gameGrid. More...
 
std::array< std::array< std::uint8_t, 4 >, 4 > getGrid ()
 Returns a copy of GridManager::gameGrid. More...
 
void save (const std::string &saveFile)
 Save the grid to disk. More...
 
void load (const std::string &saveFile)
 Load the grid from disk. More...
 

Static Public Member Functions

static bool validGrid (const std::array< std::array< std::uint8_t, 4 >, 4 > &grid)
 Checks if a grid is valid. More...
 
static bool validGrid (const std::uint8_t grid[4][4])
 Checks if a grid is valid. More...
 
static std::array< std::array< std::uint8_t, 4 >, 4 > generateRandomGrid ()
 Creates a new, random and valid grid, which can be assigned using GridManager::setGrid. More...
 

Static Public Attributes

static constexpr std::size_t NO_TILE {0}
 The number representing the lack of a tile. More...
 
static constexpr std::size_t TILE_MAX {15}
 The maximum tile number. More...
 
static constexpr std::size_t TILE_MIN {0}
 The mininum tile number. More...
 
static constexpr std::size_t X_MAX {3}
 The maximum x coordinate. More...
 
static constexpr std::size_t X_MIN {0}
 The mininum x coordinate. More...
 
static constexpr std::size_t Y_MAX {3}
 The maximum y coordinate. More...
 
static constexpr std::size_t Y_MIN {0}
 The mininum x coordinate. More...
 
static constexpr std::uint8_t GOAL_GRID [4][4]
 The grid GridManager::gameGrid must be for GridManager::hasWon to be true. More...
 

Private Member Functions

void reIndex ()
 Reads the current gameGrid and updates the index. More...
 

Private Attributes

std::array< std::array< std::uint8_t, 4 >, 4 > gameGrid {}
 The grid. More...
 
std::uint8_t index [16][2] {}
 The index to keep track of tile locations. More...
 

Detailed Description

Manages storage, initialization, movement of tiles, and saving/loading of a grid. DONT USE: IT'S DECPRECATED.

Author
John Jekel
Date
2017-2018
Deprecated:
Use Grid and GridHelp instead

Constructor & Destructor Documentation

◆ GridManager() [1/4]

GridManager::GridManager ( )

Creates a new grid, initialized to GridManager::GOAL_GRID.

◆ GridManager() [2/4]

GridManager::GridManager ( const std::string &  saveFile)

Creates a new grid from a file. May throw exceptions from GridManager::load.

Parameters
saveFileThe file to load from

◆ GridManager() [3/4]

GridManager::GridManager ( const std::array< std::array< std::uint8_t, 4 >, 4 > &  newGrid)

Creates a new grid from an existing grid. May throw exceptions from GridManager::setGrid.

Parameters
newGridThe grid to copy

◆ GridManager() [4/4]

GridManager::GridManager ( const std::uint8_t  newGrid[4][4])

Creates a new grid from an existing grid. May throw exceptions from GridManager::setGrid.

Parameters
newGridThe grid to copy

Member Function Documentation

◆ generateRandomGrid()

std::array< std::array< std::uint8_t, 4 >, 4 > GridManager::generateRandomGrid ( )
static

Creates a new, random and valid grid, which can be assigned using GridManager::setGrid.

Returns
A new grid

◆ getGrid()

std::array< std::array< std::uint8_t, 4 >, 4 > GridManager::getGrid ( )

Returns a copy of GridManager::gameGrid.

Returns
GridManager::gameGrid

◆ getTile()

std::uint8_t GridManager::getTile ( const std::uint8_t  tileX,
const std::uint8_t  tileY 
)

Gets the tile at the given coordinates.

Parameters
tileXThe x coordinate
tileYThe y coordinate
Returns
The tile
Exceptions
std::out_of_rangeFrom std::array::at, if the coordinates are off the grid (see GridManager::X_MAX, GridManager::X_MIN, GridManager::Y_MAX and GridManager::Y_MIN)

◆ getX()

std::uint8_t GridManager::getX ( const std::uint8_t  tileNum)

Gets the x coordinate of the given tile.

Parameters
tileNumThe tile
Returns
The x coordinate
Exceptions
std::invalid_argumentIf tile if greater than GridManager::TILE_MAX or less than GridManager::TILE_MIN

◆ getY()

std::uint8_t GridManager::getY ( const std::uint8_t  tileNum)

Gets the y coordinate of the given tile.

Parameters
tileNumThe tile
Returns
The y coordinate
Exceptions
std::invalid_argumentIf tile if greater than GridManager::TILE_MAX or less than GridManager::TILE_MIN

◆ hasWon()

bool GridManager::hasWon ( )

Checks if the grid is won.

Returns
If GridManager::gameGrid is equal to GridManager::GOAL_GRID

◆ load()

void GridManager::load ( const std::string &  saveFile)

Load the grid from disk.

Parameters
saveFileThe save file

◆ reIndex()

void GridManager::reIndex ( )
private

Reads the current gameGrid and updates the index.

◆ save()

void GridManager::save ( const std::string &  saveFile)

Save the grid to disk.

Parameters
saveFileThe save file

◆ setGrid() [1/2]

void GridManager::setGrid ( const std::array< std::array< std::uint8_t, 4 >, 4 > &  newGrid)

Sets GridManager::gameGrid.

Parameters
newGridThe new grid to use
Exceptions
std::invalid_argumentIf the grid is not valid

◆ setGrid() [2/2]

void GridManager::setGrid ( const std::uint8_t  newGrid[4][4])

Sets GridManager::gameGrid.

Parameters
newGridThe new grid to use
Exceptions
std::invalid_argumentIf the grid is not valid

◆ swapTile() [1/2]

void GridManager::swapTile ( const std::uint8_t  tileX,
const std::uint8_t  tileY 
)

Swaps the tile at the given coordinates with the no tile.

Parameters
tileXThe x coordinate
tileYThe y coordinate
Exceptions
std::invalid_argumentThe tile being moved is invalid or not next to the no tile

◆ swapTile() [2/2]

void GridManager::swapTile ( const std::uint8_t  tileNum)

Swaps the tile given with the no tile.

Parameters
tileNumThe tile to swap
Exceptions
std::invalid_argumentThe tile being moved is invalid or not next to the no tile

◆ validGrid() [1/2]

bool GridManager::validGrid ( const std::array< std::array< std::uint8_t, 4 >, 4 > &  grid)
static

Checks if a grid is valid.

Parameters
gridThe grid to check
Returns
If the grid is valid (true) or not (false)

◆ validGrid() [2/2]

bool GridManager::validGrid ( const std::uint8_t  grid[4][4])
static

Checks if a grid is valid.

Parameters
gridThe grid to check
Returns
If the grid is valid (true) or not (false)

◆ validMove() [1/2]

bool GridManager::validMove ( const std::int64_t  tileNum)

Checks if the tile movement will be valid.

Parameters
tileNumThe tile to check
Returns
If swapping the tile would work (true) or not (false)

◆ validMove() [2/2]

bool GridManager::validMove ( const std::int64_t  tileX,
const std::int64_t  tileY 
)

Checks if the tile movement will be valid.

Parameters
tileXThe tile's x coordinate
tileYThe tile's y coordinate
Returns
If swapping the tile would work (true) or not (false)

Member Data Documentation

◆ gameGrid

std::array<std::array<std::uint8_t, 4>, 4> Grid15::GridManager::gameGrid {}
private

The grid.

◆ GOAL_GRID

constexpr std::uint8_t Grid15::GridManager::GOAL_GRID[4][4]
static
Initial value:
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, GridManager::NO_TILE}
}

The grid GridManager::gameGrid must be for GridManager::hasWon to be true.

◆ index

std::uint8_t Grid15::GridManager::index[16][2] {}
private

The index to keep track of tile locations.

◆ NO_TILE

constexpr std::size_t Grid15::GridManager::NO_TILE {0}
static

The number representing the lack of a tile.

◆ TILE_MAX

constexpr std::size_t Grid15::GridManager::TILE_MAX {15}
static

The maximum tile number.

◆ TILE_MIN

constexpr std::size_t Grid15::GridManager::TILE_MIN {0}
static

The mininum tile number.

◆ X_MAX

constexpr std::size_t Grid15::GridManager::X_MAX {3}
static

The maximum x coordinate.

◆ X_MIN

constexpr std::size_t Grid15::GridManager::X_MIN {0}
static

The mininum x coordinate.

◆ Y_MAX

constexpr std::size_t Grid15::GridManager::Y_MAX {3}
static

The maximum y coordinate.

◆ Y_MIN

constexpr std::size_t Grid15::GridManager::Y_MIN {0}
static

The mininum x coordinate.


The documentation for this class was generated from the following files: