Skip to content

Commit 1e09e06

Browse files
committed
Edits To Game and Player
1 parent bc0063a commit 1e09e06

File tree

3 files changed

+115
-69
lines changed

3 files changed

+115
-69
lines changed

src/main/java/com/github/zipcodewilmington/casino/NumberGuess.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessGame.java

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,84 @@
11
package com.github.zipcodewilmington.casino.games.numberguess;
22

3+
import java.util.Random;
4+
import java.util.Scanner;
5+
36
import com.github.zipcodewilmington.casino.GameInterface;
47
import com.github.zipcodewilmington.casino.PlayerInterface;
58

6-
/**
7-
* Created by leon on 7/21/2020.
8-
*/
9+
910
public class NumberGuessGame implements GameInterface {
1011

12+
private final NumberGuessPlayer player;
13+
private final Random random;
14+
private final Scanner scanner;
15+
16+
private int number;
17+
private int guesses;
18+
private int guess;
19+
private boolean playAgain;
20+
21+
22+
public NumberGuessGame(NumberGuessPlayer player, Random random, Scanner scanner) {
23+
this.player = player;
24+
this.random = random;
25+
this.scanner = scanner;
26+
}
27+
28+
public void play() {
29+
System.out.println("Welcome to Number Guess Game!===");
30+
31+
boolean playAgain = true;
32+
33+
while (playAgain) {
34+
playRound();
35+
}
36+
37+
System.out.print("Do you want to play again? (yes/no): ");
38+
String response = scanner.next();
39+
playAgain = response.equalsIgnoreCase("yes");
40+
41+
42+
System.out.println("Thanks for playing Number Guess Game!");
43+
}
44+
45+
private void playRound() {
46+
number = random.nextInt(100) + 1; // Random number between 1 and 100
47+
guesses = 0;
48+
49+
while (true) {
50+
// Prompt the user for a guess
51+
System.out.print("Enter a guess between 1 and 100: ");
52+
int guess = scanner.nextInt();
53+
54+
// Count this guess
55+
guesses ++;
56+
57+
// Check if the guess is correct
58+
if (guess == number) {
59+
System.out.println("CONGRATULATIONS YOU WIN! You guessed it in " + guesses + " guesses.");
60+
break;
61+
}
62+
// Too low
63+
else if (guess < number) {
64+
System.out.println("Too Low!");
65+
}
66+
// Too high
67+
else if (guess > number) {
68+
System.out.println("Too High!");
69+
}
70+
71+
// Check if the player has used all 10 guesses
72+
if (guesses >= 10) {
73+
System.out.println("You have run out of guesses! The number was " + number + ".");
74+
break;
75+
}
76+
77+
}
78+
scanner.close();
79+
}
80+
81+
1182
@Override
1283
public void add(PlayerInterface player) {
1384
// TODO: store the player
@@ -20,6 +91,16 @@ public void remove(PlayerInterface player) {
2091

2192
@Override
2293
public void run() {
23-
// TODO: implement the number guessing game loop
94+
if (guess == number) {
95+
System.out.println("CONGRATULATIONS YOU WIN! You guessed it in " + guesses + " guesses.");
96+
}
97+
// Too low
98+
else if (guess < number) {
99+
System.out.println("Too Low!");
100+
}
101+
// Too high
102+
else if (guess > number) {
103+
System.out.println("Too High!");
104+
} // TODO: implement the number guessing game loop
24105
}
25106
}

src/main/java/com/github/zipcodewilmington/casino/games/numberguess/NumberGuessPlayer.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,44 @@
22

33
import com.github.zipcodewilmington.casino.CasinoAccount;
44
import com.github.zipcodewilmington.casino.PlayerInterface;
5+
import java.util.Scanner;
56

6-
/**
7-
* Created by leon on 7/21/2020.
8-
*/
97
public class NumberGuessPlayer implements PlayerInterface {
108

11-
private CasinoAccount casinoAccount;
9+
private final CasinoAccount account; // Standardized name
10+
private final Scanner scanner;
1211

13-
public NumberGuessPlayer(CasinoAccount casinoAccount) {
14-
this.casinoAccount = casinoAccount;
12+
public NumberGuessPlayer(CasinoAccount account) {
13+
this.account = account;
14+
this.scanner = new Scanner(System.in);
1515
}
1616

17-
@Override
18-
public CasinoAccount getArcadeAccount() {
19-
return casinoAccount;
17+
/**
18+
* Logic for the player to input a guess.
19+
*/
20+
public int makeGuess() {
21+
System.out.print("Enter your guess (1–100): ");
22+
// Check if input is an integer to avoid crashes
23+
while (!scanner.hasNextInt()) {
24+
System.out.print("That's not a number! Try again: ");
25+
scanner.next();
26+
}
27+
return scanner.nextInt();
28+
}
29+
30+
public CasinoAccount getAracadeAccount() {
31+
// Changed from 'casinoAccount' to 'account' to match the field above
32+
return account;
2033
}
2134

2235
@Override
23-
public <SomeReturnType> SomeReturnType play() {
24-
return null; // TODO: implement guess logic
36+
public void play() {
37+
// Consolidated the two play() methods into one
38+
System.out.println("Starting Number Guess Game...");
39+
40+
// Example logic:
41+
int userGuess = makeGuess();
42+
System.out.println("You guessed: " + userGuess);
43+
2544
}
2645
}

0 commit comments

Comments
 (0)