回答編集履歴

5

追記

2018/04/26 05:29

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -636,9 +636,9 @@
636
636
 
637
637
  テストメソッドの結果がtrueなら、J,Q,Kのスコア算出に関しては頭の意識外にほぼ置いて置けるのです。
638
638
 
639
- 残りのスコア計算で必要なのはaceカードかそうでないかを判定して処理を記述するだけです。
639
+ 残りのスコア計算で必要なのはaceカードかそうでないかを判定して処理を記述するだけです。そしてこのコードに関してはキーボード入力は必要ないのです。
640
-
640
+
641
- int型やString型を変数に使うのも一つの手ですが、クラス化してテストしやすさを意識してみるといいかもです。
641
+ `int`型や`String`型を変数に使うのも一つの手ですが、クラス化して**テストしやすさ**を意識してみるといいかもです。
642
642
 
643
643
 
644
644
 

4

追記

2018/04/26 05:29

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -566,8 +566,102 @@
566
566
 
567
567
  }
568
568
 
569
-
570
-
571
-
572
-
573
569
  ```
570
+
571
+
572
+
573
+ ---
574
+
575
+
576
+
577
+ > 実際の処理はできるだけMain関数には書かず自分でつくったクラス、メソッドの中に
578
+
579
+ > 書いたほうがいいのでしょうか?
580
+
581
+
582
+
583
+ コードの規模とコーディングルールとかによって変わりますが。
584
+
585
+ **ぶっちゃけて言うとteratailでよくある質問内容なら、mainに全部書いても動くことは動きます。**
586
+
587
+
588
+
589
+ 私が`main`メソッドに書かない理由は以下の2点です。
590
+
591
+ 0. `main`メソッドが`static`メソッドである点
592
+
593
+ →このため他のメソッドを呼び出す時に`static`宣言しないといけない
594
+
595
+ 0. 一つのメソッドが長いと依存が増えてテストしずらい。
596
+
597
+ →テスト容易性は大事です。今回のコードでキーボードからの外部入力が必要な部分は**Playerクラスのtrunメソッドだけ**です。他のクラスはキーボードからの外部入力は不要です。でもmainに全部記述すると、
598
+
599
+ いちいちキーボード入力を行う必要があります。
600
+
601
+ ※ここは後ほど具体的なコードで説明します。
602
+
603
+
604
+
605
+ クラス分けに関しては最初はアクター:役者(もしくは主語)をクラス化してみてどうでしょうか、今回の場合だと、以下のようになります。
606
+
607
+ |アクター|クラス|
608
+
609
+ |:--|:--:|
610
+
611
+ |ゲームマスター|GameMaster|
612
+
613
+ |プレイヤー|Player|
614
+
615
+ |ディラー|Dealer|
616
+
617
+ |カード|Card|
618
+
619
+ |カードデッキ|CardDeck|
620
+
621
+
622
+
623
+ 話を戻しますが、例えばブラックジャックのスコア計算を行いたい場合
624
+
625
+
626
+
627
+ 0. カードの11:J, 12:Q, 13:Kはスコアを10として扱う。
628
+
629
+ 0. Aceカードは1または11として扱う。
630
+
631
+ 0. 残りのカードは数字(Rank)をスコアとして加算する。
632
+
633
+
634
+
635
+ このスコア部分をテストしたい場合、回答文のコードなら以下のようなテストコードを作れます。
636
+
637
+ テストメソッドの結果がtrueなら、J,Q,Kのスコア算出に関しては頭の意識外にほぼ置いて置けるのです。
638
+
639
+ 残りのスコア計算で必要なのはaceカードかそうでないかを判定して処理を記述するだけです。
640
+
641
+ int型やString型を変数に使うのも一つの手ですが、クラス化してテストしやすさを意識してみるといいかもです。
642
+
643
+
644
+
645
+ ```Java
646
+
647
+ public static boolean testCardScore_JQK() {
648
+
649
+
650
+
651
+ Card[] jqk = { new Card("\u2660", 11), new Card("\u2660", 12), new Card("\u2660", 13) };
652
+
653
+ for (Card c : jqk) {
654
+
655
+ if (c.calcScore() != 10) {
656
+
657
+ return false;
658
+
659
+ }
660
+
661
+ }
662
+
663
+ return true;
664
+
665
+ }
666
+
667
+ ```

3

追記

2018/04/26 05:20

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -10,572 +10,564 @@
10
10
 
11
11
  ※)元の質問文の要件にはないですが、柄(Suit)対応も行いました。
12
12
 
13
- ポイントはcard_listからのカードを配る(deal)事をCardDeckクラスを新設して管理している点でしょうか。こうすることで、dealメソッドが正しく動作すれば、他のクラスではcard_listの状態を意識しなくても良くなります。
13
+ ポイントは`card_list`からのカードを配る(`deal`)事を`CardDeck`クラスを新設して管理している点でしょうか。こうすることで、`CardDeck#deal`メソッドが正しく動作すれば、他のクラスでは`card_list`内部状態を意識しなくても良くなります。
14
+
15
+
14
16
 
