Code:
#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;
int cpeanuts = 10;
int deck[DECK_SIZE];
int cardcount = 0;
int scorep;
int scorec;
char answer;
char anotheranswer = 'y';
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));
InitializeDeck(deck);
ShuffleDeck(deck);
while (ppeanuts != 0 && cpeanuts != 0 && anotheranswer == 'y')
{
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' )
{
scorep = scorep + DrawCard(deck, cardcount),
printf("Score: %i\n\n", scorep);
if(scorep >21)
break;
cardcount++,
printf("Another Card?\n\n"),
scanf("%c", &answer);
clean();
}
if (answer == 'n')
{
printf("My turn:\n\n");
while (scorec < 17)
{
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[52])
{
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;
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
void clean (void)
{
char dummy;
do
{
scanf("%c",&dummy);
} while(dummy != '\n');
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
void InitializeDeck(int deck[DECK_SIZE])
{
int index;
for (index = 0; index < DECK_SIZE; index++)
{
deck[index] = index;
}
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
int RandomNumber(int upperlimit)
{
int number;
number = rand()%(upperlimit +1);
return number;
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
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;
}
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
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;
}
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
void DisplayDeck()
{
int index;
for (index = 0; index < DECK_SIZE; index++)
{
DisplayCard(index);
}
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
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;
}
/*********************************************************************
* FunctionName(Parameter Names)
* Brief, general description of function's purpose -- what does it do?
* PRE: list all preconditions here -- things that must be true for function to work.
* POST: list all postconditions here -- things that are true when function returns.
*********************************************************************/
int DrawCard(int deck[DECK_SIZE], int count)
{
int score;
int card;
card = deck[count];
DisplayCard(card);
score = ScoreCard(card);
return score;
}