GP2HANOI::game Class Reference
[GP2Hanoi: Tower of Hanoi puzzle game]

Game implementation. More...

#include <game.h>

Collaboration diagram for GP2HANOI::game:

Collaboration graph
[legend]

Public Types

typedef std::list< unsigned
int > 
diskHeap
 Container for discs heaps.

Public Member Functions

 game (SDL_Surface *scr)
 Deafult constructor.
void init (std::string const &bgName, std::string const &clkSFXName, std::string const &errSFXName) throw (std::string const &)
 Resource initialization.
void play (void) throw (std::string const &)
 Plays the game.
 ~game ()
 Destructor.

Static Public Attributes

static int const MAXLVL = 8
 Max levels (max discs in a peg).

Private Member Functions

void blitCounter (void) throw (std::string const &)
 Blits the counter on the board.
SDL_Surface * createOptimizedImage (char **xpmData) throw (std::string const &)
 Creates and optimizes an image for blittering.
unsigned int getActivePeg (void) const
 Returns the active peg.
unsigned int getNumPegs (void) const
 Returns the number of pegs of the board.
SDL_Surface * loadOptimizedImage (std::string const &imgName) throw (std::string const &)
 Loads and optimizes an image for blittering.
Mix_Chunk * loadSoundEffect (std::string const &sfxName) throw (std::string const &)
 Loads a sound effect.
bool release (bool sound=true) throw (std::string const &)
 Drops a disk in the active peg.
void setActivePeg (unsigned int stick, bool sound=true) throw (std::string const &)
 Changes the active peg.
void show (void) throw (std::string const &)
 Shows the board on the destination surface.
bool take (bool sound=true) throw (std::string const &)
 Takes the disk on the top of the heap.
bool tokenTaken (void) const
 Returns true if a disc has been taken.

Private Attributes

SDL_Surface * abandonSpr
 sprites for the abandon game dialog box.
unsigned int activePeg
 Active Pegs.
SDL_Surface * background
 Background image.
SDL_Surface * base
 Board base sprite.
Mix_Chunk * clickSFX
 Click sound effect.
unsigned int counter
 Movements counter.
SDL_Surface * countSpr
 sprites for the movements counter.
SDL_Surface * discs
 discs sprites.
Mix_Chunk * errorSFX
 Invalid move sound effect.
std::vector< diskHeapheap
 Vector of heaps.
SDL_Surface * optSpr
 sprites for the options dialog box.
SDL_Surface * pegs
 peg sprites.
SDL_Surface * quitSpr
 sprites for the quit dialog box.
SDL_Surface * screen
 Destination surface.
unsigned int top
 Disk raised.

Detailed Description

Game implementation.

Author:
Kombo Morongo
Date:
20/03/2006
This class contains all the method that comprise game logic and board rendering.


Member Typedef Documentation

typedef std::list<unsigned int> GP2HANOI::game::diskHeap
 

Container for discs heaps.

Description
This data type is an ordered list from bottom to top where each element is a number that corresponds with the size of the disk contained in this position.


Constructor & Destructor Documentation

GP2HANOI::game::game SDL_Surface *  scr  ) 
 

Deafult constructor.

Parameters:
scr Pointer to the surface where the board will be rendered

GP2HANOI::game::~game  ) 
 

Destructor.

Warning:
Instance desturction implies freeing memory associated with surfaces and sound effects used for board rendering so, objects of this class should be always destroyed before closing SDL libraries.


Member Function Documentation

void GP2HANOI::game::blitCounter void   )  throw (std::string const &) [private]
 

Blits the counter on the board.

Descripcion
Renders the movements counter on the top right corner of the board.
Exceptions:
std::string const & Exception to throw in case of errors during rendering.

SDL_Surface * GP2HANOI::game::createOptimizedImage char **  xpmData  )  throw (std::string const &) [private]
 

Creates and optimizes an image for blittering.

Parameters:
xpmData Array to the xpm image to load.
Returns:
a pointer to the optimized surface.
Exceptions:
std::string const & Exception to throw detailing the reason of unrecoverable errors.

unsigned int GP2HANOI::game::getActivePeg void   )  const [private]
 

Returns the active peg.

Description
The active peg is the one from where discs will be taken or dropped.

unsigned int GP2HANOI::game::getNumPegs void   )  const [private]
 

Returns the number of pegs of the board.

void GP2HANOI::game::init std::string const &  bgName,
std::string const &  clkSFXName,
std::string const &  errSFXName
throw (std::string const &)
 

Resource initialization.

Parameters:
bgName Name of the file containing the background image
clkSFXName Nombre of the file containing the sound effect to play each time a disc is moved.
errSFXName Name of the file containing the sound effect to play whenever an invalid move is tried.
Exceptions:
std::string const & Exception to throw in case a resource could not be loaded.
Description
Load the resources to be used while playing the game.

SDL_Surface * GP2HANOI::game::loadOptimizedImage std::string const &  imgName  )  throw (std::string const &) [private]
 

Loads and optimizes an image for blittering.

Parameters:
imgName Name of the file to load.
Returns:
a pointer to the optimized surface.
Exceptions:
std::string const & Exception to throw detailing the reason of unrecoverable errors.

Mix_Chunk * GP2HANOI::game::loadSoundEffect std::string const &  sfxName  )  throw (std::string const &) [private]
 