15
17
  ```Java
16
18
 
19
+ import java.util.ArrayDeque;
20
+
21
+ import java.util.ArrayList;
22
+
23
+ import java.util.Arrays;
24
+
25
+ import java.util.Collections;
26
+
27
+ import java.util.Deque;
28
+
29
+ import java.util.List;
30
+
31
+ import java.util.Objects;
32
+
33
+ import java.util.Random;
34
+
35
+ import java.util.Scanner;
36
+
37
+ import java.util.stream.Collectors;
38
+
39
+ import java.util.stream.IntStream;
40
+
41
+
42
+
43
+ public class A122097 {
44
+
45
+ public static void main(String[] args) {
46
+
47
+ GameMaster game = new GameMaster();
48
+
49
+ game.start();
50
+
51
+ game.showResult();
52
+
53
+ }
54
+
55
+ }
56
+
57
+
58
+
59
+ class InputUtils {
60
+
61
+ private static Scanner sc = new Scanner(System.in);
62
+
63
+
64
+
65
+ public static int readInt() {
66
+
67
+ while (true) {
68
+
69
+ try {
70
+
71
+ return Integer.parseInt(sc.nextLine());
72
+
73
+ } catch (NumberFormatException ex) {
74
+
75
+ System.err.println(ex);
76
+
77
+ }
78
+
79
+ }
80
+
81
+ }
82
+
83
+ }
84
+
85
+
86
+
87
+ class GameMaster {
88
+
89
+ private static final CardDeck deck = new CardDeck();
90
+
91
+ private final Player dealer = new Dealer("ディラー");
92
+
93
+ private final List<Player> players = Collections.unmodifiableList(Arrays.asList(new Player("あなた"), dealer));
94
+
95
+
96
+
97
+ enum Judge {
98
+
99
+ Win("勝ちました!!"), Draw("引き分け"), Lose("負けました.....");
100
+
101
+
102
+
103
+ private final String label;
104
+
105
+
106
+
107
+ Judge(String label) {
108
+
109
+ this.label = label;
110
+
111
+ }
112
+
113
+
114
+
115
+ @Override
116
+
117
+ public String toString() {
118
+
119
+ return this.label;
120
+
121
+ }
122
+
123
+ }
124
+
125
+
126
+
127
+ public void start() {
128
+
129
+ // 参加者に2枚ずつカードを配る。
130
+
131
+ IntStream.range(0, 2).forEach((i) -> {
132
+
133
+ for (Player p : players) {
134
+
135
+ p.deal();
136
+
137
+ }
138
+
139
+ });
140
+
141
+ // ディラーのカードを表示
142
+
143
+ System.out.println(dealer);
144
+
145
+
146
+
147
+ while (true) {
148
+
149
+ int skiped = players.stream().mapToInt(Player::trun).sum();
150
+
151
+ // 参加者全員スキップなら
152
+
153
+ if (skiped == 0) {
154
+
155
+ break;
156
+
157
+ }
158
+
159
+ }
160
+
161
+ }
162
+
163
+
164
+
165
+ public static Card deal() {
166
+
167
+ return deck.deal();
168
+
169
+ }
170
+
171
+
172
+
173
+ public Judge judge(Player player, Player dealer) {
174
+
175
+ int compared = player.compareTo(dealer);
176
+
177
+ if (compared == 0) {
178
+
179
+ return Judge.Draw;
180
+
181
+ }
182
+
183
+ if (compared < 0) {
184
+
185
+ return Judge.Lose;
186
+
187
+ }
188
+
189
+ return Judge.Win;
190
+
191
+ }
192
+
193
+
194
+
195
+ public void showResult() {
196
+
197
+ System.out.println(String.join("", Collections.nCopies(40, "#")));
198
+
199
+ System.out.println("最終結果");
200
+
201
+ for (Player p : players) {
202
+
203
+ System.out.print(p);
204
+
205
+ System.out.println(" 合計:" + p.calcScore());
206
+
207
+ }
208
+
209
+ // プレイヤーとディラーの勝敗判定
210
+
211
+ Judge judge = judge(players.get(0), dealer);
212
+
213
+ System.out.println(judge);
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
221
+ class Player implements Comparable<Player> {
222
+
223
+ private static final int BLACK_JACK = 21;
224
+
225
+ // 手札
226
+
227
+ private final List<Card> myCardList = new ArrayList<>();
228
+
229
+ private final String name;
230
+
231
+ // 変数:user_stopと変数:dealer_stopはここ
232
+
233
+ protected boolean skiped = false;
234
+
235
+
236
+
237
+ public Player(String name) {
238
+
239
+ this.name = name;
240
+
241
+ }
242
+
243
+
244
+
245
+ public String getName() {
246
+
247
+ return this.name;
248
+
249
+ }
250
+
251
+
252
+
253
+ public void deal() {
254
+
255
+ // Player#Draw_Cardはここ
256
+
257
+ // 山から一枚カードを引いて手札に加える。
258
+
259
+ Card c = GameMaster.deal();
260
+
261
+ myCardList.add(c);
262
+
263
+ }
264
+
265
+
266
+
267
+ public boolean isSkip() {
268
+
269
+ return skiped || isBusting();
270
+
271
+ }
272
+
273
+
274
+
275
+ public int trun() {
276
+
277
+ if (isSkip()) {
278
+
279
+ return 0;
280
+
281
+ }
282
+
283
+ System.out.println(this.toString());
284
+
285
+ System.out.println("カードを引きますか?");
286
+
287
+ System.out.print("yes -> 1 no -> 0 を入力");
288
+
289
+ int n = 0;
290
+
291
+ do {
292
+
293
+ n = InputUtils.readInt();
294
+
295
+ if (n == 0) {
296
+
297
+ skiped = true;
298
+
299
+ return 0;
300
+
301
+ }
302
+
303
+ } while (n != 1);
304
+
305
+
306
+
307
+ this.deal();
308
+
309
+ return 1;
310
+
311
+ }
312
+
313
+
314
+
315
+ public boolean isBusting() {
316
+
317
+ // Player#Judge_burstはここ
318
+
319
+ return calcScore() > BLACK_JACK;
320
+
321
+ }
322
+
323
+
324
+
325
+ public int calcScore() {
326
+
327
+ // Player#calc_sumはここ
328
+
329
+ int aces_count = 0;
330
+
331
+ int sum = 0;
332
+
333
+ for (Card c : this.myCardList) {
334
+
335
+ int rank = c.calcScore();
336
+
337
+ if (rank == 1) {
338
+
339
+ aces_count++;
340
+
341
+ continue;
342
+
343
+ }
344
+
345
+ sum += rank;
346
+
347
+ }
348
+
349
+ if (aces_count == 0) {
350
+
351
+ return sum;
352
+
353
+ }
354
+
355
+ // Aceカードのスコア計算
356
+
357
+ int aces_sum = 11 + (aces_count - 1);
358
+
359
+ if (aces_sum + sum > BLACK_JACK) {
360
+
361
+ aces_sum = aces_count;
362
+
363
+ }
364
+
365
+ return sum + aces_sum;
366
+
367
+ }
368
+
369
+
370
+
371
+ @Override
372
+
373
+ public int compareTo(Player o) {
374
+
375
+ if (this.isBusting()) {
376
+
377
+ return -1;
378
+
379
+ }
380
+
381
+ // ディラー
382
+
383
+ if (o.isBusting()) {
384
+
385
+ return 1;
386
+
387
+ }
388
+
389
+ return Integer.compare(this.calcScore(), o.calcScore());
390
+
391
+ }
392
+
393
+
394
+
395
+ @Override
396
+
397
+ public String toString() {
398
+
399
+ // Player#Show_Listはここ
400
+
401
+ return name + "が持っているカード:" + myCardList.stream().map(String::valueOf).collect(Collectors.joining(" "));
402
+
403
+ }
404
+
405
+ }
406
+
407
+
408
+
409
+ class Dealer extends Player {
410
+
411
+ public Dealer(String name) {
412
+
413
+ super(name);
414
+
415
+ }
416
+
417
+
418
+
419
+ @Override
420
+
421
+ public int trun() {
422
+
423
+ if (isSkip()) {
424
+
425
+ return 0;
426
+
427
+ }
428
+
429
+ /// soft 17 rule
430
+
431
+ int score = this.calcScore();
432
+
433
+ if (score >= 17) {
434
+
435
+ System.out.println(this.getName() + "はカードを引きません。");
436
+
437
+ skiped = true;
438
+
439
+ return 0;
440
+
441
+ }
442
+
443
+ System.out.println(this.getName() + "はカードを引きます。");
444
+
445
+ this.deal();
446
+
447
+ return 1;
448
+
449
+ }
450
+
451
+ }
452
+
453
+
454
+
455
+ class Card {
456
+
457
+ // BLACK SPADE, BLACK CLUB, WHITE DIAMOND, WHITE HEART
458
+
459
+ public static final List<String> SUITS = Collections
460
+
461
+ .unmodifiableList(Arrays.asList("\u2660", "\u2663", "\u2662", "\u2661"));
462
+
463
+ public static final List<String> RANKS = Collections
464
+
465
+ .unmodifiableList(Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"));
466
+
467
+
468
+
469
+ private final String suit;
470
+
471
+ private final int rank;
472
+
473
+
474
+
475
+ public Card(String suit, int rank) {
476
+
477
+ Objects.requireNonNull(suit);
478
+
479
+ if (rank > RANKS.size()) {
480
+
481
+ throw new IllegalArgumentException(Integer.toString(rank));
482
+
483
+ }
484
+
485
+ this.suit = suit;
486
+
487
+ this.rank = rank;
488
+
489
+ }
490
+
491
+
492
+
493
+ public int calcScore() {
494
+
495
+ return Math.min(this.rank, 10);
496
+
497
+ }
498
+
499
+
500
+
501
+ @Override
502
+
503
+ public String toString() {
504
+
505
+ // Player#card_nameはここ
506
+
507
+ return this.suit + RANKS.get(this.rank - 1);
508
+
509
+ }
510
+
511
+ }
512
+
513
+
514
+
515
+ class CardDeck {
516
+
517
+ // Sum#mainの変数:card_list はここ
518
+
519
+ private final Deque<Card> cards;
520
+
521
+
522
+
523
+ public CardDeck() {
524
+
525
+ List<Card> card_list = new ArrayList<>(Card.RANKS.size() * Card.SUITS.size());
526
+
527
+ IntStream.rangeClosed(1, Card.RANKS.size()).forEach((i) -> {
528
+
529
+ for (String suit : Card.SUITS) {
530
+
531
+ card_list.add(new Card(suit, i));
532
+
533
+ }
534
+
535
+ });
536
+
17
- int []card_list = new int[13];
537
+ Random rnd = new Random();
538
+
539
+ // Random#setSeedでshuffleの実行結果を固定化(テスト用)
540
+
541
+ // rnd.setSeed(42);
542
+
543
+ Collections.shuffle(card_list, rnd);
544
+
545
+ cards = new ArrayDeque<>(card_list);
546
+
547
+ }
548
+
549
+
550
+
551
+ public Card deal() {
552
+
553
+ return cards.removeFirst();
554
+
555
+ }
556
+
557
+
558
+
559
+ @Override
560
+
561
+ public String toString() {
562
+
563
+ return Arrays.toString(cards.toArray(new Card[0]));
564
+
565
+ }
566
+
567
+ }
568
+
569
+
570
+
571
+
18
572
 
19
573
  ```
