I need some idea on how many classes I need for this particular problem.
I have a checker board mentioned with variables grids (minx,miny,maxx,maxy)[provided as an input]. I want place a coin in the checker board mentioning the x,y position and the direction it is looking at (North/South/West/East). If I am giving a set of directions to turn/move the coin it should display the final position with the direction it is looking at.
What would be the best approach? how many classes do we need?
The number of classes depends on the design of the program. Why is it important?
If by "need" you mean the required minimum, then the answer is 1. All Java programs can be written in a single class, just like all C programs can be put into a single source file. That would result in a very bad design and unmaintainable code, of course.
Ping & DNS - updated with new look and Ping home screen widget
Anbu Cheeralan Valan Arasu
Joined: Oct 04, 2007
Thanks Ulf. I thought in real world I need two objects (one is for checker board and another one is for coin). Is it a bad design if i create two classes (eventhough checker board will have only getters/setters)?
Joined: Mar 22, 2005
I've only cursory knowledge of Checkers, but having a class representing a coin seems like a strange design. What methods could conceivably be in it? It has a position, and could be moved, but the board would need to keep track of the position of the coins anyway, so that doesn't simplify things much.
I'm going to move this thread to the Game Development forum.
I think I would go for the coin being a class, it would have an x,y and heading private memeber with getters and setters. I instantly started to think ahead and see how I might reuse the class for other grid based games such as chess, naughts and crosses, etc. I also dont like the idea of having coin1X, coin1Y, coin1Heading, coin2X etc.
The advantages I can see of the coin being a class are:
a) To allow it to have a unique graphical representation. In a simple game this could still be a function of the board class. b) Allow the coin to store a record of its own moves. Again in a simple game could be in the board class, also the history would be lost when you decided to delete (null) the coin c) coins have their own behaviour, useful in chess perhaps. d) the board and coin classes are base classes to allow for reuse in other games such as chess.
Another thing i might do, as the board is a simple grid, is rather then using x and y cordinates to define where the coin is, I would use "grid square". Starting from 0 in the upper left going do to 63 (if we assume an 8 by 8 board, as in chess) in the lower right.
Not sure exactly what your doing, and the question "how many classes" seems an odd one.
If your building a checkers game, so that it plays checkers, you are buildign a checkers engine...
I've built a chess engine in java that uses bitboards. You could go with a simple array of length 64, where the top left corner is 0, top right is 7, bottom left is 56, bottom right is 63.
Its not necessary to use an object to represent a coin, Essentially you just want to store the position of the white coins, and the positions of the black coins.
I think theres more info on chess engines than checkers engines... And they would share a lot of techniques, but obviously checkers is much simpler since you only have 1 (or 2,crowns) types of pieces, and 32 relevant squares.
If you are new to programming, checkers would be a friendlier choice, but chess is still a good choice, something you can work on for years, as your knowledge improves.
Anbu Cheeralan Valan Arasu
Joined: Oct 04, 2007
Thanks a lot for helping me out. If it is not about checker board/coin say something about a place/toy, what would be the design? Still will you come to the same conclusion of having only one class for place?
I am giving you the actual problem. I need to deply a toy in a place where the lower left corner of the place will be assumed as 0,0 and the upper right corner will be given as input.
I am placing a toy by indicating say 1,2,N ( Facing towards North).
Then I have to give control code (eg: LRMRMR) [L-Left (90 degree turn) R-Right(90 degree turn) M-Move (always 1 step)]. I need to get the final position of the toy (in this example it would be 2,3,S)
I have designed two classes (one for toy another one for place). Place will have only getters and setters. I will pass the place object to the toy object while contructing toy object.
Is that a bad design? Because I got a review comment "It does not reflect my OOPS understanding".
I thought in real life, when I am asked to go to a place I will be given information about that place and one place is different from other. So I designed Place and toy as different classes.