Loads a sound effect.

Parameters:
sfxName Name of the file containing the sound effect to load.
Returns:
a pointer with the sound chunk.
Exceptions:
std::string const & Exception to throw detailing the reason of unrecoverable errors.

void GP2HANOI::game::play void   )  throw (std::string const &)
 

Plays the game.

Exceptions:
std::string const & Exception to throw in case of unrecoverable errors.
Description
This method contains the whole logic for playing the game. It returns only when the user has decided to quit the game.

bool GP2HANOI::game::release bool  sound = true  )  throw (std::string const &) [private]
 

Drops a disk in the active peg.

Parameters:
sound Flag for playing sound effects.
Returns:
true if the action was succesful (there was a disk taken and the disk on the top of the active peg was bigger than the disk to drop) or false on the contrary.
Exceptions:
std::string const & Exception to throw in case of errors during rendering.

void GP2HANOI::game::setActivePeg unsigned int  stick,
bool  sound = true
throw (std::string const &) [private]
 

Changes the active peg.

Parameters:
stick Number (0 .. getNumStricks() - 1) of the peg to activate.
sound Flag for playing sound effects.
Description
This method activates a peg. The active peg is the one from where discs will be taken or dropped.
Exceptions:
std::string const & Exception to throw in case the active peg is out of the (0 .. getNumStricks() - 1) range or unrecoverable errors occur.

void GP2HANOI::game::show void   )  throw (std::string const &) [private]
 

Shows the board on the destination surface.

Exceptions:
std::string const & Exception to throw in case of errors during rendering.

bool GP2HANOI::game::take bool  sound = true  )  throw (std::string const &) [private]
 

Takes the disk on the top of the heap.

Parameters:
sound Flag for playing sound effects.
Description
Takes the disc on the top of the heap in order to move it to another peg.
Returns:
true if the action was succesful (active heap was not empty and there was no previous disk taken) or false on the contrary.
Exceptions:
std::string const & Exception to throw in case of errors during rendering.

bool GP2HANOI::game::tokenTaken void   )  const [private]
 

Returns true if a disc has been taken.


Field Documentation

SDL_Surface* GP2HANOI::game::abandonSpr [private]
 

sprites for the abandon game dialog box.

Description
Pointer to the surface containing the sprites for the abandon game dialog box. Check the confirm::sprite property for details on sprite surface contents.

unsigned int GP2HANOI::game::activePeg [private]
 

Active Pegs.

Description
Variable that identifies which pegis active at any given time.

SDL_Surface* GP2HANOI::game::background [private]
 

Background image.

Description
Pointer to the surface where the background image was loaded.

SDL_Surface* GP2HANOI::game::base [private]
 

Board base sprite.

Description
Pointer to the surface where the image of the board table was loaded.

Mix_Chunk* GP2HANOI::game::clickSFX [private]
 

Click sound effect.

Description
Pointer to the sound effect to play whenever a button is pressed on any dialog box and when the user make a valid movement.

unsigned int GP2HANOI::game::counter [private]
 

Movements counter.

Description
Variable that keeps the movements count.

SDL_Surface* GP2HANOI::game::countSpr [private]
 

sprites for the movements counter.

Description
Pointer to the surface containing the sprites for the movements counter.

SDL_Surface* GP2HANOI::game::discs [private]
 

discs sprites.

Description
Pointer to the surface containing the sprites for the discs. It has eight sprites of the same size one above another representing the eight discs available for the game from the smaller (top) to the bigger (bottom).

Mix_Chunk* GP2HANOI::game::errorSFX [private]
 

Invalid move sound effect.

Description
Pointer to the sound effect to play whenever user tries an invalid movement.

std::vector<diskHeap> GP2HANOI::game::heap [private]
 

Vector of heaps.

Description
Vector of heaps containing the status of the game at any given time. Each element of the vector represents a peg from left to right. The elements in each heap contains the peg layout from top to bottom with a number representing disk size.

int const GP2HANOI::game::MAXLVL = 8 [static]
 

Max levels (max discs in a peg).

SDL_Surface* GP2HANOI::game::optSpr [private]
 

sprites for the options dialog box.

Description
Pointer to the surface containing the sprites for the options dialog box. Check the options::sprite property for details on sprite surface contents.

SDL_Surface* GP2HANOI::game::pegs [private]
 

peg sprites.

Description
Pointer to the surface containing the sprites for the pegs. It has two sprites of the same size side by side with the left sprite being the one to display for inactive pegs and the right sprite is the one to show for the active peg.

SDL_Surface* GP2HANOI::game::quitSpr [private]
 

sprites for the quit dialog box.

Description
Pointer to the surface containing the sprites for the quit dialog box. Check the confirm::sprite property for details on sprite surface contents.

SDL_Surface* GP2HANOI::game::screen [private]
 

Destination surface.

Description
Pointer to the surface where the dialog box will be desplayed.

unsigned int GP2HANOI::game::top [private]
 

Disk raised.

Description
Indication that a disk has been raised. If its value is 0, it means that no disk has been raised. Any other value indicates the size of the raised disk.
Note:
Once a disk is raised it will always be over the active peg.


The documentation for this class was generated from the following files: This project is hosted by SourceForge.net Logo