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
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1
void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]);
void suitsInHand(int suitsInMyHand[], int suitIndex);
main() {
char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
int deck[4][13] = { AVAILABLE };
int suitsInMyHand[4] = { 0, 0, 0, 0 };
int i;
srand(time(NULL));
for (i = 0; i < 4; i++)
dealAHand(suits, faces, deck, suitsInMyHand);
system("pause");
}
void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]) {
int i;
for (i = 0; i < 5; i++)
dealACard(suits, faces, deck, suitsInMyHand);
printf("\n");
}
void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]) {
int suitIndex, faceIndex;
int i;
suitIndex = rand() % 4;
faceIndex = rand() % 13;
while (deck[suitIndex][faceIndex] == TAKEN) {
suitIndex = rand() % 4;
faceIndex = rand() % 13;
}
deck[suitIndex][faceIndex] = TAKEN;
printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);
for (i = 0; i < 5; i++)
suitsInHand(suitsInMyHand, suits);
}
void suitsInHand(int suitsInMyHand[], int suitIndex) {
suitsInMyHand[suitIndex]++;
}
|