简体   繁体   中英

Calling the same method multiple times without overwriting previous values

Apologies if this is trivial to most but I just can't figure this issue out!!

I am creating a mock game where I have a start, end, and hops along. There are portals where if you go on a white portal you jump further ahead and there are black ones where you go backwards. I have set up the class as a 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;
        }
    }
}

So in my main method I call the it like create and call this class like;

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

My issue is when I want to add in more than 1 whitePortal/blackPortal

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

The values 18 - 26 override 5 - 12. How can I set this up so I can have multiple white and black portals?

It seems that your data structure is not enough to solve this problem.

You need to define a collection of whitePortals and a collection of blackPortals. If you do so calling the method whitePortal(5, 12) add a new white portal insted of setting the white portal values of the only white existing portal.

You need to define a class Portal

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

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

Then you can use it in the GameObject as follow:

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 
}

Well, you can use the following approach:

  1. Introduce a new "Portal" type with start/end attributes
  2. Replace white/black portal attributes in your class with lists for white and black portals (or any other type of collection you like)
  3. Replace getWhite/Black methods with access to lists
  4. Refactor whitePortal and blackPortal method to create new instances of a portal object and add them to an appropriate collection

You can, of course, use arrays instead of collections, but that's a bit more cumbersome.

Also, assuming portals are collections, you probably need to add helper methods for operating on those. Depending on what your actual needs are.

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;
  }
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM