Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Yahtzee code help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Yahtzee code help

    I have to create a code so we can play the game Yahtzee.
    Here's the rules if you don't know them: Yahtzee - Wikipedia, the free encyclopedia

    I have the basic layout already programmed with the dice rolling random numbers.
    The user of the program needs to be able to "hold" numbers they don't want to get rid of. The user is rolling 5 dice and they need to be able to "hold" a die if they don't want a new number for that particular die. For example, if they roll three 5s, they should be able to click on each die and it shouldn't change, but they can roll the other two dice.
    Then after they decide what they want to keep, I need a some kind of score card that decides if what they have is 3 of a kind, 4 of a kind, Yahtzee, a straight, etc.

    How would I hold the numbers they want and how do I create a score card that determines if they have certain things (straight, Yahtzee, 3 of a kind...)

    If you can point me in the direction of any online sites that would help me, please do! Thanks!



    I don't know if it will help but here's my code so far. Everything works so I'm not looking for errors!

    package dice;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Yahtzee extends JFrame implements ActionListener
    {
        //danny the dice
        D6 danny;
        D6 danny2;
        D6 danny3;
        D6 danny4;
        D6 danny5;
     
        JPanel leftSide; //will have control buttons on left
        JPanel rightSide; //will have the dice on the right (D6s)
        JButton rollAll; //roll all the dice
     
     
    public static void main (String [] args)
        {
        Yahtzee game = new Yahtzee();
        }
    public Yahtzee()
        {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout() );
     
        //adding the panel
        leftSide = new JPanel();
        //set background color to light blue
        leftSide.setBackground(new Color(200,200, 250));
        //saying what side the blue will be on
        add(leftSide,BorderLayout.WEST);
     
        //setting things up on the right side
        rightSide = new JPanel();
        rightSide.setBackground(Color.PINK);
        add(rightSide,BorderLayout.EAST);
     
        //the five dice for rolling
        danny = new D6();
        rightSide.add (danny);
     
        danny2 = new D6();
        rightSide.add (danny2);
     
        danny3 = new D6();
        rightSide.add (danny3);
     
        danny4 = new D6();
        rightSide.add (danny4);
     
        danny5 = new D6();
        rightSide.add (danny5);
     
        //rolls all the dice at once
        rollAll = new JButton("Roll All");
        leftSide.add(rollAll);
        rollAll.addActionListener(this);
     
        setSize (800,500);
        setVisible(true);
        } //end constructor
     
    public void actionPerformed(ActionEvent e)
        {
        //will roll all of the dice so they all work
        danny.roll();
        danny2.roll();
        danny3.roll();
        danny4.roll();
        danny5.roll();
        repaint();
     
        }
     
    }
     
     
    Heres program D6
     
    package dice;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
     
    public class D6 extends JPanel implements ActionListener
    {
        int face; //the number of dots when it comes to rest
        JButton rollMe; //press this button to roll this D6
     
        //constructor
        public D6()
        {
            roll();
     
            rollMe = new JButton("Roll!");
            add (rollMe);
            rollMe.addActionListener(this);
     
            setBackground(Color.yellow);
            setPreferredSize ( new Dimension(100,100));
            setVisible(true);
        }
     
        //randomize the value of face
        public void roll()
        {
            face = (int)(Math.random()*6+1);
        }
     
        //This
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource()==rollMe){roll();}
            repaint();
        }
        //draws face number of dots
        @Override
        public void paint (Graphics g )
        {
            super.paint(g);
            g.drawString(""+face,50,50);
     
        }
     
     
    }
    Last edited by KevinWorkman; February 17th, 2011 at 09:45 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,342
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Yahtzee code help

    When posting code, make sure you use the code or highlight tags. I've added them for you this time.

    But you'd be better off not posting so much code- instead, post a specific question and an SSCCE (that's a link) that goes along with it.

    Your question is pretty broad. If you're having trouble coming up with an algorithm, think about how you do this by hand without any code. How do you do this in real life? Pretend you have a friend who has no idea how to play yahtzee, or how to keep score of a game. Write out directions you could give to a person who knows nothing that would help them accomplish your goal. When you have that written out, you'll have an algorithm that should be pretty easy to translate to code.

    Also, I know that this question has been asked in these forums before (what's with all of the yahtzee assignments? Are you all in the same class?), so I'd recommend doing a search.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!