15Slide
Grid15::GridHelp Namespace Reference

Helps with movement of tiles, saving/loading, and other management features for a Grid. More...

Functions

void swapTile (const std::uint8_t tileY, const std::uint8_t tileX, Grid &grid)
 Swaps the tile at the given coordinates with the no tile of a Grid. More...
 
void swapTile (const std::uint8_t tileNum, Grid &grid)
 Swaps the tile given with the no tile of a Grid. More...
 
bool validMove (const std::uint8_t tileNum, const Grid &grid)
 Checks if the tile movement will be valid between a tile and the no tile of a Grid. More...
 
bool validMove (const std::uint8_t tileY, const std::uint8_t tileX, const Grid &grid)
 Checks if the tile movement will be valid between a tile and the no tile of a Grid. More...
 
bool hasWon (const Grid &grid)
 Checks if a Grid is won. More...
 
bool validGridArray (const Grid::gridArray_t &grid)
 Checks if a grid array is valid. More...
 
bool validIndex (const Grid::gridArray_t &grid, const Grid::index_t &index)
 Checks if an index is valid. More...
 
void safeCopy (const Grid::gridArray_t &newGrid, Grid &grid)
 Copies a Grid. Better than Grid::setGrid because it ensures validation and also creates a new index. More...
 
void safeCopy (const Grid &newGrid, Grid &grid)
 Copies a Grid. Better than Grid::setGrid because it ensures validation of the grid array and the index. More...
 
bool validGrid (const Grid &grid)
 Checks if a Grid is valid, including grid array and index. More...
 
Grid15::Grid::gridArray_t generateRandomGridArray ()
 Creates a new, random, and solvable grid array. More...
 
Grid generateRandomGrid ()
 Creates a new, random, and solvable Grid. More...
 
bool solvableGrid (const Grid::gridArray_t &gridArray)
 Checks if a grid is solvable or not. More...
 
bool solvableGrid (const Grid &grid)
 Checks if a grid is solvable or not. More...
 
void save (const std::string &saveFile, const Grid &grid)
 Saves a Grid to disk (not the index) More...
 
void load (const std::string &saveFile, Grid &grid)
 Loads a Grid from disk (and recreates the index) More...
 
void reIndex (Grid &grid)
 Reads the grid array of a Grid and updates its index. More...
 

Detailed Description

Helps with movement of tiles, saving/loading, and other management features for a Grid.

Bug:

Because of the way initilizer lists are in C++, and because this issue as noticed long into development, Y coordinates are switched with X in GridHelp.

This effects the order of function parameters, loops (i == y, j == x ) and arrays in both this library and other 15Slide code that uses it.

See https://github.com/JZJisawesome/15Slide/issues/22 for details and possible changes to this.

Author
John Jekel
Date
2017-2018

Function Documentation

◆ generateRandomGrid()

Grid Grid15::GridHelp::generateRandomGrid ( )

Creates a new, random, and solvable Grid.

Returns
A new Grid

◆ generateRandomGridArray()

Grid::gridArray_t Grid15::GridHelp::generateRandomGridArray ( )

Creates a new, random, and solvable grid array.

Returns
A new grid array

◆ hasWon()

bool Grid15::GridHelp::hasWon ( const Grid grid)

Checks if a Grid is won.

Parameters
gridThe Grid to use
Returns
If Grid::grid is equal to Grid::GOAL_GRID
Exceptions
std::invalid_argumentIf the grid array is not valid

◆ load()

void Grid15::GridHelp::load ( const std::string &  saveFile,
Grid grid 
)

Loads a Grid from disk (and recreates the index)

Parameters
saveFileThe save file
gridThe Grid to load to
Exceptions
std::ios_base::failureFrom std::ifstream; if a file operation goes wrong
std::invalid_argumentIf the newly loaded Grid is not valid

◆ reIndex()

void Grid15::GridHelp::reIndex ( Grid grid)

Reads the grid array of a Grid and updates its index.

Parameters
gridThe Grid to change
Exceptions
std::invalid_argumentIf the grid array is not valid

