Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.proxus;
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- public static int currentRound;
- public static int playerScore;
- public static int botScore;
- public static Type playerType;
- public static Type botType;
- public static void main(final String... arg) {
- Scanner scanner = new Scanner(System.in);
- print("Current round: " + (getCurrentRound() + 1));
- setPlayerType(getTypeByName(scanner.next()));
- print("You selected '" + getPlayerType().getName() + "' as your type!");
- sleep(1000L);
- print("Now selecting the type for the bot..");
- setBotType(getRandomType());
- sleep(1000L);
- print("The bot selected '" + getBotType().getName() + "' as its type!");
- sleep(1000L);
- try {
- String winnerName = getWinnerName(getWinner(getBotType(),
- getPlayerType()));
- printResultsIfNotDraw(winnerName, arg);
- } catch (Exception exception) {
- printResultsIfDraw(arg);
- }
- }
- public static void printResultsIfNotDraw(final String winnerName,
- final String... arg) {
- print("The winner is '" + winnerName + "'!");
- if (winnerName.equalsIgnoreCase("Player"))
- setPlayerScore(getPlayerScore() + 1);
- if (winnerName.equalsIgnoreCase("Bot"))
- setBotScore(getBotScore() + 1);
- sleep(1000L);
- print("Player: " + getPlayerScore());
- sleep(1000L);
- print("Bot: " + getBotScore());
- sleep(1000L);
- print("Starting new round..");
- sleep(1000L);
- print("----------------------------------------------------------------");
- sleep(1000L);
- setCurrentRound(getCurrentRound() + 1);
- main(arg);
- }
- public static void printResultsIfDraw(final String... arg) {
- print("It's a draw!");
- sleep(1000L);
- print("Player: " + getPlayerScore());
- sleep(1000L);
- print("Bot: " + getBotScore());
- sleep(1000L);
- print("Starting new round..");
- sleep(1000L);
- print("----------------------------------------------------------------");
- sleep(1000L);
- setCurrentRound(getCurrentRound() + 1);
- main(arg);
- }
- public static void sleep(final long delay) {
- try {
- Thread.sleep(delay);
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- public static String getWinnerName(final Type winner) {
- if (winner.getName().equalsIgnoreCase(getPlayerType().getName()))
- return "Player";
- if (winner.getName().equalsIgnoreCase(getBotType().getName()))
- return "Bot";
- return "N/A";
- }
- public static Type getWinner(final Type botType, final Type playerType) {
- if (isCounterType(botType, playerType))
- return playerType;
- if (isCounterType(playerType, botType))
- return botType;
- return null;
- }
- public static Type getRandomType() {
- Random random = new Random();
- return Type.values()[random.nextInt(Type.values().length)];
- }
- public static Type getTypeByName(final String name) {
- for (Type type : Type.values())
- if (type.getName().equalsIgnoreCase(name))
- return type;
- return null;
- }
- public static Type getTypeById(final int id) {
- for (Type type : Type.values())
- if (type.getId() == id)
- return type;
- return null;
- }
- public static boolean isCounterType(final Type type1, final Type type2) {
- return type1.getCounterTypeId() == type2.getId();
- }
- public static void print(final String message) {
- System.out.println(message);
- }
- public static int getCurrentRound() {
- return currentRound;
- }
- public static void setCurrentRound(final int currentRound) {
- Main.currentRound = currentRound;
- }
- public static Type getPlayerType() {
- return playerType;
- }
- public static void setPlayerType(final Type playerType) {
- Main.playerType = playerType;
- }
- public static Type getBotType() {
- return botType;
- }
- public static void setBotType(final Type botType) {
- Main.botType = botType;
- }
- public static int getPlayerScore() {
- return playerScore;
- }
- public static void setPlayerScore(final int playerScore) {
- Main.playerScore = playerScore;
- }
- public static int getBotScore() {
- return botScore;
- }
- public static void setBotScore(final int botScore) {
- Main.botScore = botScore;
- }
- enum Type {
- ROCK("Rock", 0, 1), PAPER("Paper", 1, 2), SCISSOR("Scissor", 2, 0);
- private String name;
- private int id;
- private int counterTypeId;
- private Type(final String name, final int id, final int counterTypeId) {
- setName(name);
- setId(id);
- setCounterTypeId(counterTypeId);
- }
- public String getName() {
- return name;
- }
- public void setName(final String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(final int id) {
- this.id = id;
- }
- public int getCounterTypeId() {
- return counterTypeId;
- }
- public void setCounterTypeId(final int counterTypeId) {
- this.counterTypeId = counterTypeId;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement