In article <MK*********************@newsc.telia.net>,
jo*****************@telia.com writes...
Hello!
Assume you want to store field object that a chess board consist of.
A chess board consist of 64 fields where each field is either white or
black.
Now to my question how should I implement this putFieldAt below when I have
field object
that should be placed at position row i and kolumn j. So this putFieldAt is
called 64 timmes where i has the value from 0 to 7 and j has value from 0 to
7.
If I for example choose an ArrayList or a Vector how should that be
implemented because this ArrayList or Vector is only one-dimensional.
Do you thing it would be better to choose an ordinary array[] perhaps?
public void putFieldAt(Field aField, int i, int j)
{
//Some code to store object aField in a collection.
}
Arraylist is only supposed to be for situations when you don't know the
size of the array in advance. A chess board is of fixed size.
An Array can contain any object.
The lack, white sequence of a chessboard follows a predictable sequence.
There is therefore no need to store information about the square colour.
You can calculate the colour whenever you need to know it from the index
value.
My take is that the squares do not need to know anything about
themselves. They are merely array elements to hold objects. Pieces
occupying a square may need to know where they are however. The square
itself may need to know if there is a piece on it. With this knowledge
you can call methods in the relevant piece. When a piece moves all its
attributes and capabilities move with it. This can reduce the complexity
of your code immensely.
--
DM
personal opinion only