Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to make this GUI program play rock, paper, scissors but am having trouble writing the program to make it do so. Can

I am trying to make this GUI program play rock, paper, scissors but am having trouble writing the program to make it do so. Can someone help fix the current program so it can run and play properly? I need it to have a user enter R,P, or S, then press a button "Play!" then have it play against the computer's random play. Finally, it needs to print the result whether the user won, lost or tied. Thank you

import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.Dimension; import java.util.Scanner; import java.util.Random; import javax.swing.SwingConstants; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.Font; import javax.swing.JPanel; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color;

public class Rps extends JFrame { private JTextField txtGuess; private JTextField txtPlay; // text field for the user's guess private JLabel lblOutput; private int theNumber; public void checkGuess() { // method/function to check too high or too low txtPlay.getText(); String computerPlay; int personPlay; int computerInt; String guessText = txtGuess.getText(); String message = ""; Scanner scan = new Scanner(System.in); Random generator = new Random(); computerInt = generator.nextInt(3); switch (computerInt) { case 0: computerPlay = "R"; break; case 1: computerPlay = "P"; break; case 2: computerPlay = "S"; break; default: computerPlay = "no value"; } } public void checkGuess1() { // method/function to check too high or too low // get the user's guess String guessText = txtGuess.getText(); String message = ""; try { message = "Play again?"; switch (personPlay){ case "R": switch (theNumber) { case "R": lblOutput.setText("Tied. " + message); break; case "P": lblOutput.setText("You win. " + message); break; case "S": lblOutput.setText("You lose. " + message); System.out.println(message); break; } break; case "P": switch (theNumber) { case "R": lblOutput.setText("You win. " + message); break; case "P": lblOutput.setText("Tied. " + message); break; case "S": lblOutput.setText("You lose. " + message); System.out.println(message); break; } break; case "S": switch (theNumber) { case "R": lblOutput.setText("Tied. " + message); break; case "P": lblOutput.setText("You win. " + message); break; case "S": lblOutput.setText("You lose. " + message); System.out.println(message); break; } }

public Rps() { setTitle("Rock, Paper, Scissors Game"); getContentPane().setBackground(new Color(51, 204, 255)); setBackground(new Color(51, 204, 255)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBackground(new Color(51, 204, 255)); panel.setBounds(45, 95, 361, 29); getContentPane().add(panel); panel.setLayout(null); JLabel lblPlay = new JLabel("Enter your play: (R, P, or S)"); lblPlay.setBounds(6, 6, 263, 16); panel.add(lblPlay); lblPlay.setHorizontalAlignment(SwingConstants.RIGHT); JLabel lblRps = new JLabel("Rock, Paper, Scissors Game"); lblRps.setFont(new Font("Lucida Grande", Font.BOLD, 16)); lblRps.setBounds(0, 33, 450, 29); lblRps.setVerticalAlignment(SwingConstants.TOP); lblRps.setHorizontalAlignment(SwingConstants.CENTER); getContentPane().add(lblRps);

txtPlay = new JTextField(); txtPlay.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { checkGuess(); } }); txtPlay.setHorizontalAlignment(SwingConstants.CENTER); txtPlay.setBounds(270, 1, 65, 26); panel.add(txtPlay); txtPlay.setColumns(10); JButton btnPlay = new JButton("Play!"); btnPlay.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { checkGuess(); } }); btnPlay.setBounds(167, 157, 117, 29); getContentPane().add(btnPlay); }

public static void main(String[] args) { Rps theGame = new Rps(); theGame.newGame(); theGame.setSize(new Dimension(430, 330)); theGame.setVisible(true);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions