質問編集履歴

9

タイトル変更

2018/10/21 21:33

投稿

amareno
amareno

スコア20

test CHANGED
@@ -1 +1 @@
1
- C++ トランプを4人にそれぞれ13枚配り、配ったカードの順番を並び替えて表示するプログラムを作っています
1
+ C++ トランプを配り、並び替え
test CHANGED
File without changes

8

全体的にコードを書き変えました。

2018/10/21 21:33

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -2,115 +2,55 @@
2
2
 
3
3
 
4
4
 
5
- 現在C++でプログラミングの問題を解いています。
6
-
7
- 最初にトランプをシャッフルするかるかやめるかメニューを表示
8
-
9
- cout <<"Enter 's' to shuffle, 'd' to deal, or 'e' to end: ";
10
-
11
- deal(トランプを配る)を選択したら下記のようにアウトプットするコードを書いております。
12
-
13
-
14
-
15
-
16
-
17
- Player1 Player2 Player3 Player4
18
-
19
- ---------- ---------- ----------- ----------
20
-
21
- Spade Ace Spade Jack Spade 6 Spade King
22
-
23
- Spade 10 Spade 8 Spade 2 Spade Queen
24
-
25
- Spade 5 Spade 4 Diamond King Spade 9
26
-
27
- Heart King Spade 3 Diamond Queen Spade 7
28
-
29
- Heart Queen Heart Ace Diamond Jack Heart 7
30
-
31
- Heart 10 Heart Jack Diamond 9 Heart 6
32
-
33
- Heart 8 Heart 9 Diamond 8 Heart 5
34
-
35
- Heart 4 Diamond Ace Diamond 6 Diamond 4
36
-
37
- Heart 3 Diamond 10 Diamond 5 Diamond 2
38
-
39
- Heart 2 Diamond 3 Club King Club Ace
40
-
41
- Diamond 7 Club 9 Club 10 Club Queen
42
-
43
- Club Jack Club 7 Club 6 Club 5
44
-
45
- Club 8 Club 4 Club 2 Club 3
46
-
47
-
48
-
49
- Club Two – player 3 card 13
50
-
51
- (Club Twoの位置を表示)
52
-
53
-
54
-
55
- 下記の三つのクラスCard, Deck, Playerを使って上記のように表示したいです。
56
-
57
-
58
-
59
- Cardクラス
60
-
61
- enum Suit { Spade, Heart, Diamond, Club}
62
-
63
- number 1から13のint
64
-
65
- description stringで“Spade Ace” や “Heart 5”などを表示します。
66
-
67
- オーバーロード < operator 下記の2つの条件が正しければtrue、正しくなければfalseにreturnします。
68
-
69
- 1、スペードの2からAceまでのカードが一番先頭にきて、その後ろにハートの2からAce、その後ろにダイアモンドの2からAce、最後にクラブの2からAceを表示します。clubs < diamonds < hearts < spades
70
-
71
- 2、Aceが一番先頭、2が一番後ろです。 2<3<4<5<6<7<8<9<10<Jack<Queen<King<Ace
72
-
73
-
74
-
75
- Deckクラス
76
-
77
- カード52枚のarray
78
-
79
- メンバ関数 void shuffle()ランダムにシャッフルします。
80
-
81
- 問題文にstd::shuffle、std::random_shuffleなどは使ってはいけないと書いてあるのでそれらは使えないです。rand関数などが使えます。
82
-
83
-
84
-
85
- Playerクラス
86
-
87
- • hand 持ち札、カード13枚のarray
88
-
89
- • name プレイヤーの名前。Player1、Player2、Player3、Player4
90
-
91
- メンバ関数 void sort()、 Cardクラスの < operator を使ってそれぞれのプレイヤーの持ち札を順番通りに並び替える。
92
-
93
- メンバ関数 int clubTwo()、 プレイヤーの持ち札のClub Twoのindexナンバーにreturnする。もしClub Twoが手札になければ-1にreturnする。
94
-
95
-
96
-
97
- library sortは使ってはいけないと問題文に書いてあるので使えないです。
98
-
99
- 必要に応じてメンバ関数などを追加しても良いです
100
-
101
-
102
-
103
-
104
-
105
- ここ数日このコードを書いております。
106
-
107
- 様々なwebサイト、本を参考にコードを書いていますが私の理解力が乏しく思うように書き進める事が出来ません。
108
-
109
- このコードを完成させるには皆様の助けが必要と感じたので投稿させて頂きました。
110
-
111
- 皆様の回答を参考にコードを完成に導きたいです。よろしくお願い致します。
112
-
113
-
5
+
6
+
7
+ カードをシャッフルし、プレイヤー4人にったら下記ようにアウトプットたいです。
8
+
9
+
10
+
11
+
12
+
13
+ Player 1 Player 2 Player 3 Player 4
14
+
15
+ -------- -------- -------- --------
16
+
17
+
18
+
19
+ Spade 4 Spade 2 Spade 9 Spade Ace
20
+
21
+ Heart 2 Spade 5 Spade 10 Spade 3
22
+
23
+ Heart 8 Spade 7 Spade Queen Spade 6
24
+
25
+ Heart King Spade Jack Heart 5 Spade 8
26
+
27
+ Diamond 2 Heart Ace Heart 6 Spade King
28
+
29
+ Diamond 3 Heart 3 Diamond 4 Heart 4
30
+
31
+ Diamond 9 Heart 7 Diamond 7 Heart 9
32
+
33
+ Diamond Queen Heart 10 Diamond 10 Heart Jack
34
+
35
+ Diamond King Heart Queen Club 4 Diamond Ace
36
+
37
+ Club 2 Diamond 5 Club 6 Diamond 8
38
+
39
+ Club 5 Diamond 6 Club 7 Diamond Jack
40
+
41
+ Club 9 Club 3 Club Jack Club Ace
42
+
43
+ Club 10 Club King Club Queen Club 8
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+ しかし、下記のように、Spade 0しかアウトプットされません。私のコードのどこがおかしいか教えて頂けますでしょうか?
114
54
 
115
55
 
116
56
 
@@ -120,6 +60,80 @@
120
60
 
