finshed making the game and also added the comments

This commit is contained in:
Cayden Lee Brackett 2024-11-16 22:50:05 -05:00
parent 60c3e32f6c
commit 1749566f90
2 changed files with 52 additions and 24 deletions

View File

@ -10,78 +10,106 @@ public class Screen {
static boolean rock = false; static boolean rock = false;
static boolean scissors = false; static boolean scissors = false;
static boolean paper = false; static boolean paper = false;
static int score = 0; //makes the variable and boolean
public static void main(String[] args) { public static void main(String[] args) {
JFrame frame = new JFrame("rock paper scissors"); JFrame frame = new JFrame("rock paper scissors");
frame.setSize(300,200); frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes the window and also makes it so when the window is closed the java stops
JButton rockbutton = new JButton("rock"); JButton rockbutton = new JButton("rock");
JButton paperbutton = new JButton("paper"); JButton paperbutton = new JButton("paper");
JButton scissorsbutton = new JButton("scissors"); JButton scissorsbutton = new JButton("scissors");
JPanel panel = new JPanel(); JPanel panel = new JPanel();
JLabel Label = new JLabel("play"); JLabel Label = new JLabel("play"); //makes the buttons, text, and panel
panel.add(rockbutton); panel.add(rockbutton);
panel.add(paperbutton); panel.add(paperbutton);
panel.add(scissorsbutton); panel.add(scissorsbutton);
panel.add(Label); panel.add(Label);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // adds everthing to the window and buts it in a form from top to bottom
rockbutton.setAlignmentX(JButton.CENTER_ALIGNMENT); rockbutton.setAlignmentX(JButton.CENTER_ALIGNMENT);
paperbutton.setAlignmentX(JButton.CENTER_ALIGNMENT); paperbutton.setAlignmentX(JButton.CENTER_ALIGNMENT);
scissorsbutton.setAlignmentX(JButton.CENTER_ALIGNMENT); scissorsbutton.setAlignmentX(JButton.CENTER_ALIGNMENT);
Label.setAlignmentX(JLabel.CENTER_ALIGNMENT); Label.setAlignmentX(JLabel.CENTER_ALIGNMENT); //sets everyting in the window to the center
rockbutton.addActionListener(new ActionListener(){ rockbutton.addActionListener(new ActionListener(){ //sees if you click the button and makes the corresponding boolean true
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
rock = true; rock = true;
if(rock == true) { if(rock == true) { //checks if the button is true and if it is then it forces the others to be false and you play your turn
scissors = false; scissors = false;
paper = false; paper = false;
Thecomputer.generate(args); Thecomputer.generate(args);
System.out.println("rock is true"); System.out.println("rock is true");
if(Thecomputer.aipaper == true){ if(Thecomputer.aipaper == true){
Label.setText("paper, you lose"); score += -1;
Label.setText("paper, you lose, score " + score);
}; };
if(Thecomputer.airock == true){ if(Thecomputer.airock == true){
Label.setText("rock, tie"); score += 1;
Label.setText("rock, tie, score " + score);
}; };
if(Thecomputer.aiscissors == true){ if(Thecomputer.aiscissors == true){
Label.setText("scissors, you win"); Label.setText("scissors, you win, score " + score);//adds or removes 1 from the score
}; };
}; };
} }
}); });
paperbutton.addActionListener(new ActionListener(){ paperbutton.addActionListener(new ActionListener(){ //sees if you click the button and makes the corresponding boolean true
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
paper = true; paper = true;
if(paper == true) { if(paper == true) { //checks if the button is true and if it is then it forces the others to be false and you play your turn
rock = false; rock = false;
scissors = false; scissors = false;
Thecomputer.generate(args);
System.out.println("paper is true"); System.out.println("paper is true");
if(Thecomputer.aipaper == true){
Label.setText("paper, you tie, score " + score);
};
if(Thecomputer.airock == true){
score += 1;
Label.setText("rock, you win, score " + score);
};
if(Thecomputer.aiscissors == true){
score += -1;
Label.setText("scissors, you lose, score " + score);//adds or removes 1 from the score
};
}; };
} }
}); });
scissorsbutton.addActionListener(new ActionListener(){ scissorsbutton.addActionListener(new ActionListener(){ //sees if you click the button and makes the corresponding boolean true
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
scissors = true; scissors = true;
if(scissors == true) { if(scissors == true) { //checks if the button is true and if it is then it forces the others to be false and you play your turn
paper = false; paper = false;
scissors = false; scissors = false;
Thecomputer.generate(args);
System.out.println("Scirrors is true"); System.out.println("Scirrors is true");
if(Thecomputer.aiscissors == true){
Label.setText("scissors, you tie, score " + score);
};
if(Thecomputer.airock == true){
score += -1;
Label.setText("rock, you lose, score " + score);
};
if(Thecomputer.aipaper == true){
score += 1;
Label.setText("paper, you win, score " + score); //adds or removes 1 from the score
}; };
} }
}); }});
frame.add(panel); frame.add(panel);
frame.setVisible(true); frame.setVisible(true);//make it so you can see window
} }
} }

View File

@ -4,11 +4,11 @@ import java.util.Random;
public class Thecomputer { public class Thecomputer {
static boolean airock = false; static boolean airock = false;
static boolean aipaper = false; static boolean aipaper = false;
static boolean aiscissors = false; static boolean aiscissors = false; //makes the boolean
public static void generate(String[] args) { public static void generate(String[] args) {
Random random = new Random(); Random random = new Random();
int randomnumber = random.nextInt(3); int randomnumber = random.nextInt(3); //makes a random number from 0-2
if(randomnumber == 1) { if(randomnumber == 1) { //if the number is 1 then the computer choose paper and forces the others to be false
aipaper = true; aipaper = true;
if(aipaper = true) { if(aipaper = true) {
airock = false; airock = false;
@ -16,7 +16,7 @@ public class Thecomputer {
System.out.println("aipaper is true"); System.out.println("aipaper is true");
} }
} }
if(randomnumber == 0) { if(randomnumber == 0) { //if the number is 0 then the computer choose scissors and forces the others to be false
aiscissors = true; aiscissors = true;
if(aiscissors = true) { if(aiscissors = true) {
airock = false; airock = false;
@ -24,7 +24,7 @@ public class Thecomputer {
System.out.println("aiscissors is true"); System.out.println("aiscissors is true");
} }
} }
if(randomnumber == 2) { if(randomnumber == 2) { //if the number is 2 then the computer choose rock and forces the others to be false
airock = true; airock = true;
if(airock = true) { if(airock = true) {
aiscissors = false; aiscissors = false;