question about organizing a program

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

question about organizing a program

Postby Uri Blass » 07 Dec 2004, 17:23

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);
}
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: question about organizing a program

Postby Reinhard Scharnagl » 07 Dec 2004, 18:05

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.
Reinhard Scharnagl
 
Posts: 608
Joined: 01 Oct 2004, 08:36
Location: Klein-Gerau, Germany

Re: question about organizing a program

Postby Uri Blass » 07 Dec 2004, 18:28

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
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 15 guests