Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __CARD_H__
- #define __CARD_H__
- #include <stdlib.h>
- #include <stdio.h>
- #include<time.h>
- typedef enum {HEARTS, CLUBS, SPADES, DIMONDS} suit_t;
- typedef struct card_s {
- suit_t suit;
- int value;
- } *card_t;
- typedef card_t deck[52];
- card_t Make_Card(suit_t suit, int value)
- {
- card_t tmp;
- if(value > 13 || value < 1){
- fprintf(stderr,"Value Must Be Between 1 and 13\n");
- exit(1);
- }
- tmp = (card_t)(malloc(sizeof(struct card_s)));
- tmp->suit = suit;
- tmp->value = value;
- return tmp;
- }
- void Print_Suit(FILE* fp, suit_t suit)
- {
- if (suit == HEARTS) {
- fprintf(fp, "❤");
- } else if (suit == CLUBS) {
- fprintf(fp, "♣");
- } else if (suit == DIMONDS) {
- fprintf(fp, "♦");
- } else if (suit == SPADES) {
- fprintf(fp, "♠");
- } else {
- fprintf(fp, "NAN");
- }
- }
- void Print_Card(FILE* fp, card_t card)
- {
- if(card->value == 1){
- fprintf(fp, " A");
- }
- else if(card->value == 11){
- fprintf(fp, " J");
- }
- else if(card->value == 12){
- fprintf(fp, " Q");
- }
- else if(card->value == 13){
- fprintf(fp, " K");
- }
- else if(card->value == 10){
- fprintf(fp, "10");
- }
- else{
- fprintf(fp, " %d", card->value);
- }
- Print_Suit(fp, card->suit);
- fprintf(fp, "\n");
- }
- void Create_Deck(deck mydeck)
- {
- int i;
- for (i=0;i<13;i++){
- mydeck[i*4+0] = Make_Card(DIMONDS, i+1);
- mydeck[i*4+1] = Make_Card(CLUBS, i+1);
- mydeck[i*4+2] = Make_Card(SPADES, i+1);
- mydeck[i*4+3] = Make_Card(HEARTS, i+1);
- }
- }
- int main()
- {
- int i;
- deck mydeck;
- Create_Deck(mydeck);
- srand(time(0));
- for(i=0;i<5;i++){
- int c = rand()%52;
- Print_Card(stdout, mydeck[c]);
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement