Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reddit.easy;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.Scanner;
- public class RPSLSObject {
- private static String[] confirmed = {"yes", "y", "sure", "of course"}, denied = {"no", "n", "hell no", "reddit... why..."};
- private static Scanner scan = new Scanner(System.in);
- public static int playerWin = 0, compWin = 0, noWin = 0;
- public final Choice spock = new Choice("Spock", "smashes Scissors", "vaporizes Rock");
- public final Choice lizard = new Choice("Lizard", "eats Paper", "poisons Spock");
- public final Choice rock = new Choice("Rock", "crushes Scissors", "crushes Lizard");
- public final Choice scissors = new Choice("Scissors", "cuts Paper", "decapitates Lizard");
- public final Choice paper = new Choice("Paper", "covers Rock", "disproves Spock");
- public Choice player, computer;
- static boolean AIOnly = false;
- public RPSLSObject(boolean AIOnly){
- if(!AIOnly){
- do{
- System.out.print("Player Choice: ");
- }while((player = Choice.fromString(scan.nextLine())) == null);
- System.out.println("Computer Choice: " + (computer = Choice.getRandom()).name);
- System.out.println("\nResult:\t" + player.eval(computer, false) + "\n");
- }else{
- RPSLSObject.AIOnly = true;
- System.out.println("CPU 1 Choice: " + (player = Choice.getRandom()).name);
- System.out.println("CPU 2 Choice: " + (computer = Choice.getRandom()).name);
- System.out.println("\nResult:\t" + player.eval(computer, false) + "\n");
- }
- }
- public RPSLSObject(){
- this(false);
- }
- public static void main(String[] args) throws InterruptedException{
- if(doCPU()){
- System.out.print("Iterations: ");
- int iter = Integer.parseInt(scan.nextLine());
- for(int i = 0; i< iter; i++){
- new RPSLSObject(true);
- }
- }else{
- new RPSLSObject();
- while(continue0()){
- new RPSLSObject();
- }
- }
- int total = playerWin + compWin+ noWin;
- System.out.println("Total:\t"+total);
- System.out.println(String.format("%s Win:\t%s/%s - %s%%", (AIOnly ? "CPU 1" : "Player"), playerWin, total, Math.round((float)playerWin/total*100)));
- System.out.println(String.format("%s Win:\t%s/%s - %s%%", (AIOnly ? "CPU 2": "A.I."), compWin, total, Math.round((float)compWin/total*100)));
- System.out.println(String.format("Tie Game :\t%s/%s - %s%%", noWin, total, Math.round((float)noWin/total*100)));
- scan.close();
- }
- public static boolean continue0(){
- while(true){
- System.out.print("Continue?: ");
- String in = scan.nextLine();
- if(Arrays.asList(confirmed).contains(in.toLowerCase())) return true;
- else if(Arrays.asList(denied).contains(in.toLowerCase())) return false;
- System.out.println("INVALID INPUT!\n");
- }
- }
- public static boolean doCPU(){
- while(true){
- System.out.print("CPU vs CPU?: ");
- String in = scan.nextLine();
- if(Arrays.asList(confirmed).contains(in)) return true;
- else if (Arrays.asList(denied).contains(in)) return false;
- System.out.println("INVALID!");
- }
- }
- }
- class Choice{
- public final String name;
- private final HashMap<String, String> winConditions;
- private static final ArrayList<Choice> choices = new ArrayList<Choice>();
- public Choice(String name, String... loss){
- this.name = name;
- winConditions = new HashMap<>();
- winConditions.put(loss[0].split(" ")[1], loss[0]);
- winConditions.put(loss[1].split(" ")[1], loss[1]);
- choices.add(this);
- }
- public String eval(Choice other, boolean tiebreaker){
- String temp;
- if(winConditions.containsKey(other.name)){
- if(!tiebreaker) RPSLSObject.playerWin++;
- temp = name + " " + winConditions.get(other.name);
- return temp;
- }else if(!tiebreaker && !(temp = other.eval(this, true)).contains("Tie")){
- if(!tiebreaker) RPSLSObject.compWin++;
- return temp;
- }
- if(!tiebreaker) RPSLSObject.noWin++;
- if(this.name != other.name)
- temp = String.format("Both %s and %s do nothing... Tie!", name, other.name);
- else
- temp = String.format("Double %s!... Tie!", name);
- return temp + "\n";
- }
- public static Choice fromString(String name){
- for(Choice c: choices)
- if(c.name.equalsIgnoreCase(name)) return c;
- return null;
- }
- public static Choice getRandom(){
- Random r = new Random();
- r.setSeed(System.currentTimeMillis() * r.nextLong());
- return choices.get(r.nextInt(5));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement