-
Simple blackjack program
Hello,
I'm writing a simple blackjack program and I can get the user's cards to display but I can not get the dealer's cards to display I tried inserting the pause function on every line in my code but I can not get the cards to display, also after pressing y or Y to play again the program terminates and will not restart. Any direction would be greatly appreciated, here is my code.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[] ) {
int cards[52] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int deal( int cards[] );
void shuffle( int wDeck[][ 13 ] );
srand(time(NULL));
int showCard( int card );
int totalPlayerHand = 0;
int totalDealerHand = 0;
char hit = 'H';
char playAgain = 'Y';
void pause( int secs );
int balance = 0;
int wager = 0;
int newBalance = 0;
puts( "Enter your balance: ");
scanf( "%d", &balance );
while( balance <= 0 ){
puts( "You entered an invalid balance!!!" );
puts( "Enter your balance: ");
scanf( "%d", &balance );
}
puts( "Enter your wager: ");
scanf( "%d", &wager );
while( wager > balance || wager < 10 ){
puts( "Enter a valid wager: ");
scanf( "%d", &wager );
}
while ( toupper(playAgain) == 'Y' && balance > 0 ){
int card1 = deal( cards );
int card2 = deal( cards );
totalPlayerHand = showCard( card1 );
totalPlayerHand += showCard( card2 );
printf( "You have a %d.\n", totalPlayerHand );
while ( totalPlayerHand < 21 && toupper(hit) == 'H' ){
getchar();
printf( "Do you want to hit or stay?\n" );
printf( "Press H for hit or S for stay.\n" );
scanf( "%c", &hit );
if( toupper(hit) == 'H' ){
int card = deal( cards );
totalPlayerHand += showCard( card );
printf( "You have %d.\n", totalPlayerHand );
}
else if( toupper(hit) == 'S' ){
printf( "You have %d.\n", totalPlayerHand );
}
}
if( totalPlayerHand > 21 || totalPlayerHand < totalDealerHand ){
printf( "You lose.\n" );
newBalance = balance - wager;
printf( "Your balance is $%i.\n", newBalance );
}
while ( totalDealerHand > 17 || totalDealerHand > totalPlayerHand || totalDealerHand > 21 ){
int card1 = deal( cards );
int card2 = deal( cards );
totalDealerHand = showCard( card1 );
totalDealerHand += showCard( card2 );
printf( "The dealer has %d \n", totalDealerHand );
if( totalDealerHand < 17 ){
int card = deal( cards );
totalDealerHand += showCard( card );
printf( "The dealer has %d \n", totalDealerHand );
}
else if( totalDealerHand > 21 || totalDealerHand < totalPlayerHand ){
printf( "You won!!!\n" );
newBalance = balance + wager;
printf( "Your balance is $%i.\n", newBalance );
}
}
printf( "Do you want to play again press 'Y' or 'N' \n" );
scanf( "%c", &playAgain );
getchar();
}
system( "PAUSE" );
return 0;
}
int deal( int cards[] ){
int value;
int random = 0;
random = rand() % 52 + 1;
while( cards[random] == 0 ) {
random = rand() % 52 + 1;
}
value = cards[random];
cards[random] = 0;
return value;
}
void shuffle( int wDeck[][ 13 ] ){
int row, column, card;
for( card = 1; card <= 52; card++ ){
do{
row = rand() % 4;
column = rand() % 13;
}
while( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;
}
}
int showCard( int card ){
int value;
if( card <= 10 ) {
printf( "%d ", card );
value = card;
}
else if( card == 11 ) {
printf( "J " );
value = 10;
}
else if( card == 12 ) {
printf( "Q ");
value = 10;
}
else if( card == 13 ) {
printf( "K " );
value = 10;
}
else if( card == 14 ) {
printf( "A " );
value = 11;
}
return value;
}
void pause( int secs ){
int icurrentTime = 0;
int iendtime = 0;
icurrentTime = time( NULL );
while( ( iendtime - icurrentTime ) < secs ){
iendtime = time( NULL );
}
}
-
You don't have your program looping. You need something like:
Code:
int main( void )
{
...some stuff...
do
{
...all this stuff...
prompt for yes / no
} while( answer == 'y' );
...possibly more stuff...
return 0;
}
Quzah.