With https://github.com/fyears/simple-Black- ... lackjack.c, blackjack.c will crash using the time() function for some targets.
You can do this instead.
blackjack.c shuffle function
Code: Select all
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int shuff(int cards[]) {
// Get the current time structure
struct tm* t = localtime(NULL);
int i;
int desk[52];
static unsigned int shuffle_count = 0; // Incremented on each shuffle
// Initialize the deck
for (i = 0; i < 52; i++)
desk[i] = (i / 13 + 3) * 100 + i % 13 + 1;
// Use a combination of time and shuffle count for the seed
unsigned int seed = t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600 + (++shuffle_count);
srand(seed);
// Shuffle the deck
for (i = 0; i < 52; i++) {
do {
t = rand() % 52;
} while (desk[t] == 0);
cards[i] = desk[t];
desk[t] = 0;
}
return 0;
}
Welcome to SimpleBlackJack!
Anytime you can press Ctrl+C to exit.
Enjoy! Press Enter to go on......
One of computer's cards:
*******
* *
* *
* 5 *
* *
*******
Cards of player:
*******
* *
* *
* 9 *
* *
*******
*******
* *
* *
* 8 *
* *
*******
Sum of player's cards now:17
Want more cards? Input y or n:
nSum of player's cards now:17
Computer's cards:
*******
* *
* *
* 5 *
* *
*******
*******
* *
* *
* 10 *
* *
*******
Sum of computer's cards now:15
Computer's card 3 is:
*******
* *
* *
* A *
* *
*******
Computer has chosen A as 1
Sum of computer's cards now:16
Computer's card 4 is:
*******
* *
* *
* K *
* *
*******
Sum of computer's cards now:26
Player win!
And now would you like to play again? Input 'y' or 'n':
y
OK, let's go again!
Welcome to SimpleBlackJack!
Anytime you can press Ctrl+C to exit.
Enjoy! Press Enter to go on......
One of computer's cards:
*******
* *
* *
* K *
* *
*******
Cards of player:
*******
* *
* *
* 8 *
* *
*******
*******
* *
* *
* 7 *
* *
*******
Sum of player's cards now:15
Want more cards? Input y or n:
yYou've got another card now.
and your card 3 is:
*******
* *
* *
* 3 *
* *
*******
Sum of player's cards now:18
Want more cards? Input y or n:
nSum of player's cards now:18
Computer's cards:
*******
* *
* *
* K *
* *
*******
*******
* *
* *
* 6 *
* *
*******
Sum of computer's cards now:16
Computer's card 3 is:
*******
* *
* *
* 6 *
* *
*******
Sum of computer's cards now:22
Player win!
And now would you like to play again? Input 'y' or 'n':
p.s. An overflow check should be done on shuffle_count, which I have not done yet.
Enjoy :-),
Andrew