1

I am working on a personal project to create a game. My trouble I seem to be running into is getting my variable called runs to increase value. I have set my value to zero. I tried using runs++ though it only increases by one. What I am looking to do is increase by 1 or 6 depending on the result of the if statement. If anyone can point in the right direction on how to solve would be great!

public class BatsmanDice {

 public static void rollBatsmanDice() {
  // Roll player dice decides how many runs are scored when the player rolls.
  // We have options from 1 to 6 and Owzthat.
  // When Owzthat is triggered we return to the main method and run rollUmpireDice();
  // Using an Array list to have all options available.

  ArrayList batsmanDiceOptions = new ArrayList();
  batsmanDiceOptions.add(1);
  batsmanDiceOptions.add(2);
  batsmanDiceOptions.add(3);
  batsmanDiceOptions.add(4);
  batsmanDiceOptions.add(5);
  batsmanDiceOptions.add(6);
  batsmanDiceOptions.add("Owzthat");

  int runs = 0;
  System.out.println("Total runs " + runs);

  // We take our Array list from above and shuffle it using the Collections import tool.
  Collections.shuffle(batsmanDiceOptions);

  // We then take the shuffled array list and print 1 options to screen showing the dice rolled
  // Commented out print line statement to return a random shuffled array option

  //System.out.println(batsmanDiceOptions.get(1));

  if (batsmanDiceOptions.contains(1)) {
   System.out.println(" Scored 1 Run " + batsmanDiceOptions.get(1));
  }

 }

}
  • You are using ArrayList as a [raw type](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) which is very bad practice and should be avoided. But a general question: If you just want to create a random number between 1-6 why don't you just use the standard Java `java.util.Random` class and its `nextInt` method (as in `random.nextInt(6) + 1`)? – OH GOD SPIDERS Jan 16 '20 at 17:42
  • When I was researching ways to utilize the dice options I originally was using random.nextInt. The trouble I had was that my dice includes 1 to 6 as well as a word. Would there be a better option that is best practice to randomize 6 int values and a string? – Cody Johannessen Jan 16 '20 at 17:49
  • Does it really need to be a string? Couldn't you just create random number from 0-6 (instead 1-6) and when a 0 is rolled you do the same logic that you do currently when your string is selected? – OH GOD SPIDERS Jan 16 '20 at 17:51
  • Thank you for your reply! I'll try out that option. – Cody Johannessen Jan 16 '20 at 17:58

1 Answers1

1

I'm not positive if this is what you're asking. But if you're trying to "roll" and increment runs, just do:

ArrayList<Integer> batsmanDiceOptions = new ArrayList<Integer>();

// your code

runs += batsmanDiceOptions.get(0);

To increment runs by some random value. get(0) returns a random value because you've already shuffled the ArrayList.

That being said... to simulate a dice roll, why not just increment by a random number from 1 to 6? I would recommend using ArrayList for things like picking finite items out of a hat, because then .remove() becomes useful. For dice rolls I would probably just use Random.

sleepToken
  • 1,866
  • 1
  • 14
  • 23
  • I will try that option thank you. The dice options for the game include 1 - 6 and the string "Owzthat". Perhaps I could set the random int. Then run the string variable as a random boolean. – Cody Johannessen Jan 16 '20 at 17:51
  • @CodyJohannessen I see - then ArrayList won't work for you. Could you perhaps just use a dice 1-7, and if you pick a 7, handle that case and change it to "Owzthat"? – sleepToken Jan 16 '20 at 17:53
  • Thank you for your reply I will try with the options provided! – Cody Johannessen Jan 16 '20 at 17:57