目前我正在使用 Java 设计一个大富翁游戏。
游戏中的每个玩家都可以拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个 Player 类和一个 Properties 类。组合是最好的方法吗?如果是这样,我会怎么做?
我会添加一个新类 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
}
}
用现实世界的术语来思考它。
当您在玩大富翁游戏并购买房产时,您会拿起房产卡并将其添加到您面前的房产列表中。
因此,在这种情况下,您是一个将 Property 对象添加到您的属性列表的 Player 对象。
public class Player
{
private List<Property> properties;
}
作曲作品。只要玩家有一个属性对象,并且属性对象包含所有必要的数据,你应该没问题(假设你实现了必要的 getter 和 setter 方法)。
该属性可以有一个 Owner 属性,即 Player。
您还可以在属性播放器上建立一个列表。
您将需要组合和多态性。
假设一个玩家可以拥有多个属性,您将需要一个属性列表。如果属性的属性不同,您可以应用多态和继承。您可能只会在下面看到继承,但是当您获取不同的属性并对其进行操作时,您将需要多态性。
主要:
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
}