Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Computer chess schools of thought

Author: Sven Reichard

Date: 06:38:41 07/02/02

Go up one level in this thread


On July 01, 2002 at 13:13:26, Russell Reagan wrote:

>On July 01, 2002 at 05:46:58, Sven Reichard wrote:
>
>>let me offer a third approach. I don't mean to be preaching OO practices, I just
>>want to describe how I work
>
>I tried to use an OO approach recently, but I found it to not be very good for
>my chess program. For example, let's take my Piece class. In a classic approach,
>you would have some method to determine what type of piece it was. In the OO
>approach, I had to have 6 methods to determine what kind of piece it was. For
>example...
>
>class Piece {
>  public:
>    bool isPawn();
>    bool isKnight();
>    bool isBishop();
>    bool isRook();
>    bool isQueen();
>    bool isKing();
>    // ...
>};
>
>I didn't find this approach to be the "simplest". The approach I have found to
>be the simplest is to use an array of int's for the board, and then using
>COLOR() and TYPE() macros. Maybe there is a better way of doing my class. For
>example, in my class approach, you couldn't do a switch(pieceType) { ... },
>you'd have to do a string of if(piece.isPawn() { ... } else if
>(piece.isKnight()) { ... } and so on. I guess it's not so bad, and this might be
>where some of the performance hits come in. Another example I can think of is
>that with a straight binary value for a piece you can do quick attacks, but with
>a class like this I'm not sure how you would accomplish that. Perhaps I'm just
>not a very experienced OO programmer.
>
>Russell

IMO, an OO approach would look as follows:

class Piece;
class Pawn: public Piece;
class Queen: public Piece;
<etc>

(in fact, I use a couple of intermediate classes, since the move generation code
is basically the same for QPB on one hand and KN on the other, so that gives me
LongMovingPiece and ShortMovingPiece; pawns are separate).

Instead of
switch(pieceType)
{
  case 'P': material += 1; break;
  case 'N': material += 3; break;
  <...>
};

I write

material += piece->basicValue();

where int basicValue(void) is originally a virtual method of Piece, overloaded
by all subclasses.

At some point I realize that for this particular case, a) I call basicValue a
lot; b) it doesn't do anything but return a constant integer. Then the cost of
virtual function calls gets too high. Hence, I store the basic value as a member
variable (provided I don't construct and destruct millions of pieces per minute,
which I don't), and replace all the virtual functions by one inline method in
the base class. I don't need to change any of the other code, and I end up with
code that is in principle faster than the C switch statement.

In this way, many of the switch statements can be avoided. I still have methods
isPawn() etc., but I try to use them as little as possible.

Sven.



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.