Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Random;
- import java.util.Scanner;
- public class App {
- public static void main(String[] args) {
- try (Scanner input = new Scanner(System.in)) {
- Game game = initializeGame(input);
- startGameLoop(input, game);
- endGame(input, game);
- }
- }
- private static Game initializeGame(Scanner input) {
- System.out.println("Welcome to Rock Paper Scissors!");
- System.out.println("Do you want to start a new game or resume a saved game? (new/resume)");
- String choice = input.nextLine().toLowerCase();
- if (choice.equals("resume")) {
- Game game = Game.loadGame();
- if (game == null) {
- System.out.println("No saved game found. Starting a new game.");
- return startNewGame(input);
- }
- return game;
- }
- return startNewGame(input);
- }
- private static Game startNewGame(Scanner input) {
- System.out.println("Enter the series length (e.g., 3 for best of 3): ");
- int seriesLength = input.nextInt();
- input.nextLine();
- return new Game(seriesLength);
- }
- private static void startGameLoop(Scanner input, Game game) {
- System.out.println("Press Enter to get started!");
- input.nextLine();
- System.out.println("-------");
- boolean play = true;
- while (play) {
- System.out.println("\nPlease choose rock, paper, or scissors. Enter 0 to quit, save to save the game.");
- String userChoice = input.nextLine().toLowerCase();
- play = handleUserChoice(input, game, userChoice);
- }
- }
- private static boolean handleUserChoice(Scanner input, Game game, String userChoice) {
- if (userChoice.equals("0")) {
- return false;
- } else if (userChoice.equals("save")) {
- game.saveGame();
- System.out.println("Game saved. You can resume later.");
- } else {
- System.out.println(game.play(userChoice));
- if (game.isSeriesOver()) {
- System.out.println("\n-------");
- System.out.println(game.finalResult());
- return false;
- }
- }
- return true;
- }
- private static void endGame(Scanner input, Game game) {
- System.out.println("\n-------");
- System.out.println(game.gameScore());
- System.out.println(game.finalResult());
- }
- }
- class Game implements Serializable {
- private static final long serialVersionUID = 1L;
- private static final String[] CHOICES = {"rock", "paper", "scissors"};
- private static final String INVALID_CHOICE_MESSAGE = "Invalid choice. Please choose rock, paper, or scissors.";
- private int userScore;
- private int botScore;
- private int seriesLength;
- private transient Random random;
- public Game(int seriesLength) {
- userScore = 0;
- botScore = 0;
- this.seriesLength = (seriesLength % 2 == 0) ? seriesLength + 1 : seriesLength;
- random = RandomSingleton.getInstance();
- }
- public String play(String userChoice) {
- if (!isValidChoice(userChoice)) {
- return INVALID_CHOICE_MESSAGE;
- }
- String botChoice = getBotChoice();
- String result = "Bot chose " + botChoice + ". " + determineWinner(userChoice, botChoice);
- return result;
- }
- private boolean isValidChoice(String choice) {
- for (String validChoice : CHOICES) {
- if (validChoice.equals(choice)) {
- return true;
- }
- }
- return false;
- }
- private String getBotChoice() {
- return CHOICES[random.nextInt(CHOICES.length)];
- }
- private String determineWinner(String userChoice, String botChoice) {
- if (userChoice.equals(botChoice)) {
- return "It's a tie!";
- }
- if ((userChoice.equals("rock") && botChoice.equals("scissors")) ||
- (userChoice.equals("paper") && botChoice.equals("rock")) ||
- (userChoice.equals("scissors") && botChoice.equals("paper"))) {
- userScore++;
- return "You win this round!";
- } else {
- botScore++;
- return "You lose this round!";
- }
- }
- public String gameScore() {
- return "Current Score - You: " + userScore + ", Bot: " + botScore;
- }
- public String finalResult() {
- if (userScore > botScore) {
- return "You won the series!";
- } else if (userScore < botScore) {
- return "You lost the series.";
- } else {
- return "The series is tied.";
- }
- }
- public boolean isSeriesOver() {
- return userScore > seriesLength / 2 || botScore > seriesLength / 2;
- }
- public void saveGame() {
- try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("game.dat"))) {
- out.writeObject(this);
- } catch (IOException e) {
- System.out.println("Error saving game: " + e.getMessage());
- }
- }
- public static Game loadGame() {
- try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("game.dat"))) {
- Game game = (Game) in.readObject();
- game.random = RandomSingleton.getInstance();
- return game;
- } catch (IOException | ClassNotFoundException e) {
- return null;
- }
- }
- }
- class RandomSingleton {
- private static final Random INSTANCE = new Random();
- private RandomSingleton() {}
- public static Random getInstance() {
- return INSTANCE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement