Skip to content

Feat: Adds- Optimized selectDifficulty methods with try/catch excepti… #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7f5e1cb
Feat: Adds- Optimized selectDifficulty methods with try/catch excepti…
Nautevol07VII-111 Dec 6, 2024
197dc17
Feat:Add-Start/End game time getters/setters (for time elapsed & date…
Nautevol07VII-111 Dec 6, 2024
3ddbf1a
Update GameEngine.java
nilejack Dec 6, 2024
808b3c6
Feat: Adds- player.java recordGameTime method/ slight change to playe…
Nautevol07VII-111 Dec 7, 2024
d23ff2f
Merge branch 'improved-logic' of https://github.com/code-differently/…
Nautevol07VII-111 Dec 7, 2024
af30c86
Feat Adds: prequisite for database communication(getters, setters for…
Nautevol07VII-111 Dec 9, 2024
d37cf08
Feat Adds: Tentative changes; game service will house the logic our c…
Nautevol07VII-111 Dec 9, 2024
bcf01fc
Update GameService.java
nilejack Dec 9, 2024
8d17013
Update GameService.java
nilejack Dec 9, 2024
8e0e174
Feat Adds: getters & setters for AutoPlayer.java & added logic in @Se…
Nautevol07VII-111 Dec 9, 2024
734ea0d
Merge branch 'improved-logic' of https://github.com/code-differently/…
Nautevol07VII-111 Dec 9, 2024
c03fb9b
Feat Adds: Progress on the Service endpoint
Nautevol07VII-111 Dec 10, 2024
375c35c
Feat Adds: Tentative changes to service class logic/ player.java
Nautevol07VII-111 Dec 10, 2024
2fda5fa
Feat Adds: completed try block
Nautevol07VII-111 Dec 10, 2024
2578186
Feat: fix: syntax fix
Nautevol07VII-111 Dec 10, 2024
4959e2b
Feat Fix: gameEngine.java
Nautevol07VII-111 Dec 10, 2024
96f69f5
Feats Adds: Completed GameService class and play.java
Nautevol07VII-111 Dec 11, 2024
935a28c
Update GameService.java
nilejack Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}





}



Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ public class GameEngine {

private Map<Difficulty, List<LeaderboardEntry>> 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;
Expand All @@ -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)");
Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -115,6 +154,7 @@ public void play() {
}
}


protected int playerGuess(String playerName) {
System.out.print(
" "
Expand Down Expand Up @@ -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));
}
}
}
}

Loading