> Perhaps I've coded too much C++...
> I think a better exercise is to separate the logic
> from the data and think more in terms of
> modules/interfaces and let this drive the data structures.
mmmhhmm...
> In practice this would mean calling functions to
> work with matches and games rather than using
> globals/structs throughout the code.
Yes! Exactly!
> As an example look at this code from show.c:
>
> for( pl = lMatch.plNext; pl != &lMatch; pl = pl->plNext, ++n )
{
> pmr = (moverecord *) ( (list *) pl->p )->plNext->p;
> pmgi = &pmr->g;
> assert( pmr->mt == MOVE_GAMEINFO );
> ...
> It's pretty hard to work out what the code in this
> example does, which kind of says something...
Yes, you're pinpointing the problem with this example code.
> Anyway it could look something like:
>
> for (i = 1; i < getNumGames(); i++)
> {
> game *g = getGame(i);
> pmgi = getGameInfo(g);
> ...
Here's my (first) suggestion:
GList game = match->games;
for ( ; game ; game = game->next ){
GList move = game->moves;
for ( ; move ; move = move->next )
analyse(move); /* or do something else */
}
and a data structure for game and match like in the attached file.
-Øysteintypedef struct _gnubg_game {
/* ordinal number of the game within a match */
int i;
/* match score BEFORE the game */
int anScore[2];
/* this is the Crawford game */
int fCrawfordGame;
/* who won (-1 = unfinished) */
int fWinner;
/* how many points were scored by the winner */
int nPoints;
/* the game was ended by resignation */
int fResigned;
/* how many automatic doubles were rolled */
int nAutoDoubles;
/* Type of game */
bgvariation bgv;
/* Cube used in game */
int fCubeUse;
statcontext sc;
#if USE_TIMECONTROL
/* the game ended on time */
int fTimeout;
/* how many timeouts (clock expiry) have happened during the match */
int nTimeouts[2];
#endif
int length; /* number of moves (moverecords) in this game. */
GList *moves; /* linked list of moverecords */
GList *current_move; /* link to current move */
} gnubg_game;
typedef struct _gnubg_match {
player ap[ 2 ];
int nMatchTo;
bgvariation bgv;
int fCrawford;
int fCubeUse;
int fJacoby;
int length; /* number of games in this match. */
GList *games; /* linked list of games */
GList *current_game; /* Link to current game */
int fWinner; /* Do we need this? what about final score? */
} gnubg_match;
/* Ignore this code. I'm just playing around */
void
example_code(void){
/* gnubg_match match; // Global match? */
GList game = match->games;
printf("Player name: %s\n", match->ap[0]->name);
// loop through match:
for ( ; game ; game = game->next ){
GList move = game->moves;
for ( ; move ; move = move->next )
analyse(move);
}
}
_______________________________________________
Bug-gnubg mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gnubg