-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (103 loc) · 3.87 KB
/
Main.java
File metadata and controls
118 lines (103 loc) · 3.87 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
114
115
116
117
118
import java.util.Random;
import java.util.Scanner;
public class Main {
public static final int DIGIT_COUNT = 3;
public static final int MIN_NUMBER = 1;
public static final int MAX_NUMBER = 9;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
int[] computerNumbers = generateComputerNumbers();
int[] userNumbers;
try {
userNumbers = getUserNumbers(scanner);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("게임을 종료합니다.");
scanner.close();
return;
}
while (!checkMatch(computerNumbers, userNumbers)) {
try {
userNumbers = getUserNumbers(scanner);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println("게임을 종료합니다.");
scanner.close();
return;
}
}
System.out.println("게임을 다시 시작하려면 1, 종료하려면 2를 입력하세요.");
int choice = scanner.nextInt();
if (choice != 1) {
break;
}
}
System.out.println("게임을 종료합니다.");
scanner.close();
}
public static int[] getUserNumbers(Scanner scanner) {
int[] numbers = new int[DIGIT_COUNT];
System.out.print("숫자를 입력해 주세요: ");
String input = scanner.next();
if (input.length() != DIGIT_COUNT) {
throw new IllegalArgumentException("숫자의 길이가 올바르지 않습니다.");
}
for (int i = 0; i < DIGIT_COUNT; i++) {
char ch = input.charAt(i);
if (!Character.isDigit(ch)) {
throw new IllegalArgumentException("숫자가 아닙니다.");
}
numbers[i] = Character.getNumericValue(ch);
if (numbers[i] < MIN_NUMBER || numbers[i] > MAX_NUMBER) {
throw new IllegalArgumentException("숫자가 범위를 벗어납니다.");
}
}
return numbers;
}
public static int[] generateComputerNumbers() {
int[] numbers = new int[DIGIT_COUNT];
Random random = new Random();
for (int i = 0; i < DIGIT_COUNT; i++) {
numbers[i] = random.nextInt(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER;
for (int j = 0; j < i; j++) {
if (numbers[i] == numbers[j]) {
i--;
break;
}
}
}
return numbers;
}
public static boolean checkMatch(int[] computerNumbers, int[] userNumbers) {
int strike = 0;
int ball = 0;
boolean match = false;
for (int i = 0; i < DIGIT_COUNT; i++) {
for (int j = 0; j < DIGIT_COUNT; j++) {
if (userNumbers[i] == computerNumbers[j]) {
match = true;
if (i == j) {
strike++;
} else {
ball++;
}
}
}
}
if (!match) {
System.out.println("낫싱");
} else if (strike == DIGIT_COUNT) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
return true;
} else {
System.out.printf("%d볼 %d스트라이크\n", ball, strike);
}
return false;
}
public static boolean gameRestartChoice(Scanner scanner) {
System.out.println("게임을 다시 시작하려면 1, 종료하려면 2를 입력하세요.");
int choice = scanner.nextInt();
return choice == 1;
}
}