121
61
  ```
122
62
 
63
+ Enter 1 to shuffle, 2 to deal, or 3 to end:
64
+
65
+ 1
66
+
67
+ Deck shuffled.
68
+
69
+ 2
70
+
71
+ Spade 0 Spade 0 Spade 0 Spade 0
72
+
73
+ Spade 0 Spade 0 Spade 0 Spade 0
74
+
75
+ Spade 0 Spade 0 Spade 0 Spade 0
76
+
77
+ Spade 0 Spade 0 Spade 0 Spade 0
78
+
79
+ Spade 0 Spade 0 Spade 0 Spade 0
80
+
81
+ Spade 0 Spade 0 Spade 0 Spade 0
82
+
83
+ Spade 0 Spade 0 Spade 0 Spade 0
84
+
85
+ Spade 0 Spade 0 Spade 0 Spade 0
86
+
87
+ Spade 0 Spade 0 Spade 0 Spade 0
88
+
89
+ Spade 0 Spade 0 Spade 0 Spade 0
90
+
91
+ Spade 0 Spade 0 Spade 0 Spade 0
92
+
93
+ Spade 0 Spade 0 Spade 0 Spade 0
94
+
95
+ Spade 0 Spade 0 Spade 0 Spade 0
96
+
97
+
98
+
99
+ 1
100
+
101
+ Deck shuffled.
102
+
103
+ 2
104
+
105
+ Spade 0 Spade 0 Spade 0 Spade 0
106
+
107
+ Spade 0 Spade 0 Spade 0 Spade 0
108
+
109
+ Spade 0 Spade 0 Spade 0 Spade 0
110
+
111
+ Spade 0 Spade 0 Spade 0 Spade 0
112
+
113
+ Spade 0 Spade 0 Spade 0 Spade 0
114
+
115
+ Spade 0 Spade 0 Spade 0 Spade 0
116
+
117
+ Spade 0 Spade 0 Spade 0 Spade 0
118
+
119
+ Spade 0 Spade 0 Spade 0 Spade 0
120
+
121
+ Spade 0 Spade 0 Spade 0 Spade 0
122
+
123
+ Spade 0 Spade 0 Spade 0 Spade 0
124
+
125
+ Spade 0 Spade 0 Spade 0 Spade 0
126
+
127
+ Spade 0 Spade 0 Spade 0 Spade 0
128
+
129
+ Spade 0 Spade 0 Spade 0 Spade 0
130
+
131
+
132
+
133
+ 3
134
+
135
+ Bye
136
+
123
137
 
124
138
 
125
139
 
@@ -134,8 +148,6 @@
134
148
 
135
149
  ```C++
136
150
 
137
- //epistemeさんのコードを拝借しまして、そこからから発展させようとしております。
138
-
139
151
  #include <string>
140
152
 
141
153
  #include <stdexcept>
@@ -156,376 +168,684 @@
156
168
 
157
169
 
158
170
 
