Solved

Programming with objects

Posted on 2003-11-06
3
344 Views
Last Modified: 2010-04-07
Can you advice me on whether i should represent each square as an object and putting this objects in an array, i'll will like your opinion on this please if you have a better idea it is welcome please.
0
Comment
Question by:Drake009
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 70 total points
ID: 9698459
Each squiare of what? A quilt? The blocks of a city? On a checker board?

Your question, without any context, is so vague that it cannot be answered.

Post some description of the problem you are addressing where squares fit into it, what you know about objects, whether or not the implementation of those objects matters, etc.

-bcl
0
 

Author Comment

by:Drake009
ID: 9699481
On a chess board sir, whether i should represent the squares as objects implementing in java. I wanted to implement as an array of squares so that the function which scans the board will have an easier task i hope.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9701107
(1) You probably didn't want to accept my answer (at least no the one above which conveys nothing more than a question about your question). You can go to the Community Service area and post that you accepted the wrong answer to have the question reopened.

(2) I am going to stick to higher-level design issues here (actually language independent). When you get further along you can ask Java specific questions in the Java topic area (feel free to ask any getting started questions you have here and I can help some).

(3) Let us consider what a chess square is, what attributes it has. It has location, color, and, optionally, content. A computer program has to abstract away the meaningless attributes and focus on representing the important ones. It seem obvious that whether a square is glued paper or painted or a different species of wood from its neighbors is probalby not relavent to a chess playing program (might come into play in the display but we'll ignore that for the moment).  You will want to have a square draw itself on the screen so there is a screen location (which might well change over the course of the game if the player can change her view) and a board location (e3 or king's pawn six) that won't change over time (unless you design a very unorthodox version of chess...could be interesting).

So, collecting these attributes together into a class makes sense. Then you can define the operations you need a square to support. One is draw. How it draws is not important at this point, only that it can draw itself in the right spot on the screen. You'l also need to be able to move a piece to the square (and probably have the square ask the piece to draw itself given the coordinates of the center of the square...that means a piece is probably an object, too). You'll want to be able to ask whether the square is occupied and then whether it is occupied by white (or black). You will, of course, need to be able to create 64 squares, specifying their color, board location, and initial contents (or leave initial contents for later with the "moveTo" method).

So, the interface for a square looks something like this:

Square(int row, int column, Color baseColor);
moveTo(Piece p); // move piece p to this square
moveFrom(); // move any piece away from this square
isOccupied(); // is the square occupied
isOccupant(Color c); // is there an occupant of Color c in the square?
Draw();

If you permit different views you'll also want to be able to set the screen location with a separate funciton.

Hope this helps get you started.

-bcl
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now