-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathBaseballGame.java
More file actions
40 lines (29 loc) · 1013 Bytes
/
BaseballGame.java
File metadata and controls
40 lines (29 loc) · 1013 Bytes
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
package domain;
public class BaseballGame {
private static final int NUMBER_LENGTH = 3;
private final String computerNumbers;
private GameResult currentGameResult;
public BaseballGame(String computerNumbers) {
this.computerNumbers = computerNumbers;
}
public GameResult compareNumbers(String userNumbers) {
currentGameResult = new GameResult();
for (int i = 0; i < NUMBER_LENGTH; i++) {
compareNumber(userNumbers.charAt(i), computerNumbers.charAt(i));
}
return currentGameResult;
}
private void compareNumber(char userNum, char computerNum) {
if (!computerNumbers.contains(String.valueOf(userNum))) {
return;
}
if (userNum == computerNum) {
currentGameResult.increaseStrikeCount();
return;
}
currentGameResult.increaseBallCount();
}
public boolean isEnd() {
return currentGameResult.getStrikeCount() == NUMBER_LENGTH;
}
}