Advertisement
Guest User

CPU Enabled

a guest
Apr 21st, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.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.     static boolean AIOnly = false;
  23.    
  24.     public RPSLSObject(boolean AIOnly){
  25.  
  26.         if(!AIOnly){
  27.             do{
  28.                 System.out.print("Player Choice: ");
  29.             }while((player = Choice.fromString(scan.nextLine())) == null);
  30.        
  31.             System.out.println("Computer Choice: " + (computer = Choice.getRandom()).name);
  32.             System.out.println("\nResult:\t" + player.eval(computer, false) + "\n");
  33.         }else{
  34.             RPSLSObject.AIOnly = true;
  35.             System.out.println("CPU 1 Choice: " + (player = Choice.getRandom()).name);
  36.             System.out.println("CPU 2 Choice: " + (computer = Choice.getRandom()).name);
  37.             System.out.println("\nResult:\t" + player.eval(computer, false) + "\n");
  38.         }
  39.        
  40.     }
  41.    
  42.     public RPSLSObject(){
  43.         this(false);
  44.     }
  45.    
  46.     public static void main(String[] args) throws InterruptedException{
  47.         if(doCPU()){
  48.             System.out.print("Iterations: ");
  49.             int iter = Integer.parseInt(scan.nextLine());
  50.             for(int i = 0; i< iter; i++){
  51.                 new RPSLSObject(true);
  52.             }
  53.         }else{
  54.             new RPSLSObject();
  55.             while(continue0()){
  56.                 new RPSLSObject();
  57.             }
  58.         }
  59.         int total = playerWin + compWin+ noWin;
  60.         System.out.println("Total:\t"+total);
  61.         System.out.println(String.format("%s Win:\t%s/%s - %s%%", (AIOnly ? "CPU 1" : "Player"), playerWin, total, Math.round((float)playerWin/total*100)));
  62.         System.out.println(String.format("%s Win:\t%s/%s - %s%%", (AIOnly ? "CPU 2": "A.I."), compWin, total, Math.round((float)compWin/total*100)));
  63.         System.out.println(String.format("Tie Game  :\t%s/%s - %s%%", noWin, total, Math.round((float)noWin/total*100)));
  64.         scan.close();
  65.     }
  66.    
  67.     public static boolean continue0(){
  68.         while(true){
  69.             System.out.print("Continue?: ");
  70.             String in = scan.nextLine();
  71.             if(Arrays.asList(confirmed).contains(in.toLowerCase())) return true;
  72.             else if(Arrays.asList(denied).contains(in.toLowerCase())) return false;
  73.            
  74.             System.out.println("INVALID INPUT!\n");
  75.         }
  76.     }
  77.    
  78.     public static boolean doCPU(){
  79.         while(true){
  80.             System.out.print("CPU vs CPU?: ");
  81.             String in = scan.nextLine();
  82.             if(Arrays.asList(confirmed).contains(in)) return true;
  83.             else if (Arrays.asList(denied).contains(in)) return false;
  84.            
  85.             System.out.println("INVALID!");
  86.         }
  87.     }
  88.    
  89. }
  90.  
  91. class Choice{
  92.    
  93.     public final String name;
  94.    
  95.     private final HashMap<String, String> winConditions;
  96.     private static final ArrayList<Choice> choices = new ArrayList<Choice>();
  97.    
  98.     public Choice(String name, String... loss){
  99.         this.name = name;
  100.         winConditions = new HashMap<>();
  101.         winConditions.put(loss[0].split(" ")[1], loss[0]);
  102.         winConditions.put(loss[1].split(" ")[1], loss[1]);
  103.         choices.add(this);
  104.     }
  105.    
  106.     public String eval(Choice other, boolean tiebreaker){
  107.         String temp;
  108.        
  109.         if(winConditions.containsKey(other.name)){
  110.             if(!tiebreaker) RPSLSObject.playerWin++;
  111.             temp = name + " " + winConditions.get(other.name);
  112.             return temp;
  113.         }else if(!tiebreaker && !(temp = other.eval(this, true)).contains("Tie")){
  114.             if(!tiebreaker) RPSLSObject.compWin++;
  115.             return temp;
  116.         }
  117.         if(!tiebreaker) RPSLSObject.noWin++;
  118.        
  119.         if(this.name != other.name)
  120.             temp = String.format("Both %s and %s do nothing... Tie!", name, other.name);
  121.         else
  122.             temp = String.format("Double %s!... Tie!", name);
  123.        
  124.         return temp + "\n";
  125.        
  126.     }
  127.    
  128.     public static Choice fromString(String name){
  129.         for(Choice c: choices)
  130.             if(c.name.equalsIgnoreCase(name)) return c;
  131.        
  132.         return null;
  133.     }
  134.    
  135.     public static Choice getRandom(){
  136.         Random r = new Random();
  137.         r.setSeed(System.currentTimeMillis() * r.nextLong());
  138.        
  139.         return choices.get(r.nextInt(5));
  140.     }
  141.    
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement