/*********************************************************************
* Program Name: Prog06
* Author: Ken Tiss
* Date: May 11, 2008
* Course/Section: CSC 110-001
* Program Description: This program will start with a welcome message,
* and then will prompt the player to draw 2 cards. The computer will then
* draw a card from the virtual "deck" using an array. The game will
* continue until the user decides to exit the program.
*********************************************************************/
/**********************Compiler Directives***************************/
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
/**********************Global Data Declarations*********************/
const int J = 10; //Value of a Jack playing card
const int Q = 10; //Value of a Queen playing card
const int K = 10; //Value of a King playing card
const int A = 11; //Value of an Ace playing card
/***********************Function Prototypes**************************/
//User draws 2 cards
void PlayerDraw (bool cards[], //INOUT: Virtual "deck of cards"
int& tvalue); //OUT: Player's total value of cards drawn);
//Computer draws 2 cards
void CompDraw(bool play[], //INOUT: Virtual "deck of cards"
int& tval); //INOUT: Total value of cards drawn
//Assigns values to cards
void CValue(bool draw[], //INOUT:Individual card
int& val, //OUT: Playable value of each card
string tCard); //OUT: Total suited and face value of card
//Displays results
void FinalOutCome(int& ptotal, //OUT: Player's total value of cards drawn
int& ctotal); //OUT: Computer's total value of cards drawn
/*********************************************************************
* Function Name: main
* Author: Ken Tiss
* Date: May 11, 2008
* Function Description:
* This function will call all the other functions to operate.
* Pseudocode:
* Level 0:
* ----------
*
* Assign values to cards
* Display welcome message
* User draws 2 cards
* Computer draws 2 cards
* Display results
*
* Level 1:
* ----------
* Assign values to cards
* CardValue(deck, cardnum, face, value, playval)
* Set all cards equal to True
* For trucount = 0 To (SIZE - 1) By + 1
* deck[trucount] = true;
* Display welcome message
* Display "ACE-HIGH GAME Version 1.0" & EOL
* Do
* User draws 2 cards
* UserDraw(value, playval, total)
* Computer draws 2 cards
* ComputerDraw(value, playval, total)
* Display results
* FinalResult(total, total)
* Ask for program restart
* Display "Would you like to play this game again? (yes/no) "
* Input response
* While (response == "yes")
* Display blank line
* Display & EOL
* Display exit message
Display "Thank you for playing the ACE-HIGH Game!" & EOL
*********************************************************************/
int main()
{
//Local constants
const int SIZE = 52; //Size of the virtual "deck" array
//Local variables
bool deck[SIZE]; //Virtual "deck of cards"
int trucount; //Loop-control variable used in init
int playt; //Player's total value of cards drawn
int compt; //Computer's total value of cards drawn
string win; //Statement of winner of game
string loss; //Statement of loser of game
string response; //Play-again response from user
/*****************Begin main Function Executables*****************/
//Display welcome message
//cout << setw(50) << "ACE-HIGH GAME Version 1.0" << endl;
do
{
//Set all cards equal to True
for (trucount = 0; trucount <= (SIZE - 1); trucount++)
deck[trucount] = true;
//User draws 2 cards
PlayerDraw(deck, playt);
//Computer draws 2 cards
CompDraw(deck, compt);
//Display results
FinalOutCome(playt, compt);
//Ask for program restart
cout << "Would you like to play this game again? (yes/no) ";
cin >> response;
}
while (response == "yes");
//Display blank line
cout << endl;
//Display exit message
cout << "Thank you for playing" << endl;
//Indicate to OS successful termination of program
return 0;
} //End main
/*********************************************************************
* Function Name: PlayerDraw
* Author: Ken Tiss
* Date: May 11, 2008
* Function Description: This function will allow the user to draw a
* "Card" by generating a random number, and make the drawn card "false."
*
* Pseudocode:
* Level 0:
* ----------
* User draws 2 cards
*
* Level 1:
* ----------
* User draws 2 cards
* Prompt user to draw card(s)
* Determine if card is drawn
* Generate random number
*
* Level 2
* ----------
* Prompt user to draw card(s)
* Display "Your turn to draw 2 cards. Press ENTER to continue."
* Input enter
*
* For (cardcount = 1 To 2 By +1)
*
* Display cards drawn
* Display "You have drawn a(n) " & card & " card with a face value of " & fv & EOL
*********************************************************************/
void PlayerDraw (bool cards[], //INOUT: Virtual "deck of cards"
int& tvalue) //OUT: Player's total value of cards drawn);
{
//Local constants
//Local variables
string enter = "\n"; //User's cue to draw cards
int cardcount; //Loop counter
int cardnum = 0; //Randomly-generated number of drawn card
int playval = 0; //Playable value of each card
string finCard; //Total suited and face value of card
/*****************Begin UserDraw Function Executables*****************/
//Initialize total
tvalue = 0;
//Prompt user to draw card(s)
cout << "Your turn to draw 2 cards. Press ENTER to continue.";
getline(cin, enter, '\n');
//Display blank line
cout << endl;
for (cardcount = 1; cardcount <= 2; cardcount++)
{
//Draw a card
CValue(cards, playval, finCard);
//Display card drawn
cout << "You have drawn a(n) " << finCard << " card with a face value of " << playval << endl;
//Add total value of cards drawn
tvalue += playval;
}
//Display total value of draw
cout << "Your total value for this draw is " << tvalue << "." << endl;
} //End UserDraw
/*********************************************************************
* Function Name: CompDraw
* Author: Ken Tiss
* Date: May 11, 2008
* Function Description: This function will automatically draw a
* "Card" by generating a random number, and make the drawn card "false."
*
* Pseudocode:
* Level 0:
* ----------
* Computer draws 2 cards
*
* Level 1:
* ----------
* Computer draws 2 cards
* Notify user of card draw
*
* Level 2
* ----------
* Notify user of card draw
* Display "The computer will now draw 2 cards. Press ENTER to continue."
*
* For (cardcount = 1 To 2 By +1)
* Display cards drawn
* Display "The computer has drawn a(n) " & card & " card with a face value of " & face & EOL
*********************************************************************/
void CompDraw(bool play[], //INOUT: Virtual "deck of cards"
int& tval) //INOUT: Total value of cards drawn
{
//Local constants
//Local variables
string enter = "\n"; //User's cue to draw cards
int cardcount; //Loop counter
int card = 0; //Randomly-generated number of drawn card
int fval = 0; //Face value of each card
int pval = 0; //Playable value of each card
string fCard; //Total suited and face value of card
/*****************Begin ComputerDraw Function Executables*****************/
//Initialize total
tval = 0;
//Notify user of card draw
cout << "The computer will now draw 2 cards. Press ENTER to continue.";
getline(cin, enter, '\n');
//Display blank line
cout << endl;
//Generate 2 random numbers for each card
for (cardcount = 1; cardcount <= 2; cardcount++)
{
//Draw a card
CValue(play, pval, fCard);
//Display card drawn
cout << "The computer has drawn a(n) " << fCard << " card with a face value of " << pval << endl;
//Add total value of cards drawn
tval += pval;
}
//Display total value of draw
cout << "The computer's total value for this draw is " << tval << "." << endl;
} //End ComputerDraw
/*********************************************************************
* Function Name: CValue
* Author: Ken Tiss
* Date: May 11, 2008
* Function Description: This function will assign the face and suited
* values to each of the cards.
*
* Pseudocode:
* Level 0:
* ----------
* Assign values to cards
*
* Level 1:
* ----------
* Assign values to cards
* Seed random generator
* Generate random number
* Determine if card location is used
* Assign suits to each card
* Assign face values of each card
* Assign playable values of each card
* Create total values using suits & face values
*
* Level 2:
* ----------
* Generate random number
* x = (rand() % (51 - 0 + 1)) + 0;
* Determine if card location is used
* Assign face values of each card
* If (num >= 0) And (num <=
* facev = (num + 2)
* Else If (num == 9) Then
* facev = 74
* Else If (num == 10) Then
* facev = 81
* Else If (num == 11) Then
* facev = K
* Else If (num == 12) Then
* facev = K
* Else If (num >= 13) And (num <= 21) Then
* facev = (num % 13)
* Else If (num == 22) Then
* facev = 74
* Else If (num == 23) Then
* facev = 81
* Else If (num == 24) Then
* facev = K
* Else If (num == 25) Then
* facev = A
* Else If (num >= 26) And (num <= 34) Then
* facev = (num % 26)
* Else If (num == 35) Then
* facev = 74
* Else If (num == 36) Then
* facev = 81
* Else If (num == 37) Then
* facev = K
* Else If (num == 38) Then
* facev = A
* Else If (num >= 39) And (num <= 47)
* facev = (num % 39)
* Else If (num == 48) Then
* facev = 74
* Else If (num == 49) Then
* facev = 81
* Else If (num == 50) Then
* facev = K
* Else If (num == 51)
* facev = A
* Assign suits of each card
* If ((num >= 0) & (num <= 12)) Then
* suit = "S";
* If ((num > 12) & (num <= 25) Then
* suit = "H";
* If ((num > 25) & (num <= 38)) Then
* suit = "C";
* If ((num > 38) && (num <= 51) Then
* suit = "D";
* Assign playable values of each card
* Create total values using suits & face values
* tCard = char(val + 48) + suit
*********************************************************************/
void CValue(bool draw[], //INOUT:Individual card
int& val, //OUT: Playable value of each card
string tCard) //OUT: Total suited and face value of card
{
//Local constants
//None
//Local variables
int num; //Number of card being assigned a value
int facev = 0; //Face value of each card
char suit; //Suited value assigned to each card
/*****************Begin CardValue Function Executables*****************/
//Seed random generator
srand(static_cast<unsigned int>(time(0)));
do
{
//Generate random number
num = (rand() % (51 - 0 + 1)) + 0;
if (draw[num] == true)
{
draw[num] = false;
//Assign face values of each card
if (num >= 0 && num <=
facev = (num + 2);
else if (num == 9)
facev = J;
else if (num == 10)
facev = Q;
else if (num == 11)
facev = K;
else if (num == 12)
facev = A;
else if (num >= 13 && num <= 21)
facev = (num % 13);
else if (num == 22)
facev = J;
else if (num == 23)
facev = Q;
else if (num == 24)
facev = K;
else if (num == 25)
facev = A;
else if (num >= 26 && num <= 34)
facev = (num % 26);
else if (num == 35)
facev = J;
else if (num == 36)
facev = Q;
else if (num == 37)
facev = K;
else if (num == 38)
facev = A;
else if (num >= 39 && num <= 47)
facev = (num % 39);
else if (num == 48)
facev = J;
else if (num == 49)
facev = Q;
else if (num == 50)
facev = K;
else
facev = A;
//Assign suits to each card
if ((num >= 0) && (num <= 12))
suit = 'S';
else
{
if ((num > 12) && (num <= 25))
suit = 'H';
else
{
if ((num > 25 && num <= 38))
suit = 'C';
else
{
if ((num > 38 && num <= 51))
suit = 'D';
}
}
}
}
//Assign playable values of each card
if ((facev >= 0) && (facev <= 9))
val = facev;
else if (facev == J)
{
val = J;
}
else if (facev == Q)
{
val = Q;
}
else if (facev == K)
{
val = K;
}
else
{
val = A;
}
//Create total values using suits & face values
tCard = char(val + 48) + suit;
}
while (draw[num] = false);
} //End CardValue
/*********************************************************************
* Function Name: FinalOutCome
* Author: Ken Tiss
* Date: May 11, 2008
* Function Description: This function will determine the winner/loser
* of the game, determine if a tie is needed, and then display the results.
*
* Pseudocode:
* Level 0:
* ----------
* Display results
*
* Level 1:
* ----------
* Display results
* Determine loser/winner
* Determine if tie
*
* Level 2
* ----------
* Determine loser/winner
* If (ptotal >= ctotal) And (ptotal <= 21) Then
* Display winner
* Else If (ptotal <= ctotal) And (ptotal <= 21) Then
* Display loser
* Else If (ptotal == ctotal) And (ptotal <= 21) Then
* Display tie
* End
*********************************************************************/
void FinalOutCome(int& ptotal, //OUT: Player's total value of cards drawn
int& ctotal) //OUT: Computer's total value of cards drawn
{
//Local constants
string winner = "You won! congrats!"; //Statement indicating player wins
string loser = "Sorry you lose!!"; //Statement indicating computer wins
string tie = "You tied."; //Statement indicating tied game
string bust = " busted."; //Statement indicating bust
//Local variables
/*****************Begin FinalResult Function Executables*****************/
//Determine loser/winner
if ((ptotal > ctotal) && (ptotal <= 21))
cout << winner << endl;
else if ((ptotal < ctotal) && (ptotal <= 21))
cout << loser << endl;
else if ((ptotal == ctotal) && (ptotal <= 21))
cout << tie << endl;
else if ((ptotal > ctotal) && (ptotal > 21))
{
cout << "You have" << bust << endl;
cout << loser << endl;
}
else
{
cout << "The computer has" << bust << endl;
cout << winner << endl;
}
} //End FinalResult
