mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2024-12-28 09:58:30 +01:00
move randIntArray to strutils, and use instead of similar code to juggle tiles in tray.
This commit is contained in:
parent
4561d86601
commit
9259d5e91d
4 changed files with 27 additions and 51 deletions
|
@ -217,28 +217,6 @@ newg_attrChanged( NewGameCtx* ngc, NewGameAttr attr, NGValue value )
|
|||
adjustAllRows( ngc, XP_FALSE );
|
||||
}
|
||||
|
||||
/* This belongs as a util the tray juggling code can also call */
|
||||
static void
|
||||
randIntArray( XP_U16* rnums, XP_U16 count )
|
||||
{
|
||||
XP_U16 array[count]; /* C99 thing */
|
||||
XP_U16 i;
|
||||
|
||||
for ( i = 0; i < count; ++i ) {
|
||||
rnums[i] = array[i] = i; /* initial state for comparison */
|
||||
}
|
||||
|
||||
do {
|
||||
for ( i = count; i > 0 ; ) {
|
||||
XP_U16 rIndex = ((XP_U16)XP_RANDOM()) % i;
|
||||
XP_U16 tmp = rnums[rIndex];
|
||||
--i;
|
||||
rnums[rIndex] = rnums[i];
|
||||
rnums[i] = tmp;
|
||||
}
|
||||
} while ( XP_MEMCMP( rnums, array, count * sizeof(array[0]) ) == 0 );
|
||||
} /* randIntArray */
|
||||
|
||||
typedef struct DeepValue {
|
||||
NGValue value;
|
||||
NewGameColumn col;
|
||||
|
|
|
@ -177,6 +177,27 @@ emptyStringIfNull( XP_UCHAR* str )
|
|||
return !!str? str : (XP_UCHAR*)"";
|
||||
} /* emptyStringIfNull */
|
||||
|
||||
void
|
||||
randIntArray( XP_U16* rnums, XP_U16 count )
|
||||
{
|
||||
XP_U16 array[count]; /* C99 thing */
|
||||
XP_U16 i;
|
||||
|
||||
for ( i = 0; i < count; ++i ) {
|
||||
rnums[i] = array[i] = i; /* initial state for comparison */
|
||||
}
|
||||
|
||||
do {
|
||||
for ( i = count; i > 0 ; ) {
|
||||
XP_U16 rIndex = ((XP_U16)XP_RANDOM()) % i;
|
||||
XP_U16 tmp = rnums[rIndex];
|
||||
--i;
|
||||
rnums[rIndex] = rnums[i];
|
||||
rnums[i] = tmp;
|
||||
}
|
||||
} while ( XP_MEMCMP( rnums, array, count * sizeof(array[0]) ) == 0 );
|
||||
} /* randIntArray */
|
||||
|
||||
#ifdef CPLUS
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -47,6 +47,9 @@ void replaceStringIfDifferent( MPFORMAL XP_UCHAR** curLoc,
|
|||
|
||||
XP_UCHAR* emptyStringIfNull( XP_UCHAR* str );
|
||||
|
||||
/* Produce an array of ints 0..count-1, juggled */
|
||||
void randIntArray( XP_U16* rnums, XP_U16 count );
|
||||
|
||||
#ifdef CPLUS
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -565,31 +565,12 @@ invalTrayTilesBetween( BoardCtxt* board, XP_U16 tileIndex1,
|
|||
board_invalTrayTiles( board, bits );
|
||||
} /* invalTrayTilesBetween */
|
||||
|
||||
static void
|
||||
juggleTiles( Tile* tiles, XP_U16 nTiles )
|
||||
{
|
||||
Tile newTiles[MAX_TRAY_TILES];
|
||||
|
||||
XP_ASSERT( nTiles <= MAX_TRAY_TILES );
|
||||
XP_MEMCPY( newTiles, tiles, nTiles * sizeof(newTiles[0]) );
|
||||
|
||||
/* Pull out a tile at random, and swap the top tile down into its place so
|
||||
the array doesn't get sparse. */
|
||||
while ( nTiles > 0 ) {
|
||||
XP_U16 rIndex = ((XP_U16)XP_RANDOM()) % nTiles;
|
||||
*tiles++ = newTiles[rIndex];
|
||||
newTiles[rIndex] = newTiles[--nTiles];
|
||||
}
|
||||
|
||||
} /* juggleTiles */
|
||||
|
||||
XP_Bool
|
||||
board_juggleTray( BoardCtxt* board )
|
||||
{
|
||||
XP_Bool result = XP_FALSE;
|
||||
XP_S16 turn = board->selPlayer;
|
||||
|
||||
|
||||
if ( checkRevealTray( board ) ) {
|
||||
XP_S16 nTiles;
|
||||
XP_U16 dividerLoc = board->dividerLoc[board->selPlayer];
|
||||
|
@ -599,21 +580,14 @@ board_juggleTray( BoardCtxt* board )
|
|||
if ( nTiles > 1 ) {
|
||||
XP_S16 i;
|
||||
Tile tmpT[MAX_TRAY_TILES];
|
||||
Tile newT[MAX_TRAY_TILES];
|
||||
XP_U16 newT[MAX_TRAY_TILES];
|
||||
|
||||
/* create unique indices to be juggled; then juggle 'em until
|
||||
changed. */
|
||||
for ( i = 0; i < nTiles; ++i ) {
|
||||
tmpT[i] = newT[i] = (Tile)i;
|
||||
}
|
||||
do {
|
||||
juggleTiles( newT, nTiles );
|
||||
} while ( XP_MEMCMP( newT, tmpT, nTiles * sizeof(newT[0]) ) == 0 );
|
||||
randIntArray( newT, nTiles );
|
||||
|
||||
/* save copies of the tiles in juggled order */
|
||||
for ( i = 0; i < nTiles; ++i ) {
|
||||
tmpT[i] = model_getPlayerTile( model, turn,
|
||||
dividerLoc + newT[i] );
|
||||
(Tile)(dividerLoc + newT[i]) );
|
||||
}
|
||||
|
||||
/* delete tiles off right end; put juggled ones back on the other */
|
||||
|
|
Loading…
Reference in a new issue