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
* 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,
WordNotifierInfo* notifyInfo );
void model_figureFinalScores( ModelCtxt* model, XP_S16* scores,
XP_S16* tilePenalties );
typedef struct _ScoresArray { XP_S16 arr[MAX_NUM_PLAYERS]; } ScoresArray;
void model_figureFinalScores( ModelCtxt* model, ScoresArray* scores,
ScoresArray* tilePenalties );
/* figureMoveScore is meant only for the engine's use */
XP_U16 figureMoveScore( const ModelCtxt* model, MoveInfo* moveInfo,

View file

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

View file

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

View file

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