-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathCompareNumbers.java
More file actions
67 lines (53 loc) · 1.86 KB
/
Copy pathCompareNumbers.java
File metadata and controls
67 lines (53 loc) · 1.86 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
package baseball;
import java.util.List;
public class CompareNumbers {
private boolean gameOver;
public void compareNumbers(Player player, Computer computer) {
List<Integer> randomNumberList = computer.getRandomNumberList();
player.playerInputNumber(); // 플레이어가 숫자를 입력
List<Integer> playerNumberList = player.getInputNumberList();
int strikes = calculateStrikes(randomNumberList, playerNumberList);
int balls = calculateBalls(randomNumberList, playerNumberList);
// 결과 출력
if (strikes == 0 && balls == 0) {
System.out.println("낫싱");
} else if (strikes > 0 && balls > 0) {
System.out.println(balls + "볼 " + strikes + "스트라이크");
} else if (strikes > 0) {
System.out.println(strikes + "스트라이크");
} else {
System.out.println(balls + "볼");
}
// 3 스트라이크인 경우 게임 종료
if (strikes == 3) {
printGameOverMessage();
gameOver = true;
} else {
gameOver = false;
}
}
private int calculateStrikes(List<Integer> randomNumberList, List<Integer> playerNumberList) {
int strikes = 0;
for (int i = 0; i < randomNumberList.size(); i++) {
if (randomNumberList.get(i).equals(playerNumberList.get(i))) {
strikes++;
}
}
return strikes;
}
private int calculateBalls(List<Integer> randomNumberList, List<Integer> playerNumberList) {
int balls = 0;
for (int i = 0; i < randomNumberList.size(); i++) {
if (randomNumberList.contains(playerNumberList.get(i)) && !randomNumberList.get(i).equals(playerNumberList.get(i))) {
balls++;
}
}
return balls;
}
public boolean isGameOver() { // 게임 오버 여부
return gameOver;
}
private void printGameOverMessage() {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
}
}