Re: Newbie question



Ludovic Brenta <ludovic@xxxxxxxxxxxxxxxxxx> writes:

Solution 3: introduce a new value for Piece_Type_T:

type Piece_Type_T is (None, Pawn, Knight, Bishop, Rook, Queen, King);

I don't know which is the best solution; perhaps you'd like to try
them all and see how well they integrate with the algorithms. I think
Solution 3 is the most error-prone, though.

A variation on 3 is not error prone:

type Optional_Piece_Kind is
(None, Pawn, Knight, Bishop, Rook, Queen, King);
subtype Piece_Kind is Optional_Piece_Kind range
Optional_Piece_Kind'Succ(None)..Optional_Piece_Kind'Last;

type Square(Kind: Optional_Piece_Kind is := None) is
record
case Kind is
when None => null;
when Piece_Kind => Color: ...;
end case;
end record;

type Rank is range 1..8;
type File is new Character range 'A'..'H';
-- Maybe I've got this backwards???
type Board is array (Rank, File) of Square;

As somebody said, you will want a default value for the discriminant, in
all these variations. Having a default has the odd property that it
allows you to change the discriminant by a whole-record assignment.
This oddity often confuses beginners to Ada.

- Bob
.



Relevant Pages