D
David Rasmussen
The problem comes up in all sorts of contexts. But here is an example:
We want to model the basics of chess, maybe for making a chess program,
or maybe just to make an interactive board or ...
We have to have the notions of colors, squares and pieces represented.
Ideally, we want to be able to do things like initializing the board:
void initial()
{
const Piece backLine[] =
{rook,knight,bishop,queen,king,bishop,knight,rook};
toMove = white;
int i = 0;
for (square s = a1; s <= h1; ++s)
{
board.piece = backLine[i++];
board.color = white;
}
for (square s = a2; s <= h2; ++s)
{
board.piece = pawn;
board.color = white;
}
for (square s = a3; s <= h6; ++s)
board.piece = none;
for (square s = a7; s <= h7; ++s)
{
board.piece = pawn;
board.color = black;
}
i = 0;
for (Square s = a8; s <= h8; ++s)
{
board.piece = backLine[i++];
board.color = black;
}
}
This is a simple function that has to be present in a program involving
chess, and it shows a little bit about what is needed.
The simplest way to do this, is by making enums for piece, color, and
square, like:
enum Color {white,black};
enum Piece {none,pawn,knight,bishop,queen,king}
enum Square {a1,a2,...,h8};
But already here, we are having trouble. We have to define several
functions to get Square to work as indicated above.
We could do it even simpler and less typesafe by just using ints. Then
we would have our value semantics; assignment, comparison,
incrementation etc.
And we could make everything a class. But how?
Then one would have to go through the tedious process of defining all
the operators necesary for value semantics.
In SML, one would just do
datatype Piece = none | pawn | ... | king;
(or something like that... My SML is rusty).
In Ada, one can do something similarly.
This sort of thing, defining a collection of values of the same kind,
being able to assign them, and iterate through them etc., but as typed
_values_, not as ints, is pretty essential as I see it.
I don't really know what my question is
Maybe this:
What is the _intended_ C++ way of solving this _kind_ or _class_ or
_type_ of problem?
What is the kosher OOP way to do it?
I know that one can make a hierachy like
class Color
{};
class White : public Color
{}
class Black : public Color
{}
or whatever...
But then, how will the above function look? I can't do
toMove = white;
then.
In short:
What would be a nice flexible and (type)safe design of such things?
Please give an example of how to define Color, Piece and Square, and an
outline of what the initial() function will look like.
I think this kind of problem occurs all the time.
/David
We want to model the basics of chess, maybe for making a chess program,
or maybe just to make an interactive board or ...
We have to have the notions of colors, squares and pieces represented.
Ideally, we want to be able to do things like initializing the board:
void initial()
{
const Piece backLine[] =
{rook,knight,bishop,queen,king,bishop,knight,rook};
toMove = white;
int i = 0;
for (square s = a1; s <= h1; ++s)
{
board
board
}
for (square s = a2; s <= h2; ++s)
{
board
board
}
for (square s = a3; s <= h6; ++s)
board
for (square s = a7; s <= h7; ++s)
{
board
board
}
i = 0;
for (Square s = a8; s <= h8; ++s)
{
board
board
}
}
This is a simple function that has to be present in a program involving
chess, and it shows a little bit about what is needed.
The simplest way to do this, is by making enums for piece, color, and
square, like:
enum Color {white,black};
enum Piece {none,pawn,knight,bishop,queen,king}
enum Square {a1,a2,...,h8};
But already here, we are having trouble. We have to define several
functions to get Square to work as indicated above.
We could do it even simpler and less typesafe by just using ints. Then
we would have our value semantics; assignment, comparison,
incrementation etc.
And we could make everything a class. But how?
Then one would have to go through the tedious process of defining all
the operators necesary for value semantics.
In SML, one would just do
datatype Piece = none | pawn | ... | king;
(or something like that... My SML is rusty).
In Ada, one can do something similarly.
This sort of thing, defining a collection of values of the same kind,
being able to assign them, and iterate through them etc., but as typed
_values_, not as ints, is pretty essential as I see it.
I don't really know what my question is
Maybe this:
What is the _intended_ C++ way of solving this _kind_ or _class_ or
_type_ of problem?
What is the kosher OOP way to do it?
I know that one can make a hierachy like
class Color
{};
class White : public Color
{}
class Black : public Color
{}
or whatever...
But then, how will the above function look? I can't do
toMove = white;
then.
In short:
What would be a nice flexible and (type)safe design of such things?
Please give an example of how to define Color, Piece and Square, and an
outline of what the initial() function will look like.
I think this kind of problem occurs all the time.
/David