Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reddit.easy;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.Scanner;
- public class RPSLS {
- private HashMap<String, String> conditions = new HashMap<>();
- private String choice, aiChoice;
- static private int playerWin = 0, aiWin = 0, noWin = 0;
- static private String[] confirmed = {"yes", "y", "sure", "of course"}, denied = {"no", "n", "hell no", "reddit... why..."};
- static Scanner in = new Scanner(System.in);
- {
- conditions.put("Spock", "vaporizes Rock,smashes Scissors");
- conditions.put("Scissors", "decapitates Lizard,cuts Paper");
- conditions.put("Rock", "crushes Lizard,crushes Scissors");
- conditions.put("Paper", "disproves Spock, covers Rock");
- conditions.put("Lizard", "eats Paper,poisons Spock");
- }
- public RPSLS(){
- while(true){
- System.out.print("Player Choice: ");
- choice = in.nextLine();
- if(conditions.containsKey(choice)) break;
- System.out.println("CHOOSE A CORRECT CHOICE!\n");
- }
- System.out.println("Computer Choice: " + (aiChoice = getRandomChoice()));
- System.out.println(getResult(choice, aiChoice));
- }
- private String getRandomChoice(){
- Random r = new Random();
- r.setSeed(System.currentTimeMillis());
- return (String) conditions.keySet().toArray()[r.nextInt(5)];
- }
- private String getResult(String choice, String computer){
- String str;
- if((str = conditions.get(choice)).contains(computer)){
- String[] pieces = str.split(",");
- for(String s: pieces)
- if(s.contains(computer)){
- playerWin++;
- return choice + " " + s;
- }
- }else{
- str = conditions.get(computer);
- String[] pieces = str.split(",");
- for(String s: pieces)
- if(s.contains(choice)) {
- aiWin++;
- return computer + " " + s;
- }
- }
- noWin++;
- return "Both " + choice + " and " + computer + " are just sitting there...\nTie!";
- }
- public static void main(String[] args){
- new RPSLS();
- while(continuing()){
- new RPSLS();
- }
- int total = playerWin + aiWin + noWin;
- System.out.println("Total Games:\t" + total);
- System.out.println(String.format("Player Wins:\t%s/%s - %s%%", playerWin, total, Math.round(((float) playerWin)/total * 100)));
- System.out.println(String.format("A.I. Wins :\t%s/%s - %s%%", aiWin, total, Math.round(((float) aiWin)/total * 100)));
- System.out.println(String.format("Tie Games :\t%s/%s - %s%%", noWin, total, Math.round(((float) noWin)/total * 100)));
- in.close();
- }
- private static boolean continuing(){
- while(true){
- System.out.print("\nContinue?: ");
- String in = RPSLS.in.nextLine().toLowerCase();
- if(Arrays.asList(confirmed).contains(in)) return true;
- else if(Arrays.asList(denied).contains(in)) return false;
- System.out.println("INVALID INPUT!\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement