Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mycompany.rockpapperscissor;
- import java.util.Random;
- /**
- * @author Roner Reked
- * Handles computers move.
- */
- public class Computer {
- /**
- * @return a random object from the enum RPS,
- * which contains ROCK, PAPER or SCISSORS.
- */
- public RPS getHand() {
- RPS[] moves = RPS.values();
- Random random = new Random();
- int index = random.nextInt(moves.length);
- return moves[index];
- }
- }
- package com.mycompany.rockpapperscissor;
- /**
- *
- * @author Roner Reked
- */
- public class GameRunner {
- Player human = new Player();
- Computer computer = new Computer();
- private int playerWonGames = 0,
- playerLostGames = 0,
- numberOfGames = 0;
- /**
- *
- */
- public void runGame() {
- RPS playerHand = human.getHand(); // "Hand" = Rock, paper or scissors.
- RPS computerHand = computer.getHand();
- System.out.println("\nYou played " + playerHand + ".");
- System.out.println("Computer played " + computerHand + ".\n");
- int comapreHands = playerHand.compareHands(computerHand);
- switch (comapreHands) {
- case 0: // Tie
- System.out.println("Tie!");
- break;
- case 1: // Player wins
- System.out.println(playerHand + " beats " + computerHand + ". You won!");
- playerWonGames++;
- break;
- case -1: // Computer wins
- System.out.println(computerHand + " beats " + playerHand + ". You lost.");
- playerLostGames++;
- break;
- }
- numberOfGames++;
- if (human.playAgain()) {
- runGame();
- } else {
- printStats();
- }
- }
- /**
- * Prints out the number of games you've played.
- * Calculates how many games you have tied,
- * also prints out won games and lost games.
- */
- public void printStats() {
- final int tiedGames = numberOfGames - playerWonGames - playerLostGames;
- System.out.printf("| %6s | %6s | %6s | %12s |\n",
- "WINS", "LOSSES", "TIES", "GAMES PLAYED");
- System.out.printf("| %6d | %6d | %6d | %12d |\n",
- playerWonGames, playerLostGames, tiedGames, numberOfGames);
- }
- /*public void printStats() {
- StringBuilder sb = new StringBuilder();
- final int tiedGames = numberOfGames - playerWonGames - playerLostGames;
- sb.append("[")
- .append("Wins: ")
- .append(playerWonGames)
- .append(" Losses: ")
- .append(playerLostGames)
- .append(" Ties: ")
- .append(tiedGames)
- .append(" Games Played: ")
- .append(numberOfGames)
- .append("]");
- System.out.println(sb);
- }*/
- }
- package com.mycompany.rockpapperscissor;
- import java.util.Scanner;
- /**
- * @author Roner Reked
- *
- */
- public class Player {
- Scanner userInput = new Scanner(System.in);
- /**
- * The user gets to type in rock, paper or scissor.
- *
- * @return a object from the Enum "RPS"
- * which in this case is ROCK, PAPER or SCISSORS.
- */
- public RPS getHand() {
- // Prompt the user
- System.out.print("Rock, paper, or scissors? ");
- // Get the user input
- String playerMove = userInput.nextLine().toUpperCase().replaceAll(" ", "");
- char firstLetter = playerMove.charAt(0);
- if (playerMove.startsWith("R") || playerMove.startsWith("P") || playerMove.startsWith("S")) {
- // User has entered a valid input
- switch (firstLetter) {
- case 'R':
- return RPS.ROCK;
- case 'P':
- return RPS.PAPER;
- case 'S':
- return RPS.SCISSORS;
- }
- }
- // User has not entered a valid input. Try again.
- System.out.println("Wrong input!");
- return getHand();
- }
- /**
- * @return true if the player inputs a Y as a first letter.
- */
- public boolean playAgain() {
- System.out.print("Do you want to play again? ");
- String yesOrNo = userInput.nextLine().toUpperCase();
- return yesOrNo.charAt(0) == 'Y';
- }
- }
- package com.mycompany.rockpapperscissor;
- /**
- * @author Roner Reked
- * Holds our objects and compare method for Rock paper scissors game.
- * RPS = Rock Paper Scissors.
- */
- public enum RPS {
- ROCK,
- PAPER,
- SCISSORS;
- /**
- * Compares the player and computer hands
- * to calculate a tie, win or loss.
- *
- * @param hand hand to compare to.
- * @return 1 If this > hand.
- * If it's a tie return 0.
- * If hand > this return -1.
- */
- public int compareHands(RPS hand) {
- if (this == hand) { // Tied game.
- return 0;
- }
- switch (this) {
- case ROCK:
- return (hand == SCISSORS ? 1 : -1);
- case PAPER:
- return (hand == ROCK ? 1 : -1);
- case SCISSORS:
- return (hand == PAPER ? 1 : -1);
- }
- return 0; // Never reaches this.
- }
- }
- package com.mycompany.rockpapperscissor;
- /**
- *
- * @author Roner Reked
- */
- public class RockPaperScissors {
- public static void main(String[] args) {
- GameRunner game = new GameRunner();
- game.runGame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement