12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "cards.h"
- // Limits of the deck: 13 cards over 4 different suits possible.
- #define MAX_CARD 13
- #define MAX_SUIT 4
- // This function validates the card (suit and value) and prints a description
- // of it to stdout. Sample descriptions: 2 of Hearts. King of Spades.
- // Returns 0 if the card is valid, 1 if it's not (either invalid suit or value)
- int
- describe_card(struct playingcard c)
- {
- if (c.value < 1 || c.value > 13) {
- printf("Error: you've passed an invalid card!\n");
- return 1;
- }
- if (c.suit < 1 || c.suit > 4) {
- printf("Error: your card has an invalid suit!\n");
- return 1;
- }
-
- switch(c.value) {
- case 1:
- printf("Ace");
- break;
- case 11:
- printf("Jack");
- break;
- case 12:
- printf("Queen");
- break;
- case 13:
- printf("King");
- break;
- default:
- printf("%d", c.value);
- }
-
- // (value) of (suit)
- printf(" of ");
-
- switch(c.suit) {
- case 1:
- printf("Diamonds\n");
- break;
- case 2:
- printf("Clubs\n");
- break;
- case 3:
- printf("Hearts\n");
- break;
- case 4:
- printf("Spades\n");
- break;
- }
- return 0;
- }
- // This function creates a random card from the playing deck.
- struct playingcard
- draw_card()
- {
- int dice = 0;
- struct playingcard card;
- dice = rand() % MAX_CARD;
- if (dice == 0)
- dice += 1;
- card.value = dice;
- dice = rand() % MAX_SUIT;
- if (dice == 0)
- dice += 1;
- card.suit = dice;
- return card;
- }
|