20
-
21
-
22
-
23
-
24
-
25
- ```Java
26
-
27
- import java.util.ArrayDeque;
28
-
29
- import java.util.ArrayList;
30
-
31
- import java.util.Arrays;
32
-
33
- import java.util.Collections;
34
-
35
- import java.util.Deque;
36
-
37
- import java.util.List;
38
-
39
- import java.util.Objects;
40
-
41
- import java.util.Random;
42
-
43
- import java.util.Scanner;
44
-
45
- import java.util.stream.Collectors;
46
-
47
- import java.util.stream.IntStream;
48
-
49
-
50
-
51
- public class A122097 {
52
-
53
- public static void main(String[] args) {
54
-
55
- GameMaster game = new GameMaster();
56
-
57
- game.start();
58
-
59
- game.showResult();
60
-
61
- }
62
-
63
- }
64
-
65
-
66
-
67
- class InputUtils {
68
-
69
- private static Scanner sc = new Scanner(System.in);
70
-
71
-
72
-
73
- public static int readInt() {
74
-
75
- while (true) {
76
-
77
- try {
78
-
79
- return Integer.parseInt(sc.nextLine());
80
-
81
- } catch (NumberFormatException ex) {
82
-
83
- System.err.println(ex);
84
-
85
- }
86
-
87
- }
88
-
89
- }
90
-
91
- }
92
-
93
-
94
-
95
- class GameMaster {
96
-
97
- private static final CardDeck deck = new CardDeck();
98
-
99
- private final Player dealer = new Dealer("ディラー");
100
-
101
- private final List<Player> players = Collections.unmodifiableList(Arrays.asList(new Player("あなた"), dealer));
102
-
103
-
104
-
105
- enum Judge {
106
-
107
- Win("勝ちました!!"), Draw("引き分け"), Lose("負けました.....");
108
-
109
-
110
-
111
- private final String label;
112
-
113
-
114
-
115
- Judge(String label) {
116
-
117
- this.label = label;
118
-
119
- }
120
-
121
-
122
-
123
- @Override
124
-
125
- public String toString() {
126
-
127
- return this.label;
128
-
129
- }
130
-
131
- }
132
-
133
-
134
-
135
- public void start() {
136
-
137
- // 参加者に2枚ずつカードを配る。
138
-
139
- IntStream.range(0, 2).forEach((i) -> {
140
-
141
- for (Player p : players) {
142
-
143
- p.deal();
144
-
145
- }
146
-
147
- });
148
-
149
- // ディラーのカードを表示
150
-
151
- System.out.println(dealer);
152
-
153
-
154
-
155
- while (true) {
156
-
157
- int skiped = players.stream().mapToInt(Player::trun).sum();
158
-
159
- // 参加者全員スキップなら
160
-
161
- if (skiped == 0) {
162
-
163
- break;
164
-
165
- }
166
-
167
- }
168
-
169
- }
170
-
171
-
172
-
173
- public static Card deal() {
174
-
175
- return deck.deal();
176
-
177
- }
178
-
179
-
180
-
181
- public Judge judge(Player player, Player dealer) {
182
-
183
- int compared = player.compareTo(dealer);
184
-
185
- if (compared == 0) {
186
-
187
- return Judge.Draw;
188
-
189
- }
190
-
191
- if (compared < 0) {
192
-
193
- return Judge.Lose;
194
-
195
- }
196
-
197
- return Judge.Win;
198
-
199
- }
200
-
201
-
202
-
203
- public void showResult() {
204
-
205
- System.out.println(String.join("", Collections.nCopies(40, "#")));
206
-
207
- System.out.println("最終結果");
208
-
209
- for (Player p : players) {
210
-
211
- System.out.print(p);
212
-
213
- System.out.println(" 合計:" + p.calcScore());
214
-
215
- }
216
-
217
- // プレイヤーとディラーの勝敗判定
218
-
219
- Judge judge = judge(players.get(0), dealer);
220
-
221
- System.out.println(judge);
222
-
223
- }
224
-
225
- }
226
-
227
-
228
-
229
- class Player implements Comparable<Player> {
230
-
231
- private static final int BLACK_JACK = 21;
232
-
233
- // 手札
234
-
235
- private final List<Card> myCardList = new ArrayList<>();
236
-
237
- private final String name;
238
-
239
- // 変数:user_stopと変数:dealer_stopはここ
240
-
241
- protected boolean skiped = false;
242
-
243
-
244
-
245
- public Player(String name) {
246
-
247
- this.name = name;
248
-
249
- }
250
-
251
-
252
-
253
- public String getName() {
254
-
255
- return this.name;
256
-
257
- }
258
-
259
-
260
-
261
- public void deal() {
262
-
263
- // Player#Draw_Cardはここ
264
-
265
- // 山から一枚カードを引いて手札に加える。
266
-
267
- Card c = GameMaster.deal();
268
-
269
- myCardList.add(c);
270
-
271
- }
272
-
273
-
274
-
275
- public boolean isSkip() {
276
-
277
- return skiped || isBusting();
278
-
279
- }
280
-
281
-
282
-
283
- public int trun() {
284
-
285
- if (isSkip()) {
286
-
287
- return 0;
288
-
289
- }
290
-
291
- System.out.println(this.toString());
292
-
293
- System.out.println("カードを引きますか?");
294
-
295
- System.out.print("yes -> 1 no -> 0 を入力");
296
-
297
- int n = 0;
298
-
299
- do {
300
-
301
- n = InputUtils.readInt();
302
-
303
- if (n == 0) {
304
-
305
- skiped = true;
306
-
307
- return 0;
308
-
309
- }
310
-
311
- } while (n != 1);
312
-
313
-
314
-
315
- this.deal();
316
-
317
- return 1;
318
-
319
- }
320
-
321
-
322
-
323
- public boolean isBusting() {
324
-
325
- // Player#Judge_burstはここ
326
-
327
- return calcScore() > BLACK_JACK;
328
-
329
- }
330
-
331
-
332
-
333
- public int calcScore() {
334
-
335
- // Player#calc_sumはここ
336
-
337
- int aces_count = 0;
338
-
339
- int sum = 0;
340
-
341
- for (Card c : this.myCardList) {
342
-
343
- int rank = c.calcScore();
344
-
345
- if (rank == 1) {
346
-
347
- aces_count++;
348
-
349
- continue;
350
-
351
- }
352
-
353
- sum += rank;
354
-
355
- }
356
-
357
- if (aces_count == 0) {
358
-
359
- return sum;
360
-
361
- }
362
-
363
- // Aceカードのスコア計算
364
-
365
- int aces_sum = 11 + (aces_count - 1);
366
-
367
- if (aces_sum + sum > BLACK_JACK) {
368
-
369
- aces_sum = aces_count;
370
-
371
- }
372
-
373
- return sum + aces_sum;
374
-
375
- }
376
-
377
-
378
-
379
- @Override
380
-
381
- public int compareTo(Player o) {
382
-
383
- if (this.isBusting()) {
384
-
385
- return -1;
386
-
387
- }
388
-
389
- // ディラー
390
-
391
- if (o.isBusting()) {
392
-
393
- return 1;
394
-
395
- }
396
-
397
- return Integer.compare(this.calcScore(), o.calcScore());
398
-
399
- }
400
-
401
-
402
-
403
- @Override
404
-
405
- public String toString() {
406
-
407
- // Player#Show_Listはここ
408
-
409
- return name + "が持っているカード:" + myCardList.stream().map(String::valueOf).collect(Collectors.joining(" "));
410
-
411
- }
412
-
413
- }
414
-
415
-
416
-
417
- class Dealer extends Player {
418
-
419
- public Dealer(String name) {
420
-
421
- super(name);
422
-
423
- }
424
-
425
-
426
-
427
- @Override
428
-
429
- public int trun() {
430
-
431
- if (isSkip()) {
432
-
433
- return 0;
434
-
435
- }
436
-
437
- /// soft 17 rule
438
-
439
- int score = this.calcScore();
440
-
441
- if (score >= 17) {
442
-
443
- System.out.println(this.getName() + "はカードを引きません。");
444
-
445
- skiped = true;
446
-
447
- return 0;
448
-
449
- }
450
-
451
- System.out.println(this.getName() + "はカードを引きます。");
452
-
453
- this.deal();
454
-
455
- return 1;
456
-
457
- }
458
-
459
- }
460
-
461
-
462
-
463
- class Card {
464
-
465
- // BLACK SPADE, BLACK CLUB, WHITE DIAMOND, WHITE HEART
466
-
467
- public static final List<String> SUITS = Collections
468
-
469
- .unmodifiableList(Arrays.asList("\u2660", "\u2663", "\u2662", "\u2661"));
470
-
471
- public static final List<String> RANKS = Collections
472
-
473
- .unmodifiableList(Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"));
474
-
475
-
476
-
477
- private final String suit;
478
-
479
- private final int rank;
480
-
481
-
482
-
483
- public Card(String suit, int rank) {
484
-
485
- Objects.requireNonNull(suit);
486
-
487
- if (rank > RANKS.size()) {
488
-
489
- throw new IllegalArgumentException(Integer.toString(rank));
490
-
491
- }
492
-
493
- this.suit = suit;
494
-
495
- this.rank = rank;
496
-
497
- }
498
-
499
-
500
-
501
- public int calcScore() {
502
-
503
- return Math.min(this.rank, 10);
504
-
505
- }
506
-
507
-
508
-
509
- @Override
510
-
511
- public String toString() {
512
-
513
- // Player#card_nameはここ
514
-
515
- return this.suit + RANKS.get(this.rank - 1);
516
-
517
- }
518
-
519
- }
520
-
521
-
522
-
523
- class CardDeck {
524
-
525
- // Sum#mainの変数:card_list はここ
526
-
527
- private final Deque<Card> cards;
528
-
529
-
530
-
531
- public CardDeck() {
532
-
533
- List<Card> card_list = new ArrayList<>(Card.RANKS.size() * Card.SUITS.size());
534
-
535
- IntStream.rangeClosed(1, Card.RANKS.size()).forEach((i) -> {
536
-
537
- for (String suit : Card.SUITS) {
538
-
539
- card_list.add(new Card(suit, i));
540
-
541
- }
542
-
543
- });
544
-
545
- Random rnd = new Random();
546
-
547
- // Random#setSeedでshuffleの実行結果を固定化(テスト用)
548
-
549
- // rnd.setSeed(42);
550
-
551
- Collections.shuffle(card_list, rnd);
552
-
553
- cards = new ArrayDeque<>(card_list);
554
-
555
- }
556
-
557
-
558
-
559
- public Card deal() {
560
-
561
- return cards.removeFirst();
562
-
563
- }
564
-
565
-
566
-
567
- @Override
568
-
569
- public String toString() {
570
-
571
- return Arrays.toString(cards.toArray(new Card[0]));
572
-
573
- }
574
-
575
- }
576
-
577
-
578
-
579
-
580
-
581
- ```

