I am trying write a program that will accept an input of number of dice and display a table of all possible dice combinations.

For example, if there are 2 dice the display would be:

1 1

1 2

1 3

.

1 6

2 1

2 2

.

6 5

6 6

for 3 dice it would be:

1 1 1

1 1 2

.

.

etc.

I am having problem setting up the loop/recursive part.

I thought it would've helped to set up an array. I am not sure if this is the right track or not.

My array looks like this:

Sheriff

**[Joe]: Second, Java doesn't have two-dimensional arrays, like you declare above. Java has "Arrays of arrays".**

It's true that "array of arrays" is more accurate than "two-dimensional array", but Jon never used the term two-dimensional array, so that's a side issue. Once the semicolon issue is fixed, there is nothing wrong with Jon's declaration of the showDice array. If you specify all the dimensions up front, Java will create the entire array-of-arrays structure for you. Unfortunately they didn't bother showing this in the tutorial.

"I'm not back." - Bill Harding, *Twister*

Sheriff

For the general solution, a recursive solution may be best. Try something along these lines:

That will take some refinement, but it's a start.

Non-recursive solutions are possible too, but I would favor recursion here unless you have a specific non-recursive solutino in mind.

[ December 08, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, *Twister*

But this is the closest I have gotten.

EDIT: Hmm. I see this seems to be what Jim proposes I do. So I must be very close. I wonder what else I am missing

[ December 08, 2005: Message edited by: Jon Tsai ]

A good strategy for solving problems recursively usually involves identifying the trivial case and non-trivial case. For non-trivial cases, you break off a bit or bits of work and process those bits again with the same method until you reach the trivial case. Here's an example of a recursive method that reverses a String:

Note that the recursive call occurs when you have a non-trivial case: you break down the string by separating the last character (trivial) and then reverse the substring before the last character (non-trivial). You save a recursive call by using

**s.substring(s.length()-1)**but you could write it this way as well:

The objective is to break off little bits of work at a time to try to get to the trivial case.

The recursive solution to display the possible outcomes of rolling N die is only a couple of lines longer than the reverse(String) method and the structure is almost identical.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

I have been on this problem for a while now. Even with the hints from Junilu I still can't get a handle on the problem. When I look at the reverse string exemple, it makes sense to me. But I just can't apply it to this dice problem

If anyone is willing to provide more help, it will be appreciated. Or else I think I would be going in circles forever.

Your problem is displaying the possibles rolls of N dice, right? So what would be the trivial problem? What value of N is trivial? (Go back to the reverse string problem for a hint).

For the trivial problem of N=?, write the code to show the roll combinations.

[ December 15, 2005: Message edited by: Junilu Lacar ]

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Thanks for your help. I am guessing that the trivail value of N is 1.

So...

Originally posted by Junilu Lacar:

I'll try to get you through without giving you the solution outright, OK?

Your problem is displaying the possibles rolls of N dice, right? So what would be the trivial problem? What value of N is trivial? (Go back to the reverse string problem for a hint).

For the trivial problem of N=?, write the code to show the roll combinations.

[ December 15, 2005: Message edited by: Junilu Lacar ]

Now, for the non-trivial cases. Look at the reverse() method example again. The recursive call occurs in the non-trivial case. That's because you haven't gotten to the trivial case yet. But just as you kept it simple by starting with the trivial case, try to start small with the non-trivial cases.

What is the smallest non-trivial case? Hint: It's the case where processing N would be just one recursive call away from processing the trivial case. So if N=(smallest non-trivial case), what might the recursive call look like to get to the trivial case?

Once you answer these questions, you're about 10% away from the final solution.

To take you the rest of the way, switch from thinking about dice to thinking about how an odometer behaves. Notice how the way an odometer counts miles is very similar to how you might list down the different possible rolls of N die. How are the patterns similar?

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Ok. This is where I am getting in trouble. The smallest non-trivial case would be 2. So a recursive call would be something like Call(N-1)? I don't think I am doing the following recursive call correctly. Also I am not sure what type the method should be(void, int, etc).

Sheriff

**[Jon]: I don't think I am doing the following recursive call correctly.**

Well, it's close. Have you run it? What exactly is wrong with the output? Is there any way to modify the code to fix this?

**Also I am not sure what type the method should be(void, int, etc).**

So far you're not returning anything, and you're not trying to assign a value that was returned from the method. So void seems like the right choice. If you have a method that returns something, the need for a specific return type will become more obvious. Until then, I recommend using void.

[ December 16, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, *Twister*

Ya. I did ran it. The output for 2 dice is:

11

2

3

4

5

6

21

2

3

...

61

2

3

4

5

6

It's very close to what I wanted. And I have been wracking my brain to try to figure out how to those extra "placer" digits.

I understand it might have something to do with Junilu's odometer problem.

Originally posted by Jim Yingst:

[Jon]: I don't think I am doing the following recursive call correctly.

Well, it's close. Have you run it? What exactly is wrong with the output? Is there any way to modify the code to fix this?

Also I am not sure what type the method should be(void, int, etc).

So far you're not returning anything, and you're not trying to assign a value that was returned from the method. So void seems like the right choice. If you have a method that returns something, the need for a specific return type will become more obvious. Until then, I recommend using void.

[ December 16, 2005: Message edited by: Jim Yingst ]

You're current output is

11

2

3

4

5

6

21

2

3

4

5

6

...

But what you want is

11

12

13

14

15

16

21

22

23

24

25

26

...

Doesn't that tell you that you have an output statement at the wrong level? Instead of printing out at the N level, wouldn't you want to print out at then N-1 level? That is, you really want to put off printing anything out until the very last level (the trivial case). In the non-trivial case, you just want to build up something that indicates what the current state of progress is. I'm being purposely vague because I really don't want to give the solution away.

Hint: you are allowed to pass in more than one parameter to a method.

You're soooo close. I just know you can figure this out.

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

Sheriff

"I'm not back." - Bill Harding, *Twister*

Originally posted by Jim Yingst:

Ah... ste, it looks like you're showing an example of a non-recursive solution. It's possible to solve the problem that way too - but I think if Jon is close to a recursive solution, it would be better to continue pursuing that rather than doing something very different. So Jon, I recommend you either ignore ste's solution, or if you do want to follow it, then ignore Junilu's recursive solution hints. They're really two different things. And I think you're pretty close to a recursive solution.

Ye i realised that after i read full post

Stephen Foy - Microsoft Application Development Consultant

Sheriff

"I'm not back." - Bill Harding, *Twister*

At first I did this

Like I said. it worked for 1 and 2 dice. But not for 3+. So I changed the code a little.

That seemed to work a little better. It seems like to work a little better.

But again, I am still missing something.

Sheriff

*mean*. So I'm not entirely sure what the last code is supposed to do. However, I would say it looks to me like your code is getting more complex than necessary for the solution. I liked the code you had on December 16, 2005 07:22 PM. That was probably closer to the solution than what you have now. So it might be easiest for you to go back to that version, rather than continue with what you have now. Consider: what's missing from most of the output is the output for the earlier die rolls - the numbers from higher in the call stack. Is there a way to pass on this data whenever you make the recursive call? For example, could you add a String parameter to the method signature? This would represent the output for the "earlier" dice. If you pass that info along in each recursive call, then you can write that data on every line that needs it. Can you find a way to do something like this, using code fairly similar to what you had on December 16, 2005 07:22 PM?

"I'm not back." - Bill Harding, *Twister*

Sheriff

*all*the previous numbers you need. The second parameter could also be some sort of array or list if you prefer, but I think using a String here will be simpler.

By the way, I may not have much internet access for the next few days. So I may not be able to answer your followup questions, but hopefully others will be available. Good luck.

"I'm not back." - Bill Harding, *Twister*

You're just 1 step from the final code. Your code works for N=2 because you figured out that you had to print out the state of processing the N=2 level in the trivial state (N=1). You already see the pattern from N=2 to N=1. Now generalize your solution.

Let's say you have a number of jigsaw puzzle pieces, each with a letter on it. When fitted together, the puzzle pieces will spell a word. If the word is N letters long, you'd have N puzzle pieces. Now if N people were given the task of solving the puzzle, each could take one puzzle piece, then they could figure out the word, order themselves in a line so that they spell out the word and put the puzzle together by having the first guy pass his puzzle piece to the next, the next guy fitting in his piece, passing the two pieces to the next, who fits in his piece and then passes all the pieces so far to the next guy, and so on until the last guy fits his piece in to complete the whole puzzle.

Sure that's a pretty silly way to solve a puzzle like that but then it might not be such a bad way of solving other kinds of puzzles .... (hint, hint )

[ December 18, 2005: Message edited by: Junilu Lacar ]

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

Strings. Now everything makes sense.

Especially this

The recursive solution to display the possible outcomes of rolling N die is only a couple of lines longer than the reverse(String) method and the structure is almost identical.

For a while there I couldn't figure out what that means.

Now I see...ok.. off I go to figure this out.

Jon, congratulations on figuring it out!

One last thing: you might want to take Jim's advice and assign more meaningful names to your variables.

Good job!

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

Sheriff

Somethign else to consider here (if you're not really sick of this problem by now): is N=1 really the most trivial case? I would assert that there's one more trivial, and a recursive method based around this even-mor-trivial case may reduce some of the duplication currently present in your code. Something to consider...

"I'm not back." - Bill Harding, *Twister*