So we have to create a modified blackjack game with a minimum of the functions I have in the code (no leeway for those)
If the users sum is > 21 they lose
if user sum = 21 they win
if user sum is under 21 and they don't want to go again, the user gets a hypothetical card. If said hypothetical card makes their total > 21 they win, if it makes their total < 21 they lose.
CurrentlyI still have a lot to do: I have to convert the numbers to strings (in print_card I should be printing the card in user friendly format by calling compute_face_name, which is supposed to return the face of the card in words. I'll worry about that later) I'm having a problem with passing values - I don't know how to add the next card's number to the current total, and how to print the total to the user.
EX.
-----------------------------------------------------------
Welcome to Blackjack! Below is your hand.
1
3
Your current total is 4
Would you like to test your luck? (y/n): y
13
Would you like to test your luck? (y/n): y
6
-----------------------------------------------------------
Above, after the number I want to be able to print their current total, and break if they are above 21... How could I do that?
Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<string>
using namespace std;
bool play_again();
int dealcard(int initialCard);
string print_card();
string compute_face_name();
int compute_value(int initialCard, int currentTotal);
bool another_card();
int main()
{
srand(time(0));
char play_again = 0;
int initialCard = 0;
int currentTotal = 0;
cout << "Welcome to Blackjack! Below is your hand." << endl;
compute_value(initialCard, currentTotal);
another_card();
return 0;
}
int dealcard(int initialCard)
{
int card1 = (rand() % 13) + 1;
cout << card1 << endl;
return card1;
}
int compute_value(int initialCard, int currentTotal)
{
int initialCard1 = dealcard(initialCard);
int initialCard2 = dealcard(initialCard);
currentTotal = initialCard1 + initialCard2;
cout << "Your current total is " << currentTotal << endl;
if (currentTotal > 21)
{
cout << "Unlucky for you! You already lost!" << endl;
return 0;
}
return currentTotal;
}
string print_card(int initialCard)
{
}
string compute_face_name()
{
}
bool play_again()
{
char playAgain = 0;
cout << "Would you like to play again? (y/n): ";
cin >> playAgain;
if (playAgain == 'n')
return 0;
}
bool another_card()
{
char anotherCard = 0;
int initialCard = 0;
int currentTotal = 0;
if (currentTotal > 21)
{
cout << "You are over 21: You lose!" << endl;
return 0;
}
do{
cout << "Would you like to test your luck? (y/n): ";
cin >> anotherCard;
if(anotherCard == 'y')
{
int nextCard = dealcard(initialCard);
}
else if (anotherCard == 'n')
return 0;
} while (currentTotal <= 21);
}
|