generated from OOP2020/lab1-start
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameConsole.java
54 lines (44 loc) · 1.33 KB
/
GameConsole.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Control the game by sending info back and foward to other files
*
* @author Teeranut Sawanyawat 6210545491
*/
public class GameConsole {
/**
* Play a guessing game and return the user guess back if it match with the
* secret number
*
* @param game is the GuessingName to solve
* @return the user guess
*
*/
public int play(GuessingGame game) {
Scanner input = new Scanner(System.in);
boolean result;
String message;
int user_guess = 0;
System.out.println(game.toString());
System.out.println(game.getMessage());
while (true) {
try {
System.out.print("your guess? ");
String str = input.nextLine();
user_guess = Integer.parseInt(str);
}
catch(NoSuchElementException e1){
System.exit(0);
}
catch(NumberFormatException e2) {
System.out.println("Please input an integer");
continue;
}
result = game.guess(user_guess);
message = game.getMessage();
if (result) return user_guess;
System.out.println(message);
}
}
}