2

コメントを追加!

2018/04/25 22:26

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -10,10 +10,20 @@
10
10
 
11
11
  ※)元の質問文の要件にはないですが、柄(Suit)対応も行いました。
12
12
 
13
-
13
+ ポイントはcard_listからのカードを配る(deal)事をCardDeckクラスを新設して管理している点でしょうか。こうすることで、dealメソッドが正しく動作すれば、他のクラスではcard_listの状態を意識しなくても良くなります。
14
14
 
15
15
  ```Java
16
16
 
17
+ int []card_list = new int[13];
18
+
19
+ ```
20
+
21
+
22
+
23
+
24
+
25
+ ```Java
26
+
17
27
  import java.util.ArrayDeque;
18
28
 
19
29
  import java.util.ArrayList;
@@ -46,7 +56,7 @@
46
56
 
47
57
  game.start();
48
58
 
49
- game.show_result();
59
+ game.showResult();
50
60
 
51
61
  }
52
62
 
@@ -86,9 +96,9 @@
86
96
 
87
97
  private static final CardDeck deck = new CardDeck();
88
98
 
89
- private final List<Player> players = Collections
90
-
91
- .unmodifiableList(Arrays.asList(new Player("あなた"), new Dealer("ディラー")));
99
+ private final Player dealer = new Dealer("ディラー");
100
+
101
+ private final List<Player> players = Collections.unmodifiableList(Arrays.asList(new Player("あなた"), dealer));
92
102
 
93
103
 
94
104
 
@@ -138,7 +148,7 @@
138
148
 
139
149
  // ディラーのカードを表示
140
150
 
141
- System.out.println(players.get(1));
151
+ System.out.println(dealer);
142
152
 
143
153
 
144
154
 
@@ -168,7 +178,7 @@
168
178
 
169
179
 
170
180
 
171
- public Judge Judge(Player player, Player dealer) {
181
+ public Judge judge(Player player, Player dealer) {
172
182
 
173
183
  int compared = player.compareTo(dealer);
174
184
 
@@ -190,9 +200,9 @@
190
200
 
191
201
 
192
202
 
193
- public void show_result() {
203
+ public void showResult() {
194
-
204
+
195
- System.out.println(String.join("", Collections.nCopies(30, "#")));
205
+ System.out.println(String.join("", Collections.nCopies(40, "#")));
196
206
 
197
207
  System.out.println("最終結果");
198
208
 
@@ -206,7 +216,7 @@
206
216
 
207
217
  // プレイヤーとディラーの勝敗判定
208
218
 
209
- Judge judge = Judge(players.get(0), players.get(1));
219
+ Judge judge = judge(players.get(0), dealer);
210
220
 
211
221
  System.out.println(judge);
212
222
 
@@ -226,6 +236,8 @@
226
236
 
227
237
  private final String name;
228
238
 
239
+ // 変数:user_stopと変数:dealer_stopはここ
240
+
229
241
  protected boolean skiped = false;
230
242
 
231
243
 
@@ -248,6 +260,8 @@
248
260
 
249
261
  public void deal() {
250
262
 
263
+ // Player#Draw_Cardはここ
264
+
251
265
  // 山から一枚カードを引いて手札に加える。
252
266
 
253
267
  Card c = GameMaster.deal();
@@ -308,6 +322,8 @@
308
322
 
309
323
  public boolean isBusting() {
310
324
 
325
+ // Player#Judge_burstはここ
326
+
311
327
  return calcScore() > BLACK_JACK;
312
328
 
313
329
  }
@@ -316,6 +332,8 @@
316
332
 
317
333
  public int calcScore() {
318
334
 
335
+ // Player#calc_sumはここ
336
+
319
337
  int aces_count = 0;
320
338
 
321
339
  int sum = 0;
@@ -386,6 +404,8 @@
386
404
 
387
405
  public String toString() {
388
406
 
407
+ // Player#Show_Listはここ
408
+
389
409
  return name + "が持っているカード:" + myCardList.stream().map(String::valueOf).collect(Collectors.joining(" "));
390
410
 
391
411
  }
@@ -490,6 +510,8 @@
490
510
 
491
511
  public String toString() {
492
512
 
513
+ // Player#card_nameはここ
514
+
493
515
  return this.suit + RANKS.get(this.rank - 1);
494
516
 
495
517
  }
@@ -500,6 +522,8 @@
500
522
 
501
523
  class CardDeck {
502
524
 
525
+ // Sum#mainの変数:card_list はここ
526
+
503
527
  private final Deque<Card> cards;
504
528
 
505
529
 
@@ -552,4 +576,6 @@
552
576
 
553
577
 
554
578
 
579
+
580
+
555
581
  ```

