4

現在、Java を使用してモノポリー ゲームを設計しています。

ゲーム内の各プレイヤーは、異なるプロパティを所有できます。私が抱えている問題は、各プレイヤーに異なるプロパティ オブジェクトを割り当てる方法です。Player クラスと Properties クラスの両方があります。構成はこれを行うための最良の方法でしょうか? もしそうなら、どうすればいいですか?

4

5 に答える 5

3

新しいクラス PropertyManager を追加します。

これにより、単一の場所でビジネス ルールを簡単に提供できます (適切な関心事の分離)。どちらかで構成を選択した場合に、多数のプレーヤー オブジェクトまたはプロパティ オブジェクトに飛び込む必要はありません。これにより、Player クラスや Property クラスが、将来の売買ビジネス ルールによって圧迫されることがなくなります。

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
  }
}
于 2012-07-19T00:59:19.227 に答える
2

現実世界で考えてみてください。

モノポリーをプレイしているときに物件を購入するときは、物件カードを受け取り、自分の目の前にある物件のリストに追加します。

したがって、その場合は、Property オブジェクトをプロパティ リストに追加する Player オブジェクトになります。

public class Player
{
    private List<Property> properties;
}
于 2012-07-19T00:37:12.437 に答える
0

構成が機能します。プレーヤーにプロパティ オブジェクトがあり、そのプロパティ オブジェクトに必要なすべてのデータが含まれている限り、問題はありません (必要なゲッター メソッドとセッター メソッドを実装していると仮定します)。

于 2012-07-19T00:24:33.307 に答える
0

プロパティは、プレーヤーである所有者プロパティを持つことができます。

Player of Properties でリストを作成することもできます。

于 2012-07-19T00:25:34.990 に答える
0

コンポジションとポリモーフィズムが必要になります。

プレーヤーが複数のプロパティを持つことができると仮定すると、プロパティのリストが必要になります。プロパティが持つ属性が異なる可能性がある場合は、ポリモーフィズムと継承を適用​​できます。以下ではおそらく継承のみが表示されますが、さまざまなプロパティを取得して操作する場合は、ポリモーフィズムが必要になります。

主に:

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

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

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

すべてのドメイン クラス:

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
}
于 2012-07-19T00:37:31.397 に答える