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 4 of 4

Thread: Creating a GUI for a rock, paper, scissors game

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating a GUI for a rock, paper, scissors game

    So for my class we have to create a GUI for this class that just displays 3 buttons to choose from (rock, paper, or scissors) and then it has to display who wins ( Player win: Player's choice (ROCK) beats Computer's choice (SCISSORS)) and also displays PLAYER: 1; COMPUTER: 0; (Which is the score of the games)

    Please help!
    Thanks

    package homework08;

    import java.util.Random;

    public class RockPaperScissors
    {
    // The enumerated type with the gestures a person can use -- either rock,
    // paper, or scissors.
    public enum Gesture { ROCK, PAPER, SCISSORS }

    // Instance variables we'll use to track the score of the game.
    private int computerScore, playerScore;

    public String playGame( Gesture playerChoice )
    {
    // Randomly select a gesture. Rock, paper, or scissors is selected
    // uniformly at (pseudo-)random.
    Gesture computerChoice =
    Gesture.values( )[(new Random( )).nextInt( 3 ) ];

    // The string we'll use to represent the result and new score.
    String result;

    // Find out the result -- was it a tie?
    if( playerChoice == computerChoice )
    {
    result = "Draw: Player and Computer select " + playerChoice;
    }
    // If it wasn't a tie, one of the players must have won. Instead of
    // analyzing each case, we can note the "circular" way the winner is
    // determined -- position i+1 % 3 beats position i.
    else if( (playerChoice.ordinal() + 1) % 3 == computerChoice.ordinal() )
    {
    computerScore++;
    result = "Computer Win: Computer's choice (" + computerChoice
    + ") beats " + "Player's choice (" + playerChoice + ")";
    }
    // If neither case above is true, it must be that the player won.
    else
    {
    playerScore++;
    result = "Player Win: Player's choice (" + playerChoice + ") "
    + "beats Computer's " + "choice (" + computerChoice + ")";
    }

    // Add the score to the String and return it. Note we tell Java that
    // our answer is in HTML by using <HTML>. The <BR> is an HTML command
    // asking it to break the line, moving on to the next line.
    // HINT: a JLabel will make a new line for this ...
    result = "<HTML><CENTER>" + result + "<BR>PLAYER: " + playerScore
    + "; COMPUTER: " + computerScore + "</CENTER></HTML>";

    return result;
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Creating a GUI for a rock, paper, scissors game

    What's your actual question?

    There's a tutorial on exactly this on the Static Void Games website linked in my signature, but keep in mind that copying it directly is cheating (and your teacher WILL be able to tell). But going through it might help clear some concepts up for you.
    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!

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a GUI for a rock, paper, scissors game

    My question is how to write the code for the GUI, I have this so far, and I don't know where to go from here...

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class GUI extends RockPaperScissors
    {

    private JButton rock;
    private JButton paper;
    private JButton scissors;

    public GUI()
    {

    }
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Creating a GUI for a rock, paper, scissors game

    For questions like "how do I do this", the only real answer we can give you is to point you to google and the basic tutorials. Here they are: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    If you have a more specific question, I suggest posing it in the form of "I tried to do X, expected Y, but Z happened instead" and posting an SSCCE that demonstrates what you're doing. Don't forget the code tags.

    Good luck!
    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!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    nmg13 (December 11th, 2013)

Similar Threads

  1. Rock paper scissors program not displaying winner
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 03:38 AM
  2. Replies: 4
    Last Post: February 15th, 2013, 04:19 PM
  3. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 10:34 PM
  4. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 02:34 PM
  5. Rock Paper Scissors Spock Lizard Player Problem
    By flyingcurry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:53 AM

Tags for this Thread