-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLMW20221200_mid2.java
171 lines (141 loc) · 5.66 KB
/
LMW20221200_mid2.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package mid;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class LMW20221200_mid2 {
Scanner sc = new Scanner(System.in);
Random random = new Random();
String[] rps = {"가위", "바위", "보"}; // 가위 0, 바위 1, 보 2
String[] mjb = {"묵", "찌", "빠"}; // 묵 0, 찌 1, 빠 2
boolean Turn;
int userScore = 0;
int computerScore = 0;
int round = 1;
int userChoice;
int computerChoice;
public int userInput() {
while (true) {
String user = sc.nextLine();
userChoice = -1;
switch (user) {
case "가위":
case "묵":
case "0":
userChoice = 0;
break;
case "바위":
case "찌":
case "1":
userChoice = 1;
break;
case "보":
case "빠":
case "2":
userChoice = 2;
break;
default:
System.out.println("-".repeat(20) + "\n0 ~ 2 / 가위 ~ 보 / 묵 ~ 빠 입력.");
continue;
}
break;
}
return userChoice;
}
public void playGame() {
System.out.println("가위바위보를 시작합니다.");
while (true) {
try {
int computer = random.nextInt(3);
// System.out.println(computer);
System.out.print(">>> 0 ~ 2 또는 가위 ~ 보 입력 : ");
int user = userInput();
System.out.println(">>> 나 : " + rps[user] + " | 컴퓨터 : " + rps[computer]);
if (user == computer) {
System.out.println("비겼습니다.\n");
continue;
} else if ((user == 0 && computer == 1) || (user == 1 && computer == 2) || (user == 2 && computer == 0)) {
// user : 가위 || computer : 바위 user : 바위 || computer : 보 user : 보 || computer : 가위
System.out.println("졌습니다.\n");
Turn = false;
} else {
System.out.println("이겼습니다.\n");
Turn = true;
}
} catch (InputMismatchException e) { // 다른 타입을 입력했을 때 예외 처리
System.out.println(">>> 숫자를 입력해 주세요\n" + "-".repeat(20));
} catch (IndexOutOfBoundsException e) {
System.out.println(">>> 0 ~ 2 사이의 값을 입력해주세요\n" + "-".repeat(20));
}
break;
}
gameStart();
}
public void gameStart() {
while (true) {
System.out.println("===== ROUND " + round + " =====");
switchTurn(Turn);
if (Turn) {
computerChoice = (int) (Math.random() * 3);
// System.out.println(computerChoice);
System.out.print("묵(0), 찌(1), 빠(2) 중 하나를 선택하세요: ");
userChoice = userInput();
System.out.println(">>> 나 : " + mjb[userChoice] + " | 컴퓨터 : " + mjb[computerChoice]);
} else {
computerChoice = (int) (Math.random() * 3);
// System.out.println(computerChoice);
System.out.println("컴퓨터가 입력을 기다립니다.");
userChoice = userInput();
System.out.println(">>> 나 : " + mjb[userChoice] + " | 컴퓨터 : " + mjb[computerChoice]);
}
boolean userWin = checkWin(Turn, userChoice, computerChoice);
if (userWin) {
if (Turn && userChoice == computerChoice) {
System.out.println("사용자가 이겼습니다!");
userScore++;
}
} else {
if (!Turn && computerChoice == userChoice) {
System.out.println("컴퓨터가 이겼습니다!");
computerScore++;
}
}
System.out.println("현재 스코어 - 사용자: " + userScore + ", 컴퓨터: " + computerScore);
System.out.println();
Turn = !Turn;
if ((userScore == 3) || (computerScore == 3)) {
if (userScore > computerScore) {
System.out.println("최종 스코어 - 사용자: " + userScore + ", 컴퓨터: " + computerScore);
System.out.println("사용자가 최종 승리했습니다!");
} else if (userScore < computerScore) {
System.out.println("최종 스코어 - 사용자: " + userScore + ", 컴퓨터: " + computerScore);
System.out.println("컴퓨터가 최종 승리했습니다!");
}
break;
}
round++;
}
}
public static void switchTurn(boolean Turn) {
if (Turn) {
System.out.println("=== 사용자 턴 ===");
} else {
System.out.println("=== 컴퓨터 턴 ===");
}
}
public boolean checkWin(boolean Turn, int userChoice, int computerChoice) {
if (Turn) {
if (userChoice == computerChoice) {
return true;
}
} else {
if (computerChoice == userChoice) {
return false;
}
}
return Turn;
}
public static void main(String[] args) {
LMW20221200_mid2 lmw20221200Mid = new LMW20221200_mid2();
lmw20221200Mid.playGame();
}
}