Sokoban
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SinglePlayerMap Class Reference

Class which represents single player map. More...

#include <SinglePlayerMap.h>

Inheritance diagram for SinglePlayerMap:
Map

Public Member Functions

 SinglePlayerMap (std::ifstream &map_stream)
 Constructor. More...
 
bool complete () override
 Checks if map is completed (all barrels are on targets). More...
 
void restart () override
 Restarts level. More...
 
bool solve () override
 Tries to solve map using backtracking. More...
 
bool play_solution_next () override
 If map is solved do next step in solution. More...
 
element get_field_type (int x, int y) override
 Gets type of cell. More...
 
bool is_dead_position () override
 Determines if in actual state of game is possible to move with all barrels. More...
 
void right_move () override
 
void left_move () override
 
void up_move () override
 
void down_move () override
 
void right_move_second () override
 
void left_move_second () override
 
void up_move_second () override
 
void down_move_second () override
 
- Public Member Functions inherited from Map
virtual ~Map ()
 Virtual destructor. More...
 
int get_width () const
 Gets with of map (horizontal number of cells). More...
 
int get_height () const
 Gets with of map (vertical number of cells). More...
 

Private Member Functions

int move_sokoban (int v_x, int v_y)
 General method for sokoban movement. More...
 
bool solve (int steps)
 Tries to solve puzzle (backtracking) with at most MAX_STEPS. More...
 

Private Attributes

std::vector< std::tuple< int, int > > solution
 
GameState state
 
GameState start_state
 
int play_position
 
std::map< std::string, int > visited
 

Additional Inherited Members

- Static Public Attributes inherited from Map
static const char NUMBER_OF_FIELDS = 10
 
static const int MAX_STEPS = 1000
 
static const char EOM = 'x'
 
static const element GROUND = 0
 
static const element TARGET = 1
 
static const element BARREL = 2
 
static const element BARREL_TARGET = 3
 
static const element SOKOBAN = 4
 
static const element SOKOBAN_TARGET = 5
 
static const element WALL = 6
 
static const element GRASS = 7
 
static const element SOKOBAN_2 = 8
 
static const element SOKOBAN_2_TARGET = 9
 
- Protected Member Functions inherited from Map
 Map (std::ifstream &map_stream)
 Constructor. More...
 
bool is_valid_position (int x, int y)
 Checks if position (x,y) is in map grid. More...
 
- Protected Attributes inherited from Map
std::vector< std::vector< char > > game_grid
 
int number_of_barrels
 
int width
 
int height
 
TwoPlayerGameState init_state
 Game state which is used by the constructor for general map including a position of sokoban and position of barrels. More...
 

Detailed Description

Class which represents single player map.

Constructor & Destructor Documentation

◆ SinglePlayerMap()

SinglePlayerMap::SinglePlayerMap ( std::ifstream &  map_stream)

Constructor.

Creates the sigle player map from the input file. It uses the constructor from ancestor to reach it.

See also
Map::Map()
Exceptions
corrupted_file_errorfile is in wrong format
Parameters
map_streamInput stream which the map is made from.

Member Function Documentation

◆ complete()

bool SinglePlayerMap::complete ( )
inlineoverridevirtual

Checks if map is completed (all barrels are on targets).

Returns
Bool value which determines if map is completed.

Implements Map.

◆ down_move()

void SinglePlayerMap::down_move ( )
inlineoverridevirtual

Moves the sokoban down if it is possible.

Implements Map.

◆ down_move_second()

void SinglePlayerMap::down_move_second ( )
inlineoverridevirtual

This method does nothing. There is no second sokoban

Implements Map.

◆ get_field_type()

element SinglePlayerMap::get_field_type ( int  x,
int  y 
)
inlineoverridevirtual

Gets type of cell.

Parameters
xThe coordinate x of cell.
yThe coordinate y of cell.
Returns
Byte number which represents field type according to the constants.

Implements Map.

◆ is_dead_position()

bool SinglePlayerMap::is_dead_position ( )
overridevirtual

Determines if in actual state of game is possible to move with all barrels.

Returns
Bool value which determines if in actual state of game is possible to move with all barrels.

Implements Map.

◆ left_move()

void SinglePlayerMap::left_move ( )
inlineoverridevirtual

Moves the sokoban to the left if it is possible.

Implements Map.

◆ left_move_second()

void SinglePlayerMap::left_move_second ( )
inlineoverridevirtual

This method does nothing. There is no second sokoban

Implements Map.

◆ move_sokoban()

int SinglePlayerMap::move_sokoban ( int  v_x,
int  v_y 
)
private

General method for sokoban movement.

The direction is defined by vector. Tries to move sokoban in defined direction

Parameters
v_xThe part x of vector which represents move direction of Sokoban. It may be -1,0 or 1.
v_yThe part x of vector which represents move direction of Sokoban. It may be -1,0 or 1.
Returns
0 if Sokoban was moved, -1 if Sokoban cannot be moved

◆ play_solution_next()

bool SinglePlayerMap::play_solution_next ( )
inlineoverridevirtual

If map is solved do next step in solution.

Returns
True if map is sloved and steps is done, false if map is not solved yet.

Implements Map.

◆ restart()

void SinglePlayerMap::restart ( )
inlineoverridevirtual

Restarts level.

Implements Map.

◆ right_move()

void SinglePlayerMap::right_move ( )
inlineoverridevirtual

Moves the sokoban to the right if it is possible.

Implements Map.

◆ right_move_second()

void SinglePlayerMap::right_move_second ( )
inlineoverridevirtual

This method does nothing. There is no second sokoban

Implements Map.

◆ solve() [1/2]

bool SinglePlayerMap::solve ( )
inlineoverridevirtual

Tries to solve map using backtracking.

Returns
Bool value which determines if map was solved.

Implements Map.

◆ solve() [2/2]

bool SinglePlayerMap::solve ( int  steps)
private

Tries to solve puzzle (backtracking) with at most MAX_STEPS.

The solution is in vector solution.

Parameters
stepsNumber of steps from previous recursion step.
Returns
if solving succeed

◆ up_move()

void SinglePlayerMap::up_move ( )
inlineoverridevirtual

Moves the sokoban up if it is possible.

Implements Map.

◆ up_move_second()

void SinglePlayerMap::up_move_second ( )
inlineoverridevirtual

This method does nothing. There is no second sokoban

Implements Map.

Member Data Documentation

◆ play_position

int SinglePlayerMap::play_position
private

Variable determining which step from solution should be made while solution is playing.

◆ solution

std::vector<std::tuple<int, int> > SinglePlayerMap::solution
private

A sequence of steps defined by vector for solving puzzle

◆ start_state

GameState SinglePlayerMap::start_state
private

Default state of game - for restart.

◆ state

GameState SinglePlayerMap::state
private

State of game including a position of sokoban and position of barrels.

◆ visited

std::map<std::string, int> SinglePlayerMap::visited
private

The list of visited states (and number of steps which are visited in) for backtracking.


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