Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.Component;
- import java.awt.event.*;
- import java.util.Random;
- public class RockPaperScissorsTest extends GUIFrame implements ActionListener
- {
- int rounds = 0;
- int count = 0;
- int playerChoice, setChoice;
- Panel cardPanel;
- Panel roundsPanel;
- Button next;
- Label l1, l2, l3;
- List list1;
- CardLayout cardLayout;
- boolean showRounds = true;
- boolean winner = false;
- int playerScore = 0;
- int computerScore = 0;
- Font lFont = new Font("Arial", Font.BOLD, 20);
- Font AFont = new Font("Arial", Font.BOLD, 15);
- public RockPaperScissorsTest()
- {
- super("Rock Paper Scissors!");
- cardLayout = new CardLayout();
- cardPanel = new Panel();
- cardPanel.setLayout(cardLayout);
- cardPanel.setBackground(Color.black);
- cardPanel.setForeground(Color.red);
- l1 = new Label("Choose the number of rounds", Label.CENTER);
- l1.setFont(lFont);
- l2 = new Label("Choose!", Label.CENTER);
- l2.setFont(lFont);
- cardPanel.add("Determine rounds", l1);
- cardPanel.add("Choosing", l2);
- add(cardPanel, BorderLayout.CENTER);
- roundsPanel = new Panel();
- list1 = new List(5, false);
- list1.add("One");
- list1.add("Two");
- list1.add("Three");
- list1.add("Four");
- list1.add("Five");
- list1.addActionListener(this);
- roundsPanel.add(list1);
- next = new Button("Next");
- next.addActionListener(this);
- roundsPanel.add(next);
- add(roundsPanel, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- public static void main(String args[])
- {
- RockPaperScissorsTest rpst = new RockPaperScissorsTest();
- }
- public void actionPerformed(ActionEvent e)
- {
- if(count == 0)
- {
- if(e.getSource() == next)
- {
- rounds = list1.getSelectedIndex() + 1;
- list1.removeAll();
- list1.add("Rock");
- list1.add("Paper");
- list1.add("Scissors");
- cardLayout.next(cardPanel);
- count++;
- }
- }
- else
- {
- while(winner = false)
- {
- Random generator = new Random();
- int computerChoice = (int) generator.nextInt(3);
- if(e.getSource() == next)
- {
- playerChoice = list1.getSelectedIndex();
- if(playerChoice == 0)
- {
- if(computerChoice == 0)
- {
- l3 = new Label("You chose rock | Computer chose rock | Draw", Label.CENTER);
- l3.setFont(AFont);
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- if(computerChoice == 1)
- {
- l3 = new Label("You chose rock | Computer chose paper | You lose the round", Label.CENTER);
- l3.setFont(AFont);
- computerScore += 1;
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- if(computerChoice == 2)
- {
- l3 = new Label("You chose rock | Computer chose scissors | You win the round", Label.CENTER);
- l3.setFont(AFont);
- playerScore += 1;
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- }
- if(playerChoice == 1)
- {
- if(computerChoice == 0)
- {
- l3 = new Label("You chose paper | Computer chose rock | You win the round", Label.CENTER);
- l3.setFont(AFont);
- playerScore += 1;
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- if(computerChoice == 1)
- {
- l3 = new Label("You chose paper | Computer chose paper | Draw", Label.CENTER);
- l3.setFont(AFont);
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- if(computerChoice == 2)
- {
- l3 = new Label("You chose paper | Computer chose scissors | You lose the round", Label.CENTER);
- l3.setFont(AFont);
- computerScore += 1;
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- }
- if(playerChoice == 2)
- {
- if(computerChoice == 0)
- {
- l3 = new Label("You chose scissors | Computer chose rock | You lose the round", Label.CENTER);
- l3.setFont(AFont);
- computerScore += 1;
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- if(computerChoice == 1)
- {
- l3 = new Label("You chose scissors | Computer chose paper | You win the round", Label.CENTER);
- l3.setFont(AFont);
- playerScore += 1;
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- if(computerChoice == 2)
- {
- l3 = new Label("You chose scissors | Computer chose scissors | Draw", Label.CENTER);
- l3.setFont(AFont);
- cardPanel.add("Verdict", l3);
- cardLayout.next(cardPanel);
- }
- }
- }
- if(playerScore == (rounds / 2 + 1))
- {
- winner = true;
- l3 = new Label("You win the match", Label.CENTER);
- l3.setFont(AFont);
- }
- if(computerScore == (rounds / 2 + 1))
- {
- winner = true;
- l3 = new Label("You lose the match", Label.CENTER);
- l3.setFont(AFont);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement