# Small yahtzee program

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-11-2009, 08:10 PM
kimmelim
Small yahtzee program
Having some minor problem here, im making a yahtzee game as you can see. The main issue here is how to compare the dices in a clever way, or count them, then we can figure out how many point the player will get.

Example: Get 5 dices with 1, get 5points
Get 5 dices with 6, get 30points

Another question is how I can make the player choose which number from 1 - 6 he wants to collect this time.

After the player has tried 6 times, collection dices from 1 - 6, we have to sum up the score.

Code:

```public class Yahtzee {     public Player player1;     private Dice dice1;     private Dice dice2;     private Dice dice3;     private Dice dice4;     private Dice dice5;     public void main(Player player1)     {         int a, b, c, d, e;         this.player1 = player1;         this.dice1 = new Dice();         this.dice2 = new Dice();         this.dice3 = new Dice();         this.dice4 = new Dice();         this.dice5 = new Dice();         System.out.println("Let's play Yahtzee!");         a = this.dice1.getValue();         b = this.dice2.getValue();         c = this.dice3.getValue();         d = this.dice4.getValue();         e = this.dice5.getValue();           int i=1;           int j=2;           int k=3;           int l=4;           int m=5;           int n=6;                           {             if(a==i)             {                 player1.addCredits(1);                 System.out.println("You have one dice with " + dice1.getValue() + ", you get one point,your credits are now: " + player1.getCredits());             }                         if(b==i)             {                 player1.addCredits(1);                 System.out.println("You have one dice with " + dice2.getValue() + ", you get one point,your credits are now: " + player1.getCredits());             }                         if(c==i)             {                 player1.addCredits(1);                 System.out.println("You have one dice with " + dice3.getValue() + ", you get one point,your credits are now: " + player1.getCredits());             }                         if(d==i)             {                 player1.addCredits(1);                 System.out.println("You have one dice with " + dice4.getValue() + ", you get one point,your credits are now: " + player1.getCredits());             }                         if(e==i)             {                 player1.addCredits(1);                 System.out.println("You have one dice with " + dice5.getValue() + ", you get one point,your credits are now: " + player1.getCredits());             }         {             if(a==i && b==i)             {                 player1.addCredits(2);                 System.out.println("You have two dices with " + dice1.getValue() + ", you get two points,your credits are now: " + player1.getCredits());             }             if(b==i && c==i)             {                 player1.addCredits(2);                 System.out.println("You have two dices with " + dice2.getValue() + ", you get two points,your credits are now: " + player1.getCredits());             }             if(c==i && d==i)             {                 player1.addCredits(2);                 System.out.println("You have two dices with " + dice3.getValue() + ", you get two points,your credits are now: " + player1.getCredits());             }                         if(d==i && e==i)             {                 player1.addCredits(2);                 System.out.println("You have two dices with " + dice4.getValue() + ", you get two points,your credits are now: " + player1.getCredits());             }         }     } } }```

Code:

```public class Player { private String firstname; private String lastname; public int credits; /**  * The constructor of the player class.  * @param firstname the firstname of the player.  * @param lastname the lastname of the player.  */ public Player(String firstname, String lastname){     this.firstname = firstname;     this.lastname = lastname;     credits = 100; } /**  * Method that allows alteration of the firstname.  * @param firstname the new firstname of the player.  */ public void setFirstname(String firstname){     this.firstname = firstname; } /**  * Method that allows alteration of the lastname.  * @param lastname the new lastname of the player.  */ public void setLastname(String lastname){     this.lastname = lastname; } /**  * Method that adds additional credits.  * @param sum the sum to be added.  */ public void addCredits(int sum){     credits = credits + sum; } public void removeCredits(int removecredits)     {         credits -= removecredits;     } /**  * Method that returns the firstname.  * @return firstname.  */ public String getFirstname(){     return firstname; } /**  * Method that returns the lastname.  * @return lastname.  */ public String getLastname(){     return lastname; } /**  * Method that returns the credits.  * @return credits.  */ public int getCredits(){     return credits; } /**  * Method that checks if a player has credits.  * @return true/false.  */ public boolean validPlayer(){ if(credits > 0) return true;     else return false; } /**  * Method that prints out info about the player.  */ public void print(){     System.out.println("************************");     System.out.println("Player: "+firstname+" "+lastname);     System.out.println("Credits: "+credits+" kr.");     System.out.println("************************"); } }```
Code:

