forked from ExplorerJun/java-baseball
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
62 lines (46 loc) · 1.74 KB
/
Game.java
File metadata and controls
62 lines (46 loc) · 1.74 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
import java.util.Scanner;
public class Game {
final private int STRIKE = 0;
final private int BALL = 1;
private Scanner sc;
private BallCombiGenerator generator;
private Referee referee;
public Game() {
sc = new Scanner(System.in);
generator = new BallCombiGenerator();
referee = new Referee();
}
public void play() {
int [] ansBallCombi = generator.genBallCombi();
for (int trial = 1; ; trial++) {
int [] userBallCombi;
System.out.printf("숫자를 입력해주세요: ");
String userBallCombiStr = sc.next(); //
userBallCombi = generator.toBallCombi(userBallCombiStr);
while (!generator.isValid(userBallCombi)) {
System.out.printf("잘못된 입력입니다. 숫자를 입력해주세요: ");
userBallCombiStr = sc.next(); //
userBallCombi = generator.toBallCombi(userBallCombiStr);
}
int [] judgement = referee.judge(ansBallCombi, userBallCombi);
if (judgement[STRIKE] == 3) {
System.out.printf("3개의 숫자를 모두 맞히셨습니다! 게임 종료 (%d 시도)\n", trial);
break;
}
printJudgement(judgement);
}
}
private void printJudgement(int [] judgement) {
if (judgement[STRIKE] == 0 && judgement[BALL] == 0) {
System.out.println("낫싱");
}
String str = "";
if (0 < judgement[STRIKE]) {
str += String.format("%d 스트라이크 ", judgement[STRIKE]);
}
if (0 < judgement[BALL]) {
str += String.format("%d 볼", judgement[BALL]);
}
System.out.println(str);
}
}