Advertisement
Guest User

Object Based Solution

a guest
Apr 21st, 2014
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. package reddit.easy;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8.  
  9. public class RPSLSObject {
  10.  
  11.     private static String[] confirmed = {"yes", "y", "sure", "of course"}, denied = {"no", "n", "hell no", "reddit... why..."};
  12.     private static Scanner scan = new Scanner(System.in);
  13.    
  14.     public static int playerWin = 0, compWin = 0, noWin = 0;
  15.     public final Choice spock = new Choice("Spock", "smashes Scissors", "vaporizes Rock");
  16.     public final Choice lizard = new Choice("Lizard", "eats Paper", "poisons Spock");
  17.     public final Choice rock = new Choice("Rock", "crushes Scissors", "crushes Lizard");
  18.     public final Choice scissors = new Choice("Scissors", "cuts Paper", "decapitates Lizard");
  19.     public final Choice paper = new Choice("Paper", "covers Rock", "disproves Spock");
  20.     public Choice player, computer;
  21.    
  22.     public RPSLSObject(){
  23.  
  24.         do{
  25.             System.out.print("Player Choice: ");
  26.         }while((player = Choice.fromString(scan.nextLine())) == null);
  27.        
  28.         System.out.println("Computer Choice: " + (computer = Choice.getRandom()).name);
  29.         System.out.println("\nResult:\t" + player.eval(computer, false) + "\n");
  30.        
  31.     }
  32.    
  33.     public static void main(String[] args){
  34.         new RPSLSObject();
  35.         while(continue0()){
  36.             new RPSLSObject();
  37.         }
  38.         int total = playerWin + compWin+ noWin;
  39.         System.out.println("Total:\t"+total);
  40.         System.out.println(String.format("Player Win:\t%s/%s - %s%%", playerWin, total, Math.round((float)playerWin/total*100)));
  41.         System.out.println(String.format("A.I. Win  :\t%s/%s - %s%%", compWin, total, Math.round((float)compWin/total*100)));
  42.         System.out.println(String.format("Tie Game  :\t%s/%s - %s%%", noWin, total, Math.round((float)noWin/total*100)));
  43.         scan.close();
  44.     }
  45.    
  46.     public static boolean continue0(){
  47.         while(true){
  48.             System.out.print("Continue?: ");
  49.             String in = scan.nextLine();
  50.             if(Arrays.asList(confirmed).contains(in.toLowerCase())) return true;
  51.             else if(Arrays.asList(denied).contains(in.toLowerCase())) return false;
  52.            
  53.             System.out.println("INVALID INPUT!\n");
  54.         }
  55.     }
  56.    
  57. }
  58.  
  59. class Choice{
  60.    
  61.     public final String name;
  62.    
  63.     private final HashMap<String, String> lossConditions;
  64.     private static final ArrayList<Choice> choices = new ArrayList<Choice>();
  65.    
  66.     public Choice(String name, String... loss){
  67.         this.name = name;
  68.         lossConditions = new HashMap<>();
  69.         lossConditions.put(loss[0].split(" ")[1], loss[0]);
  70.         lossConditions.put(loss[1].split(" ")[1], loss[1]);
  71.         choices.add(this);
  72.     }
  73.    
  74.     public String eval(Choice other, boolean tiebreaker){
  75.         String temp;
  76.        
  77.         if(lossConditions.containsKey(other.name)){
  78.             RPSLSObject.playerWin++;
  79.             temp = name + " " + lossConditions.get(other.name);
  80.             return temp;
  81.         }else if(!tiebreaker && !(temp = other.eval(this, true)).contains("Tie")){
  82.             RPSLSObject.compWin++;
  83.             return temp;
  84.         }
  85.         RPSLSObject.noWin++;
  86.        
  87.         if(this.name != other.name)
  88.             temp = String.format("Both %s and %s do nothing... Tie!", name, other.name);
  89.         else
  90.             temp = String.format("Double %s!... Tie!", name);
  91.        
  92.         return temp + "\n";
  93.        
  94.     }
  95.    
  96.     public static Choice fromString(String name){
  97.         for(Choice c: choices)
  98.             if(c.name.equalsIgnoreCase(name)) return c;
  99.        
  100.         return null;
  101.     }
  102.    
  103.     public static Choice getRandom(){
  104.         Random r = new Random();
  105.         r.setSeed(System.currentTimeMillis());
  106.        
  107.         return choices.get(r.nextInt(5));
  108.     }
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement