Advertisement
Guest User

Hash Map Solution

a guest
Apr 21st, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. package reddit.easy;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class RPSLS {
  9.    
  10.     private HashMap<String, String> conditions = new HashMap<>();
  11.     private String choice, aiChoice;
  12.    
  13.     static private int playerWin = 0, aiWin = 0, noWin = 0;
  14.     static private String[] confirmed = {"yes", "y", "sure", "of course"}, denied = {"no", "n", "hell no", "reddit... why..."};
  15.     static Scanner in = new Scanner(System.in);
  16.    
  17.     {
  18.         conditions.put("Spock", "vaporizes Rock,smashes Scissors");
  19.         conditions.put("Scissors", "decapitates Lizard,cuts Paper");
  20.         conditions.put("Rock", "crushes Lizard,crushes Scissors");
  21.         conditions.put("Paper", "disproves Spock, covers Rock");
  22.         conditions.put("Lizard", "eats Paper,poisons Spock");
  23.     }
  24.    
  25.     public RPSLS(){
  26.         while(true){
  27.             System.out.print("Player Choice: ");
  28.             choice = in.nextLine();
  29.             if(conditions.containsKey(choice)) break;
  30.             System.out.println("CHOOSE A CORRECT CHOICE!\n");
  31.         }
  32.        
  33.         System.out.println("Computer Choice: " + (aiChoice = getRandomChoice()));
  34.         System.out.println(getResult(choice, aiChoice));
  35.     }
  36.    
  37.     private String getRandomChoice(){
  38.         Random r = new Random();
  39.         r.setSeed(System.currentTimeMillis());
  40.        
  41.         return (String) conditions.keySet().toArray()[r.nextInt(5)];
  42.     }
  43.    
  44.     private String getResult(String choice, String computer){
  45.         String str;
  46.         if((str = conditions.get(choice)).contains(computer)){
  47.             String[] pieces = str.split(",");
  48.             for(String s: pieces)
  49.                 if(s.contains(computer)){
  50.                     playerWin++;
  51.                     return choice + " " + s;
  52.                 }
  53.         }else{
  54.             str = conditions.get(computer);
  55.             String[] pieces = str.split(",");
  56.             for(String s: pieces)
  57.                 if(s.contains(choice)) {
  58.                     aiWin++;
  59.                     return computer + " " + s;
  60.                 }
  61.         }
  62.         noWin++;
  63.         return "Both " + choice + " and " + computer + " are just sitting there...\nTie!";
  64.     }
  65.    
  66.     public static void main(String[] args){
  67.         new RPSLS();
  68.         while(continuing()){
  69.             new RPSLS();
  70.         }
  71.         int total = playerWin + aiWin + noWin;
  72.         System.out.println("Total Games:\t" + total);
  73.         System.out.println(String.format("Player Wins:\t%s/%s - %s%%", playerWin, total, Math.round(((float) playerWin)/total * 100)));
  74.         System.out.println(String.format("A.I. Wins  :\t%s/%s - %s%%", aiWin, total, Math.round(((float) aiWin)/total * 100)));
  75.         System.out.println(String.format("Tie Games  :\t%s/%s - %s%%", noWin, total, Math.round(((float) noWin)/total * 100)));
  76.        
  77.         in.close();
  78.     }
  79.    
  80.     private static boolean continuing(){
  81.         while(true){
  82.             System.out.print("\nContinue?: ");
  83.             String in = RPSLS.in.nextLine().toLowerCase();
  84.             if(Arrays.asList(confirmed).contains(in)) return true;
  85.             else if(Arrays.asList(denied).contains(in)) return false;
  86.            
  87.             System.out.println("INVALID INPUT!\n");
  88.         }
  89.     }
  90.    
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement