Connor Greene
Connor Greene

Reputation: 27

Advice for creating a rock, paper, scissors game in java swing

I'm teaching myself Java and I want to create a rock, paper, scissors game with a GUI. I have created a text based version with scanners, but I have a lot of work to go.

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class RPS extends JFrame {

  JRadioButton rock, paper, scissors;
  ButtonGroup choices;

  public static void main(String[] args) {
    new RPS();
  }
  public RPS() {
    super("Rock, Paper, Scissors");

    setSize(400,300);
    setResizable(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel p=new JPanel();

    rock = new JRadioButton("Rock");
    paper = new JRadioButton("Paper");
    scissors = new JRadioButton("Scissors");

    choices = new ButtonGroup();
    choices.add(rock);
    choices.add(paper);
    choices.add(scissors);

    p.add(rock);
    p.add(paper);
    p.add(scissors);

    add(p);

    setVisible(true);
  }
}

Here is my code. I already have it creating a window and displaying 3 radio buttons that will only allow one choice to be selected. From here, I want to implement a next button, and create logic to produce an answer based on both choices. I believe I need a card layout, but the Oracle documentation doesn't help me. I also have no idea how I can go around implementing logic. Any help is appreciated, sorry for the long post. Thanks again!

I'm sorry I didn't make it clear, I want to design this for one person to take a turn, press the nest button, then the second person will take a turn, press finish and get the results. I will present this to my 8th grade class.

Upvotes: 0

Views: 1779

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209052

Here's hot to implement CardLayout into your program. The purpose of this layout is to layer components. In your case, you would need a panel for each player. So you would need two panels.

  1. JPanel for player 1 (with own three radio buttons)
  2. JPanel for player 2 (with own three radio buttons)

The above two are what comprise the Card(layered)Layout (you would stack one on top of the other.

  1. You need a label to show the winner
  2. You need a JPanel to hold the Next Button

So all together, you layout should look like this

  Wrapped in JPanel(new BoderLayout())
-------------------------------------
|       label to show status        |   BorderLayout.NORTH
-------------------------------------
|                                   |
|        CardLayout holding         |
|        two JPanels with RBs       |   BorderLayout.CENTER
|___________________________________|
|(JPanel)   Next JButton            |   BorderLayout.SOUTH
-------------------------------------

When you click the next button, you can call the next() method of the CardLayout to show the next panel

Example

private CardLayout cardLayout = new CardLayout(10, 10); // hgap and vgap args
private JPanel cardPanel = new JPanel(cardLayout);

JPanel panel1 = new JPanel(); // holds first player
JPanel panel2 = new JPanel(); // holds second player

cardPanel.add(panel1);
cardPanel.add(panel2);

nextButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        cardLayout.next(cardPanel);
    }
});

See CardLayout docs for other movement methods

For the logic part

  1. As I suggested before, You should have six radio buttons (three for each player)
  2. In your logic you can check which ones are seleted
  3. And you may want checkWinner JButton to perform the action.

Example

 JRadioButton p1Scissors = new JRadioButton("Scissors");
 JRadioButton p1Paper = new JRadioButton("Paper");
 JRadioButton p1Rock = new JRadioButton("Rock");
 // group them

 JRadioButton p2Scissors = new JRadioButton("Scissors");
 JRadioButton p2Paper = new JRadioButton("Paper");
 JRadioButton p2Rock = new JRadioButton("Rock");
 // group them

 JLabel statusLabel = new JLabel(" ");

 JButton checkWinner = new JButton("Check Winner"); // You can add to bottom panel

 checkWinner.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
         if (p1Scissors.isSelected() && p2Rock.isSelected()){
             statusLabel.setText("Player 2 wins: Rock beats Scissors");
         } else if ( ..... ) {
             ...
         }

         ...
     }
 });

Upvotes: 2

Related Questions