Advertisement
YaBoiSwayZ

RockPaperScissors

Jun 13th, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | Source Code | 0 0
  1. import java.io.*;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class App {
  6.     public static void main(String[] args) {
  7.         try (Scanner input = new Scanner(System.in)) {
  8.             Game game = initializeGame(input);
  9.             startGameLoop(input, game);
  10.             endGame(input, game);
  11.         }
  12.     }
  13.  
  14.     private static Game initializeGame(Scanner input) {
  15.         System.out.println("Welcome to Rock Paper Scissors!");
  16.         System.out.println("Do you want to start a new game or resume a saved game? (new/resume)");
  17.         String choice = input.nextLine().toLowerCase();
  18.         if (choice.equals("resume")) {
  19.             Game game = Game.loadGame();
  20.             if (game == null) {
  21.                 System.out.println("No saved game found. Starting a new game.");
  22.                 return startNewGame(input);
  23.             }
  24.             return game;
  25.         }
  26.         return startNewGame(input);
  27.     }
  28.  
  29.     private static Game startNewGame(Scanner input) {
  30.         System.out.println("Enter the series length (e.g., 3 for best of 3): ");
  31.         int seriesLength = input.nextInt();
  32.         input.nextLine();
  33.         return new Game(seriesLength);
  34.     }
  35.  
  36.     private static void startGameLoop(Scanner input, Game game) {
  37.         System.out.println("Press Enter to get started!");
  38.         input.nextLine();
  39.         System.out.println("-------");
  40.  
  41.         boolean play = true;
  42.         while (play) {
  43.             System.out.println("\nPlease choose rock, paper, or scissors. Enter 0 to quit, save to save the game.");
  44.             String userChoice = input.nextLine().toLowerCase();
  45.             play = handleUserChoice(input, game, userChoice);
  46.         }
  47.     }
  48.  
  49.     private static boolean handleUserChoice(Scanner input, Game game, String userChoice) {
  50.         if (userChoice.equals("0")) {
  51.             return false;
  52.         } else if (userChoice.equals("save")) {
  53.             game.saveGame();
  54.             System.out.println("Game saved. You can resume later.");
  55.         } else {
  56.             System.out.println(game.play(userChoice));
  57.             if (game.isSeriesOver()) {
  58.                 System.out.println("\n-------");
  59.                 System.out.println(game.finalResult());
  60.                 return false;
  61.             }
  62.         }
  63.         return true;
  64.     }
  65.  
  66.     private static void endGame(Scanner input, Game game) {
  67.         System.out.println("\n-------");
  68.         System.out.println(game.gameScore());
  69.         System.out.println(game.finalResult());
  70.     }
  71. }
  72.  
  73. class Game implements Serializable {
  74.     private static final long serialVersionUID = 1L;
  75.     private static final String[] CHOICES = {"rock", "paper", "scissors"};
  76.     private static final String INVALID_CHOICE_MESSAGE = "Invalid choice. Please choose rock, paper, or scissors.";
  77.  
  78.     private int userScore;
  79.     private int botScore;
  80.     private int seriesLength;
  81.     private transient Random random;
  82.  
  83.     public Game(int seriesLength) {
  84.         userScore = 0;
  85.         botScore = 0;
  86.         this.seriesLength = (seriesLength % 2 == 0) ? seriesLength + 1 : seriesLength;
  87.         random = RandomSingleton.getInstance();
  88.     }
  89.  
  90.     public String play(String userChoice) {
  91.         if (!isValidChoice(userChoice)) {
  92.             return INVALID_CHOICE_MESSAGE;
  93.         }
  94.  
  95.         String botChoice = getBotChoice();
  96.         String result = "Bot chose " + botChoice + ". " + determineWinner(userChoice, botChoice);
  97.  
  98.         return result;
  99.     }
  100.  
  101.     private boolean isValidChoice(String choice) {
  102.         for (String validChoice : CHOICES) {
  103.             if (validChoice.equals(choice)) {
  104.                 return true;
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.  
  110.     private String getBotChoice() {
  111.         return CHOICES[random.nextInt(CHOICES.length)];
  112.     }
  113.  
  114.     private String determineWinner(String userChoice, String botChoice) {
  115.         if (userChoice.equals(botChoice)) {
  116.             return "It's a tie!";
  117.         }
  118.  
  119.         if ((userChoice.equals("rock") && botChoice.equals("scissors")) ||
  120.             (userChoice.equals("paper") && botChoice.equals("rock")) ||
  121.             (userChoice.equals("scissors") && botChoice.equals("paper"))) {
  122.             userScore++;
  123.             return "You win this round!";
  124.         } else {
  125.             botScore++;
  126.             return "You lose this round!";
  127.         }
  128.     }
  129.  
  130.     public String gameScore() {
  131.         return "Current Score - You: " + userScore + ", Bot: " + botScore;
  132.     }
  133.  
  134.     public String finalResult() {
  135.         if (userScore > botScore) {
  136.             return "You won the series!";
  137.         } else if (userScore < botScore) {
  138.             return "You lost the series.";
  139.         } else {
  140.             return "The series is tied.";
  141.         }
  142.     }
  143.  
  144.     public boolean isSeriesOver() {
  145.         return userScore > seriesLength / 2 || botScore > seriesLength / 2;
  146.     }
  147.  
  148.     public void saveGame() {
  149.         try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("game.dat"))) {
  150.             out.writeObject(this);
  151.         } catch (IOException e) {
  152.             System.out.println("Error saving game: " + e.getMessage());
  153.         }
  154.     }
  155.  
  156.     public static Game loadGame() {
  157.         try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("game.dat"))) {
  158.             Game game = (Game) in.readObject();
  159.             game.random = RandomSingleton.getInstance();
  160.             return game;
  161.         } catch (IOException | ClassNotFoundException e) {
  162.             return null;
  163.         }
  164.     }
  165. }
  166.  
  167. class RandomSingleton {
  168.     private static final Random INSTANCE = new Random();
  169.  
  170.     private RandomSingleton() {}
  171.  
  172.     public static Random getInstance() {
  173.         return INSTANCE;
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement