Results 1 to 11 of 11
  1. #1
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Question 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    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.

  4. #4
    Java26 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    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.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Java26 View Post
    Any help you can give would be so helpful. And fyi this is a personal project not anything school related. thanks!

    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?

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  10. #10
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    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.

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •