On 30 Jun 2006, at 13:48, Øystein Johansen wrote:
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 */
}
Hi, I'm new here, but have a lot of experience developing C programs
professionally. Anyway, the first solution (sorry, the attribution
has been lost) is almost always the correct approach; it's called
data abstraction and it makes things MUCH cleaner and means the
details of the datastructures don't matter to modules external to
the match/game/move. At the end there's some initial ideas about
what move.h, game.h and match.h might look like taking this approach.
What's the advantage of this for gnubg?
1. The header files are clear, clean and well-documented. As a
result the datatypes are easy to use.
2. The datatypes are insulated from the rest of the code, so it's
easy to change from linked lists to double linked lists or to
hashtables or even having matches stored in a database (not that
I think that is sensible :)
3. The python interface can be the same as the C interface. This
is a huge win as it means the interfaces get designed only once.
Obviously the choice of datastructures that you're suggesting
Øystein is still correct; it's just that they get hidden in
their appropriate module and are accessed via the functions
defined in their header file. If you would like some help
(re)designing the interfaces I'd be happy to oblige.
--
Paul Hankin
[Note, I haven't actually read any of the gnubg source code, so
the conventions I use for eg naming structs and pointers may
differ from the gnubg standard, but obviously the ideas here
can survive a style change].
***************** move.h:
typedef struct move_s move_s, *move_t;
/* return evaluation n for the move ( 0 <= n <
moveGetNumberOfEvaluations(move)) */
eval_t moveGetEvaluation(move_t move, int n);
/* how many different evaluations do we have for this move? */
int moveGetNumberOfEvaluations(move_t move);
/* return an evaluation of a particular type (if it exists) */
eval_t moveGetEvaluationOfType(move_t move, evalType_t evalType);
/* add an evaluation to a move. evalType specifies what sort of evaluation
ÊÊ we want (defined in eval.h) */
void moveAddEvaluation(move_t move, evalType_t evalType);
/* some move functions on moves... */
***************** game.h:
typedef struct game_s game_s, *game_t;
/* get the n'th move in the game (start at 0) */
move_t gameGetMove(game_t game, int n);
/* A different way to get a move: what's the next move after this one? */
move_t gameGetNextMove(game_t game, move_t move);
/* return the number of moves in this game */
int gameGetLength(game_t game);
/* Result for the game -------------------------------------------------------
*/
enum{
GAME_WINNER_0, /* player 0 won */
GAME_WINNER_1, /* player 1 won */
GAME_WINNER_NONE, /* noone won (perhaps the board exploded) */
GAME_WINNER_UNFINISHED /* the game is unfinished */
}GAME_WINNER_e;
enum{
GAME_ENDED_FINISH, Ê ÊÊ ÊÊ Ê/* game ended naturally */
GAME_ENDED_CUBE_PASS, Ê ÊÊ Ê/* game ended when one player cubed, the
other passed */
GAME_ENDED_BEAVER_PASSÊ ÊÊ Ê/* game ended when one player beavered, the
other passed */
}GAME_ENDED_e;
typedef struct gameResult_s{
GAME_WINNER_eÊwinner;
/* the rest only valid in winner is GAME_WINNER_0 or GAME_WINNER_1 */
GAME_ENDED_eÊreason; /* how did the game end? */
int amount; /* amount player 0 won or lost */Ê
int multiplier; /* 1=normal, 2=gammon, 3=backgammon */
int cube; /* value of cube at end */
}gameResult_s, *gameResult_t;
/* return the result of the game: */
gameResult_s gameGetResult(game_t game);
/* more game functions.. */
**************** match.h:
typedef struct match_s match_s, *match_t;
/* load a match from a file */
match_t matchLoad(FILE*f);
/* save a match to a file (returns 1 on success, 0 on failure) */
int matchSave(FILE*f);
/* how many games in this match? */
int matchGetLength(match_t match);
/* return the n'th game in the match (starting from 0) */
game_t matchGetGame(match_t match, int n);
/* return player 0 and player 1 */
player_t matchGetPlayer(match_t match, int n);
/* the match was up to how many points? */
intÊmatchGetLimit(match_t match);
/* more match functions.. */
_______________________________________________
Bug-gnubg mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gnubg