RPS/Screen.java

100 lines
3.6 KiB
Java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Screen {
static boolean rock = false;
static boolean scissors = false;
static boolean paper = false;
static int score = 0; // Makes the variable and boolean
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Set OS theme
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Rock Paper Scissors");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton rockbutton = new JButton("Rock");
JButton paperbutton = new JButton("Paper");
JButton scissorsbutton = new JButton("Scissors");
JPanel panel = new JPanel();
JLabel label = new JLabel("Play");
panel.add(rockbutton);
panel.add(paperbutton);
panel.add(scissorsbutton);
panel.add(label);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
rockbutton.setAlignmentX(JButton.CENTER_ALIGNMENT);
paperbutton.setAlignmentX(JButton.CENTER_ALIGNMENT);
scissorsbutton.setAlignmentX(JButton.CENTER_ALIGNMENT);
label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
rockbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rock = true;
scissors = false;
paper = false;
Thecomputer.generate(args);
System.out.println("Rock is true");
if (Thecomputer.aipaper) {
score -= 1;
label.setText("Paper, you lose, score " + score);
} else if (Thecomputer.airock) {
label.setText("Rock, tie, score " + score);
} else if (Thecomputer.aiscissors) {
score += 1;
label.setText("Scissors, you win, score " + score);
}
}
});
paperbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
paper = true;
rock = false;
scissors = false;
Thecomputer.generate(args);
System.out.println("Paper is true");
if (Thecomputer.aipaper) {
label.setText("Paper, tie, score " + score);
} else if (Thecomputer.airock) {
score += 1;
label.setText("Rock, you win, score " + score);
} else if (Thecomputer.aiscissors) {
score -= 1;
label.setText("Scissors, you lose, score " + score);
}
}
});
scissorsbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scissors = true;
paper = false;
rock = false;
Thecomputer.generate(args);
System.out.println("Scissors is true");
if (Thecomputer.aiscissors) {
label.setText("Scissors, tie, score " + score);
} else if (Thecomputer.airock) {
score -= 1;
label.setText("Rock, you lose, score " + score);
} else if (Thecomputer.aipaper) {
score += 1;
label.setText("Paper, you win, score " + score);
}
}
});
frame.add(panel);
frame.setVisible(true);
}
}