171
+ enum Suit {
172
+
173
+ Clubs = 1,
174
+
175
+ Diamonds = 2,
176
+
177
+ Hearts = 3,
178
+
179
+ Spades = 4
180
+
181
+ };
182
+
183
+
184
+
159
185
  class Card {
160
186
 
187
+
188
+
189
+ private:
190
+
191
+ int number;
192
+
193
+ Suit suit;
194
+
195
+ string description;
196
+
161
197
  public:
162
198
 
199
+
200
+
201
+ Card() {
202
+
203
+ number = 0;
204
+
205
+ description = "";
206
+
207
+ }
208
+
209
+
210
+
211
+ Suit getCardSuit() {
212
+
213
+ return suit;
214
+
215
+ }
216
+
217
+
218
+
219
+ void setCardSuit(Suit m_suit) {
220
+
221
+ suit = m_suit;
222
+
223
+ }
224
+
225
+
226
+
227
+ int getCardNumber() {
228
+
229
+ return number;
230
+
231
+ }
232
+
233
+
234
+
235
+ void setCardNumber(int m_num) {
236
+
237
+
238
+
239
+ number = m_num;
240
+
241
+ }
242
+
243
+
244
+
245
+ string getCardDescription() {
246
+
247
+
248
+
249
+ return description;
250
+
251
+ }
252
+
253
+
254
+
255
+ void setDescription(string m_description) {
256
+
257
+
258
+
259
+ description = m_description;
260
+
261
+ }
262
+
263
+
264
+
265
+ bool operator <(const Card& card2) {
266
+
267
+
268
+
269
+ if (suit < card2.suit)
270
+
271
+
272
+
273
+ return true;
274
+
275
+
276
+
277
+ else if (suit > card2.suit)
278
+
279
+
280
+
281
+ return false;
282
+
283
+
284
+
285
+ else {
286
+
287
+
288
+
289
+ if (number == 1)
290
+
291
+
292
+
293
+ return false;
294
+
295
+
296
+
297
+ else if (card2.number == 1)
298
+
299
+
300
+
301
+ return true;
302
+
303
+
304
+
305
+ else if (card2.number < number)
306
+
307
+
308
+
309
+ return true;
310
+
311
+ }
312
+
313
+
314
+
315
+ return false;
316
+
317
+ }
318
+
319
+ };
320
+
321
+
322
+
323
+
324
+
325
+ const int SIZE = 52;
326
+
327
+ Card deck[52];
328
+
329
+
330
+
331
+ class Deck {
332
+
333
+ Card decks[SIZE];
334
+
335
+ public:
336
+
337
+
338
+
339
+ void initialize() {
340
+
341
+
342
+
343
+
344
+
345
+ string cardDesc;
346
+
347
+
348
+
349
+ char name[10];
350
+
351
+
352
+
353
+ int setFlag;
354
+
355
+
356
+
357
+ for (int i = 1; i <= SIZE; i++) {
358
+
359
+
360
+
361
+ setFlag = 0;
362
+
363
+
364
+
365
+ if (i == 1 || i == 14 || i == 27 || i == 40) {
366
+
367
+ cardDesc = "Ace";
368
+
369
+
370
+
371
+ setFlag = 1;
372
+
373
+
374
+
375
+ } else if (i == 11 || i == 24 || i == 37 || i == 50) {
376
+
377
+ cardDesc = "Jack";
378
+
379
+
380
+
381
+ setFlag = 1;
382
+
383
+
384
+
385
+ } else if (i == 12 || i == 25 || i == 38 || i == 51) {
386
+
387
+ cardDesc = "Queen";
388
+
389
+
390
+
391
+ setFlag = 1;
392
+
393
+
394
+
395
+ } else if (i == 13 || i == 26 || i == 39 || i == 52) {
396
+
397
+ cardDesc = "King";
398
+
399
+
400
+
401
+ setFlag = 1;
402
+
403
+ }
404
+
405
+
406
+
407
+ if(i <= 13) {
408
+
409
+ decks[i - 1].setCardSuit(Spades);
410
+
411
+
412
+
413
+ decks[i - 1].setCardNumber(i);
414
+
415
+
416
+
417
+ if(setFlag == 0) {
418
+
419
+
420
+
421
+ (decks[i - 1].getCardNumber(), name, 10);
422
+
423
+
424
+
425
+ cardDesc = ((string)name);
426
+
427
+ }
428
+
429
+
430
+
431
+ cardDesc = " of Spades";
432
+
433
+
434
+
435
+ decks[i - 1].setDescription(cardDesc);
436
+
437
+
438
+
439
+ } else if (i > 13 && i <= 26) {
440
+
441
+
442
+
443
+ decks[i - 1].setCardSuit(Hearts);
444
+
445
+
446
+
447
+ decks[i - 1].setCardNumber(i - 13);
448
+
449
+
450
+
451
+ if(setFlag == 0) {
452
+
453
+
454
+
455
+ (decks[i - 1].getCardNumber(), name, 10);
456
+
457
+
458
+
459
+ cardDesc = ((string)name);
460
+
461
+ }
462
+
463
+
464
+
465
+ cardDesc = " of Hearts";
466
+
467
+
468
+
469
+ decks[i - 1].setDescription(cardDesc);
470
+
471
+
472
+
473
+ } else if(i > 26 && i <= 39) {
474
+
475
+
476
+
477
+ decks[i - 1].setCardSuit(Diamonds);
478
+
479
+
480
+
481
+ decks[i - 1].setCardNumber(i - 26);
482
+
483
+
484
+
485
+ if(setFlag == 0) {
486
+
487
+
488
+
489
+ (decks[i - 1].getCardNumber(), name, 10);
490
+
491
+
492
+
493
+ cardDesc = ((string)name);
494
+
495
+
496
+
497
+ }
498
+
499
+
500
+
163
- enum class Suit { Spade, Heart, Diamond, Club};
501
+ cardDesc = " of Diamonds";
502
+
503
+
504
+
505
+ decks[i - 1].setDescription(cardDesc);
506
+
507
+
508
+
509
+ } else {
510
+
511
+
512
+
513
+ decks[i - 1].setCardSuit(Clubs);
514
+
515
+
516
+
517
+ decks[i - 1].setCardNumber(i - 39);
518
+
519
+
520
+
521
+ if(setFlag == 0) {
522
+
523
+
524
+
525
+ (decks[i - 1].getCardNumber(), name, 10);
526
+
527
+
528
+
529
+ cardDesc = ((string)name);
530
+
531
+ }
532
+
533
+
534
+
535
+ cardDesc = (" of Clubs");
536
+
537
+
538
+
539
+ decks[i - 1].setDescription(cardDesc);
540
+
541
+ }
542
+
543
+ }
544
+
545
+ }
546
+
547
+
548
+
549
+ void shuffle() {
550
+
551
+
552
+
553
+ Card card;
554
+
555
+
556
+
557
+ srand((unsigned int)time(NULL));
558
+
559
+
560
+
561
+ for (int i = 0; i < SIZE ;i++)
562
+
563
+ {
564
+
565
+ int r = i + (rand() % (52 -i));
566
+
567
+
568
+
569
+ card = decks[i];
570
+
571
+ decks[i] = decks[r];
572
+
573
+ decks[r] = card;
574
+
575
+
576
+
577
+ }
578
+
579
+ }
580
+
581
+ };
582
+
583
+
584
+
585
+
586
+
587
+ class Player {
164
588
 
165
589
  private:
166
590
 
591
+ Card hand[13][4];
592
+
167
- int number;
593
+ string name_;
168
-
169
- Suit suit;
594
+
595
+
170
596
 
171
597
  public:
172
598
 
173
- Card(int n=0, Suit s=Suit(-1)) : number(n), suit(s) {}
174
-
175
- Card(const Card& c) : number(c.number), suit(c.suit) {}
176
-
177
-
178
-
179
- int getNumber() const {
180
-
181
- if ( number < 1 || number > 13 ) throw range_error("bad Number");
182
-
183
- return number;
184
-
185
- }
186
-
187
- Suit getSuit() const {
188
-
189
- if ( int(suit) < 0 || int(suit) > 3 ) throw range_error("bad Suit");
190
-
191
- return suit;
192
-
193
- }
194
-
195
- string description() const;
196
-
197
-
198
-
199
- int key() const {
200
-
201
- int n = getNumber() - 1;
202
-
203
- if ( n == 0 ) n = 13;
204
-
205
- return (int)getSuit() * 13 + n;
206
-
207
- }
599
+
600
+
601
+ void deal() {
602
+
603
+
604
+
605
+ for ( int i = 0; i < 13; ++i ) {
606
+
607
+ for ( int j = 0; j < 4; ++j ) {
608
+
609
+ hand[i][j] = deck[j + 4 * i];
610
+
611
+ }
612
+
613
+ }
614
+
615
+
616
+
617
+ sort(hand);
618
+
619
+
620
+
621
+ display(hand);
622
+
623
+ }
624
+
625
+
626
+
627
+
628
+
629
+ void sort(Card hand[13][4]) {
630
+
631
+ Card card;
632
+
633
+
634
+
635
+ int j;
636
+
637
+
638
+
639
+ for (int itr = 0; itr < 4; itr++)
640
+
641
+ {
642
+
643
+ for(int i = 1 ; i < 13 ; i++)
644
+
645
+ {
646
+
647
+ card = hand[i][itr];
648
+
649
+
650
+
651
+ j = i - 1;
652
+
653
+
654
+
655
+ while(j >= 0 && hand[j][itr] < card)
656
+
657
+ {
658
+
659
+ hand[j + 1][itr] = hand[j][itr];
660
+
661
+
662
+
663
+ j--;
664
+
665
+ }
666
+
667
+ hand[j + 1][itr] = card;
668
+
669
+ }
670
+
671
+ }
672
+
673
+ }
674
+
675
+
676
+
677
+ void display(Card hand[13][4]) {
678
+
679
+
680
+
681
+ Suit suit;
682
+
683
+
684
+
685
+ int number;
686
+
687
+
688
+
689
+ int numPlayers = 4;
690
+
691
+
692
+
693
+ for (int n = 0; n < 13; ++n) {
694
+
695
+ for (int i = 0; i < numPlayers; ++i) {
696
+
697
+
698
+
699
+ suit = hand[n][i].getCardSuit();
700
+
701
+
702
+
703
+ number = hand[n][i].getCardNumber();
704
+
705
+
706
+
707
+ if (suit == 1)
708
+
709
+ cout << setw(10) << "Club";
710
+
711
+
712
+
713
+ else if (suit == 2)
714
+
715
+ cout << setw(10) << "Diamond";
716
+
717
+
718
+
719
+ else if (suit == 3)
720
+
721
+ cout << setw(10) << "Heart";
722
+
723
+
724
+
725
+ else
726
+
727
+ cout << setw(10) << "Spade";
728
+
729
+
730
+
731
+ if (number == 1)
732
+
733
+ cout << setw(6) << "Ace";
734
+
735
+
736
+
737
+ else if(number == 11)
738
+
739
+ cout << setw(6) << "Jack";
740
+
741
+
742
+
743
+ else if(number == 12)
744
+
745
+ cout << setw(6) << "Queen";
746
+
747
+
748
+
749
+ else if(number == 13)
750
+
751
+ cout << setw(6) << "King";
752
+
753
+
754
+
755
+ else
756
+
757
+ cout << setw(6) << number;
758
+
759
+
760
+
761
+ }
762
+
763
+ cout << endl;
764
+
765
+ }
766
+
767
+ cout << endl;
768
+
769
+ }
208
770
 
209
771
  };
