diff --git a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/AutoPlayer.java b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/AutoPlayer.java index 0162f66..40ab5d7 100644 --- a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/AutoPlayer.java +++ b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/AutoPlayer.java @@ -1,11 +1,44 @@ package com.codedifferently.q4.team2.model; public class AutoPlayer extends Player { - AutoPlayer(int playerId, String playerName) { - super(playerId, playerName); + private int autoPlayerId; + private String autoPlayerName; + private int autoPlayerGuess; + + AutoPlayer(int autoPlayerId, String autoPlayerName) { + super(autoPlayerId, autoPlayerName); } - public static int autoPlayerGuess() { - return (int) (Math.random() * Difficulty.MEDIUM.value); + public void AutoPlayerId(int autoPlayerId) { + this.autoPlayerId = autoPlayerId; + } + + public void AutoPlayerGuess(int autoPlayerGuess) { + this.autoPlayerGuess = autoPlayerGuess; + } + + + public static int getautoPlayerGuess() { + return (int) (Math.random() * Difficulty.MEDIUM.value); //the medium enum is just a place holder + } + + public void setAutoPlayerGuess() { + this.autoPlayerGuess = autoPlayerGuess; + } + + public int getAutoPlayerId() { + return (int) (Math.random()); + } + + public void setAutoPlayerId() { + this.autoPlayerId = autoPlayerId; + } + + public String getAutoPlayerName() { + return autoPlayerName; + } + + public void setAutoPlayerName() { + this.autoPlayerName = autoPlayerName; } } diff --git a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Play.java b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Play.java new file mode 100644 index 0000000..f1f980d --- /dev/null +++ b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Play.java @@ -0,0 +1,73 @@ +package com.codedifferently.q4.team2.model; + +public class Play { + private Long playTimeElapsed; + private int playAttempts; + private int playScore; + private boolean isGameOn; + private boolean isGuessCorrect; + private int UpdatedScore; + + + public Play(Long playTimeElapsed, int playAttempts, int playScore, boolean isGameOn, boolean isGuessCorrect) { + this.playTimeElapsed = playTimeElapsed; + this.playAttempts = playAttempts; + this.playScore = playScore; + this.UpdatedScore = UpdatedScore; + } + + public boolean isGameOn() { + return isGameOn; + } + + public void isGameOn(boolean isGameOn) { + this.isGameOn = isGameOn; + } + + public Long getPlayTimeElapsed() { + return playTimeElapsed; + } + + public void setPlayTimeElapsed(Long playTimeElapsed) { + this.playTimeElapsed = playTimeElapsed; + } + + public int getPlayAttempts() { + return playAttempts; + } + public void setPlayAttempts(int playAttempts) { + this.playAttempts = playAttempts; + } + + public int getPlayScore() { + return playScore; + } + + public void setPlayScore(int playScore) { + this.playScore = playScore; + } + + public boolean getIsGuessCorrect() { + return isGuessCorrect; + } + + public void SetIsGuessCorrect(boolean isGuessCorrect) { + this.isGuessCorrect = isGuessCorrect; + } + + public int getUpdatedScore() { + return UpdatedScore; + } + + public void setUpdatedScore(int UpdatedScore) { + this.UpdatedScore = UpdatedScore; + } + + + + + +} + + + diff --git a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Player.java b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Player.java index 431232b..ebb3faf 100644 --- a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Player.java +++ b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/model/Player.java @@ -1,14 +1,27 @@ package com.codedifferently.q4.team2.model; +import java.time.LocalDateTime; +import java.time.Duration; + + public class Player { private int playerId; private String playerName; + private LocalDateTime gameStartTime; + private LocalDateTime gameEndTime; + private int playerGuess; - Player(int playerId, String playerName) { + public Player(int playerId, String playerName) { this.playerId = playerId; this.playerName = playerName; } + public void recordGameTime(LocalDateTime gameStartTime, LocalDateTime gameEndTime) { + this.gameStartTime = gameStartTime; + this.gameEndTime = gameEndTime; + + } + public String getPlayerName() { return playerName; } @@ -24,4 +37,30 @@ public int getPlayerId() { public void setPlayerId(int playerId) { this.playerId = playerId; } + + public LocalDateTime getStartGame() { + return gameStartTime; + } + + + public void setStartGame() { + this.gameStartTime = LocalDateTime.now(); + } + + public LocalDateTime getEndGame() { + return gameEndTime; + } + + public void setEndGame() { + this.gameEndTime = LocalDateTime.now(); + } + + public int getPlayerGuess() { + return (int) (Math.random() * Difficulty.MEDIUM.value); + } + + public void setPlayerGuess(int playerGuess) { + this.playerGuess = playerGuess; + } + } diff --git a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java index 94e663f..a51188f 100644 --- a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java +++ b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameEngine.java @@ -15,6 +15,31 @@ public class GameEngine { private Map> leaderboard; + private Difficulty selectDifficulty(int choice) { + try { + switch (choice) { + case 1: return Difficulty.EASY; + case 2: return Difficulty.MEDIUM; + case 3: return Difficulty.HARD; + case 0: { + exitGame(0); + return Difficulty.EASY; + + } + default: { + System.out.println("\n⚠️ Invalid difficulty selected. Defaulting to EASY level."); + return Difficulty.EASY; + } + } + } catch (Exception e) { + System.out.println("\n⚠️ An error ocurred during difficulty selection. Defaulting to EASY."); + return Difficulty.EASY; + } + + } + + + public GameEngine() { this.console = new Scanner(System.in); this.secretNumber = -1; @@ -41,6 +66,12 @@ public void start() { System.out.println(" Welcome to numMeCrazy!"); System.out.println("************** **************"); System.out.println("**************************************************\n"); + int difficultyChoice = -1; + boolean validInput = false; + while (!validInput) { + try { + if (validInput) { + System.out.println("****** Please select Game difficulty level: ******\n"); System.out.println("{1}. \t Easy (1-10)"); System.out.println("{2}. \t Medium (1-20)"); @@ -51,20 +82,17 @@ public void start() { int difficulty = console.nextInt(); System.out.println("\n\n\n\n\n\n\n"); exitGame(difficulty); - switch (difficulty) { - case 1: - level = Difficulty.EASY; - break; - case 2: - level = Difficulty.MEDIUM; - break; - case 3: - level = Difficulty.HARD; - break; - default: - break; + if (console.hasNextInt()) { + difficultyChoice = console.nextInt(); + + level = selectDifficulty(difficultyChoice); + validInput = true; + } else { + System.out.println("\n⚠️ Please enter a valid number."); + console.nextInt(); } - secretNumber = generateNumberToGuess(Difficulty.valueOf(level.name())); + + secretNumber = generateNumberToGuess(level); System.out.println("\n You Selected " + level.name() + " difficulty level \n"); System.out.println("*************************************************\n\n\n\n\n"); System.out.print("Please enter your name :\n"); @@ -73,8 +101,19 @@ public void start() { System.out.println("\n\n"); System.out.println("\n Hi, " + playerName + "!\n Let's get Started!\n\n\n\n\n"); System.out.println(" Your Number GUESS must be Between 1 and " + level.value + "\n"); + } + } catch (InputMismatchException e) { + System.out.println("\n⚠️ Invalid input. Please enter a valid number."); + console.nextLine(); // Clears the buffer +} + finally { + if (!validInput) { + System.out.println("Retrying difficulty selection..."); + } + } } } + public void play() { // Common play logic for both modes (getting guesses, validating, updating attempts) int guessedNumber = 0; @@ -115,6 +154,7 @@ public void play() { } } + protected int playerGuess(String playerName) { System.out.print( " " @@ -168,5 +208,6 @@ private void displayLeaderboard(Difficulty level) { for (int i = 0; i < scores.size(); i++) { System.out.println(" " + (i + 1) + ". " + scores.get(i)); } - } + } } + diff --git a/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameService.java b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameService.java new file mode 100644 index 0000000..f4f9948 --- /dev/null +++ b/guessing-game-v1/src/main/java/com/codedifferently/q4/team2/service/GameService.java @@ -0,0 +1,210 @@ +package com.codedifferently.q4.team2.service; + +import org.springframework.core.task.TaskTimeoutException; + +import org.springframework.stereotype.Service; + +import com.codedifferently.q4.team2.model.Difficulty; + +import org.springframework.util.StopWatch; + +import java.time.LocalDateTime; + +import java.util.Scanner; +import java.util.concurrent.TimeoutException; + +import java.util.Random; + +import java.time.Duration; + + +@Service +public class GameService { + + + public class Player { + private boolean isGameOn; + protected Scanner console; + private int playerId; + private String playerName; + private LocalDateTime gameStartTime; + private LocalDateTime gameEndTime; + + + + public Player(int playerId, String playerName) { + this.playerId = playerId; + this.playerName = playerName; + } + + public void recordGameTime(LocalDateTime gameStartTime, LocalDateTime gameEndTime) { + this.gameStartTime = gameStartTime; + this.gameEndTime = gameEndTime; + } + } + + + + public String getPlayerName(String playerName) { + Scanner console = null; + try { + if (playerName == null || playerName.trim().isEmpty()) { + console = new Scanner(System.in); + + System.out.println("Please enter a valid player name:"); + playerName = console.nextLine(); + + if (playerName == null || playerName.trim().isEmpty()) { + throw new IllegalArgumentException("Player name cannot be empty"); + } + } + + Player player = new Player(0, playerName); + + return player.playerName; + } catch (IllegalArgumentException e) { + + System.out.println("Invalid input: " + e.getMessage()); + return null; + } catch (Exception e) { + System.out.println("An unexpected error occurred: " + e.getMessage()); + return null; + } finally { + if (console != null) { + try { + console.close(); + } catch (Exception e) { + System.out.println("Error closing scanner: " + e.getMessage()); + } + } + } + + } + + public class AutoPlayer extends Player { + private Random random; + + AutoPlayer(int playerId, String playerName) { + super(playerId, playerName); + this.random = new Random(); + } + + public int getAutoPlayerId() { + return random.nextInt(1000); + } + + public int getAutoPlayerGuess(int maxRange) { + + return random.nextInt(maxRange + 1); + } + } + + + public Player generatePlayer(String playerName) { + if(playerName == null || playerName.trim().isEmpty()) { + throw new IllegalArgumentException("Player name cannot be empty"); + + } + int PlayerId = new Random().nextInt(10000); + return new Player(new Random().nextInt(10000), playerName); + } + + public AutoPlayer generateAutoPlayer() { + int autoPlayerId = new Random().nextInt(1000); + return new AutoPlayer(new Random().nextInt(1000), null); + } + + public class Play { + + public enum Difficulty { + EASY, MEDIUM, HARD + } + + private Difficulty level; + private LocalDateTime gameStartTime; + private LocalDateTime gameEndTime; + private boolean isGameOn; + private boolean isGuessCorrect; + private long playTimeElapsed; + private int playAttempts; + private int playeScore; + + private static final long MAX_GUESS_TIME = 3000; + + public Play(Difficulty level) { + this.level = level; + this.isGameOn = false; + } + + public void startGame() { + this.gameStartTime = LocalDateTime.now(); + this.isGameOn = true; + this.playAttempts = 0; + } + + private long calculatePlayTime(){ + if(gameStartTime != null && gameEndTime != null) { + return Duration.between(gameStartTime, gameEndTime).toMillis(); + } + return 0; + } + + public void endGame() { + if (isGameOn) { + this.gameEndTime = LocalDateTime.now(); + this.playTimeElapsed = calculatePlayTime(); + this.isGameOn = false; + } + } + public void recordAttempt(long guessTime) throws TimeoutException { + if (!isGameOn) { + throw new IllegalStateException("Game is not in progress"); + } + + playAttempts++; + + if (guessTime > MAX_GUESS_TIME) { + throw new TimeoutException("Guess took too long. Time limit exceeded."); + } + } + + public void updateScore(int points) { + this.playeScore += points; + } + + public int getPlayScore(int basePoints, int playAttempts) { + int playScore = basePoints; + int attemptPenalty = calculatePlayAttemptPenalty(playAttempts); + playScore -= attemptPenalty; + + playScore = Math.max(0,playScore); //Added Math.max to avoid scores with negative integers + + return playScore; + } + } + + public int calculatePlayAttemptPenalty(int playAttempts) { + switch (playAttempts) { + case 1: + case 2: + case 3: + return 0; + case 4: + case 5: + return 20; + default: + return 30; + } + } + + } +} + + + + + + + + +