-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathGame.java
More file actions
65 lines (50 loc) · 1.96 KB
/
Game.java
File metadata and controls
65 lines (50 loc) · 1.96 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
package model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Game {
private final Numbers correctNumbers;
public Game() {
this.correctNumbers = generateCorrectNumbers();
}
private Numbers generateCorrectNumbers() {
List<Integer> tempNumbers = new ArrayList<Integer>();
for (int i = 1; i <= 9; ++i) {
tempNumbers.add(i);
}
Collections.shuffle(tempNumbers);
return new Numbers(tempNumbers.subList(0, 3));
}
public GameResult getResult(Numbers userNumbers) {
int strike = correctNumbers.countStrike(userNumbers);
int ball = correctNumbers.countBall(userNumbers);
return new GameResult(strike, ball);
}
public int getCommand(String commandString) {
return validateAndParseInputString(commandString);
}
private int validateAndParseInputString(String commandString) {
if (!validateInputLength(commandString)) {
throw new IllegalArgumentException("1 또는 2만 입력해야 합니다.");
}
if (!validateInputIsNumber(commandString)) {
throw new IllegalArgumentException("1 또는 2만 입력해야 합니다.");
}
if (!validateInputIsOneOrTwo(commandString)) {
throw new IllegalArgumentException("1 또는 2만 입력해야 합니다.");
}
return convertStringToInteger(commandString);
}
private boolean validateInputLength(String commandString) {
return commandString.length() == 1;
}
private boolean validateInputIsNumber(String commandString) {
return commandString.charAt(0) >= '0' && commandString.charAt(0) <= '9';
}
private boolean validateInputIsOneOrTwo(String commandString) {
return commandString.charAt(0) == '1' || commandString.charAt(0) == '2';
}
private Integer convertStringToInteger(String commandString) {
return commandString.charAt(0) - '0';
}
}