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
|
#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]);
void suitsInHand(char *suits[], int suitsInMyHand[], void dealACard(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 };
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);
suitsInHand(suitsInMyHand, suits);
}
printf("\n");
}
void dealACard(char *suits[], char *faces[], int deck[][FACES]) {
int suitIndex, faceIndex;
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]);
}
void suitsInHand(int suitsInMyHand[], void dealACard(int suitIndex)) {
int suitIndex = 0;
if (suitsInMyHand[suitIndex] == 0) {
suitsInMyHand[0]++;
}
else if (suitsInMyHand[suitIndex] == 1) {
suitsInMyHand[1]++;
}
else if (suitsInMyHand[suitIndex] == 2) {
suitsInMyHand[2]++;
}
else if (suitsInMyHand[suitIndex] == 3) {
suitsInMyHand[3]++;
}
}
|