Designing objects for a chess game in java

I am developing a chess game in Java (without AI, only with user control) and am still used to OOP. I have two questions.

I thought that in addition to the Game , Cell , Piece and Board objects, the Player object.

My question is: do I really need to? Of course, I do not need this, but is the option considered the best option? On the one hand, it seems that Player is useful for storing information about players and should contain methods such as takeTurn() . (For my implementation, I also want to keep track of all the possible actions, so I will have a getAllMoves() method). On the other hand, isn't a player just reorganizing existing data? Each piece already has an indication of which player it belongs to. And since my game does not contain AI, for takeTurn() may make sense to belong to Game , not Player . On the other hand, it is possible that Player can only have the getAllMoves() method, which uses its data but does not take action.

The second question, relevant, if the answer to the first question is yes, how can I organize relations between objects? getAllMoves() will accept an array of cells as input; but itโ€™s strange that the Player class then depends on the fact that its cells correspond (are a subset) that are passed as input. It would be better if the data with cells are stored in different ways along with the array of all cells in the Board and updated together, which guarantees their agreement. Of course, a guarantee that they will agree will exist in any case, but it seems that the Player object should not know about the guarantee that exists in the Board object.

How do I solve these issues?

Thanks!

+6
source share
3 answers

IMHO having a Player object is a good design.
You can even refine it later so that the interface has an implementation of AIPlayer and HumanPlayer.
Today you only need the getAllMoves method, but later you may need more. Having an object will help you expand the playerโ€™s randomness.

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

I would delegate this for the ChessGame object. The player in this case has a link to the Game and delegates a call to getAllMoves to this instance of the game, for example:

pseudo code :)

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

+2
source

The question is whether you need a "Player" object - ask yourself if this object has any interaction / relationship with other objects of your system. Also, do you have any data or status information that you might want to record? I can think of a few things that you might want to track related to a player - captured parts, possibly points.

By the way, it might be a good idea to build an object model to visualize how objects are related to each other. It will also give you a better idea of โ€‹โ€‹the interfaces between objects and the required methods.

+1
source

Are you familiar with UML and more specifically with case and sequence usage diagrams? This sounds like an exercise in modeling, and it would be nice to also look at the concept of a domain model.

To use actors, such as Player, are usually transferred as classes (they replace). Other classes can be defined starting with a rough sequence diagram for each use case with your Actor class on the one hand and with the global System class on the other.

This class of the system can gradually decompose when passing through various messages. If the / messsage method does not apply directly to one instance of the class, and the attribute / internal state is that it belongs elsewhere (for example: the BankAccount class does not have the same responsibility as the BankAccountManager class). You can group methods according to nature, and you should see some other classes.

From there, there should be a simple case of determining whether 1-to-1, one-to-many, and many-to-many relationships exist between them. In Java, they will be represented by single instances or lists and directivity (does class A know about class B or is it a one-way street?) It seems to be the presence or absence of links inside the corresponding classes.

PS: Please note that what I freely describe is an approach to the methodology, while UML just presents itself with a description of the system from different representations.

+1
source

Source: https://habr.com/ru/post/894370/


All Articles