Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: debugging question

Author: Russell Reagan

Date: 16:25:34 08/14/03

Go up one level in this thread


On August 14, 2003 at 01:59:19, Uri Blass wrote:

>Thanks for the suggestion butI do not see how it is a quick fix because
>I need to change almost every line of the program
>
>for example I have the following global varaibles in my program
>
>int queens[9][2];
>int rooks[10][2];
>int bishops[10][2];
>int knights[10][2];
>int pawns[8][2];
>int numqueens[2];
>int numrooks[2];
>int numbishops[2];
>int numknights[2];
>int numpawns[2];
>
>I guess that you suggest to change it to the following:
>
>typedef struct
>{
>	int queens[9][2] queens;
>        int rooks[10][2] rooks;
>        int bishops[10][2] bishops;
>        int knights[10][2] knights;
>        int pawns[8][2]    pawns;
>        int numqueens[2]   numqueens;
>
>...
>        int numpawns[2] numpawns;
>}
>info;
>
>It means that replacing every queens by info.queens is wrong because it may
>also replace numqueens by numinfo.queens

I think that you could do it using a regular expression search and replace,
which the VC++ IDE will do, or sed, or vi.


>I am also not sure if the computer likes big structs and I am afraid that the
>fact that the varaibles are in big struct may force the computer not to do some
>optimizations.

I can't say for sure, but I would guess that it would be less than 1% if you
even lost any speed, and you gain other things which makes it worth it.


>I do not know C++ but
>I find nothing bad with global varaibles.
>
>I find them more convenient to use instead of thinking exactly
>where I use a varaible because if I later decide that I want to use it in
>another function there is no problem.

Creating classes or structs is a good idea because it allows you to make changes
more easily later on. For instance, if you wanted to make Movei support multiple
processors, you would have a very hard time doing that without making a lot of
changes to your program that has global variables all over the place. If you had
a nice modular game class, or better yet a search class, you could start up
multiple searches by adding only a few lines of code.

Even if you don't see any major changes you want to make right now, it is still
a good idea to use structs or classes because you probably will want to make
some changes at some point. Basically it is better to have a "new game" function
that takes a pointer to a game struct or class than it is to have a global
variable. For instance: void NewGame (Game * game); is better than void
NewGame(); because you can use it on ANY Game, which will allow you to make
changes more easily later on.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.