Originally Posted by
turke92
Now I get what you mean by ASCII symbols, I'll keep that in mind for future programs.
Maybe the Fisher-Yates algorithm indeed is simple, but for my 3-4 weeks of C learning it's still a bit hard, but after I learn more about pointers, I will get to it since all the snippets I found on google did this with array pointers.
FWIW... here's a simple way to intialize and deal a deck of cards without duplicates...
Code:
// deck of cards demonstration
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#define CARDS 52
const char *face[] = {"Ace","Two","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Jack","Queen","King" };
const char *suit[] = {"Spades","Hearts","Clubs","Diamonds"};
// function to shuffle the deck
int Shuffle(int *deck)
{
int i, temp, card;
printf("\nShuffling...\n\n");
for (i = CARDS - 1; i > 0; i--)
{
card = rand() % i;
temp = deck[i];
deck[i] = deck[card];
deck[card] = temp;
}
return 0;
}
// program entry point
int main (void)
{
int deck[CARDS]; // deck
int card = CARDS + 1; // card
int fac, sut; // face and suit
int i; // counter
char ch = 0; // keyboard
srand(time(NULL));
printf("Card dealer simulation... \n\n");
//initialize the deck
for (i = 0; i < CARDS; i++)
deck[i] = i;
// deal cards
do
{
if ( card >= CARDS )
card = Shuffle(deck);
// face and suit
fac = deck[card] % 13;
sut = deck[card] / 13;
// display the card
printf("%2d > %s of %s\t\t",deck[card],face[fac],suit[sut]);
// set up for next card
card++;
// wait for user input
printf("(Enter to deal, Q to quit)\n");
ch = getchar();
}
while( toupper(ch) != 'Q');
return 0;
}