-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathApplication.java
More file actions
113 lines (104 loc) · 3.4 KB
/
Application.java
File metadata and controls
113 lines (104 loc) · 3.4 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//package baseball;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
System.out.println("숫자 야구 게임을 시작합니다.");
boolean newGame;
do {
newGame = new GameController().playGame();
} while (newGame);
}
}
class GameController {
public boolean playGame() {
List<Integer> comList = RandomNumberGenerator.generateRandomNumberList();
while (true) {
System.out.print("숫자를 입력해주세요: ");
int ball = 0;
int strike = 0;
int num = InputHandler.getInputNumber();
checkValid(num);
List<Integer> myList = NumberUtil.getInts(num);
for (int i = 0; i < 3; i++) {
if (!Objects.equals(myList.get(i), comList.get(i))) {
boolean contains = comList.contains(myList.get(i));
if (contains) {
ball++;
}
}
if (Objects.equals(myList.get(i), comList.get(i))) {
strike++;
}
}
GameOutput.printResult(ball, strike);
if (strike == 3) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
return InputHandler.askForNewGame();
}
}
}
private static void checkValid(int num) {
if (num < 100 || num > 999) {
throw new IllegalArgumentException("입력 범위를 초과했습니다.");
}
}
}
class RandomNumberGenerator {
public static List<Integer> generateRandomNumberList() {
Random random = new Random();
List<Integer> comList = new ArrayList<>();
while (comList.size() < 3) {
int randomNumber = random.nextInt(9) + 1;
if (!comList.contains(randomNumber)) {
comList.add(randomNumber);
}
}
return comList;
}
}
class InputHandler {
public static int getInputNumber() {
Scanner scanner = new Scanner(System.in);
int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환
return number;
}
public static boolean askForNewGame() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int value = getInputNumber();
if (value == 1) {
return true;
} else if (value == 2) {
return false;
} else {
throw new IllegalArgumentException("올바른 입력이 아닙니다.");
}
}
}
class GameOutput {
public static void printResult(int ball, int strike) {
if (ball == 0 && strike == 0) {
System.out.println("낫싱");
} else {
if (ball != 0) {
System.out.print(ball + "볼 ");
}
if (strike != 0) {
System.out.print(strike + "스트라이크");
}
System.out.println();
}
}
}
class NumberUtil {
public static List<Integer> getInts(int num) {
List<Integer> myList = new ArrayList<>();
myList.add(num / 100);
myList.add((num / 10) % 10);
myList.add(num % 10);
return myList;
}
}