多次调用相同的方法而不覆盖以前的值

时间:2016-06-13 15:08:10

标签: java

道歉,如果这对大多数人来说是微不足道的,但我只是无法解决这个问题!!

我正在创建一个模拟游戏,我有一个开始,结束和跳跃。有门户网站,如果你进入一个白色的门户网站,你会向前跳,而在那里你会向后跳。我把课程设置为POJO;

private int totalSize;
private int minDice;
private int maxDice;
private int whitePortalStart;
private int whitePortalEnd;
private int blackPortalStart;
private int blackPortalEnd;
private int startPosition = 1;
private int currentPosition;

public GameObject(){}

public int getTotalSize() {
    return totalSize;
}

public void setTotalSize(int totalSize) throws Exception {
    if(totalSize <= 0){
        throw new Exception("Can't have a total distance of less than or equal to 0");
    } else {
        this.totalSize = totalSize;
    }
}

public int getMinDice() {
    return minDice;
}

public void setMinDice(int minDice) throws Exception {
    if(minDice <= 0){
        throw new Exception("Can't have a min dice value of less than or equal to 0");
    } else {
        this.minDice = minDice;
    }
}

public int getMaxDice() {
    return maxDice;
}

public void setMaxDice(int maxDice) throws Exception {
    if(getMinDice() > maxDice){
        throw new Exception("Cant have minimum dice number greater than the larger dice number");
    } else {
        this.maxDice = maxDice;
    }
}

public int getWhitePortalStart() {
    return whitePortalStart;
}

public void setWhitePortalStart(int whitePortalStart) throws Exception {
    this.whitePortalStart = whitePortalStart;
}

public int getWhitePortalEnd() {
    return whitePortalEnd;
}

public void setWhitePortalEnd(int whitePortalEnd) throws Exception {
    this.whitePortalEnd = whitePortalEnd;
}

public int getBlackPortalStart() {
    return blackPortalStart;
}

public void setBlackPortalStart(int blackPortalStart) throws Exception {
    this.blackPortalStart = blackPortalStart;
}

public int getBlackPortalEnd() {
    return blackPortalEnd;
}

public void setBlackPortalEnd(int blackPortalEnd) throws Exception {
    this.blackPortalEnd = blackPortalEnd;
}

public GameObject builder(int n) throws Exception {
    setTotalSize(n);
    return this;
}

public GameObject whitePortal(int m, int o) throws Exception {
    setWhitePortalStart(m);
    setWhitePortalEnd(o);
    return this;
}

public GameObject blackPortal(int o, int m) throws Exception {
    setBlackPortalStart(o);
    setBlackPortalEnd(m);
    return this;
}

public GameObject dice(int i, int j) throws Exception {
    setMinDice(i);
    setMaxDice(j);
    return this;
}

public int rollDice(){
    Random random = new Random();
    int min = getMinDice();
    int max = getMaxDice();

    return random.nextInt(max - min + 1) + min;
}

public void build(){
    int totalDistance = getTotalSize();
    currentPosition = startPosition;

    while(currentPosition < totalDistance){
        int diceValue = rollDice();

        if(currentPosition + diceValue > getTotalSize()){
            System.out.println("CurrentPosition : " + (currentPosition + diceValue) + ", is larger than the total size of the road - " + totalSize);
            continue;
        } else if(currentPosition + diceValue == getWhitePortalStart()){
            System.out.println("You landed on a white portal. Advancing from position " + (currentPosition + diceValue) + " to " + getWhitePortalEnd());
            currentPosition = getWhitePortalEnd();
        } else if(currentPosition + diceValue == getBlackPortalStart()){
            System.out.println("You landed on a black portal. Moving from position " + (currentPosition + diceValue) + " to " + getBlackPortalEnd());
            currentPosition = getBlackPortalEnd();
        } else {
            System.out.println("You landed on " + (currentPosition + diceValue));
            currentPosition += diceValue;
        }
    }
}

所以在我的main方法中,我称之为create,并调用此类,如;

WorldOfOz oz = new WorldOfOz();
    oz.go.builder(30)
        .dice(1, 4)
        .whitePortal(5, 12)
        .blackPortal(13, 2)
        .build();

我的问题是当我想添加超过1个whitePortal / blackPortal

WorldOfOz oz = new WorldOfOz();
    oz.go.builder(30)
        .dice(1, 4)
        .whitePortal(5, 12)
        .whitePortal(18, 26)
        .blackPortal(13, 2)
        .build();

值18 - 26覆盖5 - 12.我如何设置它以便我可以有多个白色和黑色门户?

2 个答案:

答案 0 :(得分:5)

您的数据结构似乎不足以解决此问题。

您需要定义whitePortals的集合和blackPortals的集合。如果您这样做,则调用方法whitePortal(5, 12)添加一个新的白色门户网站,其中包含设置唯一白色现有门户网站的白色门户网站值。

您需要定义一个班级Portal

public class Portal {
    private int portalStart;
    private int portalEnd;
    ...

    public Portal(int s, int e) {
        this.portalStart = s;
        this.portalEnd = e;
    }
}

然后您可以在GameObject中使用它,如下所示:

public GameObject {
    List<Portal> whitePortals;
    List<Portal> blackPortals;


    public GameObject() {
        whitePortals = new ArrayList<Portal>();
        blackPortals = new ArrayList<Portal>();
     }

    public GameObject addWhitePortal(int m, int o) throws Exception {
        whitePortals.add(new Portal(m, o));
        return this;
    }

    ...

    // You need to change other methods to follow a different data structure 
}

答案 1 :(得分:0)

好吧,您可以使用以下方法:

  1. 介绍具有开始/结束属性的新“门户”类型
  2. 将班级中的白/黑门户网站属性替换为白门和黑门户(或您喜欢的任何其他类型的收藏品)列表
  3. 将getWhite / Black方法替换为可访问列表
  4. 重构whitePortal和blackPortal方法以创建门户网站对象的新实例并将其添加到适当的集合
  5. 当然,您可以使用数组而不是集合,但这有点麻烦。

    另外,假设门户是集合,您可能需要添加辅助方法来操作它们。取决于您的实际需求。

    public class Portal
    {
      private int start;
      private int end;
    
      public Portal(int start, int end) { ... }
      public getStart() {...}
      public getEnd() {...}
      public setStart(int end) {...}
      public setEnd(int start) {...}
    }
    
    public class GameObject
    {
      ...
      private List<Portal> whitePortals = new ArrayList<Portal>();
      private List<Portal> blackPortals = new ArrayList<Portal>();
      ...
    
      public GameObject whitePortal(int m, int o) throws Exception {
        whitePortals.add(new Portal(m, o));
        return this;
      }
    
      public GameObject blackPortal(int o, int m) throws Exception {
        blackPortals.add(new Portal(m, o));
        return this;
      }
      ...
    }