Code:
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int drawCard();
void shuffleDeck(int deck[54]);
void game();
static int deck[52] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13};
//The main function includes pre-game stuff and instructions.
int main() {
time_t now;//Sets now to equal the current time.
time(&now);
srand(now);//Uses the current time as the seed for random numbers.
char viewInstructions;
cout<< "Welcome to Blackjack.\nWould you like to view the instructions? (Y/N)\n";
do {
cin>> viewInstructions;
cin.ignore();
switch(toupper(viewInstructions)) {
case 'Y':
cout<< "Select an option by typing its letter.\nPress enter when you are ready to play.\n";
cin.get();
game();
break;
case 'N':
game();
break;
default:
cout<< "Syntax error. Please type Y or N, then press enter.\n";
}
}while (viewInstructions != 'y' || 'Y' || 'n' || 'N');
return 0;
}
//This function starts the game.
void game() {
int playerHand[5];
int dealerHand[5];
cout<< "game is running";//DEBUG
}
//This function draws a new card.
int drawCard() {
static int cardToDraw = 0;
int drawnCard;
if (cardToDraw < 52) {//If there are cards left in the deck, draw one.
drawnCard = deck[cardToDraw];
deck[cardToDraw] = 0;//Removes the card from the deck.
cardToDraw++;//Next time the function is called, it will draw the next card.
}
else {//If there are no cards in the deck, shuffle the deck.
shuffleDeck(deck);
cardToDraw = 0;//Draw deck[0] next time the function is called.
}
cout<< "cardToDraw = "<< cardToDraw<< ".\n";//Debugging line.
return drawnCard;
}
//This function shuffles the deck.
void shuffleDeck(int deck[54]) {
int cardToSwap;//This is the card that will be swapped with the first card.
int temp;//This is used to temporarily store the value of deck[cardToSwap]
int i;
for (i = 0; i < 52; i++){//Loops through each card, swapping it with a random card from the deck.
cardToSwap = (rand() % 52);
temp = deck[cardToSwap];
deck[i] = deck[cardToSwap];
deck[cardToSwap] = temp;
}
}
I'm having some trouble getting the instruction do/while loop in main to work. For one thing, it displays the instructions no matter what I type. It also doesn't end after it calls the game function. It just waits for input from the user, then keeps waiting for more input if any is given.