Solved

Programming with objects

Posted on 2003-11-06
3
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

632 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