Code:
/* Comp 120 - 01 Assignment 3, BlackJack */
/* November 10th 2009 */
/* Author: Clifford Donovan */
/* This program runs a game of blackjack, starting with the player's turn, and
then the computer's. */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/* function prototypes */
void InitializeDeck();
int RandomNumber(int);
void ShuffleDeck();
void DisplayCard(int);
void DisplayDeck();
int ScoreCard(int);
int DrawCard(int[], int);
void clean();
#define DECK_SIZE 53
int main()
{
int ppeanuts = 10; /*player's peanuts*/
int cpeanuts = 10; /*computer's peanuts*/
int deck[DECK_SIZE]; /*deck array*/
int cardcount = 0; /*count that increases after each drawn card*/
int scorep; /*player's score*/
int scorec; /*computer's score*/
char answer; /*answer for another card question*/
char anotheranswer = 'y'; /*answer for another round question*/
printf("Welcome to ULTIMATE BlackJack \n");
printf("In ULTIMATE BlackJack, Aces are worth 1, and only 1\n");
printf("Face cards are all worth 10. All other cards are worth their face value \n");
printf("You and I start with 10 peanuts each. \nThe winner of a round takes one peanut from the loser.\n");
printf("When one of us runs out, the game ends.\n");
printf("Good Luck, you'll need it. \n\n\n");
srand(time(NULL)); /* uses time as a seed for the random function used later on */
InitializeDeck(deck); /* initializes the deck */
ShuffleDeck(deck); /* shuffles the deck */
while (ppeanuts != 0 && cpeanuts != 0 && anotheranswer == 'y')
{
if (cardcount == (DECK_SIZE - 1))
{
ShuffleDeck(deck);
}
scorep = 0; scorec = 0;
printf("Your Peanuts: %d My Peanuts: %d\n", ppeanuts, cpeanuts);
printf("--------------------------------\n\n");
printf("Your turn:\n\n");
scorep = scorep + DrawCard(deck, cardcount);
printf("Score: %i\n\n", scorep);
cardcount++;
scorep = scorep + DrawCard(deck, cardcount);
printf("Score: %i\n\n", scorep);
cardcount++;
printf("Another Card?\n\n");
scanf("%c", &answer);
clean();
while (scorep <= 21 && answer == 'y' )
{
if (cardcount == (DECK_SIZE - 1))
{
ShuffleDeck(deck);
}
scorep = scorep + DrawCard(deck, cardcount),
printf("Score: %i\n\n", scorep);
if(scorep >21)
break; /* breaks the while loop if the player busts */
cardcount++,
printf("Another Card?\n\n"),
scanf("%c", &answer);
clean();
}
if (answer == 'n')
{
printf("My turn:\n\n");
while (scorec < 17)
{
if (cardcount == (DECK_SIZE - 1))
{
ShuffleDeck(deck);
}
scorec = scorec + DrawCard(deck, cardcount),
printf("Score: %i\n\n", scorec),
cardcount++;
}
}
if (answer != 'n' && answer != 'y')
{
printf("That is not a valid answer\n");
}
if (scorep > 21)
{
printf("You Bust! I win.\n\n");
ppeanuts --;
cpeanuts ++;
printf("Another Round?");
scanf("%c", &anotheranswer);
clean();
}
if (scorec > 21)
{
printf("I Bust, You Win...This Time\n\n");
ppeanuts++;
cpeanuts--;
printf("Another Round?");
scanf("%c", &anotheranswer);
clean();
}
if (scorep > scorec && scorep < 22 && scorec < 22 )
{
printf("You Win\n\n");
ppeanuts++;
cpeanuts--;
printf("Another Round?");
scanf("%c", &anotheranswer);
clean();
}
if (scorec > scorep && scorec < 22 && scorep < 22)
{
printf("I Win. \n\n");
ppeanuts--;
cpeanuts++;
printf("Another Round?");
scanf("%c", &anotheranswer);
clean();
}
if (scorec == scorep)
{
printf("Push!\n\n");
printf("Another Round?");
scanf("%c", &anotheranswer);
clean();
}
if (cardcount == (DECK_SIZE - 1))
{
ShuffleDeck(deck);
}
if (ppeanuts < cpeanuts)
{
printf("Game Over. I win. \n\n");
}
else if (ppeanuts > cpeanuts)
{
printf("Game Over. You Win. \n\n");
}
}
system("PAUSE");
return 0;
}
/*********************************************************************
* clean()
* Reads all characters from the input buffer until NEWLINE reached
* PRE: none
* POST: input stream is empty
*********************************************************************/
void clean (void)
{
char dummy;
do
{
scanf("%c",&dummy);
}
while(dummy != '\n');
}
/*********************************************************************
* InitializeDeck(int deck[DECK_SIZE])
* Used to test and make sure the deck is shuffled correctly (if need be)
* PRE: deck array initialized in main.
* POST: makes the deck array from 0-52. 52 to tell the program to shuffle.
*********************************************************************/
void InitializeDeck(int deck[DECK_SIZE])
{
int index;
for (index = 0; index < DECK_SIZE; index++)
{
deck[index] = index;
}
}
/*********************************************************************
* RandomNumber(int upperlimit)
* Function gets a random number between 0-51, then 0-50...etc.
* PRE: index is being counted down in the ShuffleDeck function
also, time is used as a seed, in the main function.
* POST: returns the random number generated
*********************************************************************/
int RandomNumber(int upperlimit)
{
int number;
number = rand()%(upperlimit +1);
return number;
}
/*********************************************************************
* ShuffleDeck(int deck[DECK_SIZE])
* Uses the random number to shuffle the deck(array)
* PRE: random number generated correctly
* POST: Deck is shuffled
*********************************************************************/
void ShuffleDeck(int deck[DECK_SIZE])
{
int rand;
int index;
int dummy;
for(index = 51; index >= 0; index--)
{
rand = RandomNumber(index);
dummy = deck[rand];
deck[rand] = deck[index];
deck[index] = dummy;
}
}
/*********************************************************************
* DisplayCard(int size)
* Displays card and suit, in a neat little picture :)
* PRE: only takes the card as a parameter
* POST: will print the card in it's neat little format
*********************************************************************/
void DisplayCard(int size)
{
int num = 0;
int rank;
char suit;
num = (size / 13);
rank = (size % 13) ;
if (num == 0)
suit = 6;
else if (num == 1)
suit = 3;
else if (num == 2)
suit = 4;
else if (num == 3)
suit = 5;
switch(size % 13)
{
case 1:
printf("______\n"),
printf("|A |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| A|\n"),
printf("______\n");
break;
case 2:
printf("______\n"),
printf("|2 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 2|\n"),
printf("______\n");
break;
case 3:
printf("______\n"),
printf("|3 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 3|\n"),
printf("______\n");
break;
case 4:
printf("______\n"),
printf("|4 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 4|\n"),
printf("______\n");
break;
case 5:
printf("______\n"),
printf("|5 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 5|\n"),
printf("______\n");
break;
case 6:
printf("______\n"),
printf("|6 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 6|\n"),
printf("______\n");
break;
case 7:
printf("______\n"),
printf("|7 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 7|\n"),
printf("______\n");
break;
case 8:
printf("______\n"),
printf("|8 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 8|\n"),
printf("______\n");
break;
case 9:
printf("______\n"),
printf("|9 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 9|\n"),
printf("______\n");
break;
case 10:
printf("______\n"),
printf("|10 |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| 10|\n"),
printf("______\n");
break;
case 11:
printf("______\n"),
printf("|J |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| J|\n"),
printf("______\n");
break;
case 12:
printf("______\n"),
printf("|Q |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| Q|\n"),
printf("______\n");
break;
case 13:
printf("______\n"),
printf("|K |\n"),
printf("|%c |\n", suit),
printf("| %c%c |\n", suit, suit),
printf("| %c%c |\n", suit, suit),
printf("| %c|\n", suit),
printf("| K|\n"),
printf("______\n");
break;
}
}
/*********************************************************************
* DisplayDeck()
* Used to test and make sure the deck is shuffled correctly (if need be)
* PRE: Deck iniitialized
* POST: none.
*********************************************************************/
void DisplayDeck()
{
int index;
for (index = 0; index < DECK_SIZE; index++)
{
DisplayCard(index);
}
}
/*********************************************************************
* ScoreCard(int card)
* Assigns each card it's score, Ace is 1, face cards are 10,
the rest are their face value.
* PRE: Card is valid
* POST: returns the score of the card
*********************************************************************/
int ScoreCard(int card)
{
int score = 0;
if (card%13 == 0)
score = 1;
else if (card%13 > 0 && card%13 <= 9)
score = card%13;
else score = 10;
return score;
}
/*********************************************************************
* DrawCard(int deck[DECK_SIZE], int count)
* Draws a card from the deck
* PRE: takes the cardcount (the counter that counts throughout the game)
to draw the card in order
* POST: returns the the score of the drawn card from the ScoreCard function
also calls the DisplayCard function so this prints the card as well.
*********************************************************************/
int DrawCard(int deck[DECK_SIZE], int count)
{
int score;
int card;
card = deck[count];
DisplayCard(card);
score = ScoreCard(card);
return score;
}