-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathGameController.java
More file actions
68 lines (57 loc) · 1.9 KB
/
GameController.java
File metadata and controls
68 lines (57 loc) · 1.9 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package controller;
import model.Referee;
import model.player.ComputerPlayer;
import model.player.NormalPlayer;
import view.InputView;
import view.OutputView;
public class GameController {
private final InputView inputView;
private final OutputView outputView;
public GameController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void start() {
while (true) {
NormalPlayer player = new NormalPlayer();
ComputerPlayer computerPlayer = new ComputerPlayer();
play(player, computerPlayer);
outputView.printFinishMessage();
int validResume = getValidResume();
if (validResume != 1) {
return;
}
}
}
private void play(NormalPlayer player, ComputerPlayer computerPlayer) {
computerPlayer.setRandomNumbers();
Referee referee = new Referee();
boolean gameOver = false;
while (!gameOver) {
player.updateNumbers(getValidUserNumbers());
int[] score = referee.judgeGameScore(player.getNumbers(), computerPlayer.getNumbers());
outputView.printResultMessage(score[0], score[1]);
gameOver = referee.judgeGameOver(score);
}
}
private int[] getValidUserNumbers() {
while (true) {
try {
outputView.printInputMessage();
return inputView.getUserNumbers();
} catch (Exception e) {
outputView.printlnMessage(e.getMessage());
}
}
}
private int getValidResume() {
while (true) {
try {
outputView.printResumeMessage();
return inputView.getResumeOption();
} catch (Exception e) {
outputView.printlnMessage(e.getMessage());
}
}
}
}