From 372d722b588fcbdda340ff565dd44db4ab6e136a Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 10 Mar 2022 22:15:49 -0800 Subject: [PATCH] make number of tiles proportional to board size Until there's a way to specify specifically for each size, adjusting from the built-in 15x15 tileset works reasonably well. --- xwords4/android/jni/xwjni.c | 2 +- xwords4/common/dictnry.c | 25 ++++++++++++++++++++----- xwords4/common/dictnry.h | 5 +++-- xwords4/common/pool.c | 8 ++++---- xwords4/common/pool.h | 3 ++- xwords4/common/server.c | 7 ++++--- xwords4/linux/gtknewgame.c | 13 ++++++++----- 7 files changed, 42 insertions(+), 21 deletions(-) diff --git a/xwords4/android/jni/xwjni.c b/xwords4/android/jni/xwjni.c index 05c374dfc..81c97d048 100644 --- a/xwords4/android/jni/xwjni.c +++ b/xwords4/android/jni/xwjni.c @@ -1086,7 +1086,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1getTilesInfo DictionaryCtxt* dict = (DictionaryCtxt*)dictPtr; XWStreamCtxt* stream = mem_stream_make( MPPARM(mpool) globalState->vtMgr, NULL, 0, NULL ); - dict_writeTilesInfo( dict, stream ); + dict_writeTilesInfo( dict, 15, stream ); result = streamToJString( env, stream ); stream_destroy( stream, env ); diff --git a/xwords4/common/dictnry.c b/xwords4/common/dictnry.c index e16f98bf0..afaad90a3 100644 --- a/xwords4/common/dictnry.c +++ b/xwords4/common/dictnry.c @@ -477,10 +477,25 @@ dict_getNextTileString( const DictionaryCtxt* dict, Tile tile, } XP_U16 -dict_numTiles( const DictionaryCtxt* dict, Tile tile ) +dict_numTilesForSize( const DictionaryCtxt* dict, Tile tile, XP_U16 nCols ) { tile *= 2; - return dict->countsAndValues[tile]; + XP_U16 count = dict->countsAndValues[tile]; + + /* Wordlists are built assuming 15x15 boards. Different sized boards need + different numbers of tiles. The wordlist might provide for the size we + have. If not, let's adjust the count based on how many squares we have + vs. 15x15. + */ + XP_U16 pct = (nCols * nCols * 100) / (15 * 15); + XP_U16 newCount = count * pct / 100; + if ( 50 < (count * pct) % 100 ) { + ++newCount; + } + XP_LOGFF( "adjusted count %d to %d based on pct of %d", count, newCount, pct ); + count = newCount; + + return count; } /* dict_numTiles */ XP_U16 @@ -631,7 +646,7 @@ dict_tilesAreSame( const DictionaryCtxt* dict1, const DictionaryCtxt* dict2 ) } else if ( 0 != XP_STRCMP( face1, face2 ) ) { break; } - if ( dict_numTiles( dict1, ii ) != dict_numTiles( dict2, ii ) ) { + if ( dict_numTilesForSize( dict1, ii, 15 ) != dict_numTilesForSize( dict2, ii, 15 ) ) { break; } } @@ -662,12 +677,12 @@ ucharsToNarrow( const DictionaryCtxt* dict, XP_UCHAR* buf, XP_U16* bufsizep ) /* Summarize tile info in a way it can be presented to users */ void -dict_writeTilesInfo( const DictionaryCtxt* dict, XWStreamCtxt* stream ) +dict_writeTilesInfo( const DictionaryCtxt* dict, XP_U16 boardSize, XWStreamCtxt* stream ) { XP_U16 nFaces = dict_numTileFaces( dict ); for ( Tile tile = 0; tile < nFaces; ++tile ) { XP_U16 val = dict_getTileValue( dict, tile ); - XP_U16 count = dict_numTiles( dict, tile ); + XP_U16 count = dict_numTilesForSize( dict, tile, boardSize ); const XP_UCHAR* face = dict_getTileString( dict, tile ); XP_UCHAR buf[32]; XP_SNPRINTF( buf, VSIZE(buf), "%s\t%d\t%d\n", face, count, val ); diff --git a/xwords4/common/dictnry.h b/xwords4/common/dictnry.h index 71d8d4c8c..7815423b1 100644 --- a/xwords4/common/dictnry.h +++ b/xwords4/common/dictnry.h @@ -177,7 +177,7 @@ XP_Bool dict_tilesAreSame( const DictionaryCtxt* dict1, XP_Bool dict_hasBlankTile( const DictionaryCtxt* dict ); Tile dict_getBlankTile( const DictionaryCtxt* dict ); XP_U16 dict_getTileValue( const DictionaryCtxt* ctxt, Tile tile ); -XP_U16 dict_numTiles( const DictionaryCtxt* ctxt, Tile tile ); +XP_U16 dict_numTilesForSize( const DictionaryCtxt* ctxt, Tile tile, XP_U16 nCols ); XP_U16 dict_numTileFaces( const DictionaryCtxt* ctxt ); XP_U16 dict_getMaxTileChars( const DictionaryCtxt* ctxt ); @@ -207,7 +207,8 @@ const XP_UCHAR* dict_getDesc( const DictionaryCtxt* dict ); const XP_UCHAR* dict_getMd5Sum( const DictionaryCtxt* dict ); XP_Bool dict_hasDuplicates( const DictionaryCtxt* dict ); -void dict_writeTilesInfo( const DictionaryCtxt* ctxt, XWStreamCtxt* stream ); +void dict_writeTilesInfo( const DictionaryCtxt* ctxt, XP_U16 boardSize, + XWStreamCtxt* stream ); void dict_writeToStream( const DictionaryCtxt* ctxt, XWStreamCtxt* stream ); void dict_loadFromStream( DictionaryCtxt* dict, XWEnv xwe, XWStreamCtxt* stream ); diff --git a/xwords4/common/pool.c b/xwords4/common/pool.c index c1f97de43..2508a35b6 100644 --- a/xwords4/common/pool.c +++ b/xwords4/common/pool.c @@ -242,7 +242,7 @@ pool_getNTilesLeftFor( const PoolContext* pool, Tile tile ) } /* pool_remainingTileCount */ void -pool_initFromDict( PoolContext* pool, const DictionaryCtxt* dict ) +pool_initFromDict( PoolContext* pool, const DictionaryCtxt* dict, XP_U16 nCols ) { const XP_U16 numFaces = dict_numTileFaces( dict ); @@ -253,9 +253,9 @@ pool_initFromDict( PoolContext* pool, const DictionaryCtxt* dict ) numFaces * sizeof(pool->lettersLeft[0]) ); pool->numTilesLeft = 0; - for ( Tile ii = 0; ii < numFaces; ++ii ) { - XP_U16 numTiles = dict_numTiles( dict, ii ); - pool->lettersLeft[ii] = (XP_U8)numTiles; + for ( Tile tile = 0; tile < numFaces; ++tile ) { + XP_U16 numTiles = dict_numTilesForSize( dict, tile, nCols ); + pool->lettersLeft[tile] = (XP_U8)numTiles; pool->numTilesLeft += numTiles; } diff --git a/xwords4/common/pool.h b/xwords4/common/pool.h index fdaca3d56..f609d7637 100644 --- a/xwords4/common/pool.h +++ b/xwords4/common/pool.h @@ -39,7 +39,8 @@ XP_U16 pool_getNTilesLeftFor( const PoolContext* pool, Tile tile ); PoolContext* pool_make( MPFORMAL_NOCOMMA ); void pool_destroy( PoolContext* pool ); -void pool_initFromDict( PoolContext* pool, const DictionaryCtxt* dict ); +void pool_initFromDict( PoolContext* pool, const DictionaryCtxt* dict, + XP_U16 nCols ); void pool_writeToStream( PoolContext* pool, XWStreamCtxt* stream ); PoolContext* pool_makeFromStream( MPFORMAL XWStreamCtxt* stream ); diff --git a/xwords4/common/server.c b/xwords4/common/server.c index 2b6660029..50a67e7c9 100644 --- a/xwords4/common/server.c +++ b/xwords4/common/server.c @@ -2573,7 +2573,8 @@ makePoolOnce( ServerCtxt* server ) if ( server->pool == NULL ) { server->pool = pool_make( MPPARM_NOCOMMA(server->mpool) ); XP_STATUSF( "%s(): initing pool", __func__ ); - pool_initFromDict( server->pool, model_getDictionary(model)); + pool_initFromDict( server->pool, model_getDictionary(model), + server->vol.gi->boardSize ); } } @@ -4330,12 +4331,12 @@ server_formatDictCounts( ServerCtxt* server, XWEnv xwe, XWStreamCtxt* stream, stream_catString( stream, buf ); nChars = dict_numTileFaces( dict ); - + XP_U16 boardSize = server->vol.gi->boardSize; for ( tile = 0, nPrinted = 0; ; ) { XP_UCHAR buf[128]; XP_U16 count, value; - count = dict_numTiles( dict, tile ); + count = dict_numTilesForSize( dict, tile, boardSize ); if ( count > 0 ) { const XP_UCHAR* face = NULL; diff --git a/xwords4/linux/gtknewgame.c b/xwords4/linux/gtknewgame.c index 0350a727f..64b0f12b1 100644 --- a/xwords4/linux/gtknewgame.c +++ b/xwords4/linux/gtknewgame.c @@ -30,7 +30,8 @@ #include "gtkutils.h" #include "gtkask.h" -#define MAX_SIZE_CHOICES 32 +#define MIN_BOARD_SIZE 11 +#define MAX_BOARD_SIZE 23 #define BINGO_THRESHOLD "Bingo threshold" #define TRAY_SIZE "Tray size" @@ -168,7 +169,7 @@ size_combo_changed( GtkComboBox* combo, gpointer gp ) GtkNewGameState* state = (GtkNewGameState*)gp; gint index = gtk_combo_box_get_active( GTK_COMBO_BOX(combo) ); if ( index >= 0 ) { - state->nCols = MAX_COLS - index; + state->nCols = MIN_BOARD_SIZE + (index * 2); XP_LOGF( "set nCols = %d", state->nCols ); } } /* size_combo_changed */ @@ -348,14 +349,16 @@ addSizesRow( GtkNewGameState* state, GtkWidget* parent ) gtk_widget_set_sensitive( boardSizeCombo, FALSE ); } - for ( int ii = 0; ii < MAX_SIZE_CHOICES; ++ii ) { + int curEntry = 0; + for ( int siz = MIN_BOARD_SIZE; siz <= MAX_BOARD_SIZE; siz += 2 ) { char buf[10]; - XP_U16 siz = MAX_COLS - ii; + // XP_U16 siz = MAX_COLS - ii; snprintf( buf, sizeof(buf), "%dx%d", siz, siz ); gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT(boardSizeCombo), buf ); if ( siz == state->nCols ) { - gtk_combo_box_set_active( GTK_COMBO_BOX(boardSizeCombo), ii ); + gtk_combo_box_set_active( GTK_COMBO_BOX(boardSizeCombo), curEntry ); } + ++curEntry; } g_signal_connect( boardSizeCombo, "changed",