mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-29 08:34:37 +01:00
randIntArray runs once and returns whether array is modified. Tray calls
again until it is. newgame calls once: it's possible that juggling won't do anything. Still pending: do we tell users when nothing changed, or leave them to figure out that it's not a bug?
This commit is contained in:
parent
99571a7580
commit
c6598bef8a
4 changed files with 41 additions and 32 deletions
|
@ -235,31 +235,34 @@ newg_juggle( NewGameCtx* ngc )
|
|||
XP_U16 pos[MAX_NUM_PLAYERS];
|
||||
XP_U16 player;
|
||||
|
||||
/* Get a randomly juggled array of numbers 0..nPlayers-1. Then the number
|
||||
at pos[n] inicates where the entry currently at n should be. */
|
||||
randIntArray( pos, nPlayers );
|
||||
/* Get a randomly juggled array of numbers 0..nPlayers-1. Then the
|
||||
number at pos[n] inicates where the entry currently at n should
|
||||
be. */
|
||||
if ( randIntArray( pos, nPlayers ) ) {
|
||||
|
||||
|
||||
/* Deep-copy off to tmp storage. But skip lines that won't be
|
||||
moved in the juggle. */
|
||||
XP_MEMSET( &tmpPlayers, 0, sizeof(tmpPlayers) );
|
||||
for ( player = 0; player < nPlayers; ++player ) {
|
||||
if ( player != pos[player] ) {
|
||||
storePlayer( ngc, player, &tmpPlayers[player] );
|
||||
}
|
||||
}
|
||||
|
||||
for ( player = 0; player < nPlayers; ++player ) {
|
||||
if ( player != pos[player] ) {
|
||||
LocalPlayer* lp = &tmpPlayers[player];
|
||||
loadPlayer( ngc, pos[player], lp );
|
||||
if ( !!lp->name ) {
|
||||
XP_FREE( ngc->mpool, lp->name );
|
||||
}
|
||||
if ( !!lp->password ) {
|
||||
XP_FREE( ngc->mpool, lp->password );
|
||||
/* Deep-copy off to tmp storage. But skip lines that won't be moved
|
||||
in the juggle. */
|
||||
XP_MEMSET( &tmpPlayers, 0, sizeof(tmpPlayers) );
|
||||
for ( player = 0; player < nPlayers; ++player ) {
|
||||
if ( player != pos[player] ) {
|
||||
storePlayer( ngc, player, &tmpPlayers[player] );
|
||||
}
|
||||
}
|
||||
|
||||
for ( player = 0; player < nPlayers; ++player ) {
|
||||
if ( player != pos[player] ) {
|
||||
LocalPlayer* lp = &tmpPlayers[player];
|
||||
loadPlayer( ngc, pos[player], lp );
|
||||
if ( !!lp->name ) {
|
||||
XP_FREE( ngc->mpool, lp->name );
|
||||
}
|
||||
if ( !!lp->password ) {
|
||||
XP_FREE( ngc->mpool, lp->password );
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
XP_LOGF( "%s: no juggle this time", __FUNCTION__ );
|
||||
}
|
||||
}
|
||||
} /* newg_juggle */
|
||||
|
|
|
@ -177,25 +177,29 @@ emptyStringIfNull( XP_UCHAR* str )
|
|||
return !!str? str : (XP_UCHAR*)"";
|
||||
} /* emptyStringIfNull */
|
||||
|
||||
void
|
||||
XP_Bool
|
||||
randIntArray( XP_U16* rnums, XP_U16 count )
|
||||
{
|
||||
XP_U16 array[count]; /* C99 thing */
|
||||
XP_Bool changed = XP_FALSE;
|
||||
XP_U16 i;
|
||||
|
||||
for ( i = 0; i < count; ++i ) {
|
||||
rnums[i] = array[i] = i; /* initial state for comparison */
|
||||
rnums[i] = i;
|
||||
}
|
||||
|
||||
do {
|
||||
for ( i = count; i > 0 ; ) {
|
||||
XP_U16 rIndex = ((XP_U16)XP_RANDOM()) % i;
|
||||
for ( i = count; i > 0 ; ) {
|
||||
XP_U16 rIndex = ((XP_U16)XP_RANDOM()) % i;
|
||||
if ( --i != rIndex ) {
|
||||
XP_U16 tmp = rnums[rIndex];
|
||||
--i;
|
||||
rnums[rIndex] = rnums[i];
|
||||
rnums[i] = tmp;
|
||||
if ( !changed ) {
|
||||
changed = XP_TRUE;
|
||||
}
|
||||
}
|
||||
} while ( XP_MEMCMP( rnums, array, count * sizeof(array[0]) ) == 0 );
|
||||
}
|
||||
|
||||
return changed;
|
||||
} /* randIntArray */
|
||||
|
||||
#ifdef CPLUS
|
||||
|
|
|
@ -48,7 +48,7 @@ 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 );
|
||||
XP_Bool randIntArray( XP_U16* rnums, XP_U16 count );
|
||||
|
||||
#ifdef CPLUS
|
||||
}
|
||||
|
|
|
@ -583,7 +583,9 @@ board_juggleTray( BoardCtxt* board )
|
|||
Tile tmpT[MAX_TRAY_TILES];
|
||||
XP_U16 newT[MAX_TRAY_TILES];
|
||||
|
||||
randIntArray( newT, nTiles );
|
||||
/* loop until there'll be change */
|
||||
while ( !randIntArray( newT, nTiles ) ) {
|
||||
}
|
||||
|
||||
/* save copies of the tiles in juggled order */
|
||||
for ( i = 0; i < nTiles; ++i ) {
|
||||
|
|
Loading…
Add table
Reference in a new issue