OK, I've spent hours trying to figure out what the issue is, but at this point I am just looking at the same stuff over and over. The game accepts a bet and them shows you one dealer card and then your cards. The first issue is that the dealer card is shown just fine, but the player cards come back with a symbol that corresponds with 204 and the ASCII table. If I choose to hit and receive another card, it then tells me my cards value is 26212(always the same amount).
If I play another game however, then the dealer cards come up with the same symbol and do not work anymore. I figure that the issue must be in the sumOfCardValues() and showCards(), but I just can't figure it out. I'll put in here the code for the Card.h, card.cpp and source.cpp, if anyone can get me pointed in the correct direction, I would appreciate it.
#include "Card.h"
#include <time.h>
bool Card::randomizerSeeded = false;
Card::Card()
{
//seed randomizer only once for all card objects to ensure unique card
if (!randomizerSeeded)
{
srand(time(NULL));
randomizerSeeded = true;
//create random number from 3 to 6 for suit
short min = 3;
short max = 6;
suit = rand() % (max - min + 1) + min;
//create random number from 2 to 14 for face value
min = 2;
max = 14;
short number = rand() % (max - min + 1) + min;
//if else statement to set face and number value
if (number >=2 && number <=9)
{
value = number;
face = number + 48;
}
elseif (number == 10)
{
value = number;
face = 'T';
}
elseif (number == 11)
{
value = 10;
face = 'J';
}
elseif (number == 12)
{
value = 10;
face = 'Q';
}
elseif (number == 13)
{
value = 10;
face = 'K';
}
elseif (number == 14)
{
value = 11;
face = 'A';
}
else
{
value = -1;
face = 'E';
}
}
}
Card::~Card()
{
}
string Card::toString()
{
string output = "";
output += suit;
output += face;
return output;
}
bool Card::flipAceToOne()
{
if (value==11)
{
value = 1;
returntrue;
}
else
{
returnfalse;
}
}
#include<iostream>
#include<string>
#include<conio.h>
#include "Card.h"
#include <vector> //Standard Template Library
usingnamespace std;
//protypes
string showCards( vector<Card> cards);
short sumOfCardValues(vector<Card> cards);
void playHand(short &cash);
//declare constant variables
intconst EXIT_VALUE = 3;
//global varible
short cash = 100;
//Entry into main application
int main()
{
cout << "Welcome to Chris' Blackjack Table!" << endl;
cout << "\nYour starting balance is $" << cash << endl;
//pause
cout << "Press any key to continue..." << endl;
_getch();
short choice = 0;
system("cls"); //clears console
cout << "MENU\n" << endl;
cout << "1) Play a hand" << endl;
cout << "2) Check my balance" << endl;
cout << "3) Exit" << endl;
cout << "\nEnter your choice: ";
cin >> choice;
//initiate menu loop and switch block based on user choice
do
{
switch (choice) {
case 1:
playHand(cash);
break;
case 2:
cout << "\nChecking my balance..." << endl;
break;
case 3:
cout << "\nGoodbye!" << endl;
break;
default:
cout << "\nError, please choose from menu options." << endl;
break;
}
//pause
cout << "\nPress any key to continue..." << endl;
_getch();
} while (choice != EXIT_VALUE);
return 0;
}
//show cards in the vector (resizable array)
string showCards( vector<Card>cards)
{
string output = " ";
for (Card c : cards)
{
output += c.toString() + " ";
}
return output;
}
//Adds the total value of the cards
short sumOfCardValues(vector<Card>cards)
{
short total = 0;
for (Card c : cards)
{
total = total + c.getValue();
}
return total;
}
// play a hand of blackjack
void playHand(short &cash)
{
//create two ArrayLists to hold Card objects
vector<Card> playerCards;
vector<Card> dealerCards;
short dealerCardsTotal = 0;
short playerCardsTotal = 0;
//get bet amount
short wager = 0;
cout << "\nEnter your wager amount: ";
cin >> wager;
//create two cards for the dealer to show the first one
Card card1;
Card card2;
dealerCards.push_back(card1);
dealerCards.push_back(card2);
dealerCardsTotal = sumOfCardValues(dealerCards);
cout << "\nDealer shows a: " << dealerCards[0].toString() << endl;
//create two cards for the player and show them both
playerCards.push_back(Card() );
playerCards.push_back(Card() );
playerCardsTotal = sumOfCardValues(playerCards);
cout << "\nYour cards: " << showCards(playerCards) << endl;
//loop until player stands: ('S')
char answer = '?';
do
{
cout << "\nDo you want to hit or stand (H/S)? ";
cin.sync();
cin >> answer;
cin.sync();
if (toupper(answer) == 'H')
{
//give a card to the player
Card c;
cout << "\nYou were dealt this card: " << c.toString() << endl;
playerCards.push_back(c); //adds card to players hand
//sum up the card values
playerCardsTotal = sumOfCardValues(playerCards);
//did the player bust?
if (playerCardsTotal > 21)
{
//do you have an Ace to flip value to a 1
for (Card c : playerCards)
{
if (c.getValue() ==11)
{
cout << "\nYour total is " << playerCardsTotal << endl;
c.flipAceToOne();
cout << "However, you have an ace that was flipped to a '1' " << endl;
playerCardsTotal = sumOfCardValues(playerCards);
cout << "\nYour new total is " << playerCardsTotal << endl;
//if good, break out of the loop. Otherwise continue loop and look for Aces
if (playerCardsTotal <= 21)
break;
}
}
}
//show the cards and the total
cout << "\nHere are your cards: " << showCards(playerCards) << endl;
cout << "Your total is " << playerCardsTotal << endl;
//if players busts, stop the loop
if (playerCardsTotal > 21)
answer = 'S';
}
}
while (toupper(answer) != 'S');
//Determine whether player earns or losses money(if playercards > lose money, if playercards < earn money
if (playerCardsTotal > 21)
{
cout << "\nYou busted!" << endl;
cash = cash - wager;
}
else
{
//if the player stands, make the dealer hit until reaching 17 or greater
do
{
if (dealerCardsTotal < 17)
{
Card c;
cout << "\nDealer was dealt: " << c.toString() << endl;
dealerCards.push_back(c);
cout << "\nDealer cards: " << showCards(dealerCards) << endl;
dealerCardsTotal = sumOfCardValues(dealerCards);
cout << "Dealer total: " << dealerCardsTotal << endl;
}
}
while (dealerCardsTotal < 17);
//show the cards for the dealer and player
cout << "\nYour cards: " << showCards(playerCards) << " (" << playerCardsTotal << " )" << endl;
cout << "\nDealer cards: " << showCards(dealerCards) << " (" << dealerCardsTotal << " )" << endl;
//determine winner
if (dealerCardsTotal > 21)
{
cout << "\nThe dealer busts!" << endl;
cash += wager;
}
elseif (dealerCardsTotal > playerCardsTotal)
{
cout << "\nDealer wins." << endl;
cash -= wager;
}
elseif (playerCardsTotal > dealerCardsTotal)
{
cout << "\nYou Win! " << endl;;
cash += wager;
}
else
{
cout << "\nYour pushed the dealer (tie)" << endl;
}
}
//Display players cash position
cout << "Your cash balance is $" << cash << endl;
}
The logical error was causing the entire issue. I may have never noticed that if you didn't point it out. The suit is returned as an actual club, diamond, heart or spade so there was no need in this case to use the switch statement. I think the short variable returns the associated symbol from the ASCII table. Because I didn't close my if statement, it was pulling the wrong numbers and not randomizing the output.
One other question. Any idea why my my switch block may not be functioning properly. Whichever case I select, the program stays in there and never breaks back out. So if choice ==1 I go to playHand(), but it stays in there. It'll break out and go to the _getch(); but then go right back to the previous selection. If I select to check my balance, it will show the balance, then say press any key to continue and go right back to showing my balance. It's like it is stuck in a loop for each case.
You need to change your 'choice' variable somewhere inside your loop.
Assuming your source.cpp from your initial post, you could move the do { from line 45 to 33.
ahhhhh... I see. I had the cin>>choice; outside of the do statement. With it written that way the user cannot input their choice for the menu options after the initial input. Thanks for the help.