Skip to content

Commit cbcb11a

Browse files
committed
file io
1 parent eaf237f commit cbcb11a

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ void check(int guess) {
1616
throw new IllegalArgumentException("Invalid number!");
1717
}
1818
}
19+
20+
void quit(int input) {
21+
// 0 is quit
22+
// 1 is continue
23+
if (input != 0 || input != 1) {
24+
throw new IllegalArgumentException("Invalid number!");
25+
}
26+
}
1927
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void run() {
66
DebugInfo debugInfo = new DebugInfo();
77
GameInfo gameInfo = new GameInfo();
88
GameLogic logic = new GameLogic();
9+
CheckGuess check = new CheckGuess();
910

1011
debugInfo.launchDebug();
1112
gameInfo.about();
@@ -25,13 +26,10 @@ void run() {
2526
System.out.print("\nPlay again? (1 -> Y / 0 -> N) ");
2627
try {
2728
play = scan.userGuess();
28-
if (play == 0) {
29-
break;
30-
} else if (play != 1) {
31-
throw new IllegalArgumentException("");
32-
}
29+
check.quit(play);
3330
} catch (Exception e) {
34-
System.out.println("Error reading input!");
31+
debugInfo.gameCrash();
32+
e.printStackTrace();
3533
break;
3634
}
3735
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.project516.NumberGuessingGame;
22

3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
37
/**
48
* Reads version information for the Number Guessing Game. Currently returns a placeholder version
59
* string.
@@ -12,6 +16,13 @@ public class ReadVersionFile {
1216
* @return the version string
1317
*/
1418
public String readVersion() {
15-
return "rolling";
19+
String filePath = "version.txt";
20+
String content = "rolling"; // Placeholder version
21+
try {
22+
content = new String(Files.readAllBytes(Paths.get(filePath)));
23+
} catch (IOException e) {
24+
System.err.println("Error reading file: " + e.getMessage());
25+
}
26+
return content;
1627
}
1728
}

0 commit comments

Comments
 (0)