wood burning stoves*
The moose likes Beginning Java and the fly likes Displaying dice combinations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Displaying dice combinations" Watch "Displaying dice combinations" New topic
Author

Displaying dice combinations

Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hello. I am a new at this so please bear with me.
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:



Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

First, you're missing a semicolon:

Second, Java doesn't have two-dimensional arrays, like you declare above.
Java has "Arrays of arrays". Have a look at the Java Tutorial chapter on Arrays for some examples.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Jon: I'm not sure if the array will necessarily help you for your problem, if you simply want to print out each of these permutations. Try just putting a print statement in your loop, so you can see what you have so far. I believe you're extremely close to having this solved for exactly 2 dice, but to generalize this to n dice you'll have to change it somewhat. Before you can do that though you probably need to see your code compile and understand just what you've accomplished so far. Printing some output may help there.

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 ]
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hi guys. Thanks for the replies. I have just read them so I haven't really thought them through yet. I just wanted to say thanks for your input so far. And post the rest of my codes. Obviously it doesn't work correctly yet, or else I wouldn't be here
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 ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

Jon,

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hello.
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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

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 ]
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Junilu,
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 ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

Great! You're halfway there!

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?
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hiya,

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).

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hi Jim,

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 ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

Jon, you're sooooo close. What's missing is a link between the current case and the previous case.

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.
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Maybe this will help.



Stephen Foy - Microsoft Application Development Consultant
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
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
Ranch Hand

Joined: Oct 17, 2005
Posts: 143


Hmmm...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Uhhh... I think I liked Jon's last code better. Let's not distract him too much. Also I suspect this is a school assignment, so if you do come up with some code that works, please don't post the complete solution. Jon needs to work this out himself. Thanks.
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hiya. Just wanted to say I appreciate all your help, and that I am still working on the problem. I have the 1 die and dice conditions worked out. Now I am trying to figure out 3,4,5...N dice.

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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm. When I see variables with names like x and y, it's hard for me to tell what they 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?
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hi Jim,
Using my earlier code, it would be something like this.

Is this what you mean? I was trying to figure out how to make it into N dice so I guess I bloated the codes.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That's closer, yes. The problem with that second variable y is that it's only one number (at a time), and when there are more than 2 dice you need to pass in information from more than one number. (From more than one previous die roll.) That's why I suggested making that second parameter a String - it can contain a list of 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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

Jon,

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 ]
Jon Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Holy mackrel!
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 Tsai
Greenhorn

Joined: Dec 07, 2005
Posts: 10
Hiya. You guys are great!
And I can't believe I kept to try to print it out directly by just doing the recursive loops. The idea of having temporary holder totally escaped me. oh well.

Thanks again. Now I can go on with my plans to take over the world

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5



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!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Cool. Congratulations.

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Displaying dice combinations
 
Similar Threads
Probability Question
All possible combinations of the elements in the string array
Beginner Yahtzee Progam
Just a little question
help on Yahtzee