Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h> //used for srand((unsigned) time(NULL)) function
#include <conio.h> //used for getch() function, to pause
typedef struct card {
int value; /* A=1, j=11, q=12, k=13 */
char suit; /* Spades='S' Clubs='C' Hearts='H' Dia='D' */
} card_t;
typedef struct deck {
card_t cards[208];
int top; // must be initialized to 0
} deck_t;
typedef struct hand {
card_t cards[10];
int num_cards; /// must be initialized to 0
} hand_t;
//Score keeper
typedef struct keeper {
int win;
int loss;
} keeper_t;
//Bank
typedef struct bank {
int cash;
int bet;
} bank_t;
//Print user's hand function
void print_hand (hand_t x, int play) {
int i;
printf("Player %d hand:\n", play);
for (i=0; i < x.num_cards; i++) {
printf ("[%c%d]", x.cards[i].suit, x.cards[i].value);
}
printf ("\n");
}
//Print Dealer's Hand Function
void print_dealer (hand_t x) {
int i;
printf("Dealers hand:\n");
for (i=0; i < 1; i++) {
printf ("[%c%d] [??]", x.cards[i].suit, x.cards[i].value);
}
printf ("\n");
}
//Function checks value of hand
int handval (hand_t x, int p, int play) {
int i,j;
p=0;
//printf("You have:%d cards\n", x.num_cards);//Temp code to check cards DELETE
for (i=0; i < x.num_cards; i++){
if (x.cards[i].value>10){ //makes all face cards = 10
x.cards[i].value=10;}
p =( x.cards[i].value )+p;}
//printf ("Player %d, You have a total of %d\n", play, p);
return p;
}
//Function checks value of dealer hand
int handvald (hand_t x, int p, int c) {
int i;
p=0;
//printf("You have:%d cards\n", x.num_cards);//Temp code to check cards DELETE
if (c==0){
for (i=0; i < x.num_cards-1; i++){
if (x.cards[i].value>10){ //makes all face cards = 10
x.cards[i].value=10;}
p =( x.cards[0].value );}
//printf ("Dealer subtotal is %d\n", p);
}
else if (c!=0){
for (i=0; i < x.num_cards; i++){
if (x.cards[i].value>10){ //makes all face cards = 10
x.cards[i].value=10;}
p =( x.cards[i].value )+p;}
//printf ("Dealer total is %d\n", p);
}
return p;
}
void fileresults();
int main () {
deck_t myDeck;
myDeck.top = 0;
//initialize variables
int num_players;
int play;
char choice;
char cur_suit;
int i, j, k, p, s;
int cash=500;
int turn;
int bust;
p = 0;
k = 0;
for (i=0; i<4; i++) { // suit types
if (i == 0) cur_suit = 'S';
else if (i == 1) cur_suit = 'C';
else if (i == 2) cur_suit = 'H';
else if (i == 3) cur_suit = 'D';
for (j=1; j <= 13; j++) { //Create 4 decks
myDeck.cards[k].suit = cur_suit;
myDeck.cards[k].value = j;
k++;
}
for (j=1; j <= 13; j++) {
myDeck.cards[k].suit = cur_suit;
myDeck.cards[k].value = j;
k++;
}
for (j=1; j <= 13; j++) {
myDeck.cards[k].suit = cur_suit;
myDeck.cards[k].value = j;
k++;
}
for (j=1; j <= 13; j++) {
myDeck.cards[k].suit = cur_suit;
myDeck.cards[k].value = j;
k++;
}
}
// Displays All four decks
/*for (i=0; i<208; i++) {
printf ("[%c%d]\n", myDeck.cards[i].suit,
myDeck.cards[i].value);
}*/
while (1) {
int user;
printf("--------------------------------------------\n");
printf("Welcome to Blackjack!\n");
printf("1.Play the game!\n");
printf("2.Rules\n");
printf("3.Quit\n");
printf("--------------------------------------------\n");
printf("Please make a selection:");
scanf (" %d", &user);
switch(user) {
case 1 :
printf("Please enter the number of players:");//Ask for num players
scanf(" %d", &num_players);
//Initialize score keeper and bank for up to 3 players and a dealer
bank_t banky[3];
keeper_t playe[3], deale;
for (i=1; i <= num_players; i++){
playe[i].loss=0;
playe[i].win=0;
banky[i].cash=500;
}
card_t temp;
srand((unsigned) time(NULL));//Creates random seed for rand function
for (i=0; i<1000; i++) { //shuffle
j = rand() % 208;
k = rand() % 208;
temp = myDeck.cards[j];
myDeck.cards[j] = myDeck.cards[k];
myDeck.cards[k] = temp;
}
//Displays decks after shuffle is done
/*printf ("-------------------------\n");
for (i=0; i<208; i++) {
printf ("[%c%d]\n", myDeck.cards[i].suit,
myDeck.cards[i].value);
}
*/
//////////// Create up to 4 hands and deal 2 cards to each player, show all player cards, but only one dealer card
while (myDeck.top<208){
hand_t player[3], dealer;
dealer.num_cards = 0;
for (j=0; j <2; j++){dealer.cards[ dealer.num_cards ] = myDeck.cards[ myDeck.top ];
dealer.num_cards++;
myDeck.top++;
}
bust=0;
turn=0;
for (i=1; i <= num_players; i++){ //Creates a hand with two cards for each player
player[i].num_cards = 0;
p = 0;
turn++;
play=i;
printf("Player %d\nYou have %d\n",play, banky[i].cash);
printf("Player %d \n", play);
printf("Okay, now how much of it do you want to lose, I mean, bet? \n");
scanf("%d", &banky[i].bet);
for (j=0; j < 2; j++) {
player[i].cards[ player[i].num_cards ] = myDeck.cards[ myDeck.top ];
player[i].num_cards++;
myDeck.top++;
}
printf ("---------------\n");
print_dealer (dealer);
p=handvald(dealer,p, 0);
printf ("Dealer subtotal is %d\n", p);
if (handvald(dealer,p, 1) < 17){
dealer.cards[ dealer.num_cards ] = myDeck.cards[ myDeck.top ];
dealer.num_cards++;
myDeck.top++;
}
printf ("---------------\n");
print_hand (player[i], play=i);
printf ("---------------\n");
//handval(player[i],p,play);
p=handval(player[i],p,play);
printf ("Player %d, You have a total of %d\n", play, p);
printf ("Player %d, would you like to stay or hit?(s/h):\n", play);
while (1){
scanf("%c", &choice);
if (p<21 && choice =='h'||choice =='H') {
player[i].cards [ player[i].num_cards ] = myDeck.cards[ myDeck.top ];
player[i].num_cards++;
myDeck.top++;
printf ("---------------\n");
print_hand (player[i], play=i);
p=handval(player[i],p,play); //Without this, will not stop when cards>21.
printf ("Player %d, You have a total of %d\n", play, p);
printf ("Player %d, would you like to stay or hit?(s/h):\n", play);
//printf("cards used %d\n", myDeck.top);//Just being used to test how many cards used DELETE
}
/*else if(p==21 && handvald(dealer,p, 1)!=21){
printf("You got 21! You won!!\n");
playe[i].win++;
printf("You have won %d times and lost %d times\n",playe[i].win, playe[i].loss);
printf ("---------------\n");
banky[i].cash = banky[i].cash + banky[i].bet;
printf("Aren't you a happy little boy. You now have: $%d\n", banky[i].cash);
printf("Press any key to continue...\n");
_getch();
break;
}
else if(p>21){
printf("Sorry you went over 21! You lose!\n");
playe[i].loss++;
printf("You have won %d times and lost %d times\n",playe[i].win, playe[i].loss);
banky[i].cash = banky[i].cash - banky[i].bet;
printf("Sad face %c. You now have: $%d\n", 2, banky[i].cash);
printf ("---------------\n");
bust++;
break;
}*/
else if( (p==21) || (p>21) || (p<21 && choice == 's' || choice== 'S')){
if (turn==num_players){//Needs work
for (i=1; i<=num_players; i++){
play=i;
printf("-------------------Player %d-------------------\n", play);
p=handval(player[i],p,play);
printf ("Player %d, You have a total of %d\n", play, p);
p=handvald(dealer,p, 1);
printf ("Dealer total is %d\n", p);//Print Dealer total
//Player>Dealer
if(handval(player[i],p,play)<21 && handval(player[i],p,play)>handvald(dealer,p, 1)){
//p=handval(player[i],p,play);
//printf ("Player %d, You have a total of %d\n", play, p);
//p=handvald(dealer,p, 1);
//printf ("Dealer total is %d\n", p);//Print Dealer total
printf("Player %d won!!\n", play);
playe[i].win++;
banky[i].cash = banky[i].cash + banky[i].bet;
printf ("---------------\n");
printf("Aren't you a happy little boy. You now have: $%d\n", banky[i].cash);
}
//Player=21 and dealer not 21
else if(handval(player[i],p,play)==21 && handvald(dealer,p, 1)!=21){
printf("You got 21! You won!!\n");
playe[i].win++;
//printf("You have won %d times and lost %d times\n",playe[i].win, playe[i].loss);
banky[i].cash = banky[i].cash + banky[i].bet;
printf ("---------------\n");
printf("Aren't you a happy little boy. You now have: $%d\n", banky[i].cash);
}
//Player>21 BUST
else if(handval(player[i],p,play)>21){
printf("Sorry you went over 21! You lose!\n");
playe[i].loss++;
banky[i].cash = banky[i].cash - banky[i].bet;
printf ("---------------\n");
printf("Sad face %c. You now have: $%d\n", 1, banky[i].cash);
}
//Dealer>Player
else if(handval(player[i],p,play)<handvald(dealer,p, 1) && handvald(dealer,p, 1)<22 || handval(player[i],p,play)==handvald(dealer,p, 1)){
printf("The dealer won, sorry.\n", play);
playe[i].loss++;
//printf("You have won %d times and lost %d times\n",playe[i].win, playe[i].loss);
banky[i].cash = banky[i].cash - banky[i].bet;
printf ("---------------\n");
printf("Sad face %c. You now have: $%d\n", 1, banky[i].cash);
}
//Dealer==Player-Dealer wins
else if(handval(player[i],p,play)==handvald(dealer,p, 1)){
printf("Player %d,You tied the dealer. The dealer won, sorry.\n", play);
playe[i].loss++;
banky[i].cash = banky[i].cash - banky[i].bet;
printf ("---------------\n");
printf("Sad face %c. You now have: $%d\n", 1, banky[i].cash);
}
else if(handvald(dealer,p, 1)>21 && handval(player[i],p,play)!=21 && handval(player[i],p,play)<21){
printf("The Dealer Busted, you win!\n");
playe[i].win++;
banky[i].cash = banky[i].cash + banky[i].bet;
printf ("---------------\n");
printf("Aren't you a happy little boy. You now have: $%d\n", banky[i].cash);
}
printf("You have won %d times and lost %d times\n",playe[i].win, playe[i].loss);
printf("-----------------------------------------------\n");
}}
//printf("Turns: %d\n", turn); //Temp to check that turn is working DELETE
printf("Press any key to continue:\n");
_getch(); //Requires key be pressed before displaying more
printf("-----------------------------------------------\n");
break;}
}}
if (myDeck.top==208){
break;
}
}
break;//Break from case 1
case 2://Case 2 Rules of game
printf("------\n");
printf("--------------\n");
printf("----------------------------------------------------------------------------------------------------\n");
printf("Rules of Play\n");
printf(" \n");
printf(" \n");
printf("1. You do not talk about Blackjack.\n");
printf("2. You DO NOT talk about Blackjack.\n");
printf("3. If someone says 'stop' or goes limp, taps out the fight is over.\n");
printf(" \n");
printf(" \n");
printf("4. The game starts with 4 decks\n");
printf("5. Each player begins the game with $500\n");
printf("6. The dealer has to hit until he has a score of 17 or higher\n");
printf("7. The Black Jack hand (Ace of Spades with a Black 10 or Face Card)\ndoubles the reward.\n");
printf("8. When the score between a user and the dealer is tied the dealer wins.\n");
printf("9. You can bet more than you have, but if you lose, you need to get lube and be creative to come up with the cash. The dealer doesn't like being indebted to.\n");
printf("----------------------------------------------------------------------------------------------------\n");
printf("--------------\n");
printf("------\n");
printf("Press any key to continue:");
_getch();
system("cls");
break;
case 3:
printf("Good bye!\n");
exit(1);
default:
printf("That is not a valid choice, please enter a number from 1-3\n");
break;
}
}
}