質問編集履歴
2
出力結果追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,48 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
+
出力結果
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
CPU1 vs CPU2
|
34
|
+
|
35
|
+
グー vs グー 引分
|
36
|
+
|
37
|
+
チョキ vs チョキ 引分
|
38
|
+
|
39
|
+
パー vs パー 引分
|
40
|
+
|
41
|
+
チョキ vs パー 勝者 CPU1
|
42
|
+
|
43
|
+
グー vs パー 勝者 CPU2
|
44
|
+
|
45
|
+
チョキ vs パー 勝者 CPU1
|
46
|
+
|
47
|
+
パー vs パー 引分
|
48
|
+
|
49
|
+
チョキ vs グー 勝者 CPU2
|
50
|
+
|
51
|
+
チョキ vs グー 連勝 CPU2
|
52
|
+
|
53
|
+
パー vs パー 引分
|
54
|
+
|
55
|
+
パー vs チョキ 連勝 CPU2
|
56
|
+
|
57
|
+
チョキ vs パー 勝者 CPU1
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
最終勝者は CPU1
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
書き直したプログラム
|
70
|
+
|
29
71
|
```java
|
30
72
|
|
31
73
|
package _memo;
|
1
ご教示頂いたことを意識して書き直し
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,9 +14,17 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
|
17
|
+
-追記-
|
18
|
+
|
18
|
-
|
19
|
+
ご教示頂いたことを意識して書き直しました。
|
20
|
+
|
21
|
+
なるべく人や物や行動などのオブジェクト指向も意識したつもりです。
|
22
|
+
|
23
|
+
まだまだ改善するところはあるかと思いますが自分なりにはとても満足しています。
|
24
|
+
|
19
|
-
|
25
|
+
ありがとうございました。
|
26
|
+
|
27
|
+
|
20
28
|
|
21
29
|
```java
|
22
30
|
|
@@ -28,6 +36,330 @@
|
|
28
36
|
|
29
37
|
|
30
38
|
|
39
|
+
public class memo_qa147649 {
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
public static void main(String[] args) {
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
Player[] players = new Player[2];
|
48
|
+
|
49
|
+
players[0] = new Player("CPU1");
|
50
|
+
|
51
|
+
players[1] = new Player("CPU2");
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
System.out.printf("%n最終勝者は %s%n", game(players, 3).name);
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
private static class Player {
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
private String name;
|
68
|
+
|
69
|
+
private int wins;
|
70
|
+
|
71
|
+
private Hands hand;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
Player(String name) {
|
76
|
+
|
77
|
+
this.name = name;
|
78
|
+
|
79
|
+
this.wins = 0;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
private static Player game(Player[] players, int winCount) {
|
88
|
+
|
89
|
+
Player winner = null;
|
90
|
+
|
91
|
+
Player lastWinner = null;
|
92
|
+
|
93
|
+
Player theWinner = null;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
Hands[] hand = new Hands[2];
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
System.out.printf("%s vs %s%n", players[0].name, players[1].name);
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
while (theWinner == null) {
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
players[0].hand = Hands.choice();
|
110
|
+
|
111
|
+
players[1].hand = Hands.choice();
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
System.out.printf("%s vs %s ", players[0].hand.text, players[1].hand.text);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
WLD wld = rsp(players[0].hand, players[1].hand);
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
if (wld == WLD.DRAW) {
|
124
|
+
|
125
|
+
System.out.println(WLD.DRAW.text);
|
126
|
+
|
127
|
+
continue;
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
if (wld == WLD.WIN) {
|
134
|
+
|
135
|
+
winner = players[0];
|
136
|
+
|
137
|
+
} else
|
138
|
+
|
139
|
+
winner = players[1];
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
if (lastWinner == winner) {
|
144
|
+
|
145
|
+
lastWinner = winner;
|
146
|
+
|
147
|
+
System.out.printf("連勝 %s%n", winner.name);
|
148
|
+
|
149
|
+
} else {
|
150
|
+
|
151
|
+
winner.wins += 1;
|
152
|
+
|
153
|
+
lastWinner = winner;
|
154
|
+
|
155
|
+
System.out.printf("勝者 %s%n", winner.name);
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
if (winner.wins == winCount) {
|
162
|
+
|
163
|
+
theWinner = winner;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
return theWinner;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
private static WLD rsp(Hands hand1, Hands hand2) {
|
176
|
+
|
177
|
+
WLD wld = WLD.DRAW;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
if (hand1 != hand2) {
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
switch (hand1) {
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
// player1:グー
|
190
|
+
|
191
|
+
case ROCK:
|
192
|
+
|
193
|
+
if (hand2 == Hands.SCISSORS) {
|
194
|
+
|
195
|
+
wld = WLD.WIN;
|
196
|
+
|
197
|
+
} else {
|
198
|
+
|
199
|
+
wld = WLD.LOSE;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
break;
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
// player1:チョキ
|
208
|
+
|
209
|
+
case SCISSORS:
|
210
|
+
|
211
|
+
if (hand2 == Hands.PAPER) {
|
212
|
+
|
213
|
+
wld = WLD.WIN;
|
214
|
+
|
215
|
+
} else {
|
216
|
+
|
217
|
+
wld = WLD.LOSE;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
break;
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// player1:パー
|
226
|
+
|
227
|
+
case PAPER:
|
228
|
+
|
229
|
+
if (hand2 == Hands.ROCK) {
|
230
|
+
|
231
|
+
wld = WLD.WIN;
|
232
|
+
|
233
|
+
} else {
|
234
|
+
|
235
|
+
wld = WLD.LOSE;
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
break;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
return wld;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
private enum WLD {
|
252
|
+
|
253
|
+
DRAW(-1, "引分"), LOSE(0, "負け"), WIN(1, "勝ち");
|
254
|
+
|
255
|
+
private final int id;
|
256
|
+
|
257
|
+
private final String text;
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
private WLD(final int id, final String text) {
|
262
|
+
|
263
|
+
this.id = id;
|
264
|
+
|
265
|
+
this.text = text;
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
private enum Hands {
|
274
|
+
|
275
|
+
ROCK(0, "グー "), SCISSORS(1, "チョキ"), PAPER(2, "パー ");
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
private final int id;
|
280
|
+
|
281
|
+
private final String text;
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
private Hands(final int id, final String text) {
|
286
|
+
|
287
|
+
this.id = id;
|
288
|
+
|
289
|
+
this.text = text;
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
private static Hands choice() {
|
296
|
+
|
297
|
+
//r:1:グー s:2:チョ p:3:パー
|
298
|
+
|
299
|
+
Random rnd = new Random();
|
300
|
+
|
301
|
+
return getHands(rnd.nextInt(3));
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
private int getInt() {
|
308
|
+
|
309
|
+
return this.id;
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
private String getText() {
|
316
|
+
|
317
|
+
return this.text;
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
private static Hands getHands(final int id) {
|
324
|
+
|
325
|
+
Hands[] Handss = Hands.values();
|
326
|
+
|
327
|
+
for (Hands hand : Handss) {
|
328
|
+
|
329
|
+
if (hand.getInt() == id) {
|
330
|
+
|
331
|
+
return hand;
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
return null;
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
```
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
-以下初期プログラム-
|
352
|
+
|
353
|
+
```java
|
354
|
+
|
355
|
+
package _memo;
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
import java.util.Random;
|
360
|
+
|
361
|
+
|
362
|
+
|
31
363
|
import _memo.Enums.Jnk;
|
32
364
|
|
33
365
|
|