Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing objects for a chess game in java

Tags:

java

oop

I am designing a chess game in Java (no AI, only user-controlled), and still getting used to OOP. I have two questions.

I was thinking of having, in addition to Game, Cell, Piece and Board objects, a Player object.

My question is, do I really need to? Of course I don't need to, but is either option considered better design? On the one hand, it seems a Player is useful for containing information about player pieces and should contain such methods as takeTurn(). (For my implementation, I also want to keep track of all possible moves, so I'll have a method getAllMoves()). On the other hand, isn't Player simply a reorganization of existing data? Each Piece already has an indication of which player it belongs to. And since my game contains no AI, it might make sense for takeTurn() to belong to Game, rather than Player. On the first hand again, maybe Player can have only the method getAllMoves(), which uses its data but does not take action.

The second question, relevant if the answer to the first question is yes, is how do I organize the relations between the objects? getAllMoves() will take in as input an array of cells; but it feels strange that then the Player class depends on the fact that its cells match (are a subset of) the cells passed in as input. It would feel more OK if the cells-divided-by-player data would be kept together with the array of all cells in Board, and updated together, thus guaranteeing that they agree. Of course, the guarantee that they agree would exist either way, but it seems that the Player object shouldn't be aware of a guarantee taking place in the Board object.

How do I deal with these questions?

Thanks!

like image 649
user64480 Avatar asked Aug 05 '11 10:08

user64480


People also ask

Which pattern can be used to design a chess game?

Strategy Pattern : The Strategy pattern is known as a behavioural pattern – it's used to manage algorithms, relationships and responsibilities between objects. The main benefit of strategy pattern is to choose the algorithm/behaviour at runtime. Lets try to understand this by implementing this to design the chess game.

How do you make a chessboard in Java?

Approach: Create a rectangle with length and breadth of 20 unit each, with 10 rows and columns of chess. As soon as even position occurs in row and column change the color of a rectangle with BLACK, else it will be WHITE.

What are the materials used in playing chess?

Chess pieces are distinguished by appearance and made of rigid material such as wood, ivory, or plastic. Pieces are of contrasting colours, commonly white and black. The six different types of pieces are: king, rook, bishop, queen, knight, and pawn. More than 500 different patterns of chess pieces have been recorded.

Which data structure is used in chess game?

Board representation in computer chess is a data structure in a chess program representing the position on the chessboard and associated game state.


1 Answers

IMHO having a Player object is a good design.
You can even refine it later to an interface an have an AIPlayer and HumanPlayer implementation.
Today you need only the getAllMoves method but later you might need more. Having an object will help you to extend yout player hability.

For your second point, I'm not sure that the Player should implements directly the getAllMoves() method.

I would have delegate that to the ChessGame object. Player will in this case have a reference to the Game and delegates the getAllMoves call to this Game instance, like this:

pseudo-code :)

hey Game, what are the available moves for this piece ?

like image 131
Guillaume Avatar answered Oct 06 '22 20:10

Guillaume