int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit";
static const char stand[] = "stand";
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
CARD c7 = deck[next_index++];
CARD c8 = deck[next_index++];
CARD c9 = deck[next_index++];
CARD c10 = deck[next_index++];
CARD c11 = deck[next_index++];
CARD c12 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: ";
print_card(c1);
cout << " ";
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: ";
print_card(c3);
cout << " ";
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
cout << "New card: ";
print_card(c5);
cout << endl;
total_value = total_value + c5.value;
cout << "Player has: " << total_value << endl;
if(total_value < 21)
{
result1 = strcmp (word1, hit);
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
if(result1 == 0)
{
cout << "New card: ";
print_card(c6);
cout << endl;
total_value = total_value + c6.value;
cout << "Player has: " << total_value << endl;
if(total_value < 21)
{
result1 = strcmp (word1, hit);
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
if(result1 == 0)
{
cout << "New card: ";
print_card(c7);
cout << endl;
total_value = total_value + c7.value;
cout << "Player has: " << total_value << endl;
if(total_value < 21)
{
result1 = strcmp (word1, hit);
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
if(result1 == 0)
{
cout << "New card: ";
print_card(c7);
cout << endl;
total_value = total_value + c7.value;
cout << "Player has: " << total_value << endl;
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
}
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
}
}
if(total_value > dealer_value)
{
cout << "You Win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
else if(total_value == dealer_value)
{
cout << "Push!" << endl;
cout << "Current bankroll: " << bank << endl;
}
else if(total_value < dealer_value)
{
cout << "You lose!" << endl;
bank = bank - bet;
cout << "Current bankroll: " << bank << endl;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye" << endl;
}
}
}
21 Replies - 1019 Views - Last Post: 05 August 2012 - 11:32 AM
#1
Outputs when not suppose to
Posted 04 August 2012 - 03:47 PM
I have to write a blackjack program for school. I have it the most of the way done but I am having a fwe problems. I couldn't get any loops or functions to work so I ended up using nested if statements. The problem I am having is that when the player types stand it outputs for a hit and then moves on. I have a very limited knowledge of c++. Any pointers or help would be appreciated. Thank you. Here is the main function part of the code.
Replies To: Outputs when not suppose to
#2
Re: Outputs when not suppose to
Posted 04 August 2012 - 04:57 PM
Maybe compare the strings after the player has entered the word.
076 result1 = strcmp (word1, hit);
077 cout << "Options: [hit] [stand]" << endl;
078 cin >> word1;
// compare here
#3
Re: Outputs when not suppose to
Posted 04 August 2012 - 10:24 PM
Thank you for your help. That solved my problem. once you pointed that out it was pretty obvious. I have been working on it all day so everything started to look the same. I am almost done. I only have a few things left to finish off the code. I added a do while loop but it is not functioning right. It only works once instead of repeating until the bet it is above 0.
The after you hit and then stand the code doesn't do anything. I have looked through it and to me it looks like it should work. if it is going through the if statements but maybe I am wrong. Any help would be appreciated. Thank you again. Here is just the if statements. The problem starts in the if statement on line 15 and ends on Line 171.
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
do
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}while(bet > 0);
The after you hit and then stand the code doesn't do anything. I have looked through it and to me it looks like it should work. if it is going through the if statements but maybe I am wrong. Any help would be appreciated. Thank you again. Here is just the if statements. The problem starts in the if statement on line 15 and ends on Line 171.
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
cout << "New card: ";
print_card(c5);
cout << endl;
total_value = total_value + c5.value;
cout << "Player has: " << total_value << endl;
if(total_value < 21)
{
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
cout << "New card: ";
print_card(c6);
cout << endl;
total_value = total_value + c6.value;
cout << "Player has: " << total_value << endl;
if(total_value < 21)
{
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
cout << "New card: ";
print_card(c7);
cout << endl;
total_value = total_value + c7.value;
cout << "Player has: " << total_value << endl;
if(total_value < 21)
{
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
cout << "New card: ";
print_card(c7);
cout << endl;
total_value = total_value + c7.value;
cout << "Player has: " << total_value << endl;
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
outcome(total_value, dealer_value, bank, bet);
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
}
else if(total_value > 21)
{
cout << "Player bust! You lose." << endl;
}
else
{
cout << "Dealer's turn:" << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c8);
cout << endl;
dealer_value = dealer_value + c8.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c9);
cout << endl;
dealer_value = dealer_value + c9.value;
cout << "Dealer has: " << dealer_value << endl;
if(dealer_value < 17)
{
cout << "New card: ";
print_card(c10);
cout << endl;
dealer_value = dealer_value + c10.value;
cout << "Dealer has: " << dealer_value << endl;
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
else if(dealer_value >= 17)
{
cout << "Dealer stands." << endl;
outcome(total_value, dealer_value, bank, bet);
}
}
}
#4
Re: Outputs when not suppose to
Posted 04 August 2012 - 10:35 PM
You really need to step back and figure out how to make looping work. Your code has a ton of duplication in it. Even if you don't use functions, a large part of your code can be trimmed away.
#5
Re: Outputs when not suppose to
Posted 04 August 2012 - 10:41 PM
I have tried to make it work and nothing worked the way it was suppose to. I have only been doing this for a couple months. I know there is way but all the loops and functions I tried didn't work so I went with what worked.
#6
Re: Outputs when not suppose to
Posted 04 August 2012 - 11:12 PM
Pseudo code:
deal player cards
deal dealer's cards
int hit_count = 3;
while (hit_count > 0 && player_total < 21)
{
Ask player if stand or hit
if stand
break;
hit_count--;
card = deck[next_card++];
player_total += card.Value;
show player_total
}
if (player_total > 21)
print "Player busts."
int hit_count = 3;
while (hit_count > 0 && dealer_total < 17)
{
hit_count--;
card = deck[next_card++];
dealer_total += card.Value;
show dealer_total
}
if (dealer_total > 21)
print "Dealer busts."
if (player_total <= 21 && dealer_total <= 21)
{
if (player_total == dealer_total)
print "Draw."
else
print player_total > dealer_total ? "Player wins." : "Dealer wins.";
}
#7
Re: Outputs when not suppose to
Posted 05 August 2012 - 08:08 AM
Skydiver, thank you for your help. I changed my code. I have a one error and one question. I am getting the error "error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"New Card: ")) << print_card(c5)'|" at line and I do not know how to fix it. My question is on the last line of code line I am not familiar with the ? and so I am not sure how to rewrite to make it work with what I need it to output. Thanks again for your help.
The error is line 79 and I dont know what the ? means in line 124. Thanks again Skydiver.
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit";
static const char stand[] = "stand";
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
do
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}while(bet < 0);
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: ";
print_card(c1);
cout << " ";
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: ";
print_card(c3);
cout << " ";
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, stand);
if(result1 == 0)
{
break;
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: " << print_card(c5) << endl;
cout << "Player has: " << total_value << endl;
}
}
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
}
}
int hit_count = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count--;
CARD c6 = deck[next_index++];
dealer_value += c6.value;
cout << "New Card: " << print_card(c6) << endl;
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
}
else
print total_value > dealer_value ? "You win!" : "Dealer wins.";
}
}
The error is line 79 and I dont know what the ? means in line 124. Thanks again Skydiver.
#8
Re: Outputs when not suppose to
Posted 05 August 2012 - 08:29 AM
Quote
I dont know what the ? means in line 124
You need to convert line 124 from pseudo code to actual code. The ? in Skydiver post means print either "You win" or "Dealer wins" depending on the results of the results of the total_value > dealer_value comparison.
Jim
#9
Re: Outputs when not suppose to
Posted 05 August 2012 - 08:41 AM
Thank you for the quick response. I changed the code around but I still have the error that I can't figure out.
#10
Re: Outputs when not suppose to
Posted 05 August 2012 - 08:48 AM
Quote
I changed the code around but I still have the error that I can't figure out.
What is your error? If you get a compile error post the complete error message, exactly as it appears in your development environment. Otherwise, explain what exactly is wrong with your program.
Also post your current code.
Jim
#11
Re: Outputs when not suppose to
Posted 05 August 2012 - 09:04 AM
Thanks for the help. I changed that. I am having problems with my while loop line 154 in for the bet. It is only suppose to display if the bet is below zero but it displays no matter what. I cannot get the "Stand" function to work line 190. The hit works but not the stand.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)";
else if (c.f == KING)
cout << "KING (10)";
else if (c.f == QUEEN)
cout << "QUEEN (10)";
else if (c.f == JACK)
cout << "JACK (10)";
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)";
else if (c.value == 9)
cout << "NINE (9)";
else if (c.value == 8)
cout << "EIGHT (8)";
else if (c.value == 7)
cout << "SEVEN (7)";
else if (c.value == 6)
cout << "SIX (6)";
else if (c.value == 5)
cout << "FIVE (5)";
else if (c.value == 4)
cout << "FOUR (4)";
else if (c.value == 3)
cout << "THREE (3)";
else if (c.value == 2)
cout << "TWO (2)";
else
cout << "Value not recognized";
}
else
cout << "Face not recognized";
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit";
static const char stand[] = "stand";
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
while(bet <= 0);
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: ";
print_card(c1);
cout << " ";
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: ";
print_card(c3);
cout << " ";
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: ";
print_card(c5);
cout << endl;
cout << "Player has: " << total_value << endl;
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
}
break;
}
else if(result1 == 1)
{
break;
}
}
int hit_count1 = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count1--;
CARD c6 = deck[next_index++];
dealer_value += c6.value;
cout << "New Card: ";
print_card(c6);
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
}
else if(total_value > dealer_value)
{
cout << "You Win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
else if(dealer_value > total_value)
{
cout << "You lose!" << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
}
}
}
}
#12
Re: Outputs when not suppose to
Posted 05 August 2012 - 09:14 AM
Quote
I am having problems with my while loop line 154 in for the bet. It is only suppose to display if the bet is below zero but it displays no matter what.
What happens if you place a semicolon after a while statement?
while(bet <= 0);
Quote
I cannot get the "Stand" function to work line 190. The hit works but not the stand.
What do you mean by not working?
Jim
#13
Re: Outputs when not suppose to
Posted 05 August 2012 - 09:37 AM
Thanks for the help on the while loop. I found the problem on the stand issue. It actually was working. I just forgot to add a couple lines of code from the psuedo code. I do have a question on repeating the entire code from line 27 if the bankroll is greater than by asking the player if they want to play again yes or no. if they type yes then repeat and if they type no then exit. I've never done something like that before. Thanks again for the help.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)";
else if (c.f == KING)
cout << "KING (10)";
else if (c.f == QUEEN)
cout << "QUEEN (10)";
else if (c.f == JACK)
cout << "JACK (10)";
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)";
else if (c.value == 9)
cout << "NINE (9)";
else if (c.value == 8)
cout << "EIGHT (8)";
else if (c.value == 7)
cout << "SEVEN (7)";
else if (c.value == 6)
cout << "SIX (6)";
else if (c.value == 5)
cout << "FIVE (5)";
else if (c.value == 4)
cout << "FOUR (4)";
else if (c.value == 3)
cout << "THREE (3)";
else if (c.value == 2)
cout << "TWO (2)";
else
cout << "Value not recognized";
}
else
cout << "Face not recognized";
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit";
static const char stand[] = "stand";
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
while(bet <= 0)
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: ";
print_card(c1);
cout << " ";
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: ";
print_card(c3);
cout << " ";
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: ";
print_card(c5);
cout << endl;
cout << "Player has: " << total_value << endl;
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
}
break;
}
else
{
break;
}
}
cout << "Dealer's turn" << endl;
if(dealer_value > 17)
{
cout << "Dealer stands" << endl;
}
else
{
int hit_count1 = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count1--;
CARD c6 = deck[next_index++];
dealer_value += c6.value;
cout << "New Card: ";
print_card(c6);
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
}
else if(total_value > dealer_value)
{
cout << "You Win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
else if(dealer_value > total_value)
{
cout << "You lose!" << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
}
}
}
}
#14
Re: Outputs when not suppose to
Posted 05 August 2012 - 09:45 AM
Quote
I do have a question on repeating the entire code from line 27 if the bankroll is greater than by asking the player if they want to play again yes or no. if they type yes then repeat and if they type no then exit.
I suppose you mean line 270. But if you want to replay the game then think about adding a loop to repeat the shuffle, deal, and play.
Jim
#15
Re: Outputs when not suppose to
Posted 05 August 2012 - 09:53 AM
That's what I was thinking but the only loops I know are do-while and while but I do not know how to make either of those work with the text. I don't know why I put line 27 I meant line 147. I just ran into another problem while testing it that when I player hits and busts line 206 it then jumps to the dealers turn instead of exiting.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)";
else if (c.f == KING)
cout << "KING (10)";
else if (c.f == QUEEN)
cout << "QUEEN (10)";
else if (c.f == JACK)
cout << "JACK (10)";
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)";
else if (c.value == 9)
cout << "NINE (9)";
else if (c.value == 8)
cout << "EIGHT (8)";
else if (c.value == 7)
cout << "SEVEN (7)";
else if (c.value == 6)
cout << "SIX (6)";
else if (c.value == 5)
cout << "FIVE (5)";
else if (c.value == 4)
cout << "FOUR (4)";
else if (c.value == 3)
cout << "THREE (3)";
else if (c.value == 2)
cout << "TWO (2)";
else
cout << "Value not recognized";
}
else
cout << "Face not recognized";
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit";
static const char stand[] = "stand";
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
while(bet <= 0)
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: ";
print_card(c1);
cout << " ";
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: ";
print_card(c3);
cout << " ";
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if(result1 == 0)
{
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: ";
print_card(c5);
cout << endl;
cout << "Player has: " << total_value << endl;
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
}
break;
}
else
{
break;
}
}
cout << "Dealer's turn" << endl;
if(dealer_value > 17)
{
cout << "Dealer stands" << endl;
}
else
{
int hit_count1 = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count1--;
CARD c6 = deck[next_index++];
dealer_value += c6.value;
cout << "New Card: ";
print_card(c6);
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
}
else if(total_value > dealer_value)
{
cout << "You Win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
else if(dealer_value > total_value)
{
cout << "You lose!" << endl;
bank = bank - bet;
if(bank == 0)
{
cout << "Current bankroll: " << bank << endl;
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
}
}
}
}
|
|

New Topic/Question
Reply



MultiQuote




|