Skip to content

Commit ada9148

Browse files
committed
play again option
1 parent f0eece3 commit ada9148

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,7 @@ public class Main {
1313
* @param args command line arguments (not currently used)
1414
*/
1515
public static void main(String[] args) {
16-
17-
ScannerHelper scan = new ScannerHelper();
18-
DebugInfo debugInfo = new DebugInfo();
19-
GameInfo gameInfo = new GameInfo();
20-
GameLogic logic = new GameLogic();
21-
22-
debugInfo.launchDebug();
23-
24-
try {
25-
26-
gameInfo.about();
27-
logic.game();
28-
29-
} catch (Exception e) {
30-
31-
debugInfo.gameCrash();
32-
e.printStackTrace();
33-
34-
} finally {
35-
36-
scan.close();
37-
}
16+
NumberGuessingGame game = new NumberGuessingGame();
17+
game.run();
3818
}
3919
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.github.project516.NumberGuessingGame;
2+
3+
public class NumberGuessingGame {
4+
void run() {
5+
ScannerHelper scan = new ScannerHelper();
6+
DebugInfo debugInfo = new DebugInfo();
7+
GameInfo gameInfo = new GameInfo();
8+
GameLogic logic = new GameLogic();
9+
10+
debugInfo.launchDebug();
11+
gameInfo.about();
12+
int play = 1;
13+
while (play != 0) {
14+
15+
try {
16+
17+
logic.game();
18+
19+
} catch (Exception e) {
20+
21+
debugInfo.gameCrash();
22+
e.printStackTrace();
23+
24+
} finally {
25+
System.out.print("\nPlay again? (1 -> Y / 0 -> N) ");
26+
try {
27+
play = scan.userGuess();
28+
if (play == 0) {
29+
break;
30+
} else if (play != 1) {
31+
throw new IllegalArgumentException("");
32+
}
33+
} catch (Exception e) {
34+
System.out.println("Error reading input!");
35+
break;
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)