Re: oops for chess board game.
On May 3, 3:42 pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
sam_...@yahoo.co.in a =E9crit :
I'm working on developing a chess game and i decided to use c++ for
its object oriented approach.
OO is only one paradigm of C++. Saying that you choose C++ for its OO
approach is like saying you bought a statue for the shadow it casts.
You mean, of course, that refusing to consider the OO approach
is like only considering the shadow. Support for OO is one
reason to prefer C++ to some other languages.
I have a bass class unit and is
inherited to distinct number of units (like king, queen, pawns
etc...), i will have pure virtual functions in the base class for
move(), attack() etc
attack()?!?!?
Is your unit likely to loose the fight when attacking ?
The verb attack is very much a part of the vocabulary of chess.
I'm not sure, however, that it is a fundamental concept of a
piece.
For the rest, using a base class Piece, with different derived
classes for the different types of pieces, could be a good
approach. Depending on the rest of the design, of course; the
first thing to do is to define the overall program structure,
and see what fits in best. And of course, one shouldn't forget
that chess was not designed with OO in mind; the position of
other pieces, and even history, play a role in determining which
moves are legal.
which are applicable to all units and would
override them in the child classes. Function specific to pawn like
recovering of another unit once it reaches the end of the chess board
will be implemented in the child class specific to pawn.
This looks awkward. The units don't act on themselves obeing some local
rules.
No, but the rules do depend on the type of the piece.
Obviously, a function getListOfLegalMoves() would have to take
into account the rest of the board, and in a few cases, even the
exact type of some other piece. (A rook can only castle with a
king, in fact, only with a king that has never moved.) But
globally, putting the rules for moving each piece in a separate,
derived class, seems likely to be a good idea.
I also plan to use a global 2D (10*10) array of structure used to
represent the chess board along with status of each location (free/
occupied, color).
Is this a good approach? People do say that global variables in
case of oops programming, Is that correct?
The good approach would be to start the design from the top not from the
bottom:
That is true. How the board is physically represented is really
an implementation detail, not to be decided until you have
decided on the interface of the class, and what it's exact role
in the program is to be. (In addition to maintaining game
state, it might, for example, be responsible for evaluating the
value of the position it represents.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34