Advertisement
Olivki

Rock, Paper, Scissor.

Jul 11th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package se.proxus;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static int currentRound;
  9.     public static int playerScore;
  10.     public static int botScore;
  11.     public static Type playerType;
  12.     public static Type botType;
  13.  
  14.     public static void main(final String... arg) {
  15.     Scanner scanner = new Scanner(System.in);
  16.     print("Current round: " + (getCurrentRound() + 1));
  17.     setPlayerType(getTypeByName(scanner.next()));
  18.     print("You selected '" + getPlayerType().getName() + "' as your type!");
  19.     sleep(1000L);
  20.     print("Now selecting the type for the bot..");
  21.     setBotType(getRandomType());
  22.     sleep(1000L);
  23.     print("The bot selected '" + getBotType().getName() + "' as its type!");
  24.     sleep(1000L);
  25.     try {
  26.         String winnerName = getWinnerName(getWinner(getBotType(),
  27.             getPlayerType()));
  28.         printResultsIfNotDraw(winnerName, arg);
  29.     } catch (Exception exception) {
  30.         printResultsIfDraw(arg);
  31.     }
  32.     }
  33.  
  34.     public static void printResultsIfNotDraw(final String winnerName,
  35.         final String... arg) {
  36.     print("The winner is '" + winnerName + "'!");
  37.     if (winnerName.equalsIgnoreCase("Player"))
  38.         setPlayerScore(getPlayerScore() + 1);
  39.     if (winnerName.equalsIgnoreCase("Bot"))
  40.         setBotScore(getBotScore() + 1);
  41.     sleep(1000L);
  42.     print("Player: " + getPlayerScore());
  43.     sleep(1000L);
  44.     print("Bot: " + getBotScore());
  45.     sleep(1000L);
  46.     print("Starting new round..");
  47.     sleep(1000L);
  48.     print("----------------------------------------------------------------");
  49.     sleep(1000L);
  50.     setCurrentRound(getCurrentRound() + 1);
  51.     main(arg);
  52.     }
  53.  
  54.     public static void printResultsIfDraw(final String... arg) {
  55.     print("It's a draw!");
  56.     sleep(1000L);
  57.     print("Player: " + getPlayerScore());
  58.     sleep(1000L);
  59.     print("Bot: " + getBotScore());
  60.     sleep(1000L);
  61.     print("Starting new round..");
  62.     sleep(1000L);
  63.     print("----------------------------------------------------------------");
  64.     sleep(1000L);
  65.     setCurrentRound(getCurrentRound() + 1);
  66.     main(arg);
  67.     }
  68.  
  69.     public static void sleep(final long delay) {
  70.     try {
  71.         Thread.sleep(delay);
  72.     } catch (Exception exception) {
  73.         exception.printStackTrace();
  74.     }
  75.     }
  76.  
  77.     public static String getWinnerName(final Type winner) {
  78.     if (winner.getName().equalsIgnoreCase(getPlayerType().getName()))
  79.         return "Player";
  80.     if (winner.getName().equalsIgnoreCase(getBotType().getName()))
  81.         return "Bot";
  82.     return "N/A";
  83.     }
  84.  
  85.     public static Type getWinner(final Type botType, final Type playerType) {
  86.     if (isCounterType(botType, playerType))
  87.         return playerType;
  88.     if (isCounterType(playerType, botType))
  89.         return botType;
  90.     return null;
  91.     }
  92.  
  93.     public static Type getRandomType() {
  94.     Random random = new Random();
  95.     return Type.values()[random.nextInt(Type.values().length)];
  96.     }
  97.  
  98.     public static Type getTypeByName(final String name) {
  99.     for (Type type : Type.values())
  100.         if (type.getName().equalsIgnoreCase(name))
  101.         return type;
  102.     return null;
  103.     }
  104.  
  105.     public static Type getTypeById(final int id) {
  106.     for (Type type : Type.values())
  107.         if (type.getId() == id)
  108.         return type;
  109.     return null;
  110.     }
  111.  
  112.     public static boolean isCounterType(final Type type1, final Type type2) {
  113.     return type1.getCounterTypeId() == type2.getId();
  114.     }
  115.  
  116.     public static void print(final String message) {
  117.     System.out.println(message);
  118.     }
  119.  
  120.     public static int getCurrentRound() {
  121.     return currentRound;
  122.     }
  123.  
  124.     public static void setCurrentRound(final int currentRound) {
  125.     Main.currentRound = currentRound;
  126.     }
  127.  
  128.     public static Type getPlayerType() {
  129.     return playerType;
  130.     }
  131.  
  132.     public static void setPlayerType(final Type playerType) {
  133.     Main.playerType = playerType;
  134.     }
  135.  
  136.     public static Type getBotType() {
  137.     return botType;
  138.     }
  139.  
  140.     public static void setBotType(final Type botType) {
  141.     Main.botType = botType;
  142.     }
  143.  
  144.     public static int getPlayerScore() {
  145.     return playerScore;
  146.     }
  147.  
  148.     public static void setPlayerScore(final int playerScore) {
  149.     Main.playerScore = playerScore;
  150.     }
  151.  
  152.     public static int getBotScore() {
  153.     return botScore;
  154.     }
  155.  
  156.     public static void setBotScore(final int botScore) {
  157.     Main.botScore = botScore;
  158.     }
  159.  
  160.     enum Type {
  161.  
  162.     ROCK("Rock", 0, 1), PAPER("Paper", 1, 2), SCISSOR("Scissor", 2, 0);
  163.  
  164.     private String name;
  165.     private int id;
  166.     private int counterTypeId;
  167.  
  168.     private Type(final String name, final int id, final int counterTypeId) {
  169.         setName(name);
  170.         setId(id);
  171.         setCounterTypeId(counterTypeId);
  172.     }
  173.  
  174.     public String getName() {
  175.         return name;
  176.     }
  177.  
  178.     public void setName(final String name) {
  179.         this.name = name;
  180.     }
  181.  
  182.     public int getId() {
  183.         return id;
  184.     }
  185.  
  186.     public void setId(final int id) {
  187.         this.id = id;
  188.     }
  189.  
  190.     public int getCounterTypeId() {
  191.         return counterTypeId;
  192.     }
  193.  
  194.     public void setCounterTypeId(final int counterTypeId) {
  195.         this.counterTypeId = counterTypeId;
  196.     }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement