blackjack game

Hello,
so this is a last minute call for help. Now I am pretty much a beginner, been doing programming and c++ in general for not full 2 months. I have created a very basic yet fully/sort of working BlackJack game that hits all of the criteria for my assignment. ALTHOUGH I have been given a last minute notice that I am not allowed to use std::vector - my program is made using essentially that. I am allowed to only use Structs and arrays apparently. Could someone point me in a direction to how I could change the minimal amount of this code to use arrays instead ?


#include <iostream>
#include <vector>
#include <time.h>
#include <algorithm>



char getFromKeyboard();

char suits[4] = { 'h', 'd', 's', 'c' };
char ranks[13] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };



struct Card
{

char suit;
char rank;


void printCard()
{
std::cout << rank << suit;
}
};

void createDeck(std::vector<Card>& deck)
{


suits[0] = (int)3;
suits[1] = (int)4;
suits[2] = (int)6;
suits[3] = (int)5;

for (int s = 0; s < 4; s++)
{
for (int r = 0; r < 13; r++)
{
Card playerCard;
playerCard.rank = ranks[r];
playerCard.suit = suits[s];
deck.push_back(playerCard);

}

}

}


int cardDraw(std::vector<Card>& deck)
{

int valCard;
char choice;
bool menu;

if (ranks[12] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[11] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[10] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[9] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[8] == deck.back().rank)
{
valCard = 9;
}
else if (ranks[7] == deck.back().rank)
{
valCard = 8;
}
else if (ranks[6] == deck.back().rank)
{
valCard = 7;
}
else if (ranks[5] == deck.back().rank)
{
valCard = 6;
}
else if (ranks[4] == deck.back().rank)
{
valCard = 5;
}
else if (ranks[3] == deck.back().rank)
{
valCard = 4;
}
else if (ranks[2] == deck.back().rank)
{
valCard = 3;
}
else if (ranks[1] == deck.back().rank)
{
valCard = 2;
}
else if (ranks[0] == deck.back().rank)
{

menu = true;

while (menu == true)
{
std::cout << "You've received " << deck.back().rank << deck.back().suit << "\n\n";
std::cout << "Please choose a value [1] for 1 or [2] for 11:\n\n";
choice = getFromKeyboard();

if (choice == '1')
{
valCard = 1;
menu = false;
}
else if (choice == '2')
{
valCard = 11;
menu = false;
}
else if (choice != '1' || choice != '2')
{
std::cout << "\n\nOOPS!! You've inputted a wrong number, please try again!!!\n\n";

}
}

}

return valCard;
}

int compAceValidation(std::vector<Card>& deck, int compTotalPts)
{


if (compTotalPts > 10) // if the dealers total points are higher than 10
{
compTotalPts = 1;
}
else if (compTotalPts <= 10) // if the dealers total points are lower or equal to 10
{
compTotalPts = 11;
}

return compTotalPts;
}

int cardCompDraw(std::vector<Card>& deck, int compTotalPts)
{

int valCard;


if (ranks[12] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[11] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[10] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[9] == deck.back().rank)
{
valCard = 10;
}
else if (ranks[8] == deck.back().rank)
{
valCard = 9;
}
else if (ranks[7] == deck.back().rank)
{
valCard = 8;
}
else if (ranks[6] == deck.back().rank)
{
valCard = 7;
}
else if (ranks[5] == deck.back().rank)
{
valCard = 6;
}
else if (ranks[4] == deck.back().rank)
{

valCard = 5;
}
else if (ranks[3] == deck.back().rank)
{
valCard = 4;
}
else if (ranks[2] == deck.back().rank)
{
valCard = 3;
}
else if (ranks[1] == deck.back().rank)
{
valCard = 2;
}
if (deck.back().rank == ranks[0]) // Handling second card draw Ace values for dealer
{
valCard = compAceValidation(deck, compTotalPts);
}


return valCard;
}



int playerFirstCard(std::vector<Card>& deck)
{
int playerTotalPts;

if (deck.back().rank == ranks[0])
{
playerTotalPts = 11;
std::cout << deck.back().rank << deck.back().suit << " = " << playerTotalPts << "\n\n";
}
else if (deck.back().rank != ranks[0])
{

playerTotalPts = cardDraw(deck);
std::cout << deck.back().rank << deck.back().suit << " = " << playerTotalPts << "\n\n";
}
deck.pop_back();

playerTotalPts = cardDraw(deck) + playerTotalPts;

std::cout << deck.back().rank << deck.back().suit << " = " << playerTotalPts << "\n\n";
deck.pop_back();


return playerTotalPts;

}

int compFirstCard(std::vector<Card>& deck)
{
int compTotalPts = 0;


if (deck.back().rank == ranks[0])
{
compTotalPts = 11;
std::cout << deck.back().rank << deck.back().suit << " = " << compTotalPts << "\n\n";
}
else if (deck.back().rank != ranks[0])
{

compTotalPts = cardCompDraw(deck, compTotalPts);
std::cout << deck.back().rank << deck.back().suit << " = " << compTotalPts << "\n\n";
}
deck.pop_back();



return compTotalPts;

}
Last edited on
Heres the second bit :/