```import java.util.Random; public class Dice {    private int value;  private Random random;  /**  * The constructor of the Dice class.  */  public Dice() {   if (random == null) {     random = new Random();   }   this.trowDice();  }  /**  * Method that returns the value of the dice.  * @return value.  */  public int getValue() {   return this.value;  }  /**  * Method that returns the value of the dice.  * @return value.  */  public int trowDice() {   this.value = random.nextInt(6) + 1;   return this.value;  } }```
• 03-11-2009, 08:47 PM
Fubarable
Use arrays, Luke, use arrays.
• 03-11-2009, 10:20 PM
kimmelim
Something like this?
Code:

```import java.util.ArrayList; public class Yahtzee {     public Player player1;     private Dice dice1;     private Dice dice2;     private Dice dice3;     private Dice dice4;     private Dice dice5;     public void main(String[]arg)     {         int[] Dice = {dice1.trowDice(), dice2.trowDice(), dice3.trowDice(), dice4.trowDice(), dice5.trowDice()};         System.out.println("Let's play Yahtzee!");                           { } } }```
• 03-11-2009, 10:31 PM
Fubarable
I meant to use an array of Dice. If you do this, you will be able to simplify your code immensely making it much easier for you (and us) to debug.
• 03-11-2009, 10:51 PM
Fubarable
Please describe the game rules a bit more.
• 03-11-2009, 11:01 PM
kimmelim
This will be a forced yahtzee game. It goes like this:

The player has 5 dices, at each level the player can throw the dices three times, to get the best possible score.
The player can save the dices with the correct number, before he tries again.

The player has to collect dices with the correct number on the dice.
Level One = collect dices with 1 (gives 1 point pr correct dice)
Level Two = collect dices with 2 (gives 2 point pr correct dice)
Level Three = collect dices with 3 (gives 3 point pr correct dice)
Level Four = collect dices with 4 (gives 4 point pr correct dice)
Level Five = collect dices with 5 (gives 5 point pr correct dice)
Level Six = collect dices with 6 (gives 6 point pr correct dice)

The sum the player gets has to be summed up, and if it is above or 42 exact, the player gets 50 bonus points.
• 03-11-2009, 11:15 PM
Fubarable
I'm sorry, but my brain must be slow today. This means:
Quote:

Level Four = collect dices with 4 (gives 4 point pr correct dice)
?

And also, I take it no points for straights?
• 03-11-2009, 11:50 PM
kimmelim
That is correct, straight, full house and so on does not apply in this version ; )
• 03-11-2009, 11:54 PM
Fubarable
I'm sorry, but I'm still unsure about the quote in my post above regarding:

Level XXX = collect ......
• 03-12-2009, 12:00 AM
kimmelim
collect, with this I mean if you get for example two dices with 4, you collect them, and next trow you only got three dices left. Does this make more sense now?

Example:

Throw one:
dice1 = 1
dice2= 1
dice3 = 2
dice4 = 3
dice5 = 6

collect dice1 and dice2, since they had the value desired at lvl1
• 03-12-2009, 12:05 AM
Fubarable
So you collect dice1 and dice2, and then throw dice3-dice5,
say only one has rolled 1, you collect it, throw the other two, and no 1's occur,... so the score for the turn is 3 for the 3 ones?
• 03-12-2009, 12:08 AM
kimmelim
3 ones means 3 points (score) yes ; )
• 03-12-2009, 12:12 AM
angryboy
i'm confused as well. so i did some digging, looks like a fun game.
Quote:

On each turn, a player gets up to three rolls of the dice. He or she can save any dice that are wanted to complete a combination and then re-roll the other dice. After the third roll, the player must find a place to put the score (though he or she can choose to end the turn and score after one or two rolls, if desired). If the resulting combination of dice will not fit in any unused scoring category, the player must place a "zero" in one of the unused boxes. Each player's total score is calculated by summing all thirteen score boxes.
Yahtzee - Wikipedia, the free encyclopedia
• 03-12-2009, 12:15 AM
Fubarable
I would not roll the Dice in its constructor, but rather simply construct a Dice object. It should be rolled by a public int method called, well, roll(), and roll can return an int and it should also set the value field, as you'll need this at the end.

