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
|
#include <iostream>
#include <iomanip> // for setw
using namespace std;
char player1;
char player2;
int p1score = 0;
int p2score = 0;
struct Card
{
char f;
char s;
};
void filldeck(Card [], char [], char []);
void shuffle(Card []);
void deal(Card []);
void whoWon();
int main()
{
Card deck[52];
char face[13] =
{'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
char suit[4] = {'H', 'D', 'C', 'S'};
filldeck(deck, face, suit);
shuffle( deck );
deal( deck );
return 0;
}
void filldeck(Card wDeck[], char wFace[], char wSuit[])
{
for (int i = 0; i < 52; i++)
{
wDeck[i].f = wFace[ i % 13 ];
wDeck[i].s = wSuit[ i / 13 ];
}
}
void shuffle(Card wDeck [])
{
srand( time(0) );
for (int i = 0; i < 52; i++)
{
int j = rand() % 52;
Card temp = wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
void deal(Card wDeck [] )
{
int count = 0;
for (int i = 0; i < 52; i++){
if (count %2 == 0){
player1 = wDeck[i].f;
cout << "Player 1's card:" <<setw(5) << wDeck[ i ].f <<
" of " << wDeck[i].s << endl;
}
else{
player2 = wDeck[i].f;
cout << "Player 2's card:" <<setw(5) << wDeck[ i ].f <<
" of " << wDeck[i].s << endl;
}
whoWon();
count++;
}
cout << "Player 1's score is: " << p1score << endl;
cout << "Player 2's score is: " << p2score << endl;
}
void whoWon(){
if(player1 > player2){
cout << "player 1 wins" << endl;
p1score++
}
else if(player2 >player1){
cout << "player 2 wins" << endl;
p2score++
}
else{
cout << "Its a Draw" << endl;
}
}
|