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
|
// Programmieren 1, Praktikum 5, Aufgabe 1
// Sven Eisenhauer
// 16.12.2004
//
// file: main.cpp
//
// purpose: Shuffle a standard deck of 52 cards and deal them
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::left;
using std::right;
#include <iomanip>
using std::setprecision;
using std::setw;
// contains function prototypes for functions srand and rand
#include <cstdlib>
#include <ctime>
const int maxColumns=13;
const int maxRows=4;
const int maxCards=52;
// prototypes
void shuffle( int [][ maxColumns ] );
void deal( const int [][ maxColumns ], const char *[], const char *[] );
int main()
{
// initialize suit array
const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
// initialize face array
const char *face[ 13 ] =
{ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
// initialize deck array
int deck[ maxRows ][ maxColumns ] =
{
{ 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 }
};
srand( time( 0 ) ); // seed random number generator
shuffle( deck );
deal( deck, face, suit );
return 0; // indicates successful termination
} // end main
// shuffle cards in deck
void shuffle( int wDeck[][ 13 ] )
{
int temp=0;
int randRow,
randCol;
for (int column=0;column<maxColumns;column++) {
for (int row=0;row<maxRows;row++) {
randCol=rand()%maxColumns;
randRow=rand()%maxRows;
temp=wDeck[row][column];
wDeck[row][column]=wDeck[randRow][randCol];
wDeck[randRow][randCol]=temp;
}
}
for (int row=0;row<maxRows;row++) {
for (column=0;column<maxColumns;column++) {
cout << setw(4) << wDeck[row][column];
}
cout<<endl;
}
} // end function shuffle
// deal cards in deck
void deal( const int wDeck[][ 13 ], const char *wFace[],
const char *wSuit[] )
{
enum found {FALSE,TRUE};
int row,column;
found cardFound=FALSE;
// for each of the 52 cards
for ( int card = 1; card <= maxCards; card++ ) {
cardFound=FALSE;
row=0;
while ((row<maxRows)&&cardFound==FALSE)
{
column=0;
while((column<maxColumns)&&cardFound==FALSE)
{
// if slot contains current card, display card
if ( wDeck[ row ][ column ] == card ) {
cout << setw( 5 ) << right << wFace[ column ]
<< " of " << setw( 8 ) << left
<< wSuit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
cardFound=TRUE;
}
column++;
} // end while
row++;
}
}
} // end function deal
|