Create type to hold array rather than passing array whose lenght is assumed.

This commit is contained in:
ehouse 2009-02-23 04:01:15 +00:00
parent 7581f35c4b
commit 14c1e9fd8b
4 changed files with 60 additions and 57 deletions

View file

@ -1,6 +1,6 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */ /* -*- fill-column: 78; compile-command: "cd ../linux && make -j MEMDEBUG=TRUE"; -*- */
/* /*
* Copyright 1997 - 2000 by Eric House (xwords@eehouse.org). All rights reserved. * Copyright 1997 - 2009 by Eric House (xwords@eehouse.org). All rights reserved.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -235,8 +235,9 @@ XP_Bool model_checkMoveLegal( ModelCtxt* model, XP_S16 player,
XWStreamCtxt* stream, XWStreamCtxt* stream,
WordNotifierInfo* notifyInfo ); WordNotifierInfo* notifyInfo );
void model_figureFinalScores( ModelCtxt* model, XP_S16* scores, typedef struct _ScoresArray { XP_S16 arr[MAX_NUM_PLAYERS]; } ScoresArray;
XP_S16* tilePenalties ); void model_figureFinalScores( ModelCtxt* model, ScoresArray* scores,
ScoresArray* tilePenalties );
/* figureMoveScore is meant only for the engine's use */ /* figureMoveScore is meant only for the engine's use */
XP_U16 figureMoveScore( const ModelCtxt* model, MoveInfo* moveInfo, XP_U16 figureMoveScore( const ModelCtxt* model, MoveInfo* moveInfo,

View file

@ -149,10 +149,10 @@ model_getPlayerScore( ModelCtxt* model, XP_S16 player )
* player. * player.
*/ */
void void
model_figureFinalScores( ModelCtxt* model, XP_S16* finalScoresP, model_figureFinalScores( ModelCtxt* model, ScoresArray* finalScoresP,
XP_S16* tilePenalties ) ScoresArray* tilePenaltiesP )
{ {
XP_S16 i, j; XP_S16 ii, jj;
XP_S16 penalties[MAX_NUM_PLAYERS]; XP_S16 penalties[MAX_NUM_PLAYERS];
XP_S16 totalPenalty; XP_S16 totalPenalty;
XP_U16 nPlayers = model->nPlayers; XP_U16 nPlayers = model->nPlayers;
@ -162,49 +162,51 @@ model_figureFinalScores( ModelCtxt* model, XP_S16* finalScoresP,
DictionaryCtxt* dict = model_getDictionary( model ); DictionaryCtxt* dict = model_getDictionary( model );
CurGameInfo* gi = model->vol.gi; CurGameInfo* gi = model->vol.gi;
XP_MEMSET( finalScoresP, 0, sizeof(*finalScoresP) * MAX_NUM_PLAYERS ); if ( !!finalScoresP ) {
XP_MEMSET( finalScoresP, 0, sizeof(*finalScoresP) );
}
totalPenalty = 0; totalPenalty = 0;
for ( player = model->players, i = 0; i < nPlayers; ++player, ++i ) { for ( player = model->players, ii = 0; ii < nPlayers; ++player, ++ii ) {
tray = model_getPlayerTiles( model, i ); tray = model_getPlayerTiles( model, ii );
penalties[i] = 0; penalties[ii] = 0;
/* if there are no tiles left and this guy's the first done, make a /* if there are no tiles left and this guy's the first done, make a
note of it in case he's to get a bonus. Note that this assumes note of it in case he's to get a bonus. Note that this assumes
only one player can be out of tiles. */ only one player can be out of tiles. */
if ( (tray->nTiles == 0) && (firstDoneIndex == -1) ) { if ( (tray->nTiles == 0) && (firstDoneIndex == -1) ) {
firstDoneIndex = i; firstDoneIndex = ii;
} else { } else {
for ( j = tray->nTiles-1; j >= 0; --j ) { for ( jj = tray->nTiles-1; jj >= 0; --jj ) {
penalties[i] += dict_getTileValue( dict, tray->tiles[j] ); penalties[ii] += dict_getTileValue( dict, tray->tiles[jj] );
} }
} }
/* include tiles in pending move too for the player whose turn it /* include tiles in pending move too for the player whose turn it
is. */ is. */
for ( j = player->nPending - 1; j >= 0; --j ) { for ( jj = player->nPending - 1; jj >= 0; --jj ) {
Tile tile = player->pendingTiles[j].tile; Tile tile = player->pendingTiles[jj].tile;
penalties[i] += dict_getTileValue(dict, (Tile)(tile & TILE_VALUE_MASK)); penalties[ii] += dict_getTileValue(dict,
(Tile)(tile & TILE_VALUE_MASK));
} }
totalPenalty += penalties[i]; totalPenalty += penalties[ii];
} }
/* now total everybody's scores */ /* now total everybody's scores */
for ( i = 0; i < nPlayers; ++i ) { for ( ii = 0; ii < nPlayers; ++ii ) {
XP_S16 score = model_getPlayerScore( model, i ); XP_S16 penalty = (ii == firstDoneIndex)? totalPenalty: -penalties[ii];
XP_S16 penalty;
penalty = (i == firstDoneIndex)? totalPenalty: -penalties[i]; if ( !!finalScoresP ) {
finalScoresP[i] = score + penalty; XP_S16 score = model_getPlayerScore( model, ii );
if ( gi->timerEnabled ) {
if ( !!tilePenalties ) { score -= player_timePenalty( gi, ii );
tilePenalties[i] = penalty; }
finalScoresP->arr[ii] = score + penalty;
} }
if ( gi->timerEnabled ) { if ( !!tilePenaltiesP ) {
penalty = player_timePenalty( gi, i ); tilePenaltiesP->arr[ii] = penalty;
finalScoresP[i] -= penalty;
} }
} }
} /* model_figureFinalScores */ } /* model_figureFinalScores */

View file

@ -47,7 +47,7 @@ void
drawScoreBoard( BoardCtxt* board ) drawScoreBoard( BoardCtxt* board )
{ {
if ( board->scoreBoardInvalid ) { if ( board->scoreBoardInvalid ) {
short i; short ii;
XP_U16 nPlayers = board->gi->nPlayers; XP_U16 nPlayers = board->gi->nPlayers;
@ -62,7 +62,7 @@ drawScoreBoard( BoardCtxt* board )
XP_U16 totalDim, extra, nShares, remWidth, remHeight, remDim; XP_U16 totalDim, extra, nShares, remWidth, remHeight, remDim;
DrawScoreData* dp; DrawScoreData* dp;
DrawScoreData datum[MAX_NUM_PLAYERS]; DrawScoreData datum[MAX_NUM_PLAYERS];
XP_S16 scores[MAX_NUM_PLAYERS]; ScoresArray scores;
XP_Bool isVertical = !board->scoreSplitHor; XP_Bool isVertical = !board->scoreSplitHor;
#ifdef KEYBOARD_NAV #ifdef KEYBOARD_NAV
XP_Rect cursorRect; XP_Rect cursorRect;
@ -103,10 +103,10 @@ drawScoreBoard( BoardCtxt* board )
/* Get the scores from the model or by calculating them based on /* Get the scores from the model or by calculating them based on
the end-of-game state. */ the end-of-game state. */
if ( board->gameOver ) { if ( board->gameOver ) {
model_figureFinalScores( model, scores, (XP_S16*)NULL ); model_figureFinalScores( model, &scores, NULL );
} else { } else {
for ( i = 0; i < nPlayers; ++i ) { for ( ii = 0; ii < nPlayers; ++ii ) {
scores[i] = model_getPlayerScore( model, i ); scores.arr[ii] = model_getPlayerScore( model, ii );
} }
} }
@ -114,27 +114,27 @@ drawScoreBoard( BoardCtxt* board )
/* figure spacing for each scoreboard entry */ /* figure spacing for each scoreboard entry */
XP_MEMSET( &datum, 0, sizeof(datum) ); XP_MEMSET( &datum, 0, sizeof(datum) );
for ( dp = datum, i = 0; i < nPlayers; ++i, ++dp ) { for ( dp = datum, ii = 0; ii < nPlayers; ++ii, ++dp ) {
LocalPlayer* lp = &board->gi->players[i]; LocalPlayer* lp = &board->gi->players[ii];
/* This is a hack! */ /* This is a hack! */
dp->dsi.lsc = board_ScoreCallback; dp->dsi.lsc = board_ScoreCallback;
dp->dsi.lscClosure = model; dp->dsi.lscClosure = model;
#ifdef KEYBOARD_NAV #ifdef KEYBOARD_NAV
if ( (i == cursorIndex) || focusAll ) { if ( (ii == cursorIndex) || focusAll ) {
dp->dsi.flags |= CELL_ISCURSOR; dp->dsi.flags |= CELL_ISCURSOR;
} }
#endif #endif
dp->dsi.playerNum = i; dp->dsi.playerNum = ii;
dp->dsi.totalScore = scores[i]; dp->dsi.totalScore = scores.arr[ii];
dp->dsi.isTurn = (i == curTurn); dp->dsi.isTurn = (ii == curTurn);
dp->dsi.name = emptyStringIfNull(lp->name); dp->dsi.name = emptyStringIfNull(lp->name);
dp->dsi.selected = board->trayVisState != TRAY_HIDDEN dp->dsi.selected = board->trayVisState != TRAY_HIDDEN
&& i==selPlayer; && ii==selPlayer;
dp->dsi.isRobot = lp->isRobot; dp->dsi.isRobot = lp->isRobot;
dp->dsi.isRemote = !lp->isLocal; dp->dsi.isRemote = !lp->isLocal;
dp->dsi.nTilesLeft = (nTilesInPool > 0)? -1: dp->dsi.nTilesLeft = (nTilesInPool > 0)? -1:
model_getNumTilesTotal( model, i ); model_getNumTilesTotal( model, ii );
draw_measureScoreText( board->draw, &scoreRect, draw_measureScoreText( board->draw, &scoreRect,
&dp->dsi, &dp->width, &dp->height ); &dp->dsi, &dp->width, &dp->height );
XP_ASSERT( dp->width <= scoreRect.width ); XP_ASSERT( dp->width <= scoreRect.width );
@ -171,10 +171,10 @@ drawScoreBoard( BoardCtxt* board )
board->remDim = remDim; board->remDim = remDim;
for ( dp = datum, i = 0; i < nPlayers; ++dp, ++i ) { for ( dp = datum, ii = 0; ii < nPlayers; ++dp, ++ii ) {
XP_Rect innerRect; XP_Rect innerRect;
XP_U16 dim = isVertical? dp->height:dp->width; XP_U16 dim = isVertical? dp->height:dp->width;
*adjustDim = board->pti[i].scoreDims = dim + extra; *adjustDim = board->pti[ii].scoreDims = dim + extra;
innerRect.width = dp->width; innerRect.width = dp->width;
innerRect.height = dp->height; innerRect.height = dp->height;
@ -186,9 +186,9 @@ drawScoreBoard( BoardCtxt* board )
draw_score_drawPlayer( board->draw, &innerRect, &scoreRect, draw_score_drawPlayer( board->draw, &innerRect, &scoreRect,
&dp->dsi ); &dp->dsi );
#ifdef KEYBOARD_NAV #ifdef KEYBOARD_NAV
XP_MEMCPY( &board->pti[i].scoreRects, &scoreRect, XP_MEMCPY( &board->pti[ii].scoreRects, &scoreRect,
sizeof(scoreRect) ); sizeof(scoreRect) );
if ( i == cursorIndex ) { if ( ii == cursorIndex ) {
cursorRect = scoreRect; cursorRect = scoreRect;
cursorRectP = &cursorRect; cursorRectP = &cursorRect;
} }

View file

@ -2485,11 +2485,11 @@ server_formatRemainingTiles( ServerCtxt* server, XWStreamCtxt* stream,
void void
server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream ) server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream )
{ {
XP_S16 scores[MAX_NUM_PLAYERS]; ScoresArray scores;
XP_S16 tilePenalties[MAX_NUM_PLAYERS]; ScoresArray tilePenalties;
XP_S16 highestIndex; XP_S16 highestIndex;
XP_S16 highestScore; XP_S16 highestScore;
XP_U16 place, nPlayers, i; XP_U16 place, nPlayers, ii;
XP_S16 curScore; XP_S16 curScore;
ModelCtxt* model = server->vol.model; ModelCtxt* model = server->vol.model;
const XP_UCHAR* addString = util_getUserString( server->vol.util, const XP_UCHAR* addString = util_getUserString( server->vol.util,
@ -2502,7 +2502,7 @@ server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream )
XP_ASSERT( server->nv.gameState == XWSTATE_GAMEOVER ); XP_ASSERT( server->nv.gameState == XWSTATE_GAMEOVER );
model_figureFinalScores( model, scores, tilePenalties ); model_figureFinalScores( model, &scores, &tilePenalties );
nPlayers = gi->nPlayers; nPlayers = gi->nPlayers;
@ -2514,10 +2514,10 @@ server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream )
highestScore = IMPOSSIBLY_LOW_SCORE; highestScore = IMPOSSIBLY_LOW_SCORE;
highestIndex = -1; highestIndex = -1;
for ( i = 0; i < nPlayers; ++i ) { for ( ii = 0; ii < nPlayers; ++ii ) {
if ( scores[i] > highestScore ) { if ( scores.arr[ii] > highestScore ) {
highestIndex = i; highestIndex = ii;
highestScore = scores[i]; highestScore = scores.arr[ii];
} }
} }
@ -2526,7 +2526,7 @@ server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream )
} else if ( place > 1 ) { } else if ( place > 1 ) {
stream_putString( stream, XP_CR ); stream_putString( stream, XP_CR );
} }
scores[highestIndex] = IMPOSSIBLY_LOW_SCORE; scores.arr[highestIndex] = IMPOSSIBLY_LOW_SCORE;
curScore = model_getPlayerScore( model, highestIndex ); curScore = model_getPlayerScore( model, highestIndex );
@ -2547,8 +2547,8 @@ server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream )
XP_SNPRINTF( tmpbuf, sizeof(tmpbuf), XP_SNPRINTF( tmpbuf, sizeof(tmpbuf),
(firstDone? addString:subString), (firstDone? addString:subString),
firstDone? firstDone?
tilePenalties[highestIndex]: tilePenalties.arr[highestIndex]:
-tilePenalties[highestIndex] ); -tilePenalties.arr[highestIndex] );
XP_SNPRINTF( buf, sizeof(buf), XP_SNPRINTF( buf, sizeof(buf),
(XP_UCHAR*)"[%d] %s: %d" XP_CR " (%d %s%s)", (XP_UCHAR*)"[%d] %s: %d" XP_CR " (%d %s%s)",