Results 1 to 11 of 11

# Thread: How can this be done?

- 01-24-2011, 07:19 PM #1Member
- Join Date
- Dec 2010
- Posts
- 11

- Rep Power
- 0

## How can this be done?

Hello. I'm a moderate java programmer with a lot of C++ friends that help me when they can. I have been working on a program for Yahtzee, the dice game. I have started five random number generators, one for each "Dice".

Here's where i am stuck. I need to find a way to check how many times a number (like 5) occurs in these six random generators. if it occurs twice then i need to check the other numbers for occurrences. if there were two 5's and three 4's then it would output a full house.

I need to know how to check those occurrences of six different generators. I thought about trying to convert the random generators into Strings and then the Strings into a string array. Run a command to check occurrences through the array. but i cant find the command.

Any help you can give would be so helpful. And fyi this is a personal project not anything school related. thanks!

- 01-24-2011, 07:25 PM #2
How would you do this "by hand" without any code?

I don't see a need to use Strings at all.

You might find some useful functions in the Collections API: Collections (Java Platform SE 6)How to Ask Questions the Smart Way

Static Void Games - GameDev tutorials, free Java and JavaScript hosting!

Static Void Games forum - Come say hello!

- 01-24-2011, 07:29 PM #3Member
- Join Date
- Dec 2010
- Posts
- 11

- Rep Power
- 0

The only reason i used strings was so that the user could choose which dice to re-roll. but that's a whole different hurdle to be crossed later. How would you do it just using Int variables. i can always convert to strings later.

- 01-24-2011, 07:33 PM #4Member
- Join Date
- Dec 2010
- Posts
- 11

- Rep Power
- 0

and to do this by hand. you would roll the five dice. and then look for matches. like poker. one pair, two pair, three of a kind, full house, flush, or five of a kind. points are awarded accordingly.

- 01-24-2011, 07:59 PM #5
One of the challenges faced by programmers is taking a task that's obvious to a human (such as "look for matches") and breaking it down into what you're actually doing, step by step, in a way a computer can understand.

Pretend you have a friend who has no idea how to look for matches. What would you tell him to do? Write out a specific set of instructions that he could follow and succeed, and you'll have an algorithm that should be pretty easy to convert into code.How to Ask Questions the Smart Way

Static Void Games - GameDev tutorials, free Java and JavaScript hosting!

Static Void Games forum - Come say hello!

- 01-24-2011, 10:32 PM #6

i would create an array of 6 integers with an index between 0 to 5 initialized with 0. after you get your 5 generators results you can add 1 to each array accordingly to the number (number -1, since the index is zero-based) generated. the number generated can be used as index. after all generated number has been assigned you can evaluate the array. if there is a sum of 3 and and sum of 2 then you will know that there is a full house, if there is a sum of 5 you will know there is a yahtzee and so on. for the next game you can reinitialize your array. the implementation of this approach will need a little lines of code. do you got the idea?

- 01-24-2011, 11:15 PM #7Senior Member
- Join Date
- Mar 2010
- Posts
- 952

- Rep Power
- 8

I think it makes more sense to write a method for each scoring possibility. So you'd have a set of methods like:

public int scoreAces() {...}

public int scoreTwos() {...}

public int scoreThrees() {...}

...

public int scoreThreeOfAKind() {...}

public int scoreFourOfAKind() {...}

public int scoreSmallStraight() {...}

public int scoreLargeStraight() {...}

public int scoreYahtzee() {...}

This way you're only looking for one combination at a time, rather than trying to look at everything all at once. Where you have human players, you're not going to try to score until after the third roll when they choose a category anyway. And even if you're implementing a computer player, it still makes sense to follow this approach, I think -- go through each of these methods and see which one returns the highest score.

-Gary-

- 01-25-2011, 02:39 AM #8
Use nested loops and compare each die to every other die. Each time you find a match increment a counter. When you are done the counter will tell you what the hand is as each type of hand (pair, 2 pair, three of a kind etc) has a unique value.

- 01-25-2011, 05:22 AM #9Senior Member
- Join Date
- Jan 2009
- Posts
- 671

- Rep Power
- 9

I would add the Dice to a sorted set of some kind (probably a TreeSet), then you just have to check consecutive elements for number of 'N' in a row.

- 01-26-2011, 02:29 PM #10Senior Member
- Join Date
- Sep 2010
- Posts
- 109

- Rep Power
- 0

Well, you could use an array of a sort, the an array sort and you would see how many times you rolled the same number, then you can pick from the array to get the data you needed but this could very well be over complicating this for your needs. Also, is it GUI based or text based? This changes the completely of the program in different ways.

- 01-26-2011, 03:21 PM #11
assume you store your roll in a array like

Java Code:public ArrayList<Integer> roll() { int r=0; int z=0; // the init() initialize the array with zeros if there is a previous roll init(); for (int i = 0; i < 5; i++) { r = (int)(Math.random()*6); z = result.get(r); result.set(r, z+1); } setProposition(result); return result; }

then for setting a proposition you could use this code

Java Code:public void setProposition(ArrayList<Integer> result) { boolean found = false; // is there a yahtzee for (int i=0; i<6; i++) { if (result.get(i)==5) { proposition= "Yahtzee"; sum = 50; return; // are there four of a kind } else if (result.get(i)==4) { proposition= "Four-Of-A-Kind"; sum = result.get(i)* (i+1); return; } // and so on }

so it doesn't matter which dice is a yahtzee, the criterium you have to check is if there is a dice with a sum of 5, and for a full-house there must be a dice with a sum of 2 AND and other with a sum of 3. i think, this approach simplify the evaluation of the roll.

## Bookmarks