Skip to content

Commit 8edf303

Browse files
authored
Merge pull request #64 from Project516/copilot/fix-game-logic-issues
Fix game logic issues: spelling, critical quit bug, resource management, and error handling
2 parents 19ecc63 + 0be2e5d commit 8edf303

7 files changed

Lines changed: 120 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
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.project516.NumberGuessingGame;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class CheckGuessTest {
8+
@Test
9+
void quitAcceptsZero() {
10+
CheckGuess check = new CheckGuess();
11+
assertDoesNotThrow(() -> check.quit(0), "quit should accept 0");
12+
}
13+
14+
@Test
15+
void quitAcceptsOne() {
16+
CheckGuess check = new CheckGuess();
17+
assertDoesNotThrow(() -> check.quit(1), "quit should accept 1");
18+
}
19+
20+
@Test
21+
void quitRejectsOtherNumbers() {
22+
CheckGuess check = new CheckGuess();
23+
assertThrows(
24+
IllegalArgumentException.class,
25+
() -> check.quit(2),
26+
"quit should reject numbers other than 0 or 1");
27+
assertThrows(
28+
IllegalArgumentException.class,
29+
() -> check.quit(-1),
30+
"quit should reject numbers other than 0 or 1");
31+
}
32+
33+
@Test
34+
void checkAcceptsValidGuess() {
35+
CheckGuess check = new CheckGuess();
36+
assertDoesNotThrow(() -> check.check(50), "check should accept valid guess");
37+
assertDoesNotThrow(() -> check.check(1), "check should accept 1");
38+
assertDoesNotThrow(() -> check.check(100), "check should accept 100");
39+
}
40+
41+
@Test
42+
void checkRejectsInvalidGuess() {
43+
CheckGuess check = new CheckGuess();
44+
assertThrows(IllegalArgumentException.class, () -> check.check(0), "check should reject 0");
45+
assertThrows(
46+
IllegalArgumentException.class,
47+
() -> check.check(102),
48+
"check should reject numbers > 101");
49+
}
50+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.project516.NumberGuessingGame;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ReadVersionFileTest {
8+
@Test
9+
void readVersionReturnsNonEmptyString() {
10+
ReadVersionFile versionFile = new ReadVersionFile();
11+
String version = versionFile.readVersion();
12+
assertNotNull(version, "Version should not be null");
13+
assertFalse(version.isEmpty(), "Version should not be empty");
14+
}
15+
16+
@Test
17+
void readVersionReturnsRolling() {
18+
ReadVersionFile versionFile = new ReadVersionFile();
19+
String version = versionFile.readVersion();
20+
assertEquals("rolling", version, "Version should be 'rolling'");
21+
}
22+
}

0 commit comments

Comments
 (0)