目前我正在设计一个使用Java的大富翁游戏。拥有另一个对象的对象
游戏中的每个玩家都可以拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个Player类和一个Properties类。组成是做这件事的最好方法吗?如果是这样,我该怎么做?
目前我正在设计一个使用Java的大富翁游戏。拥有另一个对象的对象
游戏中的每个玩家都可以拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个Player类和一个Properties类。组成是做这件事的最好方法吗?如果是这样,我该怎么做?
我想补充一个新的类PropertyManager中。
这使您可以轻松地在单个位置提供业务规则(良好的关注点分离),而不必在通过一堆玩家或属性对象进行潜水时,如果您选择合成。这将使玩家和/或财产类别在未来随着买卖业务规则而变得沉重。
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-19 01:50:22
组成作品。只要玩家有一个属性对象,并且属性对象包含所有必要的数据,你应该没问题(假设你实现了必要的getter和setter方法)。
该属性可以拥有属于播放器的所有者属性。
你也可以在属性播放器上建立一个列表。
想想现实世界中的条件。
当您在玩Monopoly并购买房产时,您将房产卡添加到您的房产列表中。
因此,在这种情况下,您是将Property对象添加到属性列表的Player对象。
public class Player
{
private List<Property> properties;
}
您将需要组合和多态性。
假设玩家可以有多个属性,您将需要一个属性列表。如果属性的属性可能不同,则可以应用多态性和继承。您可能只会看到下面的继承,但是当您获取不同的属性并操作它们时,您将需要多态性。
在主:
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-19 00:24:43
我还没有尝试过任何东西,但我已经在脑海中玩过它了。最初我计划在每个属性中都有一个int类型的所有者变量。所有者将由玩家的索引值决定(因为他们已经被放入列表中)。我觉得这种方法不是非常有效和有限的。 – 2012-07-19 01:13:27