Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.11 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.Component;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. public class RockPaperScissorsTest extends GUIFrame implements ActionListener
  7. {
  8.  
  9.   int rounds = 0;
  10.   int count = 0;
  11.   int playerChoice, setChoice;
  12.   Panel cardPanel;
  13.   Panel roundsPanel;
  14.   Button next;
  15.   Label l1, l2, l3;
  16.   List list1;
  17.   CardLayout cardLayout;
  18.   boolean showRounds = true;
  19.   boolean winner = false;
  20.   int playerScore = 0;
  21.   int computerScore = 0;
  22.   Font lFont = new Font("Arial", Font.BOLD, 20);
  23.   Font AFont = new Font("Arial", Font.BOLD, 15);
  24.  
  25.   public RockPaperScissorsTest()
  26.    {
  27.     super("Rock Paper Scissors!");
  28.     cardLayout = new CardLayout();
  29.     cardPanel = new Panel();
  30.     cardPanel.setLayout(cardLayout);
  31.     cardPanel.setBackground(Color.black);
  32.     cardPanel.setForeground(Color.red);
  33.  
  34.     l1 = new Label("Choose the number of rounds", Label.CENTER);
  35.     l1.setFont(lFont);
  36.     l2 = new Label("Choose!", Label.CENTER);
  37.     l2.setFont(lFont);
  38.  
  39.     cardPanel.add("Determine rounds", l1);
  40.     cardPanel.add("Choosing", l2);
  41.     add(cardPanel, BorderLayout.CENTER);
  42.  
  43.          roundsPanel = new Panel();
  44.          list1 = new List(5, false);
  45.          list1.add("One");
  46.          list1.add("Two");
  47.          list1.add("Three");
  48.          list1.add("Four");
  49.          list1.add("Five");
  50.          list1.addActionListener(this);
  51.          roundsPanel.add(list1);
  52.          next = new Button("Next");
  53.          next.addActionListener(this);
  54.          roundsPanel.add(next);
  55.          add(roundsPanel, BorderLayout.SOUTH);
  56.  
  57.     pack();
  58.     setVisible(true);
  59.   }
  60.  
  61.   public static void main(String args[])
  62.   {
  63.     RockPaperScissorsTest rpst = new RockPaperScissorsTest();
  64.   }
  65.  
  66.   public void actionPerformed(ActionEvent e)
  67.   {
  68.  
  69.     if(count == 0)
  70.     {
  71.         if(e.getSource() == next)
  72.         {
  73.             rounds = list1.getSelectedIndex() + 1;
  74.              list1.removeAll();
  75.              list1.add("Rock");
  76.              list1.add("Paper");
  77.              list1.add("Scissors");
  78.              cardLayout.next(cardPanel);
  79.              count++;
  80.         }
  81.     }
  82.     else
  83.     {
  84.         while(winner = false)
  85.         {
  86.             Random generator = new Random();
  87.             int computerChoice = (int) generator.nextInt(3);
  88.  
  89.             if(e.getSource() == next)
  90.             {
  91.               playerChoice = list1.getSelectedIndex();
  92.  
  93.               if(playerChoice == 0)
  94.               {
  95.                 if(computerChoice == 0)
  96.                 {
  97.                     l3 = new Label("You chose rock | Computer chose rock | Draw", Label.CENTER);
  98.                     l3.setFont(AFont);
  99.                                 cardPanel.add("Verdict", l3);
  100.             cardLayout.next(cardPanel);
  101.                 }
  102.                 if(computerChoice == 1)
  103.                 {
  104.                     l3 = new Label("You chose rock | Computer chose paper | You lose the round", Label.CENTER);
  105.                     l3.setFont(AFont);
  106.                     computerScore += 1;
  107.                                 cardPanel.add("Verdict", l3);
  108.             cardLayout.next(cardPanel);
  109.                 }
  110.                 if(computerChoice == 2)
  111.                 {
  112.                     l3 = new Label("You chose rock | Computer chose scissors | You win the round", Label.CENTER);
  113.                     l3.setFont(AFont);
  114.                     playerScore += 1;
  115.                                 cardPanel.add("Verdict", l3);
  116.             cardLayout.next(cardPanel);
  117.                 }
  118.              }
  119.              if(playerChoice == 1)
  120.              {
  121.                 if(computerChoice == 0)
  122.                 {
  123.                     l3 = new Label("You chose paper | Computer chose rock | You win the round", Label.CENTER);
  124.                     l3.setFont(AFont);
  125.                     playerScore += 1;
  126.                                 cardPanel.add("Verdict", l3);
  127.             cardLayout.next(cardPanel);
  128.                 }
  129.                 if(computerChoice == 1)
  130.                 {
  131.                     l3 = new Label("You chose paper | Computer chose paper | Draw", Label.CENTER);
  132.                     l3.setFont(AFont);
  133.                                 cardPanel.add("Verdict", l3);
  134.             cardLayout.next(cardPanel);
  135.                 }
  136.                 if(computerChoice == 2)
  137.                 {
  138.                     l3 = new Label("You chose paper | Computer chose scissors | You lose the round", Label.CENTER);
  139.                     l3.setFont(AFont);
  140.                     computerScore += 1;
  141.                                 cardPanel.add("Verdict", l3);
  142.             cardLayout.next(cardPanel);
  143.                 }
  144.               }
  145.              if(playerChoice == 2)
  146.              {
  147.                 if(computerChoice == 0)
  148.                 {
  149.                     l3 = new Label("You chose scissors | Computer chose rock | You lose the round", Label.CENTER);
  150.                     l3.setFont(AFont);
  151.                     computerScore += 1;
  152.                                 cardPanel.add("Verdict", l3);
  153.             cardLayout.next(cardPanel);
  154.                 }
  155.                 if(computerChoice == 1)
  156.                 {
  157.                     l3 = new Label("You chose scissors | Computer chose paper | You win the round", Label.CENTER);
  158.                     l3.setFont(AFont);
  159.                     playerScore += 1;
  160.                                 cardPanel.add("Verdict", l3);
  161.             cardLayout.next(cardPanel);
  162.                 }
  163.                 if(computerChoice == 2)
  164.                 {
  165.                     l3 = new Label("You chose scissors | Computer chose scissors | Draw", Label.CENTER);
  166.                     l3.setFont(AFont);
  167.                                 cardPanel.add("Verdict", l3);
  168.             cardLayout.next(cardPanel);
  169.                 }
  170.               }
  171.         }
  172.         if(playerScore == (rounds / 2 + 1))
  173.         {
  174.             winner = true;
  175.         l3 = new Label("You win the match", Label.CENTER);
  176.             l3.setFont(AFont);
  177.         }
  178.         if(computerScore == (rounds / 2 + 1))
  179.         {
  180.             winner = true;
  181.             l3 = new Label("You lose the match", Label.CENTER);
  182.             l3.setFont(AFont);
  183.         }
  184.         }
  185.     }
  186.   }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement