- If you get 21, you win at that point and the house does not draw (game over).
- If you bust (go over 21), you lose at that point and the house does not draw (game over).
- If you hold with less than 21, the house will keep drawing until his score is bigger
than yours (he wins), or his score is 21 (he wins), or his score is over 21 (you win).
**** I left the notes in and filled in the code i though needed according the the parameters given within the notes, but I am missing something or doing something wrong. Help or even just a point in the right direction would be appreciated.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void play21();
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random();
int main()
{
char keepPlaying = 'n';
keepPlaying = toupper(keepPlaying);
do
{
play21();
cout << "Do you want to play another hand? (y/n): ";
cin >> keepPlaying;
}
while (keepPlaying == 'y' || keepPlaying == 'Y');
}
void play21()
{
int person = 0; // your score
int house = 0; // computer's score
// radomize the cards
srand(static_cast<int>(time(0)));
// Your first two cards are dealt
// and your score is displayed. The cards
// that were dealt will be displayed
// in the dealCards function.
person = dealCards(2, "Your cards: ");
cout << " = " << person << endl;
// The house's first two cards are dealt
// and house's score is displayed. The cards
// that were dealt will be displayed in
// the dealCards function.
house = dealCards(2, "Computer cards: ");
cout << " = " << house << endl;
// Now we see if you want another card.
// Inside the hit function, your score will
// be updated and we will see if you busted
hit(person);
cout << endl;
// We use this while loop to see if the house has to take another card.
// If you have not busted and are holding with less than 21, house has to hit.
while (house < person && house < 21 && person < 21)
{
house = house + dealCards(1, "The house takes another card");
cout << "The house now has " << house << endl;
}
// Now we find out who won
determineWinner(person, house);
}
int dealCards(int numberOfCards, string message)
{
int total = 0;
cout >> "Your cards: ";
for (x+ 1; x <= numberOfCards; x += 1)
{
random();
cout << total
total = total + x;
}
return total;
// First, you need to display the message that was passed in.
// Then you need to deal the number of cards passed in.
// This can be done using a loop (for loop would be easier).
// Within the loop, call the Random() function to deal the card.
// This card must be displayed using a cout.
// It then needs to be added to a total variable.
// When the loop is finished, the total is returned.
}
void hit(int &playerScore)
{
char answer = ' ';
cout >> "Do you want to hit: ";
cin << answer;
while (answer == "y" || answer == "Y" && playerScore < 21)
{
cout << dealCards(1,string "Your cards =: );
playersScore =playerScore + dealCards(1, "Your score is: ");
}
if (playerScore > 21)
cout << "You Bust!!!";
else
{ cout << "Do you want to hit: ";
cin >> answer;
}
// Notice playerScore is passed in by reference.
// That means it will have a new value when this function is done.
// You need to ask the player if they want to hit (take another card).
// So you need a cout/cin combination. The cin should put the
// player's response in a variable (probably a char variable).
// Once the player has responded, you need to set up a loop to get the
// the next card and add it to the player's score. This will probably
// be a while loop because you want to keep asking the player if they want
// to hit as long as the following are true: the player wants another card
// AND the player's score is less than 21.
// In the loop, call the dealCards function to deal 1 card. Then add that card
// to the player's score and display the new score. If the player's score is less
// than 21, ask the player if they want to hit again.
// When the loop is finished, you need to check if the player's score is over 21
// or exactly 21. In either case, display a message.
}
void determineWinner(int humanScore, int houseScore)
{
if (humanScore == 21)
cout << "Blackjack!! You win!!!" << endl;
else if (humanScore < 21 && houseScore > 21)
cout << "Dealer Bust!! You win!!" << endl;
else if (humanScore > 21)
cout << "You Bust....Dealer wins" << endl;
else if (humanScore < 21 && houseScore == 21)
cout << "Dealer has Blackjack...You loose" << endl;
else (humanScore < 21 && houseScore > humanScore)
cout << "Dealer wins." << endl;
// This function will be a nested if-else statement, although it is
// possible to have only one if-else combination.
// You need to display a message using a cout that says who won.
// You win if:
// - you have 21
// - you have less than 21, but the house went over 21
// The house wins if:
// - you go over 21
// - the house gets 21 after you hold with less than 21
// - neither of you has 21, but the house's score is bigger than your score
// Out of all this, you have to figure out how your if-else statements work.
}
int Random()
{
// returns a random number between 1 and 10
return 1 + rand() % (10 - 1 + 1);
}

New Topic/Question
Reply




MultiQuote





|