Game Development Reference
In-Depth Information
9. clPuzzle::Timer() calls the Update() method, which calculates new
coordinates for each of the tiles. This is required to allow the tiles to return
to their position once the player releases touches:
void clPuzzle::Timer( float DeltaSeconds )
{
for ( int i = 0; i != FColumns; i++ )
{
for ( int j = 0; j != FRows; j++ )
GetTile( i, j )->Update( DeltaSeconds );
}
}
10. The initial state of the game is generated in the Retoss() method:
void Puzzle::Retoss(int W, int H)
{
FColumns = W;
FRows = H;
FTiles.resize( FColumns * FRows );
11. First, we create all the tiles at their initial positions:
for ( int i = 0; i != FColumns; i++ )
for ( int j = 0; j != FRows; j++ )
FTiles[j * FColumns + i] =
clTile( i, FRows - j - 1, FColumns, FRows );
12. Then, we use Knuth shufle, also known as Fisher-Yates shufle
( http://en.wikipedia.org/wiki/Fisher-Yates_shuffle )
to generate a random permutation of the tiles:
for ( int i = 0; i != FColumns; i++ )
{
for ( int j = 0; j != FRows; j++ )
{
int NewI = Math::RandomInRange( i, FColumns - 1 );
int NewJ = Math::RandomInRange( j, FRows - 1 );
SwapTiles( i, j, NewI, NewJ );
}
}
}
 
Search WWH ::




Custom Search