practice/blackjack/blackjack.c

77 lines
1.1 KiB
C
Raw Normal View History

2016-11-07 19:35:17 -07:00
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define CARD_MAX 51
#define len(x) sizeof(x)/sizeof(x[0])
void print_deck (int *deckOfCards);
void shuffel (int *array, int n);
static int rand_int(int n);
int main (int argc, char *argv[])
{
int i;
int deckOfCards[CARD_MAX];
time_t t;
srand((unsigned) time(&t));
/* Clubs > Diamonds > Hearts > Spades
* 0-12 13-25 26-38 39-51
*/
for (i = 0; i<CARD_MAX; i++){
deckOfCards[i]=i;
}
shuffel(deckOfCards, CARD_MAX);
print_deck(deckOfCards);
return 0;
}
// Add a starting position. I can have a variable that simply means "continue drawing/printing cards from here"
void print_deck(int *deckOfCards){
int i;
for (i = 0; i<CARD_MAX; i++){
printf("%d\n", deckOfCards[i]);
}
}
void shuffel (int *array, int n){
int i, j, tmp;
for (i=n; i>-1 ;i--){
j = rand_int(i + 1);
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
static int rand_int(int n){
int limit = RAND_MAX - RAND_MAX % n;
int rnd;
do {
rnd=rand();
}
while (rnd >= limit);
return rnd % n;
}