Rock,Paper,Scissors Game:
import javax.swing.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class RockPaperScissors extends JFrame implements ActionListener{ JButton rockButton,paperButton,scissorsButton;
JLabel computerChoice;
JLabel computerScoreLabel,playerScoreLabel; RockPaperScissorsAfter rockPaperScissorsAfter; public RockPaperScissors() {
super(“Rock Paper Scissors”); setSize(450,574); setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); rockPaperScissorsAfter=new RockPaperScissorsAfter(); addGuiComponents();
}
![ONLEI Technologies project by Chandrika](https://onleitechnologies.com/wp-content/uploads/2024/05/WhatsApp-Image-2024-05-30-at-13.23.29-768x1024.jpeg)
public class RockPaperScissors extends JFrame implements ActionListener{ JButton rockButton,paperButton,scissorsButton;
JLabel computerChoice;
JLabel computerScoreLabel,playerScoreLabel; RockPaperScissorsAfter rockPaperScissorsAfter; public RockPaperScissors() {
super(“Rock Paper Scissors”); setSize(450,574); setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); rockPaperScissorsAfter=new RockPaperScissorsAfter(); addGuiComponents();
}
private void addGuiComponents(){ computerScoreLabel=new JLabel(“Computer:0”); computerScoreLabel.setBounds(0,43,450,30);
computerScoreLabel.setFont(new Font(“Dialog”,Font.BOLD,26)); computerScoreLabel.setHorizontalAlignment(SwingConstants.CENTER); add(computerScoreLabel);
computerChoice=new JLabel(“?”);
computerChoice.setBounds(175,118,98,81);
computerChoice.setFont(new Font(“Dialog”,Font.PLAIN,18)); computerChoice.setHorizontalAlignment(SwingConstants.CENTER);
computerChoice.setBorder(BorderFactory.createLineBorder(Color.BLACK)); add(computerChoice);
playerScoreLabel=new JLabel(“Player:0”); playerScoreLabel.setBounds(0,317,450,30); playerScoreLabel.setFont(new Font(“Dialog”,Font.BOLD,26)); playerScoreLabel.setHorizontalAlignment(SwingConstants.CENTER); add(playerScoreLabel);
rockButton=new JButton(“Rock”); rockButton.setBounds(40,387,105,81); rockButton.setFont(new Font(“Dialog”,Font.PLAIN,18)); rockButton.addActionListener(this);
add(rockButton);
paperButton=new JButton(“Paper”); paperButton.setBounds(165,387,105,81); paperButton.setFont(new Font(“Dialog”,Font.PLAIN,18)); paperButton.addActionListener(this);
add(paperButton);
scissorsButton=new JButton(“Scissors”);
scissorsButton.setBounds(290,387,105,81); scissorsButton.setFont(new Font(“Dialog”,Font.PLAIN,18)); scissorsButton.addActionListener(this); add(scissorsButton);
}
private void showDialog(String message){
JDialog resultDialog=new JDialog(this,”Result”,true); resultDialog.setSize(227,124); resultDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); resultDialog.setResizable(false);
JLabel resultLabel=new JLabel(message); resultLabel.setFont(new Font(“Dialog”,Font.BOLD,18)); resultLabel.setHorizontalAlignment(SwingConstants.CENTER); resultDialog.add(resultLabel,BorderLayout.CENTER);
JButton tryAgainButton=new JButton(“Try Again?”); tryAgainButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) { computerChoice.setText(“?”);
resultDialog.dispose();
}
});
resultDialog.add(tryAgainButton,BorderLayout.SOUTH); resultDialog.setLocationRelativeTo(this); resultDialog.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
String playerChoice= actionEvent.getActionCommand().toString();
String result=rockPaperScissorsAfter.playRockPaperScissor(playerChoice); computerChoice.setText(rockPaperScissorsAfter.getComputerChoice());
computerScoreLabel.setText(“Computer: “+rockPaperScissorsAfter.getComputerScore()); playerScoreLabel.setText(“Player: “+rockPaperScissorsAfter.getPlayerScore());
showDialog(result);
}
}
import javax.swing.*;
public class App {
public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
RockPaperScissors rockPaperScissors=new RockPaperScissors(); rockPaperScissors.setVisible(true);
}
});
}
}
import java.util.Random;
public class RockPaperScissorsAfter {
private static final String[] computerchoices={“Rock”,”Paper”,”Scissors”}; private String computerChoice;
private int computerScore,playerScore;
public String getComputerChoice() { return computerChoice;
}
public int getComputerScore() { return computerScore;
}
public int getPlayerScore() { return playerScore;
}
private Random random;
public RockPaperScissorsAfter(){ random=new Random();
}
public String playRockPaperScissor(String playerChoice){ computerChoice=computerchoices[random.nextInt(computerchoices.length)]; String result;
if(computerChoice.equals(“Rock”)){ if(playerChoice.equals(“Paper”)){
result=”Player Wins”; playerScore++;
} else if (playerChoice.equals(“Scissors”)) { result=”Computer Wins”; computerScore++;
}
else {
result=”Draw”;
}
} else if (computerChoice.equals(“Paper”)) {
if(playerChoice.equals(“Scissors”)){ result=”Player Wins”; playerScore++;
} else if (playerChoice.equals(“Rock”)) { result=”Computer Wins”; computerScore++;
}
else {
result=”Draw”;
}
}else {
if(playerChoice.equals(“Rock”)){ result=”Player Wins”; playerScore++;
} else if (playerChoice.equals(“Paper”)) { result=”Computer Wins”; computerScore++;
}
else {
result=”Draw”;
}
}
return result;
}
}
Output: