Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package com.rps.logic;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Random;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. /**
  10.  * @author _Jordan <jordan.abraham1997@gmail.com>
  11.  */
  12. public class RPSActionListener implements ActionListener {
  13.  
  14.     /**
  15.      * @author _Jordan <jordan.abraham1997@gmail.com>
  16.      */
  17.     public enum Action {
  18.         ROCK("paper"), PAPER("scissors"), SCISSORS("rock");
  19.  
  20.         /**
  21.          * Represents the weakness to each action.
  22.          */
  23.         private final String weakness;
  24.  
  25.         /**
  26.          * Constructs a new {@code Action} object.
  27.          *
  28.          * @param weakness The weakness to each action.
  29.          */
  30.         private Action(String weakness) {
  31.             this.weakness = weakness;
  32.         }
  33.  
  34.         /**
  35.          * Gets the weakness.
  36.          *
  37.          * @return the weakness
  38.          */
  39.         public String getWeakness() {
  40.             return weakness;
  41.         }
  42.     }
  43.  
  44.     /**
  45.      * Represents a random instance to perform 'computer' actions.
  46.      */
  47.     private final Random random = new Random();
  48.  
  49.     /*
  50.      * (non-Javadoc)
  51.      *
  52.      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  53.      */
  54.     @Override
  55.     public void actionPerformed(ActionEvent event) {
  56.         /** TRIM IT JUST INCASE lol */
  57.         switch (event.getActionCommand().trim()) {
  58.         case "Rock":
  59.         case "Paper":
  60.         case "Scissors":
  61.             performAction(Action.valueOf(event.getActionCommand().toUpperCase()), getRandomComputerAction(random.nextInt(3)));
  62.             break;
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Performs a game action.
  68.      *
  69.      * @param playerAction The action of the player.
  70.      * @param computerAction The action of the computer.
  71.      */
  72.     private void performAction(Action playerAction, Action computerAction) {
  73.         if (playerAction.equals(computerAction)) {
  74.             performAction(playerAction, getRandomComputerAction(random.nextInt(3)));
  75.             return;
  76.         }
  77.         if (playerAction.getWeakness().equalsIgnoreCase(computerAction.name())) {
  78.             JOptionPane.showMessageDialog(null, "Player: " + playerAction.name() + ", Computer: " + computerAction.name() + " = LOSE!", "LOSE!", JOptionPane.PLAIN_MESSAGE);
  79.         } else {
  80.             JOptionPane.showMessageDialog(null, "Player: " + playerAction.name() + ", Computer: " + computerAction.name() + " = WIN!", "WIN!", JOptionPane.PLAIN_MESSAGE);
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Generates a random computer action.
  86.      *
  87.      * @param id The action id.
  88.      * @return The action generated.
  89.      */
  90.     private Action getRandomComputerAction(int id) {
  91.         for (Action i : Action.values()) {
  92.             if (i.ordinal() == id) {
  93.                 return i;
  94.             }
  95.         }
  96.         return null;
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement