/** * Assignment 2, COMP268 Class: RockPaperScissorsLizardSpock.java * * @description Provide a class to play the game rock, paper, scissors, lizard spock. * @author: mo khan Student ID: 3431709 * @date Jun 6, 2019 * @version 1.0 */ package Q2; import java.util.*; public class RockPaperScissorsLizardSpock { private int consecutiveWins = 0; private int lastWinner; public static final int PLAYER1 = 1; public static final int PLAYER2 = 2; public static final int LIZARD = 4; public static final int PAPER = 2; public static final int ROCK = 1; public static final int SCISSORS = 3; public static final int SPOCK = 5; /** * Returns the # of consecutive wins for the current winner. * * @return the # of consecutive wins for the current winner. */ public int getConsecutiveWins() { return this.consecutiveWins; } /** * Returns the id of the current winner. See PLAYER1 and PLAYER2 constants for the possible ids. * * @return the id of the current winner. */ public int getLastWinner() { return this.lastWinner; } /** * Returns a random integer between 1 - 5 * * @return a random integer between 1 - 5 */ public int random() { return new Random().nextInt(4) + 1; } /** * Plays the game until a winner has 4 consecutive wins. * * @param player1 the choice made by player 1 * @param player2 the choice made by player 2 */ public void play(int player1, int player2) { this.puts("Player 1: %s", convert(player1)); this.puts("Player 2: %s", convert(player2)); this.incrementWinsFor(this.determineWinner(player1, player2)); } /** * Converts a integer to the equivalent name. * * @param i the integer to convert to a name. * @return the name for the corresponding integer value. */ public static String convert(int i) { switch (i) { case ROCK: return "rock"; case PAPER: return "paper"; case SCISSORS: return "scissor"; case LIZARD: return "lizard"; case SPOCK: return "spock"; default: return "error"; } } private int determineWinner(int player1Roll, int player2Roll) { if (player1Roll == player2Roll) return 0; switch (player1Roll) { case SCISSORS: return (player2Roll == PAPER || player2Roll == LIZARD) ? PLAYER1 : PLAYER2; case PAPER: return (player2Roll == ROCK || player2Roll == SPOCK) ? PLAYER1 : PLAYER2; case ROCK: return (player2Roll == LIZARD || player2Roll == SCISSORS) ? PLAYER1 : PLAYER2; case LIZARD: return (player2Roll == SPOCK || player2Roll == PAPER) ? PLAYER1 : PLAYER2; case SPOCK: return (player2Roll == SCISSORS || player2Roll == ROCK) ? PLAYER1 : PLAYER2; default: return 0; } } private void puts(String message, Object... args) { System.out.println(String.format(message, args)); } private void newline() { System.out.println(); } private void incrementWinsFor(int winner) { if (winner == 0) return; if (this.lastWinner == winner) { this.consecutiveWins++; } else if (winner > 0) { this.lastWinner = winner; this.consecutiveWins = 1; } } /** * The entrypoint into the console application. * * @param args an argument vector that represents the command line arguments */ public static void main(String[] args) { RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock(); int round = 0; game.puts("Starting a new game of Rock, paper, scissors, lizard, spock..."); while (true) { round++; game.newline(); game.puts("Round: %d", round); game.puts("--------------------------------------"); game.play(game.random(), game.random()); if (game.getLastWinner() > 0) game.puts( "Player %d has %d consecutive wins.", game.getLastWinner(), game.getConsecutiveWins()); if (game.getLastWinner() > 0 && game.getConsecutiveWins() == 4) { game.newline(); game.puts("**************************************"); game.puts(" The winner is player %d! ", game.getLastWinner()); game.puts("**************************************"); game.newline(); break; } } } }