-
Notifications
You must be signed in to change notification settings - Fork 904
Expand file tree
/
Copy pathBadCaseGame.java
More file actions
109 lines (96 loc) · 3.42 KB
/
BadCaseGame.java
File metadata and controls
109 lines (96 loc) · 3.42 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
package baseboll.myTrial.allInOne;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
public class BadCaseGame {
private final BufferedReader br;
public BadCaseGame(BufferedReader br) {
this.br = br;
}
public void game() throws IOException {
String answer = String.valueOf(makeRandomAnswer());
progressGame(answer);
}
private void progressGame(String answer) throws IOException {
String input = getInput(); // 사용자에게 입력값 받아오기
if (!isPossible(input)) progressGame(answer);
if (isCorrect(input, answer)) {
continueOrNot();
return;
}
countTotal(input, answer);
progressGame(answer);
}
private boolean isPossible(String input) {
return false;
}
private void countTotal(String input, String answer){
int strikeCount = strikeCountChecker(input, answer);
int ballCount = ballCountChecker(input, answer);
StringBuilder sb = new StringBuilder();
if (strikeCount == 0 & ballCount == 0) {
System.out.println("낫싱");
return;
}
if (strikeCount != 0) sb.append(strikeCount).append("스트라이크");
if (ballCount != 0) sb.append(ballCount).append("볼");
System.out.println(sb.toString());
}
private int makeRandomAnswer() {
List<Integer> digits = new ArrayList<>();
for (int i = 0; i < 10; i++) {
digits.add(i);
}
Collections.shuffle(digits);
int firstDigit = digits.get(0);
int secondDigit = digits.get(1);
int thirdDigit = digits.get(2);
return firstDigit * 100 + secondDigit * 10 + thirdDigit;
}
private String getInput() throws IOException {
System.out.println("숫자를 입력 해 주세요");
return br.readLine();
}
private void continueOrNot() throws IOException {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String input = br.readLine();
if (input.equals("1")) {
game();
return;
}
if (input.equals("2")) {
return;
}
System.out.println("잘못된 입력입니다.");
}
private int strikeCountChecker(String input, String answer){
String[] given = input.split("");
String[] ans = answer.split("");
int correctCount = 0;
for (int i = 0; i < given.length; i++) {
correctCount += strikeCheck(given[i], ans[i]);
}
return correctCount;
}
private int ballCountChecker(String input, String answer){
String[] given = input.split("");
Set<String> set = new HashSet<>(Arrays.asList(given));
int count = 0;
for (String s : set) {
count += ballCheck(s, answer);
}
int alreadyCountedStrike = strikeCountChecker(input, answer);
if (alreadyCountedStrike != 0) count -= alreadyCountedStrike;
return count;
}
private int strikeCheck(String s, String d){
return s.equals(d) ? 1 : 0;
}
private int ballCheck(String input, String answer){
return answer.contains(input) ? 1 : 0;
}
private boolean isCorrect(String input, String answer){
return input.equals(answer);
}
}