mercredi 11 septembre 2019

OOP composition, how to find "direction" of a relationship for a chess

I am training my OOP skill on a project where I try to find the best design for a simple chessgame. So far, I made a design like this while trying to follow good principles (not complete though) :

enter image description here

  • "Tile" class which represent a single square
  • "Board" class which is basically a 2D array of tiles
  • "Piece" class which contains the movement logic & the color of the piece
  • many other classes which are not involved by the topic of this thread

I do have few question about that design :

  1. Should I add a new attribute to Piece class to directly access position through this class ? Or I must manage all those things through Board class ?

  2. More importantly for me, how should I link player to each Piece ? I have trouble to see the "direction" of the relationship between the objects.

I can design my player class like that :

public class Player {
  private List<Piece> pieces;

I could also design Piece class like this :

public class Piece {
  private Player belongsTo;

I could also include this logic in Board class :

public class Board {
  private Map<Piece, Player> pieces:

Is there any "better design" between those 3 solutions ? Any rule to apply in those case ?

Any advise or observation about my design or general OOP is super welcomed,

Thank you,

Aucun commentaire:

Enregistrer un commentaire