Library to handle a deck of cards
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

89 lines
2.5 KiB

/*
* example-01.c
*
* This file is part of the deckhandler library
* <https://github.com/theimpossibleastronaut/deckhandler>
*
* Copyright 2019 Andy <andy400-dev@yahoo.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "deckhandler.h"
int main (int argc, char *argv[])
{
/* declare a deck using type "st_deck_dh" (defined in deckhandler.h) */
st_deck_dh deck_a;
/* initialize the deck */
deck_init_dh (&deck_a);
int deals_num = 0;
int deals_max = 3;
/* random generator used by deck_shuffle_dh() */
srand (time (NULL));
do
{
/* shuffle the deck */
puts ("\n\n\t]=[ Shuffling ]=[\n");
deck_shuffle_dh (&deck_a);
/* show each card in the deck */
int deal;
for (deal = 0; deal < CARDS_IN_DECK; deal++)
{
/* The "faces" and "suits" arrays are initialized in deckhandler.c */
printf ("%s of %s\n",
get_card_face(deck_a.card[deal]),
get_card_suit(deck_a.card[deal]));
}
}while (deals_num++ < deals_max);
puts ("\n\n\t]=[ Create 4 decks, shuffle each one, and deal them all out ]=[\n");
int total_decks = 4;
st_deck_dh deck_num[total_decks];
int which_deck;
for (which_deck = 0; which_deck < total_decks; which_deck++)
{
deck_init_dh (&deck_num[which_deck]);
deck_shuffle_dh (&deck_num[which_deck]);
}
int card;
/* There's no function in the library (yet) that shuffles multiple decks
* together, but using the method below will provide a close simulation */
for (card = 0; card < CARDS_IN_DECK; card++)
{
for (which_deck = 0; which_deck < total_decks; which_deck++)
{
printf ("%s of %s\n",
get_card_face(deck_num[which_deck].card[card]),
get_card_suit(deck_num[which_deck].card[card]));
}
}
return 0;
}