0

I am writing a small game for fun that uses an action system for multiple teammates. I do not know how to represent the range of all members 0-9 in an array in a while loop.

I know that, there is a way to do closedOpen, but I don't know how that would fit into the code.

int[2][10][0] hea //Using two teams, each with 10 members, who have multiple traits
// ^ I know this isn't perfect syntax 

while (hea[0][0-9][0]!=0){ // Tests for if at least one member of team has actions
    Actions
}
// Is there a way to represent the middle step in the array without typing out all and statements
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39

3 Answers3

0

One of the most direct ways is to write a (private) method to do the checks all over the array. Something like:

/** Tests if at least one member of the team has actions left */
private boolean haveActionsLeft(int [][][] hea, int team, int members) {
  for (int m = 0; m < members; m++) {
    if (hea[team][m][0] >= 0) {
      return true;
    }
  }
  return false;
}

And then you can call the function into your while statement's condition:

...
while (haveActionsLeft(hea, 0, 10)) {
  Actions
}
Thoozee
  • 1
  • 1
  • So from how I understand the code, does the function calling import the hea array? I'm new, so I don't yet understand new methods. I would guess you place this new function above the public class? Also, I think it would be just greater than, instead of equals also, because that would signify having no actions. – CableCaberCan Oct 16 '19 at 17:20
  • I am also wondering if you can set members as fixed, so instead of always typing 10 in the function call, can I just type ```haveActionsLeft(int [][][] hea, int team, int members = 8) ``` – CableCaberCan Oct 16 '19 at 17:26
  • 1) In Java arrays are objects, so their reference is passed by values in methods calls. This is quite confusing at the beginning, so I suggest you to read some other questions about the topic; for example, I think [this](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) could be helpful. – Thoozee Oct 18 '19 at 12:01
  • 2) Unluckily, in Java you cannot assign default values to method's parameters. but if you want to accomplish an implementation where the method call _without_the last parameter behaves as if the parameter was 8, you can use a powerful "technique" called *overloading*. [continues...] – Thoozee Oct 18 '19 at 12:02
  • You can define two methods: both with the same name `haveActionsLeft` and both with the same parameters list, _except for the last one_: the first method would have the parameter `int members` in the last position, while the second would have nothing in that place. In practice: one would have 3 parameters, while the other only 2. Then you can implement the second method assuming that the members number is 8. Once you do this, you'll be free to call `haveActionsLeft` wherever you need in your program, and Java will be able to understand if you're meaning the first one or the second. – Thoozee Oct 18 '19 at 12:02
  • Please, note that this is not a proper explanation of what *overloading* really is and how does it work, but this will be only the "effect" you would experience in your code based on what you asked. For a better explanation, you can read [here](https://stackoverflow.com/questions/9598166/java-method-overloading). Ps. sorry for the splitted message. I'm new on StackOverflow and I don't know if there's a more correct way to answer :) – Thoozee Oct 18 '19 at 12:04
0

How about a for loop?:

for (int i = 0; i < hea.length; i++) {
    int[][] team = hea[i];
    for (int member = 0; member < team.length; member++) {
        int[] traits = team[member];
        //Check actions
    }
}

Speaking from a game design perspective, I would not shy away from using classes for things like this:

public class Team {
    private final List<Player> players = new ArrayList<>();
    public static final int MAX_SIZE = 10;
}

public class Player {
    public String getName();
    public List<Action> getActions();
}

public class Action {
    private final int[] basic; //however you wish to implement
}

public class Game {
    private final List<Team> teams = new ArrayList<>();
    public void addPlayer(Player player);
}

Check out the Oracle Java Tutorials for a more in-depth explaination on learning java.

Rogue
  • 11,105
  • 5
  • 45
  • 71
0

If I'm not mistaken by your intent, this is the perfect use case for a for loop

A for loop takes a value and increments (or decrements) it and is a good way to iterate through a fixed number of options.

for(int i = 0; i < 9; i++) {
   if(hea[0][i][0] != 0) {
      Actions
   }
}

This says: start i at 0, and go through the loop. At the end of the loop, increment i. continue until i reaches 9 and then break the loop.

Darkserum
  • 11
  • 4