سؤال

Currently I am designing a Monopoly game using Java.

Each player in the game can own different properties. The issue I'm having is how to assign different property objects to each player. I have both a Player class and a Properties class. Would composition be the best way to go about doing this? If so, how would I do it?

هل كانت مفيدة؟

المحلول

I would add a new class PropertyManager.

This allows you to easily provide business rules in a single location (good separation of concerns) rather than having to dive through a bunch of player or property objects if you opt for composition in either. This will keep the Player and or Property classes from becoming weighed down with buying/selling business rules in the future.

public final class PropertyManager {

  /**
   * The PropertyManager instance, example usage:
   *   PropertyManager.INSTANCE.buyProperty(property, buyer);
   * NB: Good candidate for dependency injection instead if you are doing this.
   */
  public static final PropertyManager INSTANCE = new PropertyManager();

  private static final Map<Property, Player> propertyListing = new HashMap<Property, Player>();

  /**
   * Buy a property from the banker, banker or property manager could maintain
   * Collection of available properties
   */
  public void buyProperty(Player buyer, Property property) {
    if (propertyListing.containsKey(property)) {
      throw new IllegalStateException("Unable to buy unless owner sells it");
    }
    propertyListing.put(property, buyer);
  }

  /**
   * Broker a transaction between two players for the sale of a property
   */
  public void sellProperty(Player seller, Player buyer, Property property) {
    if (!propertyListing.containsKey(property)) {
      throw new IllegalStateException("Unable to sell Property which is not owned");
    }
    Player owner = propertyListing.get(property);
    if (!owner.equals(seller)) {
      throw new IllegalStateException("Unable to sell property seller doesn't own");
    }
    // TODO : Deduct/transfer monies (ensure sufficient value in buyer account etc)
    propertyListing.put(property, buyer); 
  }

  /**
   * Retrieve a List of all of the Player's properties
   */
  public List<Property> getProperties(Player owner) {
    // TODO Either iterate through the propertyListing or use a second Map for player to List<Property>, NB: they should be guarded with a shared lock if you do this (threading).
  }

  /**
   * Retrieve the owner of a Property or null if it is unowned
   */
  @Nullable // javax annotation indiciates can be null
  public Player getOwner(Property property) {
    return propertyListing.get(property);
  }

  /**
   * Hide the constructor as the only property manager should be INSTANCE
   */
  private PropertyManager() {
    // Prevent further instantiation
  }
}

نصائح أخرى

Think about it in real world terms.

When you're playing Monopoly and you purchase a property you take the property card and add it to your list of properties in front of you.

So in that case, you are a Player object adding Property objects to your property list.

public class Player
{
    private List<Property> properties;
}

Composition works. As long as a player has a properties object, and the properties object contains all the necessary data, you should be fine (assuming you implement the necessary getter and setter methods).

the property can have an Owner property that is the Player.

You could also build a list on the Player of Properties.

You will need composition and polymorphism.

Assuming a player can have more than one property, you will need a List of properties. If Properties can differ in the attributes they have, you can apply Polymorphism and Inheritance. You will probably only see Inheritance below, but you will need Polymorphism when you are getting the different properties out and manipulating them.

In main:

public static void main(String args[]){
  Player player1 = new Player();

  BlackProperty blackProperty = new BlackProperty();
  BlueProperty blueProperty = new BlueProperty();

  player1.addProperty(blackProperty);
  player1.addProperty(blueProperty);
}

All your domain classes:

public class Player{
  private List<Properties> propertyList;

  // getters and setters

  public void addProperty(Properties properties){
    if(this.propertyList == null){
      this.propertyList = new ArrayList<Properties>();
    }

    this.propertyList.add(properties);
  }
}

public class Properties{
  private int noOfFloor;
  // getters and setters
}

public class BlackProperty extend Properties{
  private String noOfGate;
  // getters and setters
}

public class BlueProperty extend Properties{
  private String noOfLawn;
  // getters and setters
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top