diff --git a/xwords4/common/model.c b/xwords4/common/model.c index 8368fcc32..e991dd91a 100644 --- a/xwords4/common/model.c +++ b/xwords4/common/model.c @@ -58,7 +58,7 @@ static CellTile getModelTileRaw( const ModelCtxt* model, XP_U16 col, static void setModelTileRaw( ModelCtxt* model, XP_U16 col, XP_U16 row, CellTile tile ); static void assignPlayerTiles( ModelCtxt* model, XP_S16 turn, - TrayTileSet* tiles ); + const TrayTileSet* tiles ); static void makeTileTrade( ModelCtxt* model, XP_S16 player, TrayTileSet* oldTiles, TrayTileSet* newTiles ); static XP_S16 commitTurn( ModelCtxt* model, XP_S16 turn, @@ -1349,22 +1349,52 @@ model_moveTileOnTray( ModelCtxt* model, XP_S16 turn, XP_S16 indexCur, } /* model_moveTileOnTray */ static void -assignPlayerTiles( ModelCtxt* model, XP_S16 turn, TrayTileSet* tiles ) +assignPlayerTiles( ModelCtxt* model, XP_S16 turn, const TrayTileSet* tiles ) { - Tile* tilep = tiles->tiles; + const Tile* tilep = tiles->tiles; XP_U16 nTiles = tiles->nTiles; while ( nTiles-- ) { model_addPlayerTile( model, turn, -1, *tilep++ ); } -} /* model_addPlayerTiles */ +} /* assignPlayerTiles */ void -model_assignPlayerTiles( ModelCtxt* model, XP_S16 turn, TrayTileSet* tiles ) +model_assignPlayerTiles( ModelCtxt* model, XP_S16 turn, + const TrayTileSet* tiles ) { stack_addAssign( model->vol.stack, turn, tiles ); assignPlayerTiles( model, turn, tiles ); -} /* model_addPlayerTiles */ +} /* model_assignPlayerTiles */ + +void +model_sortTiles( ModelCtxt* model, XP_S16 turn ) +{ + PlayerCtxt* player = &model->players[turn]; + XP_S16 ii; + + TrayTileSet tiles = { .nTiles = 0 }; + XP_S16 nTiles; + + XP_ASSERT( turn >= 0 ); + + for ( nTiles = model_getNumTilesInTray( model, turn ) - 1; + nTiles >= 0; --nTiles ) { + Tile min = 0xFF; + XP_U16 minIndex = 0; + for ( ii = 0; ii <= nTiles; ++ii ) { + Tile tile = player->trayTiles.tiles[ii]; + if ( tile < min ) { + min = tile; + minIndex = ii; + } + } + tiles.tiles[tiles.nTiles++] = + removePlayerTile( model, turn, minIndex ); + } + + assignPlayerTiles( model, turn, &tiles ); +} /* model_sortTiles */ XP_U16 model_getNumTilesInTray( ModelCtxt* model, XP_S16 turn ) @@ -1373,7 +1403,7 @@ model_getNumTilesInTray( ModelCtxt* model, XP_S16 turn ) XP_ASSERT( turn >= 0 ); player = &model->players[turn]; return player->trayTiles.nTiles; -} /* model_getNumPlayerTiles */ +} /* model_getNumTilesInTray */ XP_U16 model_getNumTilesTotal( ModelCtxt* model, XP_S16 turn ) diff --git a/xwords4/common/model.h b/xwords4/common/model.h index 2390fd713..85fb1a071 100644 --- a/xwords4/common/model.h +++ b/xwords4/common/model.h @@ -126,7 +126,7 @@ void model_listPlacedBlanks( ModelCtxt* model, XP_U16 turn, XP_U16 model_getCellOwner( ModelCtxt* model, XP_U16 col, XP_U16 row ); void model_assignPlayerTiles( ModelCtxt* model, XP_S16 turn, - TrayTileSet* tiles ); + const TrayTileSet* tiles ); Tile model_getPlayerTile( ModelCtxt* model, XP_S16 turn, XP_S16 index ); Tile model_removePlayerTile( ModelCtxt* model, XP_S16 turn, XP_S16 index ); @@ -139,6 +139,7 @@ void model_moveTileOnTray( ModelCtxt* model, XP_S16 turn, XP_S16 indexCur, player. Don't even think about modifying the array!!!! */ const TrayTileSet* model_getPlayerTiles( ModelCtxt* model, XP_S16 turn ); +void model_sortTiles( ModelCtxt* model, XP_S16 turn ); XP_U16 model_getNumTilesInTray( ModelCtxt* model, XP_S16 turn ); XP_U16 model_getNumTilesTotal( ModelCtxt* model, XP_S16 turn ); void model_moveBoardToTray( ModelCtxt* model, XP_S16 turn, diff --git a/xwords4/common/movestak.c b/xwords4/common/movestak.c index 7976a516f..4bb44740b 100644 --- a/xwords4/common/movestak.c +++ b/xwords4/common/movestak.c @@ -293,7 +293,7 @@ stack_addTrade( StackCtxt* stack, XP_U16 turn, } /* stack_addTrade */ void -stack_addAssign( StackCtxt* stack, XP_U16 turn, TrayTileSet* tiles ) +stack_addAssign( StackCtxt* stack, XP_U16 turn, const TrayTileSet* tiles ) { StackEntry move; diff --git a/xwords4/common/movestak.h b/xwords4/common/movestak.h index 1bc0a3aad..c287aa807 100644 --- a/xwords4/common/movestak.h +++ b/xwords4/common/movestak.h @@ -79,7 +79,8 @@ void stack_addMove( StackCtxt* stack, XP_U16 turn, MoveInfo* moveInfo, void stack_addPhony( StackCtxt* stack, XP_U16 turn, MoveInfo* moveInfo ); void stack_addTrade( StackCtxt* stack, XP_U16 turn, TrayTileSet* oldTiles, TrayTileSet* newTiles ); -void stack_addAssign( StackCtxt* stack, XP_U16 turn, TrayTileSet* tiles ); +void stack_addAssign( StackCtxt* stack, XP_U16 turn, + const TrayTileSet* tiles ); XP_U16 stack_getNEntries( StackCtxt* stack ); diff --git a/xwords4/common/server.c b/xwords4/common/server.c index adec52e70..2f8b8fa80 100644 --- a/xwords4/common/server.c +++ b/xwords4/common/server.c @@ -1116,6 +1116,8 @@ client_readInitialMessage( ServerCtxt* server, XWStreamCtxt* stream ) /* remove what the server's assigned so we won't conflict later. */ pool_removeTiles( pool, &tiles ); + + model_sortTiles( model, i ); } SETSTATE( server, XWSTATE_INTURN ); @@ -1580,6 +1582,7 @@ assignTilesToAll( ServerCtxt* server ) TrayTileSet newTiles; fetchTiles( server, i, numAssigned, NULL, &newTiles ); model_assignPlayerTiles( model, i, &newTiles ); + model_sortTiles( model, i ); } } /* assignTilesToAll */ @@ -2028,6 +2031,7 @@ server_commitMove( ServerCtxt* server ) #endif model_commitTurn( model, turn, &newTiles ); + model_sortTiles( model, turn ); if ( !isLegalMove && !isClient ) { badWordMoveUndoAndTellUser( server, &server->illegalWordInfo );