-
Notifications
You must be signed in to change notification settings - Fork 589
Expand file tree
/
Copy pathGame.java
More file actions
154 lines (131 loc) · 4.26 KB
/
Game.java
File metadata and controls
154 lines (131 loc) · 4.26 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package baseball;
import camp.nextstep.edu.missionutils.Randoms;
import static camp.nextstep.edu.missionutils.Console.readLine;
import java.util.ArrayList;
import java.util.List;
public class Game {
public List<Integer> GetUser(){
System.out.print("숫자를 입력해주세요 : ");
String inputString = readLine();
if(inputString.length()>=4){
throw new IllegalArgumentException();
}
int intValue;
try {
intValue = Integer.parseInt(inputString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
if (intValue <= 1 && intValue <= 9) {
throw new IllegalArgumentException();
}
List<Integer> user = new ArrayList<>();
for (int i=0; i<inputString.length(); i++) {
user.add(inputString.charAt(i)-'0');
}
return user;
}
public List<Integer> GetComputer(){
List<Integer> computer = new ArrayList<>();
while (computer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}
return computer;
}
public List<Integer> checkResult(List<Integer> user, List<Integer> computer){
List<Integer> result = new ArrayList<>();
List<Integer> tmp = new ArrayList<>();
for (int i=0; i<3; i++) {
tmp.add(computer.get(i));
}
int strike=0;
int ball=0;
//같은자리에 같은숫자 = S, 다른자리에 같은 숫자 = B;
for (int i=0; i<3; i++) {
if (user.get(i) == tmp.get(i)) {
strike += 1;
}
}
for (int i=0; i<3; i++) {
if (user.get(0) == tmp.get(i)) {
ball += 1;
tmp.set(i,0);
continue;
}
if (user.get(1) == tmp.get(i)) {
ball += 1;
tmp.set(i,0);
continue;
}
if (user.get(2) == tmp.get(i)) {
ball += 1;
tmp.set(i,0);
continue;
}
}
result.add(strike);
result.add(ball-strike);
return result;
}
public int printResult(List<Integer> result){
if(result.get(0) == 3){
System.out.println("3스트라이크");
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String exitcode = readLine();
int intValue;
try {
intValue = Integer.parseInt(exitcode);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
if (intValue != 1 && intValue != 2) {
throw new IllegalArgumentException();
}
if(intValue == 2){
return 2;
}
else{
return 3;
}
}
else if(result.get(0)==0 && result.get(1) == 0){
System.out.println("낫싱");
}
else if(result.get(0) !=0 && result.get(1) == 0){
System.out.printf("%d스트라이크",result.get(0));
System.out.println();
}
else if(result.get(0)==0 && result.get(1) != 0){
System.out.printf("%d볼",result.get(1));
System.out.println();
}
else{
System.out.printf("%d볼 %d스트라이크",result.get(0), result.get(1));
System.out.println();
}
return 1;
}
public void playGame(){
Game game = new Game();
List<Integer> computer = game.GetComputer();
List<Integer> user = game.GetUser();
int flag;
while(true){
flag = printResult(game.checkResult(user,computer));
if(flag==1){
user = game.GetUser();
}
else if(flag==3){
computer = game.GetComputer();
user = game.GetUser();
}
else{
break;
}
}
}
}