I finished it and it works for the most part. I had a difficult time using the 'A' as 11 and 1. I found a way of doing it with TONS of extra code and writing things to paper first, planning. I sure to you regular programmers this code will look ugly, but it works and it is my first big program
The problem I could not solve yet is the 'do while' loop for the "Press 'h' to hit or 's' to stay". If I just hit the return key with no value first it is in an endless loop that I can't get out of even if I enter a value and hit return again.
One question I have that I am unsure of are the string zero - terminators. My char playerCards[15] terminates when I set it up. But then during the execution of the code I will reset the zero terminator from [15] to [3] as I add cards to the list. Is that wrong to do? Can a line of code look like this and be OK, char playerCards[15] = {'-','-','-','-','\0','-','-','-','-','-','-','-','-','-','\0'};
Anyway here is the game.
Thanks for your help.
-Lars
The problem I could not solve yet is the 'do while' loop for the "Press 'h' to hit or 's' to stay". If I just hit the return key with no value first it is in an endless loop that I can't get out of even if I enter a value and hit return again.
One question I have that I am unsure of are the string zero - terminators. My char playerCards[15] terminates when I set it up. But then during the execution of the code I will reset the zero terminator from [15] to [3] as I add cards to the list. Is that wrong to do? Can a line of code look like this and be OK, char playerCards[15] = {'-','-','-','-','\0','-','-','-','-','-','-','-','-','-','\0'};
Anyway here is the game.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
void pCharToInt(int *pTotal,int *pReduceAceValueToOne, char card);
void dCharToInt(int *dTotal, int *dReduceAceValueToOne, char card);
int main (int argc, const char * argv[]) {
int i,randNumberRoll,nextCard,deck,pTotal,dTotal, pBust,dBust,blackJack;
int gameCount, pWin,dWin, checkForAce, pAcesFound, pReduceAceValueToOne;
int dAcesFound, dReduceAceValueToOne, displayHideFirstTime;
char playerResponce;
char mixDeck[53];
char cards[53] = {'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','T',
'T','T','T','J','J','J','J','Q','Q','Q','Q','K','K','K','K',
'A','A','A','A','\0'};
srand(clock());
nextCard = 0;
deck = 52;
nextCard = 0;
gameCount = 0;
pWin = 0;
dWin = 0;
//----------------------------------------- Mixes the deck to mixDeck array
printf("***** Welcome to BlackJack *****\n");
sleep(1);
printf("BEST OUT OF 5 GAMES\n\n");
for(i=0 ; i< deck ; i++)
{
randNumberRoll = rand() % (deck);
do{
if (cards[randNumberRoll] == '-')
randNumberRoll = rand() % (deck);
}while(cards[randNumberRoll] == '-');
mixDeck[i] = cards[randNumberRoll];
cards[randNumberRoll] = '-';
}
mixDeck[53] = '\0';
//------------------------------------------------------Game Starts
do{
gameCount++;
pTotal = 0;
dTotal = 0;
blackJack = 0;
pBust = 0;
dBust = 0;
pReduceAceValueToOne = 0;
pAcesFound = 0;
dReduceAceValueToOne = 0;
dAcesFound = 0;
displayHideFirstTime = 1;
char playerCards[15] = {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','\0'};
char dealerCards[15] = {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','\0'};
printf("/////// GAME %d\n", gameCount);
sleep(1);
//------------------------------------------------------Deals computer cards
dealerCards[0] = mixDeck[nextCard];
dCharToInt(&dTotal,&dReduceAceValueToOne, mixDeck[nextCard]);
nextCard++;
dealerCards[2] = mixDeck[nextCard];
dCharToInt(&dTotal,&dReduceAceValueToOne, mixDeck[nextCard]);
nextCard++;
if(dTotal == 22) //----Checks to see if you got 2 A's
{
dTotal = 12;
pAcesFound++;
}
printf("\n");
printf("----> Dealer Cards %c - *?* \n\n",dealerCards[0]);
//-----------------------------------------------------Deals player cards
playerCards[0] = mixDeck[nextCard];
pCharToInt(&pTotal,&pReduceAceValueToOne, mixDeck[nextCard]);
nextCard++;
playerCards[2] = mixDeck[nextCard];
pCharToInt(&pTotal,&pReduceAceValueToOne, mixDeck[nextCard]);
nextCard++;
playerCards[3] = '\0';
printf("-> Player Cards %s total = %d \n",playerCards, pTotal);
if(pTotal == 21)
{
printf(" **!!**> YOU GOT BLACK JACK<**!!**\n\n");
pWin++;
blackJack = 1;
}
if(pTotal == 22) //----Checks to see if you got 2 A's
{
pTotal = 12;
}
//--------------------------------------------Player Hit or Stay loop
i = 3;
do {
if(blackJack == 1)
{
break;
}
do{
playerResponce = 0; //----reset player response to null to avoid inf loop.
printf(" // **Press 'h' for 'Hit' or 's' to Stay:");
scanf("%c", &playerResponce);
getchar();// --- removes any extra stuff in scanf buffer
}while(playerResponce != 's' && playerResponce != 'h');
if(playerResponce == 's')
{
displayHideFirstTime = 0;
break;
}
if (playerResponce == 'h')
{
displayHideFirstTime = 0;
playerCards[i] = '-';
i++;
playerCards[i] = mixDeck[nextCard];
i++;
playerCards[i] = '\0';
pCharToInt(&pTotal,&pReduceAceValueToOne, mixDeck[nextCard]);
printf("\n");
printf("----------------------------------\n");
if (displayHideFirstTime == 0) // avoids duplicating cosole printf
{
printf("Player cards %s total = %d \n",playerCards ,pTotal);
displayHideFirstTime = 0;
}
if (pTotal > 21) // Checks for Ace to reduce pTotal
{
for(checkForAce = 0; i < playerCards[checkForAce]; checkForAce++ ){
if (playerCards[checkForAce] == 'A')
{
pAcesFound++;
if(pAcesFound > pReduceAceValueToOne)
{
pReduceAceValueToOne++;
pTotal -= 10;
break;
}
}
}
}
pAcesFound = 0; //--- resets the Ace counter
if (pTotal > 21)
{
sleep(1);
printf("Player cards %s total = %d ",playerCards ,pTotal);
printf("// **** YOU BUSTED YOUR HAND****\n\n");
dWin++;
pBust = 1;
break;
}
printf("----------------------------------\n");
nextCard++;
}
}while ((playerResponce != 'h') || (playerResponce != 's'));
printf("\n");
//---------------------------------------------------------------- Computers hand
i = 3;
do{
if (pBust == 1) //-- checks to see if player busted
{
break;
}
if (blackJack == 1) //-- checks to see if player got blackjack
{
break;
}
printf("\n");
printf("----------------------------------\n");
printf("----> Dealer Cards %s total = %d \n", dealerCards,dTotal);
printf("----------------------------------\n");
if (dTotal < 17)
{
sleep(1);
dealerCards[i] = '-';
i++;
dealerCards[i] = mixDeck[nextCard];
i++;
dealerCards[i] = '\0';
dCharToInt(&dTotal,&dReduceAceValueToOne, mixDeck[nextCard]);
nextCard++;
printf("----> Dealer Cards %s total = %d \n", dealerCards,dTotal);
}
if (dTotal > 21) // Checks for Ace to reduce dTotal
{
for(checkForAce = 0; i < dealerCards[checkForAce]; checkForAce++ ){
if (dealerCards[checkForAce] == 'A')
{
dAcesFound++;
if(dAcesFound > dReduceAceValueToOne)
{
dReduceAceValueToOne++;
dTotal -= 10;
break;
}
}
}
}
dAcesFound = 0; //--- resets the Ace counter
if(dTotal > 21)
{
printf("*****DEALER BUSTED YOU WIN****\n\n");
pWin++;
printf("-------------------------\n");
dBust = 1;
break;
}
}while(dTotal < 17);
while( pBust != 1){
if(blackJack == 1)
{
break;
}
if(dBust == 1)
{
break;
}
if(pBust == 1 || dBust == 1)
{
break;
}
if(dTotal == pTotal)
{
printf("\n");
printf("------> WE PUSHED\n");
break;
}
if (dTotal > pTotal)
{
printf("\n");
printf("------> DEALER WINS <------\n");
dWin++;
break;
}
if(dTotal < pTotal)
{
printf("\n");
printf("------> YOU WIN <-----\n");
pWin++;
break;
}
}
displayHideFirstTime = 1;
}while(gameCount < 5);
//------------------------------------------------ Print results of the game
if (pWin == dWin)
{
printf("*** GAME OVER ***\n\n");
printf("***<WE TIED -- me %d - you %d >****\n", dWin,pWin);
}
if (pWin> dWin)
{
printf("*** GAME OVER ***\n\n");
printf("---> YOU WON -- you won %d - I won %d - I must power down in shame\n",pWin, dWin);
}
if (pWin < dWin)
{
printf("*** GAME OVER ***\n\n");
printf("----< I WON -- I won %d Games- you won %d games>- learn to program better", dWin, pWin);
}
return 0;
}
void pCharToInt(int *pTotal,int *pReduceAceValueToOne, char card)
{
int i;
switch (card) {
case '2':
i = 2;
break;
case '3':
i = 3;
break;
case '4':
i = 4;
break;
case '5':
i = 5;
break;
case '6':
i = 6;
break;
case '7':
i = 7;
break;
case '8':
i = 8;
break;
case '9':
i = 9;
break;
case 'T':
i = 10;
break;
case 'J':
i = 10;
break;
case 'Q':
i = 10;
break;
case 'K':
i = 10;
break;
case 'A':
if (*pTotal>= 11)
{
i = 1;
}
else
{
i = 11;
}
break;
default:
i = 9999;
break;
}
if (card == 'A')
if (i == 1)
{
*pReduceAceValueToOne = (*pReduceAceValueToOne + 1);
}
*pTotal = *pTotal + i;
}
void dCharToInt(int *dTotal, int *dReduceAceValueToOne, char card)
{
int i;
switch (card) {
case '2':
i = 2;
break;
case '3':
i = 3;
break;
case '4':
i = 4;
break;
case '5':
i = 5;
break;
case '6':
i = 6;
break;
case '7':
i = 7;
break;
case '8':
i = 8;
break;
case '9':
i = 9;
break;
case 'T':
i = 10;
break;
case 'J':
i = 10;
break;
case 'Q':
i = 10;
break;
case 'K':
i = 10;
break;
case 'A':
if (*dTotal>= 11)
{
i = 1;
}
else
{
i = 11;
}
break;
default:
i = 9999;
break;
}
if (card == 'A')
if (i == 1)
{
*dReduceAceValueToOne = (*dReduceAceValueToOne + 1);
}
*dTotal = *dTotal + i;
}
Thanks for your help.
-Lars