Skip to content

Commit 537bb15

Browse files
CopilotProject516
andcommitted
Fix game logic issues: spelling, quit logic, version reading, GUI naming, scanner management
Co-authored-by: Project516 <138796702+Project516@users.noreply.github.com>
1 parent 64b237c commit 537bb15

5 files changed

Lines changed: 48 additions & 36 deletions

File tree

app/src/main/java/io/github/project516/NumberGuessingGame/CheckGuess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void check(int guess) {
2020
void quit(int input) {
2121
// 0 is quit
2222
// 1 is continue
23-
if (input != 0 || input != 1) {
23+
if (input != 0 && input != 1) {
2424
throw new IllegalArgumentException("Invalid number!");
2525
}
2626
}

app/src/main/java/io/github/project516/NumberGuessingGame/GUI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void createWindow() {
3333
* @param args command line arguments (not used)
3434
*/
3535
public static void main(String[] args) { // Test GUI
36-
GUI GUI = new GUI();
37-
GUI.createWindow();
36+
GUI gui = new GUI();
37+
gui.createWindow();
3838
}
3939
}

app/src/main/java/io/github/project516/NumberGuessingGame/GameLogic.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* input, and guess validation.
66
*/
77
public class GameLogic {
8-
// TODO port game logic from Main to GameLogic
98
/**
109
* Runs the main game loop. Generates a random number and prompts the user to guess it. Provides
1110
* feedback on each guess and tracks the number of attempts.
11+
*
12+
* @param scan the ScannerHelper instance to use for user input
1213
*/
13-
void game() {
14+
void game(ScannerHelper scan) {
1415

1516
RandomNumber ranNumber = new RandomNumber();
1617
int number = ranNumber.number(100);
1718
int numOfGuesses = 0;
18-
ScannerHelper scan = new ScannerHelper();
1919
CheckGuess check = new CheckGuess();
2020

2121
while (true) {
@@ -24,13 +24,12 @@ void game() {
2424
int guess = scan.userGuess();
2525
check.check(guess);
2626
if (guess > number) {
27-
System.out.println("You guessed to much!");
27+
System.out.println("You guessed too much!");
2828
} else if (guess < number) {
29-
System.out.println("You guessed to little!");
29+
System.out.println("You guessed too little!");
3030
} else {
3131
numOfGuesses++;
3232
System.out.println("Took you " + numOfGuesses + " guesses!");
33-
scan.close();
3433
break;
3534
}
3635
numOfGuesses++;

app/src/main/java/io/github/project516/NumberGuessingGame/NumberGuessingGame.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,38 @@ void run() {
88
GameLogic logic = new GameLogic();
99
CheckGuess check = new CheckGuess();
1010

11-
debugInfo.launchDebug();
12-
gameInfo.about();
13-
int play = 1;
14-
while (play != 0) {
11+
try {
12+
debugInfo.launchDebug();
13+
gameInfo.about();
14+
int play = 1;
15+
while (play != 0) {
1516

16-
try {
17-
18-
logic.game();
19-
20-
} catch (Exception e) {
17+
try {
2118

22-
debugInfo.gameCrash();
23-
e.printStackTrace();
19+
logic.game(scan);
2420

25-
} finally {
26-
System.out.print("\nPlay again? (1 -> Y / 0 -> N) ");
27-
try {
28-
play = scan.userGuess();
29-
check.quit(play);
3021
} catch (Exception e) {
31-
debugInfo.gameCrash();
22+
23+
System.err.println("\n===Program Crashed!===");
24+
System.err.println("Error: " + e.getMessage());
3225
e.printStackTrace();
33-
break;
26+
27+
} finally {
28+
System.out.print("\nPlay again? (1 -> Y / 0 -> N) ");
29+
try {
30+
play = scan.userGuess();
31+
check.quit(play);
32+
} catch (Exception e) {
33+
System.err.println("\n===Program Crashed!===");
34+
System.err.println("Error: " + e.getMessage());
35+
e.printStackTrace();
36+
break;
37+
}
3438
}
3539
}
40+
System.out.println("\nThank you for playing!");
41+
} finally {
42+
scan.close();
3643
}
3744
}
3845
}

app/src/main/java/io/github/project516/NumberGuessingGame/ReadVersionFile.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
package io.github.project516.NumberGuessingGame;
22

3+
import java.io.BufferedReader;
34
import java.io.IOException;
4-
import java.nio.file.Files;
5-
import java.nio.file.Paths;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
67

78
/**
89
* Reads version information for the Number Guessing Game. Currently returns a placeholder version
910
* string.
1011
*/
1112
public class ReadVersionFile {
12-
// TODO - make this read the file in the resources directory
1313
/**
14-
* Retrieves the current version of the game. Currently returns "rolling" as a placeholder.
14+
* Retrieves the current version of the game by reading from the version.txt resource file.
1515
*
16-
* @return the version string
16+
* @return the version string, or "rolling" if the file cannot be read
1717
*/
1818
public String readVersion() {
19-
String filePath = "version.txt";
2019
String content = "rolling"; // Placeholder version
21-
try {
22-
content = new String(Files.readAllBytes(Paths.get(filePath)));
20+
try (InputStream inputStream =
21+
getClass().getClassLoader().getResourceAsStream("version.txt");
22+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
23+
if (inputStream != null) {
24+
content = reader.readLine();
25+
if (content != null) {
26+
content = content.trim();
27+
}
28+
}
2329
} catch (IOException e) {
24-
System.err.println("Error reading file: " + e.getMessage());
30+
System.err.println("Error reading version file: " + e.getMessage());
2531
}
2632
return content;
2733
}

0 commit comments

Comments
 (0)