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

 }

}
4
  • You are using ArrayList as a raw type 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)? Commented Jan 16, 2020 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? Commented Jan 16, 2020 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? Commented Jan 16, 2020 at 17:51
  • Thank you for your reply! I'll try out that option. Commented Jan 16, 2020 at 17:58

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@CodyJohannessen I see - then ArrayList<Integer> 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"?
Thank you for your reply I will try with the options provided!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.