Solved

Programming with objects

Posted on 2003-11-06
3
341 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

11 Experts available now in Live!

Get 1:1 Help Now