Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Computer chess schools of thought

Author: Sune Fischer

Date: 10:32:15 07/01/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

Couldn't you make a type inside the classes?

class Piece{
public:
  int type;
  int color;
  int square;
...

}

class Piece::Pawn{
 public:
  int type;
  int square;
...

 void Pawn{type=PAWN};  // constructor
}

so that if you have a piece, it's type member variable will tell you what kind
it is?

The only big difference between this and C is that your pieces become classes
rather than just arrays.

-S.



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.