210
772
 
211
773
 
212
774
 
775
+
776
+
777
+ int main() {
778
+
779
+
780
+
781
+ Deck decks;
782
+
783
+
784
+
785
+ decks.initialize();
786
+
787
+
788
+
789
+ Player hand;
790
+
791
+
792
+
793
+ int enter;
794
+
795
+
796
+
797
+ cout << "Enter 1 to shuffle, 2 to deal, or 3 to end: " << endl;
798
+
799
+
800
+
801
+ do {
802
+
803
+ cin >> enter;
804
+
805
+
806
+
807
+
808
+
809
+ switch (enter) {
810
+
811
+ case 1:
812
+
813
+ decks.shuffle();
814
+
213
- inline bool operator<(const Card& c1, const Card& c2) {
815
+ cout << "Deck shuffled." << endl;
816
+
214
-
817
+ break;
818
+
819
+
820
+
821
+ case 2:
822
+
823
+ hand.deal();
824
+
825
+ break;
826
+
827
+
828
+
829
+ case 3:
830
+
215
- return c1.key() < c2.key();
831
+ cout << "Bye" << endl;
832
+
833
+ break;
834
+
835
+
836
+
837
+ default:
838
+
839
+ break;
840
+
841
+ }
842
+
843
+
844
+
845
+ } while (enter != 3);
216
846
 
217
847
  }
218
848
 
219
849
 
220
850
 
221
- string to_string(const Card::Suit& s) {
222
-
223
- switch ( s ) {
224
-
225
- case Card::Suit::Spade : return "Spade";
226
-
227
- case Card::Suit::Heart : return "Heart";
228
-
229
- case Card::Suit::Diamond : return "Diamond";
230
-
231
- case Card::Suit::Club : return "Club";
232
-
233
- }
234
-
235
- return string();
236
-
237
- }
238
-
239
-
240
-
241
- string Card::description() const {
242
-
243
- static const char* nums[] = { "Ace", "2", "3", "4", "5",
244
-
245
- "6", "7", "8", "9", "10",
246
-
247
- "Jack", "Queen", "King" };
248
-
249
- return to_string(getSuit()) + " " + nums[getNumber()-1];
250
-
251
- }
252
-
253
-
254
-
255
- inline ostream& operator<<(ostream& stream, const Card& c) {
256
-
257
- return stream << c.description();
258
-
259
- }
260
-
261
-
262
-
263
- const int SIZE = 52;
264
-
265
- Card deck[52];
266
-
267
-
268
-
269
- class Deck {
270
-
271
-
272
-
273
- Card decks[SIZE];
274
-
275
- public:
276
-
277
-
278
-
279
- void shuffle() {
280
-
281
-
282
-
283
- Card card;
284
-
285
-
286
-
287
- //int numRandom;
288
-
289
-
290
-
291
- srand((unsigned int)time(NULL));
292
-
293
-
294
-
295
- for (int i = 0; i < SIZE ;i++)
296
-
297
- {
298
-
299
- // Random for remaining positions.
300
-
301
- int r = i + (rand() % (52 -i));
302
-
303
-
304
-
305
- card = decks[i];
306
-
307
- decks[i] = decks[r];
308
-
309
- decks[r] = card;
310
-
311
-
312
-
313
- //swap(decks[i], decks[r]);
314
-
315
- }
316
-
317
- }
318
-
319
- };
320
-
321
-
322
-
323
-
324
-
325
- class Player {
326
-
327
- private:
328
-
329
- Card hand[13][4];
330
-
331
- string name_;
332
-
333
-
334
-
335
- public:
336
-
337
-
338
-
339
- void deal() {
340
-
341
-
342
-
343
- // 4人のプレイヤーにカードを配る
344
-
345
-
346
-
347
- for ( int i = 0; i < 13; ++i ) {
348
-
349
- for ( int j = 0; j < 4; ++j ) {
350
-
351
- hand[i][j] = deck[j + 4 * i];
352
-
353
-
354
-
355
- }
356
-
357
- }
358
-
359
-
360
-
361
- sort(hand);
362
-
363
-
364
-
365
- display(hand);
366
-
367
-
368
-
369
- }
370
-
371
-
372
-
373
-
374
-
375
- void sort(Card hand[13][4]) {
376
-
377
-
378
-
379
- // 各プレイヤーの手札をソートする
380
-
381
- Card card;
382
-
383
-
384
-
385
- int j;
386
-
387
-
388
-
389
- for (int itr = 0; itr < 4; itr++)
390
-
391
- {
392
-
393
- for(int i = 1 ; i < 13 ; i++)
394
-
395
- {
396
-
397
- card = hand[i][itr];
398
-
399
-
400
-
401
- j = i - 1;
402
-
403
-
404
-
405
- while(j >= 0 && hand[j][itr] < card)
406
-
407
- {
408
-
409
- hand[j + 1][itr] = hand[j][itr];
410
-
411
-
412
-
413
- j--;
414
-
415
- }
416
-
417
- hand[j + 1][itr] = card;
418
-
419
- }
420
-
421
- }
422
-
423
- }
424
-
425
-
426
-
427
- void display(Card hand[13][4]) {
428
-
429
-
430
-
431
- int numPlayers = 4;
432
-
433
-
434
-
435
- for (int n = 0; n < 13; ++n) {
436
-
437
- for (int i = 0; i < numPlayers; ++i) {
438
-
439
- //cout << setw(16) <<
440
-
441
- }
442
-
443
- cout << endl;
444
-
445
- }
446
-
447
-
448
-
449
- }
450
-
451
- };
452
-
453
-
454
-
455
-
456
-
457
- int main() {
458
-
459
-
460
-
461
- Deck decks;
462
-
463
-
464
-
465
- int enter;
466
-
467
-
468
-
469
- cout << "Enter 1 to shuffle, 2 to deal, or 3 to end: " << endl;
470
-
471
-
472
-
473
- do {
474
-
475
- cin >> enter;
476
-
477
-
478
-
479
-
480
-
481
- switch (enter) {
482
-
483
- case 1:
484
-
485
- decks.shuffle();
486
-
487
- cout << "Deck shuffled." << endl;
488
-
489
- break;
490
-
491
-
492
-
493
- case 2:
494
-
495
- decks.deal();
496
-
497
- break;
498
-
499
-
500
-
501
- case 3:
502
-
503
- cout << "Bye" << endl;
504
-
505
- break;
506
-
507
-
508
-
509
- default:
510
-
511
- break;
512
-
513
- }
514
-
515
-
516
-
517
- } while (enter != 3);
518
-
519
- }
520
-
521
-
522
-
523
-
524
-
525
851
  ```
526
-
527
- おかしいところが多いと思います。
528
-
529
- おかしいところについて、こうしたら良いなど少しでもご指導頂けますでしょうか。
530
-
531
- よろしくお願いいたします。

7

こーど変更

2018/10/21 21:29

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -492,7 +492,7 @@
492
492
 
493
493
  case 2:
494
494
 
495
-
495
+ decks.deal();
496
496
 
497
497
  break;
498
498
 

6

少し説明文を補足しました

2018/10/21 10:32

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -78,7 +78,7 @@
78
78
 
79
79
  メンバ関数 void shuffle()ランダムにシャッフルします。
80
80
 
81
- 問題文にstd::shuffle、std::random_shuffleなどは使ってはいけないと書いてあるのでそれらは使えないです。
81
+ 問題文にstd::shuffle、std::random_shuffleなどは使ってはいけないと書いてあるのでそれらは使えないです。rand関数などが使えます。
82
82
 
83
83
 
84
84
 
@@ -94,7 +94,7 @@
94
94
 
95
95
 
96
96
 
97
- library sortや、std::shuffle、std::random_shuffleなどは使ってはいけないです。
97
+ library sortは使ってはいけないと問題文に書いてあるの使えないです。
98
98
 
99
99
  必要に応じてメンバ関数などを追加しても良いです
100
100
 
@@ -458,7 +458,7 @@
458
458
 
459
459
 
460
460
 
461
- Deck deck;
461
+ Deck decks;
462
462
 
463
463
 
464
464
 
@@ -482,7 +482,7 @@
482
482
 
483
483
  case 1:
484
484
 
485
- deck.shuffle();
485
+ decks.shuffle();
486
486
 
487
487
  cout << "Deck shuffled." << endl;
488
488
 

5

エラーメッセージの削除

2018/10/21 10:20

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -120,51 +120,7 @@
120
120
 
121
121
  ```
122
122
 
123
- Enter 1 to shuffle, 2 to deal, or 3 to end:
123
+
124
-
125
- 1
126
-
127
- Deck shuffled.
128
-
129
- 2
130
-
131
- Spade 4 Spade 6 Spade 3 Spade 2
132
-
133
- Heart 2 Spade 8 Spade 10 Spade 5
134
-
135
- Heart 3 Spade Queen Spade Jack Spade 7
136
-
137
- Heart 4 Spade Ace Heart 5 Spade 9
138
-
139
- Heart 7 Heart 8 Heart 6 Spade King
140
-
141
- Heart Ace Heart 10 Heart 9 Diamond 7
142
-
143
- Diamond 2 Heart Queen Heart Jack Diamond 8
144
-
145
- Diamond 3 Heart King Diamond 5 Diamond 9
146
-
147
- Diamond 6 Diamond 4 Diamond Jack Diamond Ace
148
-
149
- Diamond 10 Diamond Queen Diamond King Club 2
150
-
151
- Club 8 Club 4 Club 6 Club 3
152
-
153
- Club Jack Club 5 Club King Club 7
154
-
155
- Club Queen Club 9 Club Ace Club 10
156
-
157
- 1
158
-
159
- Deck shuffled.
160
-
161
- 2
162
-
163
- libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
164
-
165
-
166
-
167
- Process finished with exit code 6
168
124
 
169
125
 
170
126
 

4

まだまだ途中ですがコードを書き進めております。

2018/10/21 07:01

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -190,10 +190,6 @@
190
190
 
191
191
  #include <algorithm>
192
192
 
193
- #include <vector>
194
-
195
- //#include <random>
196
-
197
193
  #include <stdio.h>
198
194
 
199
195
  #include <time.h>
@@ -316,57 +312,185 @@
316
312
 
317
313
  class Deck {
318
314
 
315
+
316
+
317
+ Card decks[SIZE];
318
+
319
+ public:
320
+
321
+
322
+
323
+ void shuffle() {
324
+
325
+
326
+
327
+ Card card;
328
+
329
+
330
+
331
+ //int numRandom;
332
+
333
+
334
+
335
+ srand((unsigned int)time(NULL));
336
+
337
+
338
+
339
+ for (int i = 0; i < SIZE ;i++)
340
+
341
+ {
342
+
343
+ // Random for remaining positions.
344
+
345
+ int r = i + (rand() % (52 -i));
346
+
347
+
348
+
349
+ card = decks[i];
350
+
351
+ decks[i] = decks[r];
352
+
353
+ decks[r] = card;
354
+
355
+
356
+
357
+ //swap(decks[i], decks[r]);
358
+
359
+ }
360
+
361
+ }
362
+
363
+ };
364
+
365
+
366
+
367
+
368
+
369
+ class Player {
370
+
319
371
  private:
320
372
 
321
- vector<Card> deck_;
322
-
323
- Card decks[SIZE];
373
+ Card hand[13][4];
374
+
375
+ string name_;
376
+
377
+
324
378
 
325
379
  public:
326
380
 
381
+
382
+
327
- Deck() {
383
+ void deal() {
384
+
385
+
386
+
387
+ // 4人のプレイヤーにカードを配る
388
+
389
+
328
390
 
329
391
  for ( int i = 0; i < 13; ++i ) {
330
392
 
331
393
  for ( int j = 0; j < 4; ++j ) {
332
394
 
333
- deck_.push_back(Card(i+1,Card::Suit(j)));
395
+ hand[i][j] = deck[j + 4 * i];
396
+
397
+
334
398
 
335
399
  }
336
400
 
337
401
  }
338
402
 
403
+
404
+
405
+ sort(hand);
406
+
407
+
408
+
409
+ display(hand);
410
+
411
+
412
+
339
- }
413
+ }
340
-
414
+
415
+
416
+
417
+
418
+
341
- void shuffle() {
419
+ void sort(Card hand[13][4]) {
342
-
420
+
421
+
422
+
343
- srand((unsigned int)time(NULL));
423
+ // 各プレイヤーの手札をソートする
424
+
344
-
425
+ Card card;
426
+
427
+
428
+
345
-
429
+ int j;
346
-
430
+
431
+
432
+
347
- for (int i=0; i<SIZE ;i++)
433
+ for (int itr = 0; itr < 4; itr++)
348
434
 
349
435
  {
350
436
 
351
- // Random for remaining positions.
352
-
353
- int r = i + (rand() % (52 -i));
437
+ for(int i = 1 ; i < 13 ; i++)
438
+
354
-
439
+ {
440
+
355
-
441
+ card = hand[i][itr];
442
+
443
+
444
+
356
-
445
+ j = i - 1;
446
+
447
+
448
+
449
+ while(j >= 0 && hand[j][itr] < card)
450
+
451
+ {
452
+
453
+ hand[j + 1][itr] = hand[j][itr];
454
+
455
+
456
+
457
+ j--;
458
+
459
+ }
460
+
357
- swap(deck_[i], deck_[r]);
461
+ hand[j + 1][itr] = card;
462
+
463
+ }
358
464
 
359
465
  }
360
466
 
361
- // static std::mt19937 gen;
362
-
363
- // std::shuffle(deck_.begin(), deck_.end(), gen);
364
-
365
- }
467
+ }
468
+
469
+
470
+
366
-
471
+ void display(Card hand[13][4]) {
472
+
473
+
474
+
475
+ int numPlayers = 4;
476
+
477
+
478
+
367
- Card draw() { Card c = deck_.back(); deck_.pop_back(); return c; }
479
+ for (int n = 0; n < 13; ++n) {
368
-
480
+
369
- bool empty() const { return deck_.empty(); }
481
+ for (int i = 0; i < numPlayers; ++i) {
482
+
483
+ //cout << setw(16) <<
484
+
485
+ }
486
+
487
+ cout << endl;
488
+
489
+ }
490
+
491
+
492
+
493
+ }
370
494
 
371
495
  };
372
496
 
@@ -374,32 +498,6 @@
374
498
 
375
499
 
376
500
 
377
- class Player {
378
-
379
- private:
380
-
381
- vector<Card> hand;
382
-
383
- string name_;
384
-
385
-
386
-
387
- public:
388
-
389
- //std::sortを使わずに他のBubble sort、Selection sort、Merge sortなどを使ってソートする方法を現在探しています。
390
-
391
- void sort() { std::sort(hand.begin(), hand.end()); }
392
-
393
- void add(const Card& card) { hand.push_back(card); }
394
-
395
- const Card& at(int n) const { return hand.at(n); }
396
-
397
- };
398
-
399
-
400
-
401
-
402
-
403
501
  int main() {
404
502
 
405
503
 
@@ -436,49 +534,9 @@
436
534
 
437
535
 
438
536
 
439
- case 2: {
537
+ case 2:
440
-
441
- // 4人のプレイヤーにカードを配る
538
+
442
-
443
- const int nplayers = 4;
539
+
444
-
445
- Player players[nplayers];
446
-
447
- for (int i = 0; !deck.empty(); i = (i + 1) % nplayers) {
448
-
449
- Card c = deck.draw();
450
-
451
- players[i].add(c);;
452
-
453
- }
454
-
455
-
456
-
457
- // 各プレイヤーの手札をソートする
458
-
459
- for (auto &player : players) {
460
-
461
- player.sort();
462
-
463
- }
464
-
465
-
466
-
467
- // 各プレイヤーの手札を出力する
468
-
469
- for (int n = 0; n < 13; ++n) {
470
-
471
- for (int i = 0; i < nplayers; ++i) {
472
-
473
- std::cout << std::setw(16) << players[i].at(n);
474
-
475
- }
476
-
477
- std::cout << std::endl;
478
-
479
- }
480
-
481
- }
482
540
 
483
541
  break;
484
542
 

3

//epistemeさんのコードを拝借しまして、そこからから発展させようとしております。

2018/10/21 07:00

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -178,6 +178,8 @@
178
178
 
179
179
  ```C++
180
180
 
181
+ //epistemeさんのコードを拝借しまして、そこからから発展させようとしております。
182
+
181
183
  #include <string>
182
184
 
183
185
  #include <stdexcept>

2

コードを書き進めております。

2018/10/21 02:55

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -78,7 +78,7 @@
78
78
 
79
79
  メンバ関数 void shuffle()ランダムにシャッフルします。
80
80
 
81
- std::shuffle、std::random_shuffleなどは使ってはいけないです。
81
+ 問題文にstd::shuffle、std::random_shuffleなどは使ってはいけないと書いてあるのそれらは使えないです。
82
82
 
83
83
 
84
84
 
@@ -120,6 +120,52 @@
120
120
 
121
121
  ```
122
122
 
123
+ Enter 1 to shuffle, 2 to deal, or 3 to end:
124
+
125
+ 1
126
+
127
+ Deck shuffled.
128
+
129
+ 2
130
+
131
+ Spade 4 Spade 6 Spade 3 Spade 2
132
+
133
+ Heart 2 Spade 8 Spade 10 Spade 5
134
+
135
+ Heart 3 Spade Queen Spade Jack Spade 7
136
+
137
+ Heart 4 Spade Ace Heart 5 Spade 9
138
+
139
+ Heart 7 Heart 8 Heart 6 Spade King
140
+
141
+ Heart Ace Heart 10 Heart 9 Diamond 7
142
+
143
+ Diamond 2 Heart Queen Heart Jack Diamond 8
144
+
145
+ Diamond 3 Heart King Diamond 5 Diamond 9
146
+
147
+ Diamond 6 Diamond 4 Diamond Jack Diamond Ace
148
+
149
+ Diamond 10 Diamond Queen Diamond King Club 2
150
+
151
+ Club 8 Club 4 Club 6 Club 3
152
+
153
+ Club Jack Club 5 Club King Club 7
154
+
155
+ Club Queen Club 9 Club Ace Club 10
156
+
157
+ 1
158
+
159
+ Deck shuffled.
160
+
161
+ 2
162
+
163
+ libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
164
+
165
+
166
+
167
+ Process finished with exit code 6
168
+
123
169
 
124
170
 
125
171
  ```
@@ -140,14 +186,16 @@
140
186
 
141
187
  #include <iomanip>
142
188
 
189
+ #include <algorithm>
190
+
191
+ #include <vector>
192
+
193
+ //#include <random>
194
+
195
+ #include <stdio.h>
196
+
143
197
  #include <time.h>
144
198
 
145
- #include <stdlib.h>
146
-
147
- #include <algorithm>
148
-
149
- #include <cctype>
150
-
151
199
 
152
200
 
153
201
  using namespace std;
@@ -168,7 +216,11 @@
168
216
 
169
217
  public:
170
218
 
171
- Card (int n=0, Suit s=Suit(-1)) : number(n), suit(s) {}
219
+ Card(int n=0, Suit s=Suit(-1)) : number(n), suit(s) {}
220
+
221
+ Card(const Card& c) : number(c.number), suit(c.suit) {}
222
+
223
+
172
224
 
173
225
  int getNumber() const {
174
226
 
@@ -190,394 +242,268 @@
190
242
 
191
243
 
192
244
 
193
-
245
+ int key() const {
246
+
194
-
247
+ int n = getNumber() - 1;
248
+
249
+ if ( n == 0 ) n = 13;
250
+
251
+ return (int)getSuit() * 13 + n;
252
+
253
+ }
254
+
255
+ };
256
+
257
+
258
+
195
- bool operator < (const Card& c) {
259
+ inline bool operator<(const Card& c1, const Card& c2) {
260
+
196
-
261
+ return c1.key() < c2.key();
262
+
263
+ }
264
+
265
+
266
+
267
+ string to_string(const Card::Suit& s) {
268
+
269
+ switch ( s ) {
270
+
197
- if ("Club" < "Diamond" < "Heart" < "Spade") {
271
+ case Card::Suit::Spade : return "Spade";
272
+
198
-
273
+ case Card::Suit::Heart : return "Heart";
274
+
275
+ case Card::Suit::Diamond : return "Diamond";
276
+
277
+ case Card::Suit::Club : return "Club";
278
+
279
+ }
280
+
199
- return true;
281
+ return string();
282
+
283
+ }
284
+
285
+
286
+
287
+ string Card::description() const {
288
+
289
+ static const char* nums[] = { "Ace", "2", "3", "4", "5",
290
+
291
+ "6", "7", "8", "9", "10",
292
+
293
+ "Jack", "Queen", "King" };
294
+
295
+ return to_string(getSuit()) + " " + nums[getNumber()-1];
296
+
297
+ }
298
+
299
+
300
+
301
+ inline ostream& operator<<(ostream& stream, const Card& c) {
302
+
303
+ return stream << c.description();
304
+
305
+ }
306
+
307
+
308
+
309
+ const int SIZE = 52;
310
+
311
+ Card deck[52];
312
+
313
+
314
+
315
+ class Deck {
316
+
317
+ private:
318
+
319
+ vector<Card> deck_;
320
+
321
+ Card decks[SIZE];
322
+
323
+ public:
324
+
325
+ Deck() {
326
+
327
+ for ( int i = 0; i < 13; ++i ) {
328
+
329
+ for ( int j = 0; j < 4; ++j ) {
330
+
331
+ deck_.push_back(Card(i+1,Card::Suit(j)));
332
+
333
+ }
200
334
 
201
335
  }
202
336
 
337
+ }
338
+
203
- if (suit = c.suit) {
339
+ void shuffle() {
340
+
204
-
341
+ srand((unsigned int)time(NULL));
342
+
343
+
344
+
345
+ for (int i=0; i<SIZE ;i++)
346
+
347
+ {
348
+
349
+ // Random for remaining positions.
350
+
205
- if () {
351
+ int r = i + (rand() % (52 -i));
206
-
352
+
353
+
354
+
207
- return true;
355
+ swap(deck_[i], deck_[r]);
208
356
 
209
357
  }
210
358
 
211
- return false;
359
+ // static std::mt19937 gen;
360
+
361
+ // std::shuffle(deck_.begin(), deck_.end(), gen);
212
362
 
213
363
  }
214
364
 
365
+ Card draw() { Card c = deck_.back(); deck_.pop_back(); return c; }
366
+
367
+ bool empty() const { return deck_.empty(); }
368
+
215
369
  };
216
370
 
217
371
 
218
372
 
219
373
 
220
374
 
375
+ class Player {
376
+
377
+ private:
378
+
379
+ vector<Card> hand;
380
+
381
+ string name_;
382
+
383
+
384
+
385
+ public:
386
+
387
+ //std::sortを使わずに他のBubble sort、Selection sort、Merge sortなどを使ってソートする方法を現在探しています。
388
+
389
+ void sort() { std::sort(hand.begin(), hand.end()); }
390
+
391
+ void add(const Card& card) { hand.push_back(card); }
392
+
221
- string to_string(const Card::Suit& s) {
393
+ const Card& at(int n) const { return hand.at(n); }
394
+
222
-
395
+ };
396
+
397
+
398
+
399
+
400
+
401
+ int main() {
402
+
403
+
404
+
405
+ Deck deck;
406
+
407
+
408
+
409
+ int enter;
410
+
411
+
412
+
413
+ cout << "Enter 1 to shuffle, 2 to deal, or 3 to end: " << endl;
414
+
415
+
416
+
417
+ do {
418
+
419
+ cin >> enter;
420
+
421
+
422
+
423
+
424
+
223
- switch ( s ) {
425
+ switch (enter) {
426
+
224
-
427
+ case 1:
428
+
429
+ deck.shuffle();
430
+
225
- case Card::Suit::Spade : return "Spade";
431
+ cout << "Deck shuffled." << endl;
432
+
226
-
433
+ break;
434
+
435
+
436
+
437
+ case 2: {
438
+
439
+ // 4人のプレイヤーにカードを配る
440
+
441
+ const int nplayers = 4;
442
+
443
+ Player players[nplayers];
444
+
227
- case Card::Suit::Heart : return "Heart";
445
+ for (int i = 0; !deck.empty(); i = (i + 1) % nplayers) {
228
-
229
- case Card::Suit::Diamond : return "Diamond";
446
+
230
-
231
- case Card::Suit::Club : return "Club";
447
+ Card c = deck.draw();
448
+
232
-
449
+ players[i].add(c);;
450
+
233
- }
451
+ }
452
+
453
+
454
+
234
-
455
+ // 各プレイヤーの手札をソートする
456
+
457
+ for (auto &player : players) {
458
+
235
- return string();
459
+ player.sort();
460
+
461
+ }
462
+
463
+
464
+
465
+ // 各プレイヤーの手札を出力する
466
+
467
+ for (int n = 0; n < 13; ++n) {
468
+
469
+ for (int i = 0; i < nplayers; ++i) {
470
+
471
+ std::cout << std::setw(16) << players[i].at(n);
472
+
473
+ }
474
+
475
+ std::cout << std::endl;
476
+
477
+ }
478
+
479
+ }
480
+
481
+ break;
482
+
483
+
484
+
485
+ case 3:
486
+
487
+ cout << "Bye" << endl;
488
+
489
+ break;
490
+
491
+
492
+
493
+ default:
494
+
495
+ break;
496
+
497
+ }
498
+
499
+
500
+
501
+ } while (enter != 3);
236
502
 
237
503
  }
238
504
 
239
505
 
240
506
 
241
- string Card::description() const {
242
-
243
- static const char* nums[] = { "Ace", "2", "3", "4", "5",
244
-
245
- "6", "7", "8", "9", "10",
246
-
247
- "Jack", "Queen", "King" };
248
-
249
- return string(nums[getNumber()-1]) + " " + to_string(getSuit());
250
-
251
- }
252
-
253
-
254
-
255
- class Deck {
256
-
257
- private:
258
-
259
- Card deck[52] = {};
260
-
261
-
262
-
263
- public:
264
-
265
-
266
-
267
- // Function which shuffle and print the array
268
-
269
- void shuffle(int deck[], int n)
270
-
271
- {
272
-
273
-
274
-
275
- Card card;
276
-
277
-
278
-
279
- for(int i = 0; i < 52; i++)
280
-
281
- {
282
-
283
- int b = (rand() + time(0)) % 52;
284
-
285
- Card temp = deck[i];
286
-
287
- deck[i] = deck[b];
288
-
289
- deck[b] = temp;
290
-
291
- }
292
-
293
- //
294
-
295
- // // Initialize seed randomly
296
-
297
- // srand(time(0));
298
-
299
- //
300
-
301
- // for (int i=0; i<n ;i++)
302
-
303
- // {
304
-
305
- // // Random for remaining positions.
306
-
307
- // int r = i + (rand() % (52 -i));
308
-
309
- //
310
-
311
- // swap(deck[i], deck[r]);
312
-
313
- // }
314
-
315
- }
316
-
317
-
318
-
319
- // void Deck::shuffle(){
320
-
321
- // Card card;
322
-
323
- // bool match = false;
324
-
325
- // for (int i=0;i<52;i++){
326
-
327
- // do{
328
-
329
- // card.setCard();
330
-
331
- // match = cardInDeck(card, i);
332
-
333
- // }while(match == true);
334
-
335
- // match = false;
336
-
337
- // cards[i] = card;
338
-
339
- // }
340
-
341
- // numDrawn = 0;
342
-
343
- // }
344
-
345
-
346
-
347
- };
348
-
349
-
350
-
351
-
352
-
353
- class Player {
354
-
355
- private:
356
-
357
- int hand[13];
358
-
359
- string name;
360
-
361
- int size;
362
-
363
-
364
-
365
- public:
366
-
367
-
368
-
369
- void sort(int hand[], int size) {
370
-
371
-
372
-
373
- string temp;
374
-
375
- bool madeAswap;
376
-
377
-
378
-
379
- do {
380
-
381
- madeAswap = false;
382
-
383
- for (int count = 0; count < (size - 1); count++) {
384
-
385
-
386
-
387
- if (hand[count] > hand[count + 1]) {
388
-
389
-
390
-
391
- temp = hand[count];
392
-
393
- hand[count] = hand[count + 1];
394
-
395
- hand[count + 1] = temp;
396
-
397
- madeAswap = true;
398
-
399
-
400
-
401
- }
402
-
403
- }
404
-
405
-
406
-
407
- } while(madeAswap);
408
-
409
- }
410
-
411
-
412
-
413
-
414
-
415
- int clubTwo(const int hand[], int size, string name) {
416
-
417
-
418
-
419
- int first = 0,
420
-
421
- last = size - 1,
422
-
423
- middle,
424
-
425
- position = -1;
426
-
427
- bool found = false;
428
-
429
-
430
-
431
- while (!found && first <= last)
432
-
433
- {
434
-
435
- middle = (first + last) / 2;
436
-
437
- if (hand[middle] == name)
438
-
439
- {
440
-
441
- found = true;
442
-
443
- position = middle;
444
-
445
- }
446
-
447
- else if (hand[middle] > name)
448
-
449
- last = middle - 1;
450
-
451
- else
452
-
453
- first = middle + 1;
454
-
455
- }
456
-
457
- return position;
458
-
459
- }
460
-
461
-
462
-
463
- };
464
-
465
-
466
-
467
-
468
-
469
- int main() {
470
-
471
- Card deck[13][4];
472
-
473
- for ( int i = 0; i < 13; ++i ) {
474
-
475
- for ( int j = 0; j < 4; ++j ) {
476
-
477
- deck[i][j] = Card(i+1,Card::Suit(j));
478
-
479
- }
480
-
481
- }
482
-
483
-
484
-
485
- for ( auto& row : deck ) {
486
-
487
- for ( auto& card : row ) {
488
-
489
- cout << setw(16) << card.description();
490
-
491
- }
492
-
493
- cout << endl;
494
-
495
- }
496
-
497
-
498
-
499
-
500
-
501
- int enter;
502
-
503
-
504
-
505
- cout <<"Enter 1 to shuffle, 2 to deal, or 3 to end: " << endl;
506
-
507
-
508
-
509
- do {
510
-
511
- cin >> enter;
512
-
513
-
514
-
515
- int cards;
516
-
517
-
518
-
519
- switch(enter){
520
-
521
- case 1:
522
-
523
- deck.shuffle();
524
-
525
- cout<<"Deck shuffled."<<endl;
526
-
527
- cards = 0;
528
-
529
- break;
530
-
531
- case 2:
532
-
533
- if (cards == 52){
534
-
535
- deck.shuffle();
536
-
537
- cards = 0;
538
-
539
- break;
540
-
541
- case 3:
542
-
543
- break;
544
-
545
-
546
-
547
- } while(enter != 3);
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
- Deck de = new Deck;
560
-
561
-
562
-
563
- de.shuffle(deck, 52);
564
-
565
-
566
-
567
-
568
-
569
- for (int i=0; i<52; i++)
570
-
571
- cout << deck[i] << " ";
572
-
573
- cout << endl;
574
-
575
-
576
-
577
-
578
-
579
- }
580
-
581
507
 
582
508
 
583
509
  ```

1

switchのところを少し変更しました。

2018/10/20 19:02

投稿

amareno
amareno

スコア20

test CHANGED
File without changes
test CHANGED
@@ -144,6 +144,10 @@
144
144
 
145
145
  #include <stdlib.h>
146
146
 
147
+ #include <algorithm>
148
+
149
+ #include <cctype>
150
+
147
151
 
148
152
 
149
153
  using namespace std;
@@ -406,7 +410,7 @@
406
410
 
407
411
 
408
412
 
409
-
413
+
410
414
 
411
415
  int clubTwo(const int hand[], int size, string name) {
412
416
 
@@ -494,11 +498,11 @@
494
498
 
495
499
 
496
500
 
497
- char enter;
501
+ int enter;
498
-
499
-
500
-
502
+
503
+
504
+
501
- cout <<"Enter 's' to shuffle, 'd' to deal, or 'e' to end: " << endl;
505
+ cout <<"Enter 1 to shuffle, 2 to deal, or 3 to end: " << endl;
502
506
 
503
507
 
504
508
 
@@ -514,13 +518,9 @@
514
518
 
515
519
  switch(enter){
516
520
 
517
- case 'E':
521
+ case 1:
518
-
519
- case 'e':break;
522
+
520
-
521
- case 'S':
522
-
523
- case 's':deck.shuffle();
523
+ deck.shuffle();
524
524
 
525
525
  cout<<"Deck shuffled."<<endl;
526
526
 
@@ -528,9 +528,9 @@
528
528
 
529
529
  break;
530
530
 
531
- case 'D':
531
+ case 2:
532
-
532
+
533
- case 'd':if (cards == 52){
533
+ if (cards == 52){
534
534
 
535
535
  deck.shuffle();
536
536
 
@@ -538,7 +538,21 @@
538
538
 
539
539
  break;
540
540
 
541
+ case 3:
542
+
543
+ break;
544
+
545
+
546
+
541
- } while(enter != 'e');
547
+ } while(enter != 3);
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+
542
556
 
543
557
 
544
558
 
@@ -550,16 +564,18 @@
550
564
 
551
565
 
552
566
 
553
- // Printing all shuffled elements of cards
567
+
554
568
 
555
569
  for (int i=0; i<52; i++)
556
570
 
557
- cout << a[i] << " ";
571
+ cout << deck[i] << " ";
558
572
 
559
573
  cout << endl;
560
574
 
561
575
 
562
576
 
577
+
578
+
563
579
  }
564
580
 
565
581