Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can I get my dice roller to completely loop and sort results correctly?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How can I get my dice roller to completely loop and sort results correctly?

    So I have this code that I wrote and for now it only works with two dice. I am currently trying to get it to recognize more than two dice or possibly one die but for now I keep getting exceptions in the program. I know why but I'm not sure how to fix it. The only things I can say for sure if that I need to make my array size up properly with the number of dice I'm throwing like and also fits the results of the dice thrown since it needs to keep track of how many times a certain number came out depending on how many dice were thrown, how many sides they had, and how many times they were thrown. Can anyone please help me with this? Thank you.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package dice.roller;
     
    /**
     *
     * @author Marlin
     */
    import java.util.*;
     
    public class DiceRoller {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            String name;
            int roll, sides, dice, add = 0, tmp = 0;
            int[] result;
            //temp
            result = new int[11];
     
            System.out.println("Hello, what is your name?");
     
            name = keyboard.next();
     
            System.out.println("Hello, " + name + ", how are you today?");
            System.out.println("How many times would you like to roll the dice?");
     
            roll = keyboard.nextInt();
     
            while (tmp < 11) {
                result[tmp] = 0;
                tmp++;
            }
     
            System.out.println("You want to roll the dice " + roll + " times.");
     
            System.out.println("How many dice would you like to roll?");
     
            dice = keyboard.nextInt();
     
            System.out.println("You would like to throw " + dice + " dice.");
     
            for (int i = 0; i < roll; i++) {
                add = 0;
                for (int x = 0; x < dice; x++) {
                    int die1 = (int) (Math.random() * 6) + 1;
                    // int die2 = (int)(Math.random()*6) + 1;
     
                    //  add = die1 + die2;
                    add += die1;
                }
     
                //System.out.println(add);
     
                ++result[add - 2];
            }
     
            tmp = 0;
            while (tmp < 11) {
                int counter = tmp + 2;
                System.out.println("The result " + counter + " came out " + result[tmp] + " times");
                tmp++;
            }
        }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How can I get my dice roller to completely loop and sort results correctly?

    Quote Originally Posted by Skynet928 View Post
    ...more than two dice ...how many sides...]
    Look at your program, and think back to the time when you were designing it.

    What statements in the program depend on the number of dice being two?
    What statements in the program depend on each die having six sides?

    You already have variables "dice" and "sides" that you should be able to incorporate into the program in places where they make a difference. For this discussion, I'll call them N and S.

    Well, one part is easy: Change the random number call to let the value of a single die be 1, 2, ... , S rather than 1, 2, ..., 6 that you have now.

    Next:

    What is the range of values that you can get if you toss N of those dice? That will determine the size that the result array should be.

    Lowest value of a single play is N, highest is N*S

    The easiest way to accommodate this range of sums might be to declare an array with N*S+1 elements, knowing that 0, 1, ..., N-1 won't be used, and the sums will be held in N, N+1, ... , N*S

    Then, for each play, you simply add up the N dice values and store the sum in the array element with index equal to the sum. If the sum is, say 8, store it in result[8].

    After a play, if you want to know and report, say, the number of sevens that were thrown, just look at result[7].

    If you don't want to have the useless lower values, then you should be able to use your present approach and figure out what the size of the array will be and figure out how the index depends on the sum so that it will be stored in the correct array member. (Same approach to associating an index value to retrieve a given sum.)

    I mean, look at the "magic numbers" in statements like the following that depend on two six-sided dice). For example:
     
            result = new int[11];
     
            while (tmp < 11) {
     
                ++result[add - 2];
    .
                int counter = tmp + 2;

    See what I mean? Where did the numbers 11 and 2 come from?

    They depend on the number of dice and the number of sides of each die.

    Start by changing the constants 2 and 11 to expressions involving your dice and sides variables such that they work for 2 six-sided dice. Then try with three six-sided dice. (Set dice equal to 2 and sides equal to 6.)

    Now, don't just start plugging stuff into the program and see if you get lucky. Work it out on pencil and paper to make sure the expressions make sense for different numbers of dice and different numbers of sides, then test. Maybe put some print statements in the loop that shows the individual die values and the sum. That might help you pull things together:
            for (int i = 0; i < roll; i++) {
                sum = 0;
                for (int x = 0; x < dice; x++) {
                    //
                    // Change the next line to take into account the number of sides
                    int die1 = (int) (Math.random() * ???) + 1;
     
                    System.out.print(die1 + " "); // For debugging
     
                    sum += die1;
                }
     
                System.out.println("--> " + sum); // For debugging
     
                // Put code here to increment the appropriate element of result 
                ++result[what???]
            }

    Test with two six-sided dice. Test a lot. Make sure it does and reports exactly what you expect.

    Change it to test with three six-sided dice. Test a lot.


    Then, change to, say, two 12-sided dice and see how the values of the expressions change. Test.

    Etc.


    Cheers!

    Z

Similar Threads

  1. How do i display each loop results
    By Amshank24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 14th, 2013, 07:43 AM
  2. [SOLVED] Dice Roller
    By astraya008 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 8th, 2012, 06:40 PM
  3. LDAP Sort 2 attributes returns NO results. Works fine if sorted on one.
    By funnyguy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 30th, 2011, 01:31 AM
  4. Stumped by while loop results
    By mwr76 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 3rd, 2011, 09:31 AM
  5. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM