Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

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

    Would be very helpfull with some answers ; )


    Java 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());
                }
            }
        }
    }
    }

    Java 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("************************");
    }
    }
    Java 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;
     }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Use arrays, Luke, use arrays.

  3. #3
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default Something like this?

    Java 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!");
              
                    {
    
    }
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please describe the game rules a bit more.

  6. #6
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sorry, but my brain must be slow today. This means:
    Level Four = collect dices with 4 (gives 4 point pr correct dice)
    ?

    And also, I take it no points for straights?

  8. #8
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    That is correct, straight, full house and so on does not apply in this version ; )

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sorry, but I'm still unsure about the quote in my post above regarding:

    Level XXX = collect ......

  10. #10
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    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

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  12. #12
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    3 ones means 3 points (score) yes ; )

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i'm confused as well. so i did some digging, looks like a fun game.
    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
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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, ...
    Last edited by Fubarable; 03-12-2009 at 12:37 AM.

  15. #15
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    hmm...

    Java 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

  16. #16
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    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.

    Java 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;
    	}
    }

  17. #17
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    okk good luck, but did that help by any chance

  18. #18
    kimmelim is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

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

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kimmelim View Post
    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. ;)

  20. #20
    anteup is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    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

Page 1 of 2 12 LastLast

Similar Threads

  1. Proofreading this small Java program
    By almina in forum New To Java
    Replies: 5
    Last Post: 10-23-2009, 08:42 AM
  2. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 02:01 AM
  3. Please HELP Java small program
    By afrttoh in forum New To Java
    Replies: 14
    Last Post: 11-08-2008, 03:29 AM
  4. small error
    By ayoood in forum New To Java
    Replies: 23
    Last Post: 05-27-2008, 01:18 PM
  5. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 09:28 AM

Posting Permissions

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