1

追記

2018/04/25 22:24

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -8,6 +8,10 @@
8
8
 
9
9
 
10
10
 
11
+ ※)元の質問文の要件にはないですが、柄(Suit)対応も行いました。
12
+
13
+
14
+
11
15
  ```Java
12
16
 
13
17
  import java.util.ArrayDeque;
@@ -18,13 +22,17 @@
18
22
 
19
23
  import java.util.Collections;
20
24
 
25
+ import java.util.Deque;
26
+
21
27
  import java.util.List;
22
28
 
29
+ import java.util.Objects;
30
+
23
31
  import java.util.Random;
24
32
 
25
33
  import java.util.Scanner;
26
34
 
27
- import java.util.StringJoiner;
35
+ import java.util.stream.Collectors;
28
36
 
29
37
  import java.util.stream.IntStream;
30
38
 
@@ -54,7 +62,19 @@
54
62
 
55
63
  public static int readInt() {
56
64
 
65
+ while (true) {
66
+
67
+ try {
68
+
57
- return Integer.parseInt(sc.nextLine());
69
+ return Integer.parseInt(sc.nextLine());
70
+
71
+ } catch (NumberFormatException ex) {
72
+
73
+ System.err.println(ex);
74
+
75
+ }
76
+
77
+ }
58
78
 
59
79
  }
60
80
 
@@ -64,9 +84,11 @@
64
84
 
65
85
  class GameMaster {
66
86
 
67
- private final Deck deck = new Deck();
87
+ private static final CardDeck deck = new CardDeck();
88
+
68
-
89
+ private final List<Player> players = Collections
90
+
69
- private final Player[] players = { new Player("あなた", deck), new Dealer("ディラー", deck) };
91
+ .unmodifiableList(Arrays.asList(new Player("あなた"), new Dealer("ディラー")));
70
92
 
71
93
 
72
94
 
@@ -100,11 +122,11 @@
100
122
 
101
123
 
102
124
 
103
- GameMaster() {
125
+ public void start() {
104
-
126
+
105
- // 最初に2枚配る。
127
+ // 参加者に2枚ずつカードを配る。
106
-
128
+
107
- for (int i = 0; i < 2; i++) {
129
+ IntStream.range(0, 2).forEach((i) -> {
108
130
 
109
131
  for (Player p : players) {
110
132
 
@@ -112,115 +134,399 @@
112
134
 
113
135
  }
114
136
 
115
- }
137
+ });
116
-
117
- }
138
+
118
-
119
-
120
-
121
- public void start() {
139
+ // ディラーのカードを表示
140
+
141
+ System.out.println(players.get(1));
142
+
143
+
122
144
 
123
145
  while (true) {
124
146
 
147
+ int skiped = players.stream().mapToInt(Player::trun).sum();
148
+
149
+ // 参加者全員スキップなら
150
+
125
- int skiped = 0;
151
+ if (skiped == 0) {
126
-
127
- for (Player p : players) {
152
+
128
-
129
- skiped += p.Turn();
153
+ break;
130
-
131
- //System.out.println(p);
132
154
 
133
155
  }
134
156
 
157
+ }
158
+
159
+ }
160
+
161
+
162
+
163
+ public static Card deal() {
164
+
135
- // 全員スキップなら
165
+ return deck.deal();
166
+
136
-
167
+ }
168
+
169
+
170
+
171
+ public Judge Judge(Player player, Player dealer) {
172
+
173
+ int compared = player.compareTo(dealer);
174
+
137
- if (skiped == 0) {
175
+ if (compared == 0) {
176
+
138
-
177
+ return Judge.Draw;
178
+
179
+ }
180
+
181
+ if (compared < 0) {
182
+
183
+ return Judge.Lose;
184
+
185
+ }
186
+
187
+ return Judge.Win;
188
+
189
+ }
190
+
191
+
192
+
193
+ public void show_result() {
194
+
195
+ System.out.println(String.join("", Collections.nCopies(30, "#")));
196
+
197
+ System.out.println("最終結果");
198
+
199
+ for (Player p : players) {
200
+
201
+ System.out.print(p);
202
+
203
+ System.out.println(" 合計:" + p.calcScore());
204
+
205
+ }
206
+
207
+ // プレイヤーとディラーの勝敗判定
208
+
209
+ Judge judge = Judge(players.get(0), players.get(1));
210
+
211
+ System.out.println(judge);
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+ class Player implements Comparable<Player> {
220
+
221
+ private static final int BLACK_JACK = 21;
222
+
223
+ // 手札
224
+
225
+ private final List<Card> myCardList = new ArrayList<>();
226
+
227
+ private final String name;
228
+
229
+ protected boolean skiped = false;
230
+
231
+
232
+
233
+ public Player(String name) {
234
+
235
+ this.name = name;
236
+
237
+ }
238
+
239
+
240
+
241
+ public String getName() {
242
+
243
+ return this.name;
244
+
245
+ }
246
+
247
+
248
+
249
+ public void deal() {
250
+
251
+ // 山から一枚カードを引いて手札に加える。
252
+
253
+ Card c = GameMaster.deal();
254
+
255
+ myCardList.add(c);
256
+
257
+ }
258
+
259
+
260
+
261
+ public boolean isSkip() {
262
+
263
+ return skiped || isBusting();
264
+
265
+ }
266
+
267
+
268
+
269
+ public int trun() {
270
+
271
+ if (isSkip()) {
272
+
139
- break;
273
+ return 0;
274
+
275
+ }
276
+
277
+ System.out.println(this.toString());
278
+
279
+ System.out.println("カードを引きますか?");
280
+
281
+ System.out.print("yes -> 1 no -> 0 を入力");
282
+
283
+ int n = 0;
284
+
285
+ do {
286
+
287
+ n = InputUtils.readInt();
288
+
289
+ if (n == 0) {
290
+
291
+ skiped = true;
292
+
293
+ return 0;
140
294
 
141
295
  }
142
296
 
143
- }
144
-
145
- }
146
-
147
-
148
-
149
- public Judge Judge(Player player, Player dealer) {
150
-
151
- int compared = player.compareTo(dealer);
152
-
153
- if (compared == 0) {
154
-
155
- return Judge.Draw;
156
-
157
- }
158
-
159
- if (compared < 0) {
160
-
161
- return Judge.Lose;
162
-
163
- }
164
-
165
- return Judge.Win;
166
-
167
- }
168
-
169
-
170
-
171
- public void show_result() {
172
-
173
- System.out.println(String.join("", Collections.nCopies(30, "#")));
174
-
175
- System.out.println("最終結果");
176
-
177
- for (Player p : players) {
178
-
179
- System.out.print(p);
180
-
181
- System.out.println(" 合計:" + p.calcScore());
182
-
183
- }
184
-
185
- // プレイヤーとディラーの勝敗判定
186
-
187
- Judge judge = Judge(players[0], players[1]);
188
-
189
- System.out.println(judge);
190
-
191
- }
192
-
193
- }
194
-
195
-
196
-
197
- class Player implements Comparable<Player> {
198
-
199
- private final Deck deck;
200
-
201
- // 手札
202
-
203
- private final List<Card> myCardList = new ArrayList<>();
204
-
205
- private final String myName;
206
-
207
- private boolean skiped = false;
208
-
209
-
210
-
211
- public Player(String name, Deck deck) {
212
-
213
- this.myName = name;
214
-
215
- this.deck = deck;
216
-
217
- }
218
-
219
-
220
-
221
- public String getName() {
222
-
223
- return this.myName;
297
+ } while (n != 1);
298
+
299
+
300
+
301
+ this.deal();
302
+
303
+ return 1;
304
+
305
+ }
306
+
307
+
308
+
309
+ public boolean isBusting() {
310
+
311
+ return calcScore() > BLACK_JACK;
312
+
313
+ }
314
+
315
+
316
+
317
+ public int calcScore() {
318
+
319
+ int aces_count = 0;
320
+
321
+ int sum = 0;
322
+
323
+ for (Card c : this.myCardList) {
324
+
325
+ int rank = c.calcScore();
326
+
327
+ if (rank == 1) {
328
+
329
+ aces_count++;
330
+
331
+ continue;
332
+
333
+ }
334
+
335
+ sum += rank;
336
+
337
+ }
338
+
339
+ if (aces_count == 0) {
340
+
341
+ return sum;
342
+
343
+ }
344
+
345
+ // Aceカードのスコア計算
346
+
347
+ int aces_sum = 11 + (aces_count - 1);
348
+
349
+ if (aces_sum + sum > BLACK_JACK) {
350
+
351
+ aces_sum = aces_count;
352
+
353
+ }
354
+
355
+ return sum + aces_sum;
356
+
357
+ }
358
+
359
+
360
+
361
+ @Override
362
+
363
+ public int compareTo(Player o) {
364
+
365
+ if (this.isBusting()) {
366
+
367
+ return -1;
368
+
369
+ }
370
+
371
+ // ディラー
372
+
373
+ if (o.isBusting()) {
374
+
375
+ return 1;
376
+
377
+ }
378
+
379
+ return Integer.compare(this.calcScore(), o.calcScore());
380
+
381
+ }
382
+
383
+
384
+
385
+ @Override
386
+
387
+ public String toString() {
388
+
389
+ return name + "が持っているカード:" + myCardList.stream().map(String::valueOf).collect(Collectors.joining(" "));
390
+
391
+ }
392
+
393
+ }
394
+
395
+
396
+
397
+ class Dealer extends Player {
398
+
399
+ public Dealer(String name) {
400
+
401
+ super(name);
402
+
403
+ }
404
+
405
+
406
+
407
+ @Override
408
+
409
+ public int trun() {
410
+
411
+ if (isSkip()) {
412
+
413
+ return 0;
414
+
415
+ }
416
+
417
+ /// soft 17 rule
418
+
419
+ int score = this.calcScore();
420
+
421
+ if (score >= 17) {
422
+
423
+ System.out.println(this.getName() + "はカードを引きません。");
424
+
425
+ skiped = true;
426
+
427
+ return 0;
428
+
429
+ }
430
+
431
+ System.out.println(this.getName() + "はカードを引きます。");
432
+
433
+ this.deal();
434
+
435
+ return 1;
436
+
437
+ }
438
+
439
+ }
440
+
441
+
442
+
443
+ class Card {
444
+
445
+ // BLACK SPADE, BLACK CLUB, WHITE DIAMOND, WHITE HEART
446
+
447
+ public static final List<String> SUITS = Collections
448
+
449
+ .unmodifiableList(Arrays.asList("\u2660", "\u2663", "\u2662", "\u2661"));
450
+
451
+ public static final List<String> RANKS = Collections
452
+
453
+ .unmodifiableList(Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"));
454
+
455
+
456
+
457
+ private final String suit;
458
+
459
+ private final int rank;
460
+
461
+
462
+
463
+ public Card(String suit, int rank) {
464
+
465
+ Objects.requireNonNull(suit);
466
+
467
+ if (rank > RANKS.size()) {
468
+
469
+ throw new IllegalArgumentException(Integer.toString(rank));
470
+
471
+ }
472
+
473
+ this.suit = suit;
474
+
475
+ this.rank = rank;
476
+
477
+ }
478
+
479
+
480
+
481
+ public int calcScore() {
482
+
483
+ return Math.min(this.rank, 10);
484
+
485
+ }
486
+
487
+
488
+
489
+ @Override
490
+
491
+ public String toString() {
492
+
493
+ return this.suit + RANKS.get(this.rank - 1);
494
+
495
+ }
496
+
497
+ }
498
+
499
+
500
+
501
+ class CardDeck {
502
+
503
+ private final Deque<Card> cards;
504
+
505
+
506
+
507
+ public CardDeck() {
508
+
509
+ List<Card> card_list = new ArrayList<>(Card.RANKS.size() * Card.SUITS.size());
510
+
511
+ IntStream.rangeClosed(1, Card.RANKS.size()).forEach((i) -> {
512
+
513
+ for (String suit : Card.SUITS) {
514
+
515
+ card_list.add(new Card(suit, i));
516
+
517
+ }
518
+
519
+ });
520
+
521
+ Random rnd = new Random();
522
+
523
+ // Random#setSeedでshuffleの実行結果を固定化(テスト用)
524
+
525
+ // rnd.setSeed(42);
526
+
527
+ Collections.shuffle(card_list, rnd);
528
+
529
+ cards = new ArrayDeque<>(card_list);
224
530
 
225
531
  }
226
532
 
@@ -228,105 +534,7 @@
228
534
 
229
535
  public Card deal() {
230
536
 
231
- // 山から一枚カードを引いて手札に加える。
232
-
233
- Card c = this.deck.deal();
234
-
235
- myCardList.add(c);
236
-
237
- return c;
238
-
239
- }
240
-
241
-
242
-
243
- public boolean isSkip() {
244
-
245
- return skiped || isBurst();
537
+ return cards.removeFirst();
246
-
247
- }
248
-
249
-
250
-
251
- public int Turn() {
252
-
253
- if (isSkip()) {
254
-
255
- return 0;
256
-
257
- }
258
-
259
- System.out.println(this.toString());
260
-
261
- System.out.println("カードを引きますか?");
262
-
263
- System.out.println("yes -> 1 no -> 0 を入力");
264
-
265
- int n = InputUtils.readInt();
266
-
267
- if (n == 0) {
268
-
269
- skiped = true;
270
-
271
- return 0;
272
-
273
- }
274
-
275
- this.deal();
276
-
277
-
278
-
279
- return 1;
280
-
281
- }
282
-
283
-
284
-
285
- public boolean isBurst() {
286
-
287
- return calcScore() > 21;
288
-
289
- }
290
-
291
-
292
-
293
- public int calcScore() {
294
-
295
- int aces_count = 0;
296
-
297
- int sum = 0;
298
-
299
- for (Card c : this.myCardList) {
300
-
301
- int rank = c.calcScore();
302
-
303
- if (rank == 1) {
304
-
305
- aces_count++;
306
-
307
- continue;
308
-
309
- }
310
-
311
- sum += rank;
312
-
313
- }
314
-
315
- // カードのAのスコア計算
316
-
317
- if (aces_count == 0) {
318
-
319
- return sum;
320
-
321
- }
322
-
323
- int aces_sum = 11 + (aces_count - 1);
324
-
325
- if (aces_sum + sum > 21)
326
-
327
- aces_sum = aces_count;
328
-
329
- return sum + aces_sum;
330
538
 
331
539
  }
332
540
 
@@ -334,216 +542,8 @@
334
542
 
335
543
  @Override
336
544
 
337
- public int compareTo(Player o) {
338
-
339
- if (this.isBurst()) {
340
-
341
- return -1;
342
-
343
- }
344
-
345
- // ディラー
346
-
347
- if (o.isBurst()) {
348
-
349
- return 1;
350
-
351
- }
352
-
353
- return Integer.compare(this.calcScore(), o.calcScore());
354
-
355
- }
356
-
357
-
358
-
359
- @Override
360
-
361
545
  public String toString() {
362
546
 
363
- StringJoiner joiner = new StringJoiner(" ");
364
-
365
- for (Card c : this.myCardList) {
366
-
367
- joiner.add(c.toString());
368
-
369
- }
370
-
371
- return myName + "が持っているカード:" + joiner.toString();
372
-
373
- }
374
-
375
- }
376
-
377
-
378
-
379
- class Dealer extends Player {
380
-
381
- public Dealer(String name, Deck deck) {
382
-
383
- super(name, deck);
384
-
385
- }
386
-
387
-
388
-
389
- @Override
390
-
391
- public int Turn() {
392
-
393
- /// soft 17 rule
394
-
395
- int score = this.calcScore();
396
-
397
- if (score >= 17) {
398
-
399
- System.out.println(this.getName() + "はカードを引きません。");
400
-
401
- return 0;
402
-
403
- }
404
-
405
- System.out.println(this.getName() + "はカードを引きます。");
406
-
407
- this.deal();
408
-
409
- return 1;
410
-
411
- }
412
-
413
- }
414
-
415
-
416
-
417
- class Card {
418
-
419
- private final String Mark;
420
-
421
- private final int Rank;
422
-
423
-
424
-
425
- public Card(String Mark, int Rank) {
426
-
427
- this.Mark = Mark;
428
-
429
- this.Rank = Rank;
430
-
431
- }
432
-
433
-
434
-
435
- public int calcScore() {
436
-
437
- if (this.Rank > 10) {
438
-
439
- return 10;
440
-
441
- }
442
-
443
- return this.Rank;
444
-
445
- }
446
-
447
-
448
-
449
- @Override
450
-
451
- public String toString() {
452
-
453
- final String r;
454
-
455
- switch (this.Rank) {
456
-
457
- case 1:
458
-
459
- r = "A";
460
-
461
- break;
462
-
463
- case 11:
464
-
465
- r = "J";
466
-
467
- break;
468
-
469
- case 12:
470
-
471
- r = "Q";
472
-
473
- break;
474
-
475
- case 13:
476
-
477
- r = "K";
478
-
479
- break;
480
-
481
- default:
482
-
483
- r = Integer.toString(this.Rank);
484
-
485
- break;
486
-
487
- }
488
-
489
- return this.Mark + r;
490
-
491
- }
492
-
493
- }
494
-
495
-
496
-
497
- class Deck {
498
-
499
- private final ArrayDeque<Card> cards;
500
-
501
-
502
-
503
- public Deck() {
504
-
505
- List<Card> card_list = new ArrayList<>();
506
-
507
- IntStream.rangeClosed(1, 13).forEach((i) -> {
508
-
509
- card_list.add(new Card("\u2660", i));
510
-
511
- card_list.add(new Card("\u2663", i));
512
-
513
- card_list.add(new Card("\u2662", i));
514
-
515
- card_list.add(new Card("\u2661", i));
516
-
517
- });
518
-
519
-
520
-
521
- Random rnd = new Random();
522
-
523
- // Random#setSeedでshuffleの結果を固定(テスト用)
524
-
525
- // rnd.setSeed(42);
526
-
527
- Collections.shuffle(card_list, rnd);
528
-
529
- cards = new ArrayDeque<>(card_list);
530
-
531
- }
532
-
533
-
534
-
535
- public Card deal() {
536
-
537
- return cards.removeFirst();
538
-
539
- }
540
-
541
-
542
-
543
- @Override
544
-
545
- public String toString() {
546
-
547
547
  return Arrays.toString(cards.toArray(new Card[0]));
548
548
 
549
549
  }