473,473 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to store object when we have two-dimensional chessboard

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.
}

//Tony
Oct 8 '05 #1
3 7160
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
Oct 9 '05 #2

"David Moss" <q0******@mail.connect.usq.edu.au> wrote in message
news:MP************************@news.bigpond.com.. .
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.


Alternatively, you don't need to store a chessboard at all. You can just
have a collection of pieces, each piece storing its position on the board as
some numeric type (e.g. int). You could just have the pieces sorted by their
position (finding the content of a square is O(log(n))), or you could have a
hashtable where the key is the position and the value is the piece (finding
the content of a square is O(1)).

- Oliver
Oct 12 '05 #3
In article <SN93f.15899$Io.13961@clgrps13>, ow***@castortech.com
writes...

"David Moss" <q0******@mail.connect.usq.edu.au> wrote in message
news:MP************************@news.bigpond.com.. .
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.


Alternatively, you don't need to store a chessboard at all. You can just
have a collection of pieces, each piece storing its position on the board as
some numeric type (e.g. int). You could just have the pieces sorted by their
position (finding the content of a square is O(log(n))), or you could have a
hashtable where the key is the position and the value is the piece (finding
the content of a square is O(1)).


True.
I suggested an array because you have to drive a game from somewhere and
an array of length 64 is about as simple as it gets. I wouldn't worry
about making it an 8x8, that is useful for humans but not particularly
useful for a computer.

--
DM
personal opinion only
Oct 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher Brandsdal | last post by:
Hi! I have a delicatg problem.... I have made a registration form for adding my friends information in a database. The problem is that I want to connect the persons with companies in the same...
9
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
3
by: Karen Grube | last post by:
Hi! Each week, we receive a two-page PDF file from UPS along with a separate flat file (a CSV) The PDF file contains the overview of our weekly invoice and the CSV contains the details of each...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
3
by: ExclusiveResorts | last post by:
Can the CallContext be used reliably for storing request specific data? We are developing an application library that uses the CallContext to keep an IdentityMap (hashtable of business objects...
11
by: Crirus | last post by:
I have a map in a game. I need to make a list with all visible objects for a player So, any building and any unit offer a range of visibility over the map. Now VB question: How can I store...
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
3
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to...
1
by: Joe | last post by:
Hello, I'm currently using a C# class library which is also converted quickly to a console app by adding a MAIN and adjusting the building configuration. I'm using this page as a reference to the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.