0

I want to write a program that simulates the following: I have 6 dice, and I roll with some dice every time.

When I don't roll with a die, I just assume that I rolled 0 with that.

I want to list all the possible variations I can get this way. A few examples:

1,2,4,6,0,1

3,5,1,0,0,4

6,6,4,2,0,0 etc.

Any ideas on how to do this? (I am using Java, but of course, I'm only interested in the general concept.)

7
  • 3
    What have you tried? Dec 8, 2012 at 19:18
  • Look at the "Related" section at the right. There are already dozens of questions asking for this.
    – JB Nizet
    Dec 8, 2012 at 19:19
  • Of course I did search before I asked the question, but I couldn't find anything like this. Dec 8, 2012 at 19:27
  • As for the code, I didn't get far after I realized that my original concept was extremely stupid, I was just thinking since then. (originally my code just subtracted 1 from each dice's value, until it was 0, and then it went to the next one) Dec 8, 2012 at 19:27
  • 1
    I’m voting to close this question because it is a well understood problem, but the OP has shown no thought or effort whatsoever.
    – duffymo
    Nov 8, 2022 at 14:22

5 Answers 5

3

You can use a recursive method, keeping track of depth: EDIT, maybe this method would be better:

class Main {
    public static void roll(String s, int depth) {
        if(depth == 0)
            System.out.println(s.substring(1));
        else
            for(int i = 0; i <= 6; i++)
                roll(s + "," + i, depth - 1);
    }
    public static void main(String[] args) {
        roll("", 6); //2nd parameter > 0
    }
}
1
  • Awesome! I'll accept your answer because I was looking for a recursive solution, but also thanks to all the others! Dec 8, 2012 at 19:34
3

Since you specifically asked for "only the general concept", here are two general approaches, either:

  • Use a 6-level nested for-loop for just do an exhaustive enumeration of all possible rolls between 0-6 (more efficient)
  • Just use 1 for-loop to generate all numbers between 0-66666, and discard any numbers that contain 7, 8, 9; and then print the numbers with some formatted padding, and commas (cleaner code to look at if you don't care about the small efficiency difference)
3
  • Thanks! I thought of the 6 level nested loop first, but I thought that there must be a more convenient solution. (I still wonder if this could be done with recursion somehow.) I think I'll go with the second, great idea, thanks! Dec 8, 2012 at 19:30
  • The first method is very messy, and will be very hard to modify/update in the future. The second method is extremely inefficient.
    – arshajii
    Dec 8, 2012 at 19:32
  • Yeah, but I only need it for this particular problem, so it would do fine. Altough the recursive method is nicer. Dec 8, 2012 at 19:33
1

Just for elegance sake, I would write a recursive method that calls loops through 0-7 for a given index then itself to initialize the next index.

It could then initialize an array or abritrary size.

0

Simple Python implementation.

This is when you just print.

def PrintAllPerms(n, str_):
    if (n == 0):
        print str_
    else:
        for i in ["1","2","3","4","5","6"]:
            str_ = str_ + i
            PrintAllPerms(n-1, str_)
            str_ = str_[:-1]

PrintAllPerms(2,"")

This is when you want to return the whole permutation.

def PrintAllPerms(n, arr, str_):
    if (n == 0):
        arr.append(str_)
        return arr
    else:
        for i in ["1","2","3","4","5","6"]:
            str_ = str_ + i
            arr = PrintAllPerms(n-1,arr,str_)
            str_ = str_[:-1]
        return arr

PrintAllPerms(2,[],"")
-2
public class DiceTest {
    public static void main(String[] args) {
        int[] dice = {0, 1, 2, 3, 4, 5, 6 };
        for (int i : dice) {
            for (int j : dice) {
                System.out.println("Printing dice values : " + i + " " + j);
            }
        }
    }
}
1
  • 2
    Please provide some context to your answer. Dec 19, 2018 at 16:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.