◆ safeCopy() [1/2]

void Grid15::GridHelp::safeCopy ( const Grid::gridArray_t newGrid,
Grid grid 
)

Copies a Grid. Better than Grid::setGrid because it ensures validation and also creates a new index.

Parameters
newGridThe new grid array to use
gridThe Grid to copy to
Exceptions
std::invalid_argumentIf the new Grid is invalid

◆ safeCopy() [2/2]

void Grid15::GridHelp::safeCopy ( const Grid newGrid,
Grid grid 
)

Copies a Grid. Better than Grid::setGrid because it ensures validation of the grid array and the index.

Parameters
newGridThe new grid to
gridThe Grid to copy to
Exceptions
std::invalid_argumentIf the new Grid is invalid

◆ save()

void Grid15::GridHelp::save ( const std::string &  saveFile,
const Grid grid 
)

Saves a Grid to disk (not the index)

Parameters
saveFileThe save file
gridThe Grid to save
Exceptions
std::ios_base::failureFrom std::ifstream; if a file operation goes wrong
std::invalid_argumentIf the Grid is not valid (ignores index)

◆ solvableGrid() [1/2]

bool Grid15::GridHelp::solvableGrid ( const Grid::gridArray_t gridArray)

Checks if a grid is solvable or not.

This function never contained any licenceless code: it was always 15Slide source, so the false comment that was here has been removed

Parameters
gridArrayThe grid array to check
Returns
If the grid is solvable or not
Exceptions
std::invalid_argumentIf the new Grid is invalid (index and/or grid array)

◆ solvableGrid() [2/2]

bool Grid15::GridHelp::solvableGrid ( const Grid grid)

Checks if a grid is solvable or not.

This function is a rewrite of the previous, unlicenced code, which I wanted to remove because I am worried about legal problems. I wrote it with information from https://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html, from scratch to be clear.

Parameters
gridThe Grid to check
Returns
If the Grid is solvable (index and/or grid array) or not
Exceptions
std::invalid_argumentIf the new Grid is invalid (index and/or grid array)

◆ swapTile() [1/2]

void Grid15::GridHelp::swapTile ( const std::uint8_t  tileY,
const std::uint8_t  tileX,
Grid grid 
)

Swaps the tile at the given coordinates with the no tile of a Grid.

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

◆ swapTile() [2/2]

void Grid15::GridHelp::swapTile ( const std::uint8_t  tileNum,
Grid grid 
)

Swaps the tile given with the no tile of a Grid.

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

◆ validGrid()

bool Grid15::GridHelp::validGrid ( const Grid grid)

Checks if a Grid is valid, including grid array and index.

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

◆ validGridArray()

bool Grid15::GridHelp::validGridArray ( const Grid::gridArray_t grid)

Checks if a grid array is valid.

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

◆ validIndex()

bool Grid15::GridHelp::validIndex ( const Grid::gridArray_t grid,
const Grid::index_t index 
)

Checks if an index is valid.

Parameters
gridThe grid array to compare the index to
indexThe index
Returns
If the index is valid (true) or not (false)
Exceptions
std::invalid_argumentIf the gridArray (not the index) is invalid

◆ validMove() [1/2]

bool Grid15::GridHelp::validMove ( const std::uint8_t  tileNum,
const Grid grid 
)

Checks if the tile movement will be valid between a tile and the no tile of a Grid.

Parameters
tileNumThe tile to check
gridThe Grid to use
Returns
If swapping the tile would work (true) or not (false)
Exceptions
std::invalid_argumentIf the Grid is not valid

◆ validMove() [2/2]

bool Grid15::GridHelp::validMove ( const std::uint8_t  tileY,
const std::uint8_t  tileX,
const Grid grid 
)

Checks if the tile movement will be valid between a tile and the no tile of a Grid.

Parameters
tileYThe tile's y coordinate
tileXThe tile's x coordinate
gridThe Grid to use
Returns
If swapping the tile would work (true) or not (false)
Exceptions
std::invalid_argumentIf the Grid array is not valid