From 5104ca6bc5b632fff22e637d731115976e84e2f5 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 5 May 2012 17:33:07 -0700 Subject: [PATCH] always sort tiles prior to including in hash -- as a fix for the case where a game is opened that was created by an earlier version that didn't save assigned tiles in sorted order. --- xwords4/common/model.c | 29 ----------------------------- xwords4/common/movestak.c | 8 +++++--- xwords4/common/strutils.c | 27 +++++++++++++++++++++++++++ xwords4/common/strutils.h | 2 ++ 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/xwords4/common/model.c b/xwords4/common/model.c index 5032a7779..42a2880c2 100644 --- a/xwords4/common/model.c +++ b/xwords4/common/model.c @@ -72,8 +72,6 @@ static void buildModelFromStack( ModelCtxt* model, StackCtxt* stack, MovePrintFuncPost mpfpo, void* closure ); static void setPendingCounts( ModelCtxt* model, XP_S16 turn ); static XP_S16 setContains( const TrayTileSet* tiles, Tile tile ); -static void sortTiles( TrayTileSet* dest, const TrayTileSet* src ); -static void removeTile( TrayTileSet* tiles, XP_U16 index ); static void loadPlayerCtxt( const ModelCtxt* model, XWStreamCtxt* stream, XP_U16 version, PlayerCtxt* pc ); static void writePlayerCtxt( const ModelCtxt* model, XWStreamCtxt* stream, @@ -2413,16 +2411,6 @@ writePlayerCtxt( const ModelCtxt* model, XWStreamCtxt* stream, } } /* writePlayerCtxt */ -static void -removeTile( TrayTileSet* tiles, XP_U16 index ) -{ - XP_U16 ii; - --tiles->nTiles; - for ( ii = index; ii < tiles->nTiles; ++ii ) { - tiles->tiles[ii] = tiles->tiles[ii+1]; - } -} - static XP_S16 setContains( const TrayTileSet* tiles, Tile tile ) { @@ -2439,23 +2427,6 @@ setContains( const TrayTileSet* tiles, Tile tile ) return result; } -static void -sortTiles( TrayTileSet* dest, const TrayTileSet* src ) -{ - TrayTileSet tmp = *src; - dest->nTiles = 0; - while ( 0 < tmp.nTiles ) { - XP_U16 ii, smallest; - for ( smallest = ii = 0; ii < tmp.nTiles; ++ii ) { - if ( tmp.tiles[ii] < tmp.tiles[smallest] ) { - smallest = ii; - } - } - dest->tiles[dest->nTiles++] = tmp.tiles[smallest]; - removeTile( &tmp, smallest ); - } -} - #ifdef CPLUS } #endif diff --git a/xwords4/common/movestak.c b/xwords4/common/movestak.c index 32fcecef1..fa47b251f 100644 --- a/xwords4/common/movestak.c +++ b/xwords4/common/movestak.c @@ -104,9 +104,11 @@ static XP_U32 augmentFor( XP_U32 hash, const StackEntry* entry ) { switch( entry->moveType ) { - case ASSIGN_TYPE: - hash = augmentHash( hash, (XP_U8*)&entry->u.assign, - sizeof(entry->u.assign) ); + case ASSIGN_TYPE: { + TrayTileSet tiles; + sortTiles( &tiles, &entry->u.assign.tiles ); + hash = augmentHash( hash, (XP_U8*)&tiles, sizeof(tiles) ); + } break; case MOVE_TYPE: hash = augmentHash( hash, (XP_U8*)&entry->u.move, diff --git a/xwords4/common/strutils.c b/xwords4/common/strutils.c index 315b55227..cc1c762d9 100644 --- a/xwords4/common/strutils.c +++ b/xwords4/common/strutils.c @@ -73,6 +73,33 @@ traySetFromStream( XWStreamCtxt* stream, TrayTileSet* ts ) ts->nTiles = (XP_U8)nTiles; } /* traySetFromStream */ +void +removeTile( TrayTileSet* tiles, XP_U16 index ) +{ + XP_U16 ii; + --tiles->nTiles; + for ( ii = index; ii < tiles->nTiles; ++ii ) { + tiles->tiles[ii] = tiles->tiles[ii+1]; + } +} + +void +sortTiles( TrayTileSet* dest, const TrayTileSet* src ) +{ + TrayTileSet tmp = *src; + dest->nTiles = 0; + while ( 0 < tmp.nTiles ) { + XP_U16 ii, smallest; + for ( smallest = ii = 0; ii < tmp.nTiles; ++ii ) { + if ( tmp.tiles[ii] < tmp.tiles[smallest] ) { + smallest = ii; + } + } + dest->tiles[dest->nTiles++] = tmp.tiles[smallest]; + removeTile( &tmp, smallest ); + } +} + #if 0 static void signedToStream( XWStreamCtxt* stream, XP_U16 nBits, XP_S32 num ) diff --git a/xwords4/common/strutils.h b/xwords4/common/strutils.h index 9fa036342..b02671541 100644 --- a/xwords4/common/strutils.h +++ b/xwords4/common/strutils.h @@ -34,6 +34,8 @@ XP_U16 bitsForMax( XP_U32 n ); void traySetToStream( XWStreamCtxt* stream, const TrayTileSet* ts ); void traySetFromStream( XWStreamCtxt* stream, TrayTileSet* ts ); +void sortTiles( TrayTileSet* dest, const TrayTileSet* src ); +void removeTile( TrayTileSet* tiles, XP_U16 index ); XP_S32 signedFromStream( XWStreamCtxt* stream, XP_U16 nBits ); void signedToStream( XWStreamCtxt* stream, XP_U16 nBits, XP_S32 num );