Everything pretty much works now! Thanks for the help!
I have two problems at the moment - one is that when the dealer is picking an ace, it will display (through a cout) the correct value (1 or 11) depending on its current sum but it wont add to the cardSumDealer for some reason? Like if it picks its first card as an ace, it should change its value to an 11 as its sum is below 10 but it uses it as a 1. It always uses it as a 1.
My second problem is fixed. I made a mistake with my vector. My bad.
Apparently I need to shorten this. I will leave the main code below but here is the problem area:
//FIRST CARD FOR DEALER
dealerCardOne = drawCard();
//ace
dealerOneVal = getCardVal(dealerCardOne);
int ace1 = 0;
if (dealerOneVal == 1)
{
if (cardSumDealer <= 10)
{
cout << endl << "The dealer chose 11" << endl; //REMOVE
ace1 = 11;
}
else if(cardSumDealer > 10)
{
cout << endl << "The dealer chose 1" << endl; //REMOVE
ace1 = 1;
}
cardSumDealer += ace1;
}
else
cardSumDealer += dealerOneVal;
The problem here is that where ace1 should be added to the cardSumDealer, it always only adds 1 to it no matter if it couts that the dealer chose 11. I'm not sure what is going on.
#include <iostream>
#define _WIN32_WINNT 0x0500
#include<fstream> //files
#include <cmath> //calculations
#include <iomanip>
#include <stdlib.h> //randoms
#include <cstdlib>
#include <time.h> //regulating time
#include <windows.h>
#include <vector> //vectors
#include <algorithm>// std::sort
#include <string>
#include <windows.system.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //for handeling my colors
//COLOR FUNCTIONS
void whiteText(void) {
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY)); //Sets text color to white
}
void redText(void) {
SetConsoleTextAttribute(hConsole, (FOREGROUND_RED | FOREGROUND_INTENSITY));
}
void darkRedText(void) {
SetConsoleTextAttribute(hConsole, 4);
}
void greenText(void) {
SetConsoleTextAttribute(hConsole, (FOREGROUND_GREEN | FOREGROUND_INTENSITY));
}
void darkGreenText(void) {
SetConsoleTextAttribute(hConsole, 2);
}
void blueText(void) {
SetConsoleTextAttribute(hConsole, (FOREGROUND_BLUE | FOREGROUND_INTENSITY));
}
void yellowText(void) {
SetConsoleTextAttribute(hConsole, 14);
}
void titleText(void) {
SetConsoleTextAttribute(hConsole, 267);
}
//Initializing Functions
int drawCard();
void shuffleCards();
void fixCard(int randomfillervalue, int who);
int getCardVal(int randomfillervalue);
void title();
int aceControl(int a, int c);
int Win();
void Bust();
int Tie();
void Lose();
void compareHands(int ran1, int ran2);
void updateFile(int q);
//SETTING UP THE CARD VECTOR
vector<int> cards = { 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 };
const vector<int> permCards = { 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};
//global variables
int money;
int bet = 0;
//files
ifstream fin;//opening the input file
ofstream fout; //opening the output file
int main()
{
//Console Size
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r); //stores the console's current dimensions
//MoveWindow(window_handle, x, y, width, height, redraw_window);
MoveWindow(console, r.left, r.top, 800, 800, TRUE);
bool run = true;
char saveChar;
char character;
fin.open("SaveFile.txt");
//WELCOMING
system("CLS");
title();
cout << "Welcome to Blackjack! Press any key and 'enter' to begin playing." << endl;
cin >> character;
//REDRAWING THE TITLE
system("CLS");
title();
while (run)
{
bool bettingBool = true;
system("CLS");
title();
//READING SAVE FILE
cout << "Would you like to use a previously save file and the money you made? If so, press 'y' and then 'enter'. If not, press any other key and that file will be overwritten." << endl;
cin >> saveChar;
//REDRAWING THE TITLE
system("CLS");
title();
//STARTING BETTING
if (saveChar == 'y') //accessing the savefile
{
fin >> money;
cout << "You currently have";
blueText();
cout << " $";
cout << money;
cout << "." << endl; //getting the current amount of money from save file
whiteText();
fin.close();
if (money <= 0) //preventing complete lack of money
{
cout << "You do not have enough money in this save file to continue. Resetting the file to $200..." << endl;
money = 200;
updateFile(money);
}
cout << "How much would you like to bet?" << endl; //asking for bet amount
while (bettingBool)
{
cin >> bet;
if (bet > money) //preventing over-betting
cout << "\nYou cannot bet that amount. Please enter another amount..." << endl;
else
{
money -= bet;
cout << "You now have";
blueText();
cout << " $";
cout << money;
whiteText();
cout << "." << endl; //printing out new amount of money
updateFile(money);
bettingBool = false; //exiting this while loop
}
}
}
else //if you do not access a save file
{
money = 200;
updateFile(money);
cout << "You currently have the starting amount of:";
blueText();
cout << " $";
cout << money;
whiteText();
cout << "." << endl;
cout << "How much would you like to bet?" << endl;
while (bettingBool)
{
cin >> bet;
if (bet > money) //preventing over-betting
cout << "\nYou cannot bet that amount. Please enter another amount..." << endl;
else
{
bettingBool = false;
money -= bet;
cout << "You now have";
blueText();
cout << " $";
cout << money;
whiteText();
cout << "." << endl;
whiteText();
updateFile(money);
}
}
}
//REDRAWING THE TITLE
Sleep(2000);
system("CLS");
title();
shuffleCards();
Sleep(2000);
system("CLS");
title();
cout << "The Dealer will now distribute two cards to you and two to themself - only one of their cards will be visible to you." << endl;
Sleep(1000);
int firstCard, secondCard, thirdCard; //player cards
int dealerCardOne, dealerCardTwo, dealerCardThree; //dealer cards
int cardSumPlayer = 0, cardSumDealer = 0; //player/dealer sums
int cardOneVal, cardTwoVal, cardThreeVal; //player values
int dealerOneVal, dealerTwoVal, dealerThreeVal; //dealer values
cout << endl;
//FIRST CARD FOR PLAYER
darkGreenText();
cout << "Your cards: ";
whiteText();
firstCard = drawCard();
fixCard(firstCard, 1);
cout << endl;
//ace
cardOneVal = getCardVal(firstCard);
if (cardOneVal == 1)
cardOneVal = aceControl(cardOneVal, cardSumPlayer);
//FIRST CARD FOR DEALER
dealerCardOne = drawCard();
//ace
dealerOneVal = getCardVal(dealerCardOne);
int ace1 = 0;
if (dealerOneVal == 1)
{
if (cardSumDealer <= 10)
{
cout << endl << "The dealer chose 11" << endl; //REMOVE
ace1 = 11;
}
else if(cardSumDealer > 10)
{
cout << endl << "The dealer chose 1" << endl; //REMOVE
ace1 = 1;
}
cardSumDealer += ace1;
}
else
cardSumDealer += dealerOneVal;
//SECOND CARD FOR PLAYER
secondCard = drawCard();
fixCard(secondCard, 1);
cout << endl;
//ace
cardTwoVal = getCardVal(secondCard);
if (cardTwoVal == 1)
cardTwoVal = aceControl(cardTwoVal, cardSumPlayer);
bool runResponse = true, dealerRunResponse = true; //booleans for running the hit or stay programs for both dealer and player
//Beginning Sum
cardSumPlayer = (cardOneVal + cardTwoVal);
if (cardSumPlayer == 21)
{
money += Win();
updateFile(money);
cout << "You automatically win! Your new total money is:";
blueText();
cout << " $";
cout << money << endl;
whiteText();
runResponse = false;
dealerRunResponse = false;
}
//SECOND CARD FOR DEALER
cout << endl;
dealerCardTwo = drawCard();
darkRedText();
cout << "The Dealer's top card: ";
whiteText();
fixCard(dealerCardTwo, 2);
cout << endl;
//ace
dealerTwoVal = getCardVal(dealerCardTwo);
int ace2 = 0;
if (dealerTwoVal == 1)
{
if (cardSumDealer <= 10)
{
cout << endl << "The dealer chose 11" << endl; //REMOVE
ace2 = 11;
}
else if (cardSumDealer > 10)
{
cout << endl << "The dealer chose 1" << endl; //REMOVE
ace2 = 1;
}
cardSumDealer += ace2;
}
else
cardSumDealer += dealerTwoVal;
cout << endl;
while (runResponse) //While loop for controlling the hitting and staying
{
cout << "Your current total is ";
yellowText();
cout << cardSumPlayer;
whiteText();
cout << ". Would you like to 'h' (hit) or 's' (stay)?" << endl;
char response;
cin >> response;
if (response == 'h')
{
thirdCard = drawCard();
cout << "Your next card: ";
fixCard(thirdCard, 1);
cout << endl;
//aces
cardThreeVal = getCardVal(thirdCard);
if (cardThreeVal == 1)
cardThreeVal = aceControl(cardThreeVal, cardSumPlayer);
cardSumPlayer += cardThreeVal; //cardsum
if (cardSumPlayer == 21)
{
Win();
updateFile(money);
runResponse = false;
}
else if (cardSumPlayer > 21)
{
Bust();
cout << "You now have";
blueText();
cout << " $";
cout << money << endl;
whiteText();
updateFile(money);
runResponse = false;
dealerRunResponse = false;
}
else if (cardSumPlayer < 21)
{
runResponse = true;
}
}
else if (response == 's')
{
runResponse = false;
}
else
{
cout << "You did not enter a valid response. Please try again." << endl;
}
}
//while loop for controlling dealer cards after the first two
while (dealerRunResponse)
{
darkRedText();
cout << "The Dealer's first card: ";
whiteText();
fixCard(dealerCardOne, 2);
cout << endl;
cardSumDealer = (getCardVal(dealerCardOne) + getCardVal(dealerCardTwo));
while (cardSumDealer <= 16)
{
dealerCardThree = drawCard();
darkRedText();
cout << "The Dealer drew another card: ";
whiteText();
fixCard(dealerCardThree, 2);
cout << endl;
//ace
dealerThreeVal = getCardVal(dealerCardThree);
int ace3 = 0;
if (dealerThreeVal == 1)
{
if (cardSumDealer <= 10)
{
cout << endl << "The dealer chose 11" << endl; //REMOVE
ace3 = 11;
}
else if (cardSumDealer > 10)
{
cout << endl << "The dealer chose 1" << endl;//REMOVE
ace3 = 1;
}
cardSumDealer += ace3;
}
else
cardSumDealer += dealerThreeVal;
cout << endl;
}
if (cardSumDealer <= 21)
{
cout << endl << "The Dealer's total is : " << cardSumDealer << endl;
dealerRunResponse = false;
compareHands(cardSumPlayer, cardSumDealer);
}
else if (cardSumDealer > 21)
{
cout << "The Dealer busted. You win!" << endl;
money += Win();
cout << "You now have";
blueText();
cout << " $" << money << endl;
whiteText();
updateFile(money);
dealerRunResponse = false;
}
}
cout << "Would you like to play another hand? If so, please press 'y' and enter." << endl;
bool endRun = true;
cin >> character;
if (character == 'y')
run = true;
else
{
run = false;
}
cards.resize(52);
cards = permCards;
}
updateFile(money);
system("CLS");
return 0;
}
void updateFile(int mooney)
{
fout.open("SaveFile.txt"); //saving the new amount to the file
fout << mooney;
fout.close();
}
void compareHands(int playerHand, int dealerHand) //compares the dealer and player hands
{
if (playerHand > dealerHand)
{
cout << "You win! Congratulations!" << endl;
money += Win();
cout << "You now have";
blueText();
cout << " $" << money << endl;
whiteText();
}
else if (dealerHand > playerHand)
{
Lose();
cout << "You now have";
blueText();
cout << " $" << money << endl;
whiteText();
}
else if (dealerHand == playerHand)
{
money += Tie();
cout << "You now have";
blueText();
cout << " $" << money << endl;
whiteText();
}
}
int Tie() //activated in the case of a tie
{
int returnAmount = 0;
cout << "You neither win nor lose. It was a tie. You get your bet of $";
blueText();
cout << bet;
whiteText();
cout << " back.";
returnAmount += bet;
return returnAmount;
}
void Lose() //activated in the case of a loss
{
cout << "You lose. Sorry." << endl;
}
void Bust() //activated in the case of a bust
{
cout << "You busted. You lost: ";
blueText();
cout << "$" << bet;
whiteText();
cout << endl;
}
int Win() //activated in the case of a win
{
int winningAmount;
winningAmount = 2 * bet;
return winningAmount;
}
void title()
{
titleText();
cout << "=======================================" << endl;
cout << " _____ _ _ __ _ " << endl;
cout << "| __ | |___ ___| |_ __| |___ ___| |_ " << endl;
cout << "| __ -| | .'| _| '_| | | .'| _| '_|" << endl;
cout << "|_____|_|__,|___|_,_|_____|__,|___|_,_|" << endl;
cout << "=======================================" << endl;
whiteText();
}
int aceControl(int cardVal, int sum)
{
cout << endl << "Would you like to use your Ace as a 1 or an 11? Type '1' or '11'." << endl;
int aceValue;
bool aceRun = true;
cin >> aceValue;
while (aceRun)
{
if (aceValue == 1)
{
return 1;
aceRun = false;
}
else if (aceValue == 11)
{
return 11;
aceRun = false;
}
else
{
cout << "That is not a valid response. Please enter your answer again." << endl;
return NULL;
}
}
return NULL;
}
int drawCard()
{
int returnValue;
returnValue = cards.back();
cards.pop_back();
return returnValue;
}
void shuffleCards()
{
srand((unsigned int)time(NULL));
cout << "Shuffling the cards!" << endl;
random_shuffle(cards.begin(), cards.end());
for (int i = 0; i < cards.size(); i++)
{
cout << cards[i] << " ";
}
cout << endl;
}
void fixCard(int cardVal, int who) //couts the face of the card
{
string suit, number;
for (int i = cardVal; i == cardVal; i++)
{
if (cardVal == 1 || cardVal == 14 || cardVal == 27 || cardVal == 40)
number = "A";
else if (cardVal == 2 || cardVal == 15 || cardVal == 28 || cardVal == 41)
number = "2";
else if (cardVal == 3 || cardVal == 16 || cardVal == 29 || cardVal == 42)
number = "3";
else if (cardVal == 4 || cardVal == 17 || cardVal == 30 || cardVal == 43)
number = "4";
else if (cardVal == 5 || cardVal == 18 || cardVal == 31 || cardVal == 44)
number = "5";
else if (cardVal == 6 || cardVal == 19 || cardVal == 32 || cardVal == 45)
number = "6";
else if (cardVal == 7 || cardVal == 20 || cardVal == 33 || cardVal == 46)
number = "7";
else if (cardVal == 8 || cardVal == 21 || cardVal == 34 || cardVal == 47)
number = "8";
else if (cardVal == 9 || cardVal == 22 || cardVal == 35 || cardVal == 48)
number = "9";
else if (cardVal == 10 || cardVal == 23 || cardVal == 36 || cardVal == 49)
number = "10";
else if (cardVal == 11 || cardVal == 24 || cardVal == 37 || cardVal == 50)
number = "J";
else if (cardVal == 12 || cardVal == 25 || cardVal == 38 || cardVal == 51)
number = "Q";
else if (cardVal == 13 || cardVal == 26 || cardVal == 39 || cardVal == 52)
number = "K";
if (cardVal <= 13)
{
suit = "S";
}
else if (cardVal <= 26)
{
suit = "H";
}
else if (cardVal <= 39)
{
suit = "D";
}
else if (cardVal <= 52)
{
suit = "C";
}
//actually drawing the cards
if (who == 1)
{
greenText();
if (cardVal == 10 || cardVal == 23 || cardVal == 36 || cardVal == 49)
{
cout << endl << " ____" << endl;
cout << "|" << number << " |" << endl;
cout << "| " << suit << " |" << endl;
cout << "|__" << number << "|" << endl;
}
else
{
cout << endl << " ___" << endl;
cout << "|" << number << " |" << endl;
cout << "| " << suit << " |" << endl;
cout << "|__" << number << "|" << endl;
}
whiteText();
}
else if (who == 2)
{
redText();
if (cardVal == 10 || cardVal == 23 || cardVal == 36 || cardVal == 49)
{
cout << endl << " ____" << endl;
cout << "|" << number << " |" << endl;
cout << "| " << suit << " |" << endl;
cout << "|__" << number << "|" << endl;
}
else
{
cout << endl << " ___" << endl;
cout << "|" << number << " |" << endl;
cout << "| " << suit << " |" << endl;
cout << "|__" << number << "|" << endl;
}
whiteText();
}
}
}
int getCardVal(int cardVal) //gets the value of the card for use in blackjack
{
for (int i = cardVal; i == cardVal; i++)
{
if (cardVal == 1 || cardVal == 14 || cardVal == 27 || cardVal == 40)
return 1;
else if (cardVal == 2 || cardVal == 15 || cardVal == 28 || cardVal == 41)
return 2;
else if (cardVal == 3 || cardVal == 16 || cardVal == 29 || cardVal == 42)
return 3;
else if (cardVal == 4 || cardVal == 17 || cardVal == 30 || cardVal == 43)
return 4;
else if (cardVal == 5 || cardVal == 18 || cardVal == 31 || cardVal == 44)
return 5;
else if (cardVal == 6 || cardVal == 19 || cardVal == 32 || cardVal == 45)
return 6;
else if (cardVal == 7 || cardVal == 20 || cardVal == 33 || cardVal == 46)
return 7;
else if (cardVal == 8 || cardVal == 21 || cardVal == 34 || cardVal == 47)
return 8;
else if (cardVal == 9 || cardVal == 22 || cardVal == 35 || cardVal == 48)
return 9;
else if (cardVal == 10 || cardVal == 23 || cardVal == 36 || cardVal == 49 || cardVal == 11 || cardVal == 24 || cardVal == 37 || cardVal == 50 || cardVal == 12 || cardVal == 25 || cardVal == 38 || cardVal == 51 || cardVal == 13 || cardVal == 26 || cardVal == 39 || cardVal == 52)
return 10;
}
return NULL;
}