2

I have a class Game.

This class needs to take three arguments passed in it's constructor:

  1. List of game pieces
  2. Board Size
  3. Game piece placement 'engine'

I am dithering about the design of the game piece placement 'engine'. This object needs the list of game pieces and the board size to determine where the pieces are to be placed.

The options I can see are:

  1. The placement engine takes the list of pieces and the board size in its constructor and it has a Generate method that passes back the list of pieces and their placement
  2. Similar to 1 but the list of pieces and board size are set as properties after instantiation and then there is the Generate method as above.
  3. The placement engine either takes the board size as a constructor argument or set as a property and then there is a Generate method that takes a piece as a parameter and then passes back the placement for that piece.

I am beginning to swing towards the 3rd option as this then allows the game to handle things, otherwise the game could have a list of pieces and a board size that is different from the placement 'engine'. Generating the starting location for a piece at a time also seems more 'correct'.

How would you do it?

1
  • Which way best achieves your goals? Aug 7, 2016 at 13:45

1 Answer 1

2

I like option 1.

Pseudocode:

public class Game {
    public Game(PiecePlacementEngine ppe){}
}

public class Piece {

}

public class PieceNotInListException extends Exception {

}

public abstract class PiecePlacementEngine {
    public PiecePlacementEngine(List<Piece> pieces, int boardsize[]){}  
    public abstract Map<Piece,int[]> getAllPlacements();
    public abstract int[] getPlacement(Piece p) throws PieceNotInListException;
    public abstract int[] getBoardSize();
    public abstract List<Piece> getPieces();
}
  • No need to pass three parameters to Game's constructor since that info is in PiecePlacementEngine
  • I assumed that boardsize is an array of ints, but that could be changed.
  • I made PiecePlacementEngine abstract in my example so not to assume too many things and for it to compile. But you don't necessarily have to extend this example, just remove the abstract modifier and add bodies to the methods.
  • PiecePlacementEngine has getters for its constructor parameteres boardsize and list of pieces so Game can access them if need be.
  • PiecePlacementEngine has getters for the whole list of piece and placement key/value pairs or just the placement of an individual piece.
  • An exception is thrown if you ask for the position of a piece thar is not in the list.
  • I suggest that if a piece exists in the list but is not positioned on the board then asking for its placemente returns null.
1
  • Board size is just an int as it is a square board, although I can see that you may want to have an x size and y size but that would be coding for a circumstance that I don't have. I like your design but it feels wrong that the PiecePlacementEngine is the owner of the pieces and the board size. Is not the game the owner of the board and pieces? The Game class has a method that takes a set of co-ordinates and then determines if a piece is present. The Game keeps track of the pieces. Aug 13, 2016 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.