very rough function to load model from textual representation of

board.  It doesn't remove tiles from pool (or even check if they're
available).  Doesn't remove any model state already there.  What's
needed is to reset the game then load the model -- it's a game.h API
rather than just model.h.  I think.
This commit is contained in:
Eric House 2011-04-13 22:51:27 -07:00
parent 3cc105da0b
commit 92b255c35d
7 changed files with 69 additions and 2 deletions

View file

@ -173,6 +173,42 @@ model_writeToStream( ModelCtxt* model, XWStreamCtxt* stream )
} /* model_writeToStream */ } /* model_writeToStream */
#ifdef TEXT_MODEL #ifdef TEXT_MODEL
#include <glib.h>
#include <glib/gprintf.h>
# define EMPTY_CHAR '.'
void
model_setFromTextStream( ModelCtxt* model, XWStreamCtxt* stream )
{
const DictionaryCtxt* dict = model_getDictionary( model );
XP_U16 col, row;
for ( row = 0; row < model->nRows; ++row ) {
for ( col = 0; col < model->nCols; ++col ) {
gchar ch = stream_getU8( stream );
if ( EMPTY_CHAR == ch ) {
setModelTileRaw( model, col, row, TILE_EMPTY_BIT );
} else {
XP_Bool isBlank = g_ascii_islower( ch );
XP_UCHAR face[2] = { ch, '\0' };
if ( isBlank ) {
XP_UPPERSTR( face );
}
Tile tile = dict_tileForString( dict, face );
CellTile raw = tile & TILE_VALUE_MASK;
if ( isBlank ) {
raw |= TILE_BLANK_BIT;
}
setModelTileRaw( model, col, row, raw );
notifyBoardListeners( model, 0, col, row, XP_TRUE );
++model->vol.nTilesOnBoard;
/* Need to remove from pool too!!! */
}
}
(void)stream_getU8( stream ); /* skip \n */
}
}
void void
model_writeToTextStream( const ModelCtxt* model, XWStreamCtxt* stream ) model_writeToTextStream( const ModelCtxt* model, XWStreamCtxt* stream )
{ {
@ -183,7 +219,7 @@ model_writeToTextStream( const ModelCtxt* model, XWStreamCtxt* stream )
XP_U16 col, row; XP_U16 col, row;
for ( ii = 0; ii < width; ++ii ) { for ( ii = 0; ii < width; ++ii ) {
empty[ii] = '.'; empty[ii] = EMPTY_CHAR;
} }
for ( row = 0; row < model->nRows; ++row ) { for ( row = 0; row < model->nRows; ++row ) {
@ -572,7 +608,7 @@ setModelTileRaw( ModelCtxt* model, XP_U16 col, XP_U16 row, CellTile tile )
XP_ASSERT( col < MAX_COLS ); XP_ASSERT( col < MAX_COLS );
XP_ASSERT( row < MAX_ROWS ); XP_ASSERT( row < MAX_ROWS );
model->tiles[col][row] = tile; model->tiles[col][row] = tile;
} /* model_setTile */ } /* setModelTileRaw */
static CellTile static CellTile
getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row ) getModelTileRaw( const ModelCtxt* model, XP_U16 col, XP_U16 row )

View file

@ -111,6 +111,7 @@ void model_writeToStream( ModelCtxt* model, XWStreamCtxt* stream );
#ifdef TEXT_MODEL #ifdef TEXT_MODEL
void model_writeToTextStream( const ModelCtxt* model, XWStreamCtxt* stream ); void model_writeToTextStream( const ModelCtxt* model, XWStreamCtxt* stream );
void model_setFromTextStream( ModelCtxt* model, XWStreamCtxt* stream );
#endif #endif
void model_init( ModelCtxt* model, XP_U16 nCols, XP_U16 nRows ); void model_init( ModelCtxt* model, XP_U16 nCols, XP_U16 nRows );

View file

@ -504,6 +504,15 @@ createOrLoadObjects( GtkAppGlobals* globals )
} }
} }
#ifdef TEXT_MODEL
if ( !!params->boardFile ) {
XWStreamCtxt* stream =
streamFromFile( &globals->cGlobals, params->boardFile, globals );
model_setFromTextStream( globals->cGlobals.game.model, stream );
stream_destroy( stream );
}
#endif
#ifndef XWFEATURE_STANDALONE_ONLY #ifndef XWFEATURE_STANDALONE_ONLY
if ( !!globals->cGlobals.game.comms ) { if ( !!globals->cGlobals.game.comms ) {
comms_start( globals->cGlobals.game.comms ); comms_start( globals->cGlobals.game.comms );

View file

@ -272,6 +272,7 @@ typedef enum {
,CMD_CLOSESTDIN ,CMD_CLOSESTDIN
,CMD_QUITAFTER ,CMD_QUITAFTER
,CMD_BOARDSIZE ,CMD_BOARDSIZE
,CMD_BOARDFILE
,CMD_HIDEVALUES ,CMD_HIDEVALUES
,CMD_SKIPCONFIRM ,CMD_SKIPCONFIRM
,CMD_VERTICALSCORE ,CMD_VERTICALSCORE
@ -343,6 +344,7 @@ static CmdInfoRec CmdInfoRecs[] = {
,{ CMD_CLOSESTDIN, false, "close-stdin", "close stdin on start" } ,{ CMD_CLOSESTDIN, false, "close-stdin", "close stdin on start" }
,{ CMD_QUITAFTER, true, "quit-after", "exit <n> seconds after game's done" } ,{ CMD_QUITAFTER, true, "quit-after", "exit <n> seconds after game's done" }
,{ CMD_BOARDSIZE, true, "board-size", "board is <n> by <n> cells" } ,{ CMD_BOARDSIZE, true, "board-size", "board is <n> by <n> cells" }
,{ CMD_BOARDFILE, true, "board-file", "file holding 15x15 record of model" }
,{ CMD_HIDEVALUES, false, "hide-values", "show letters, not nums, on tiles" } ,{ CMD_HIDEVALUES, false, "hide-values", "show letters, not nums, on tiles" }
,{ CMD_SKIPCONFIRM, false, "skip-confirm", "don't confirm before commit" } ,{ CMD_SKIPCONFIRM, false, "skip-confirm", "don't confirm before commit" }
,{ CMD_VERTICALSCORE, false, "vertical", "scoreboard is vertical" } ,{ CMD_VERTICALSCORE, false, "vertical", "scoreboard is vertical" }
@ -1133,6 +1135,9 @@ main( int argc, char** argv )
case CMD_BOARDSIZE: case CMD_BOARDSIZE:
mainParams.gi.boardSize = atoi(optarg); mainParams.gi.boardSize = atoi(optarg);
break; break;
case CMD_BOARDFILE:
mainParams.boardFile = optarg;
break;
#ifdef XWFEATURE_BLUETOOTH #ifdef XWFEATURE_BLUETOOTH
case CMD_BTADDR: case CMD_BTADDR:
XP_ASSERT( conType == COMMS_CONN_NONE || XP_ASSERT( conType == COMMS_CONN_NONE ||

View file

@ -421,4 +421,13 @@ linux_lowerstr( XP_UCHAR* str )
++str; ++str;
} }
} }
void
linux_upperstr( XP_UCHAR* str )
{
while ( *str ) {
*str = toupper( *str );
++str;
}
}
#endif #endif

View file

@ -79,6 +79,9 @@ typedef struct LaunchParams {
#ifdef XWFEATURE_SLOW_ROBOT #ifdef XWFEATURE_SLOW_ROBOT
XP_U16 robotThinkMin, robotThinkMax; XP_U16 robotThinkMin, robotThinkMax;
#endif #endif
#ifdef TEXT_MODEL
char* boardFile;
#endif
DeviceRole serverRole; DeviceRole serverRole;

View file

@ -26,6 +26,7 @@
#include <string.h> /* memset */ #include <string.h> /* memset */
#include <assert.h> /* memset */ #include <assert.h> /* memset */
#include <unistd.h> #include <unistd.h>
#include <ctype.h>
#include <netinet/in.h> #include <netinet/in.h>
#ifdef PLATFORM_GTK #ifdef PLATFORM_GTK
@ -105,7 +106,10 @@ void linux_freep( void** ptrp );
#define XP_STRNCPY(s1,s2,len) strncpy((s1),(s2),(len)) #define XP_STRNCPY(s1,s2,len) strncpy((s1),(s2),(len))
#define XP_STRCMP(s1,s2) strcmp((s1),(s2)) #define XP_STRCMP(s1,s2) strcmp((s1),(s2))
void linux_lowerstr( XP_UCHAR* str ); void linux_lowerstr( XP_UCHAR* str );
void linux_upperstr( XP_UCHAR* str );
#define XP_LOWERSTR(str) linux_lowerstr(str) #define XP_LOWERSTR(str) linux_lowerstr(str)
#define XP_UPPERSTR(str) linux_upperstr(str)
#define XP_ISLOWER( ch ) islower(ch)
#define XP_RANDOM() random() #define XP_RANDOM() random()
#define XP_SNPRINTF snprintf #define XP_SNPRINTF snprintf