Checkers Game in JAVA

satpierre -  
 satpierre -
Hello,

As I am pursuing a computer science degree, I have to create a checkers game (player vs computer) as part of my project.
I’m not quite sure how to get started; I have several ideas. The goal of this project being object-oriented programming, I first thought of something like this:

Class Coordinate, with attributes: abscissa and ordinate
Class Piece, with a position (of type Coordinate) and a color (boolean -> black or white)
Class Square, with a coord (of type Coordinate) and isEmpty (boolean, true if the square is empty)
Class Board, which would be two lists of Squares and Pieces

The problem is that I’m not quite sure if what I’ve done is correct. I think it might complicate things for me...

Input for the movements must be done via the console...

If the player inputs the coordinates of a piece, I'll need to loop through the entire list of pieces to find the corresponding piece. And this for numerous tests (like checking if the piece can move, etc...)!!!

So I don’t really know what to do; if you have a solution... Could you please help me???!!!
Configuration: Windows Vista Internet Explorer 7.0

14 réponses

mateo17 Posted messages 20 Status Membre 3
 
I think you're on the right track,
is it just console mode, or are you using Swing?
In Swing, you can use a two-dimensional array of JLabels, in which you attach an image corresponding to your cell, a piece..., and you detect the click on this JLabel.
1
satpierre
 
Swing? I don’t know it. In fact, the game should display in a graphical interface, but the input of coordinates, etc. should be done in the console. The graphical interface is only there to display, in reality.
0
mateo17 Posted messages 20 Status Membre 3
 
Swing is the graphics library of Java..
What do you use for the interface?
0
satpierre
 
For now, I'm not really sure.. We used graphical interfaces only once.
I admit that I'm focusing a bit more on the code.

Is my modeling good? I'm worried that having 2 lists of pieces and squares may be too much. Some other people created a 2D array, but in that case, I feel like there's no point in putting coordinates in the piece and object class...

I admit that it's a bit unclear for me.
0
mateo17 Posted messages 20 Status Membre 3
 
Both methods can work perfectly; it all depends on how you handle it behind the scenes..
Personally, I would use the 2D array because it's easier to manage the graphical display
Do you program with Eclipse?
0
satpierre
 
Alright, but then what do you put in your 2D array? That was my first idea, but then when it came to adding squares and pieces, I found it a bit difficult to understand what to do and what to put.

What I don't want is to mix code and display. Because in the first version, it needs to be fully displayed in the console as well.

At university, we code in Scintilla text but at home, I use Eclipse instead.
0
mateo17 Posted messages 20 Status Membre 3
 
In your table, you manage your rows and columns, you create a 2D array of ints (int[][]), for example you choose to put 0 for an empty cell, 1 for a white piece, and 2 for a black piece. In console mode, you display this, and in your interface, you will create a function to convert your table...
on Eclipse, I know good editors Swing, Jigloo, and Visual Editor.
0
satpierre
 
So no class case or pawn?
I'm a bit worried that it will be less object-oriented. But it was my first idea. Then we also need to take the queens into account.
We also need to be able to undo the move made, and in the end, review the entire game.
0
mateo17 Posted messages 20 Status Membre 3
 
if worst comes to worst you can combine the two, you create a board class, where you have a 2D array of pieces (the piece class)
and in your piece object you manage the color, the coordinates...
0
satpierre
 
Okay, but how do you manage the fact that there is a piece or not on the board?
And the movements?
0
mateo17 Posted messages 20 Status Membre 3
 
In each cell of the table, you have a game piece with a value attribute (black, white, etc.). You test this value for movements.
For the movements, you retrieve the entered coordinates and test them in the 2D table. You check the surrounding cells to see if the move is possible. If so, you flip the pieces and change the value of the attribute of the game piece objects in the corresponding cells.
0
satpierre
 
Yes, I clearly see the test. However, what you mean is that the value attribute takes a different value if the piece does not exist?

On the other hand, what is the point of including coordinates when the indices for the 2D array can be used?
0
mateo17 Posted messages 20 Status Membre 3
 
yes, if there is no pawn, you put another value
you are right, the coordinates are not necessary
0
satpierre
 
Sure, thank you for your help anyway.
And how do you advise me to cancel one or more moves and to review the entire game?
I had thought of a History class containing a list of Moves. Move being a class with attributes: startingCoord of type coordinate, same for the arrival, the color, and the captured piece if there is a capture.
0