I would give Dice a boolean field, say called "collect", and a public method setCollect(boolean c) as well as a boolean getCollect() method.

Also, the roll method should not allow a change in value if collect is true.

I would also create a class DiceHand that holds the 5 dice all together and encapsulates the behavior of the 5 dice. It would hold an array of 5 dice, it would have a roll method, it would probably sort the dice array after each roll, it would all 3 rolls per turn but no more, it would have a method to allow for selecting dice to collect, a method for getting roll results, ...
• 03-12-2009, 12:27 AM
anteup
hmm...

Code:

```//you could declare a boolean variable and initialize it to false. boolean rollAgain = false //ask the user if he wants to roll any dice over in the first place //the reason you are parseing to a boolean is becasue this //method call is returning a string. //now also 'the user would have to enter only true or false rollAgain = Boolean.parseBoolean(JOptionPane.showInputDialog("roll again?  enter ONLY 'true' or 'false'")). //now if they choose they want to roll again //you are saying while they choose to roll again  while (rollAgain == true)   {             String userChoice = JOptionPane.showInputDialog("Which  dice do you  want to roll. (Enter 1, 2, 3, 4, 5, or 6)");   int dieNum = Integer.parseInt(userChoice);                         //test which die the user wanted to roll again,     //roll that specific die                       if (dieNum == 1)                               dice.roll(1);                       else                               if (dieNum == 2)                                       dice.roll(2);                               else                                       if (dieNum == 3)                                               dice.roll(3);                                         else                               if (dieNum == 4)                                       dice.roll(4);                               else                                       if (dieNum == 5)                                               dice.roll(5);                                         else                                             if(dieNum ==6)                                                 dice.roll(6)                       //display new dice numbers                       System.out.println("Rolling die " + dieNum +                                       " only: " + dice.toString());                                             //ask if user wants to roll again                       rollAgain = Boolean.parseBoolean(JOptionPane.showInputDialog                                       ("Roll again? (Enter true or false"));                                     }```
if there is anything you dont understand in this let me know
• 03-12-2009, 01:26 AM
kimmelim
just some code before I go to sleep, and off to a job interview tomorrow morning ; ) No its not a java programming job : P hehe.

Code:

```import java.util.Random; public class Yahtzee {         private int[] score;         private String[] labels;                                private int[] roll;                private int total;         private boolean[] hold;                        public void Yahtzee()         {                        labels = new String[8];                 roll = new int[5];                 hold = new boolean[5];                 for(int i=0;i<8;i++)                         score[i] = -1;                 labels[0] = "1s          ";                 labels[1] = "2s          ";                 labels[2] = "3s          ";                 labels[3] = "4s          ";                 labels[4] = "5s          ";                 labels[5] = "6s          ";                 labels[6] = "Bonus      ";                 labels[7] = "Total      ";                         }                                                                         public String getLabel(int i)         {                 String tempString = null;                 if(i>=0 && i<=6)                 {                         tempString = labels[i] + "    ";                         if(i<6) tempString += score[i];                         else tempString += total;                        }                                return tempString;         } }```
• 03-12-2009, 01:29 AM
anteup
okk good luck, but did that help by any chance
• 03-12-2009, 01:39 AM
kimmelim
It may help yes, but as you can see im new at java programming unfortually, and not as good as you ; ) All the help I can get helps, I learn new stuff every day ; )
• 03-12-2009, 01:56 AM
Fubarable
Quote:

Originally Posted by kimmelim
It may help yes, but as you can see im new at java programming unfortually, and not as good as you

Don't be quite so hasty in this judgement. ;)
• 03-12-2009, 02:23 AM
anteup
that's rite, im a beginner also I just started last month in my college class. A tip- before you go to sleep, try to think as much about java as you can and if you have a laptop, put it on your bed and place your hands on the keyboard. When you wake up your program will be written
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last