int main()
{

int playerTotalPts, compTotalPts;
char choice, choice2, choice3;
bool menu;

//std::cout << "Welcome to Black Jack\n\nTo play please press '1'\n\nTo exit press '2'\n\n";
std::vector<Card> deck;
createDeck(deck);

srand(time(NULL));
std::random_shuffle(deck.begin(), deck.end());

std::cout << "Welcome to Black-Jack!!\n\nTo begin playing please press '1'\nTo exit the game please press '2'\n\n";
choice2 = getFromKeyboard();
system("cls");


if (choice2 == '1')
{
int money = 1000;


while (true)
{

int bet = 0;


if (money <= 0)
{
std::cout << "\nYou've run out of money!! GAME OVER!!!";
break;
}
if (deck.size() < 13) // reshuffles the deck when almost out of cards
{
std::cout << "\n\nYou're running out of cards, the game is going to reshuffle the deck after this game!!\n\n";
createDeck(deck);
srand(time(NULL));
std::random_shuffle(deck.begin(), deck.end());

}


std::cout << "You currently have $" << money << " \n\nHow much would you like to bet ? : ";
std::cin >> bet;

int bonus = bet / 2;


if (bet > 0 && bet <= money)
{

money = money - bet;



std::cout << "\nPlayer cards are being dealt now\n\n";
playerTotalPts = playerFirstCard(deck);
system("PAUSE");

std::cout << "\nDealers cards are being dealt now\n\n";
compTotalPts = compFirstCard(deck);
system("PAUSE");




if (playerTotalPts < 21)
{
menu = true;
while (menu == true && playerTotalPts < 21)
{
std::cout << "\nYou have " << playerTotalPts << "points\n";
std::cout << "\nDo you want to 1 - Hit or 2 - Stay ?";
choice = getFromKeyboard();
if (choice == '1')
{
playerTotalPts = cardDraw(deck) + playerTotalPts;
std::cout << "\n\n" << deck.back().rank << deck.back().suit << " = " << playerTotalPts << "\n\n";
deck.pop_back();
}
else if (choice == '2')
{
std::cout << "You stuck with " << playerTotalPts << "points\n\n";
menu = false;
}
else if (choice != '1' || choice != '2')
{
std::cout << "OOPS!! Something went wrong try again please!!!\n\n";
}

}
}
else if (playerTotalPts == 21)
{
std::cout << "\nCongratulations!!! You've got Black-Jack!!\n\n";
}
if (playerTotalPts > 21)
{
std::cout << "Unlucky!!! You were over 21 YOU LOSE!!!\n\n";
system("PAUSE");
system("cls");

}


if (playerTotalPts <= 21)
{
std::cout << "\nDealers turn to draw cards\n\n";

while (compTotalPts < 21)
{
if (compTotalPts < 17)
{
compTotalPts = cardCompDraw(deck, compTotalPts) + compTotalPts;
std::cout << deck.back().rank << deck.back().suit << " = " << compTotalPts << "\n\n";
deck.pop_back();
}
else if (compTotalPts >= 17)
{
std::cout << "Dealer stays on = " << compTotalPts << "points\n\n";
if (compTotalPts == playerTotalPts)
{
std::cout << "\nIts a DRAW, bets are returned\n\n";
int win = bet;
money = money + win;
system("PAUSE");
system("cls");
}
else if (compTotalPts > playerTotalPts)
{
std::cout << "Unlucky!! The house wins!!\n\n";
system("PAUSE");
system("cls");
}
else if (compTotalPts < playerTotalPts && playerTotalPts < 21)
{
int win = bet * 2;
std::cout << "Congratulations you win $" << win << "!!!\n\n";
money = money + win;
system("PAUSE");
system("cls");
}
else if (playerTotalPts == 21)
{

int win = bonus + (bet * 2);
std::cout << "Congratulations you have BlackJack you win $" << win << " !!\n\n";
money = money + win;
system("PAUSE");
system("cls");
}

break;
}
}
if (compTotalPts > 21)
{
if (playerTotalPts < 21)
{
int win = bet * 2;
std::cout << "\nThe dealer busts, player wins $" << win << "!!\n\n";
money = money + win;
system("PAUSE");
system("cls");
}
else if (playerTotalPts == 21)
{

int win = bonus + (bet * 2);
std::cout << "\nThe dealer busts, player has BlackJack and wins $" << win << "!!\n\n";
money = money + win;
system("PAUSE");
system("cls");
}
}
if (compTotalPts == 21)
{
std::cout << "\nDealer has BlackJack\n\n";
if (playerTotalPts < 21)
{
std::cout << "\nThe dealer wins!!!\n\n";
system("PAUSE");
system("cls");

}
if (playerTotalPts == 21)
{
std::cout << "\nIts a DRAW, bets are returned\n\n";
int win = bet;
money = money + win;
system("PAUSE");
system("cls");
}
}
}


std::cout << "\n\nIf you wish to continue playing press 'Y'\n\nIf you wish to exit the game press 'E'\n\n";
choice3 = getFromKeyboard();

if (choice3 == 'y' || choice3 == 'Y')
{
system("cls");
}
else if (choice3 == 'e' || choice3 == 'E')
{
std::cout << "\n\nThank you for playing!!!";
break;
}

std::cout << "\n\n";



for (Card playerCard : deck)
{
playerCard.printCard();
std::cout << ", ";

}

}




}


}

else if (choice2 == '2')
{
std::cout << "Thank you for playing!!";
}



std::cin.get();
return 0;
}

char getFromKeyboard()
{
char rtn;
std::cin >> rtn;

std::cin.ignore(INT_MAX, '\n');

return rtn;
}

Topic archived. No new replies allowed.