Page 1 of 1

question about organizing a program

PostPosted: 07 Dec 2004, 17:23
by Uri Blass
How do you organize your program to avoid writing similiar code often?

I want to have function to calculate different informations about pgn files.

The problem is that I do not like to write the same code number of times and today I need to do it(the situation now is better than the situation in the past but still not ideal and I will be happy to get some good advise how to organize my program better)

Here are the functions that I use now to calculate sum of perft.
The problem is that if I want to write a function to calculate the sum of passed pawns that I have in the pgn file based on function that calculate the number of passed pawn in specific position I will need similiar code that is the same except the fact that instead of perft I use a different function and instead of sumperftpgn I will have sum_passed_pawn_pgn.

void calc_sum_perft(char *gamesname,int i)
{
int m;
BitBoard sumperftpgn=0;
if (i<1)
return;
setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
m=get_next_move_in_pgn();
while (m>0)
{
sumperftpgn+=perft(i-1);
makemove(m);
m=get_next_move_in_pgn();
}
printf("perft=%I64u \n",sumperftpgn);
}
void perftpgn(char *gamesname,int depth)
{
if (startpgn(gamesname))
calc_sum_perft(gamesname,depth);
}

Re: question about organizing a program

PostPosted: 07 Dec 2004, 18:05
by Reinhard Scharnagl
Hi Uri,

why not write a procedure iterating PGN positions and calling for each a dereferenced variable function pointer you could change matching your actual interests.

Reinhard.

Re: question about organizing a program

PostPosted: 07 Dec 2004, 18:28
by Uri Blass
Hi Reinhard,
I think that you are right and it is the best solution to use pointers for functions.

I have no experience with using pointers to functions so
I will look at my C book to see exactly how it is used in another case.

Uri