Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
// This is our standard input/output library
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//Defining
typedef struct {
	char cSuit;
	int nVal;
} Card;

//Function Prototypes
void printCard(int cardNumber, char cSuit);	//Prints card number and suit
void dealCard(Card deck[], int *index, Card *card);//Access to function in MAIN
void welcomeMenu(void);//Prints welcomeMenu
void hitMenu(void);//Prints hitMenu
void showMenu(void);//Prints showMenu
void exitMessage(void);//Prints exitMessage
void hitMessage(void);//Prints hitMessage

//Global Variables

int main(void)
{
	welcomeMenu();	//Function that prints menu for game
	int menuChoice = -1;
	int nCardValue = 2;	//Prints card value
	int playerCardsCount = 0;	//Inital count set to zero 
	int compCardsCount = 0;	//Inital count set to zero
	int index = 0;
	int hitMenu;	//Print hit menu
	int nComputerTotal = 0;
	int nPlayerTotal = 0;
	char cSuit = 3;	//Prints suit symbol
	Card Deck[52];	//Assign an array for Card Deck
	Card playerCards[5];	//Assign an array of 5 cards to player
	Card computerCards[5];	//Assign an array of 5 cards to computer

	srand(time(NULL));	// seed random number generator from time
////////////////////////////////////////////////////////////////////////////////////////////////////////    
	for (int i = 0; i < 52; i++)
	{
		Deck[i].nVal = nCardValue;
		Deck[i].cSuit = cSuit;
		nCardValue++;
		if (nCardValue > 14)
		{
			nCardValue = 2;
			cSuit++;
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////    
	//for loop to randomize the selection of cards a maxium of 299 times
	//for loop first shuffles up to 199 and adds another 100 more
	for (int i = 0; i < ((rand() % 200) + 100); i++)
	{
		int nFirst = (rand() % 52);
		int nSecond = nFirst;
		while (nSecond == nFirst)
		{
			nSecond = (rand() % 52);
		}
		Card Temp;

		//assigning temp cards
		Temp.nVal = Deck[nFirst].nVal;
		Temp.cSuit = Deck[nFirst].cSuit;

		//assign second card to first
		Deck[nFirst].nVal = Deck[nSecond].nVal;
		Deck[nFirst].cSuit = Deck[nSecond].cSuit;

		//assign second card to temp position
		Deck[nSecond].nVal = Temp.nVal;
		Deck[nSecond].cSuit = Temp.cSuit;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////    
	while (menuChoice != 0)
	{
		showMenu();
		scanf("  		%d", &menuChoice);
		if (menuChoice == 1)	//Input from player
		{
			//Calling the function dealCard and feeding in the Deck into the function
			//Then passing in the address of index from the pointer of function
			//Then call in the address of card a from the pointer
			
			//Player Card 1
			dealCard(Deck, &index, &playerCards[playerCardsCount]);	//Sub 0
			playerCardsCount++;	//update playersCard count
			printf("\n");

			//Computer Card 1
			dealCard(Deck, &index, &computerCards[compCardsCount]);	//Sub 0
			compCardsCount++;	//update computerCard count
			printf("\n");

			/*//Player Card 2
			dealCard(Deck, &index, &playerCards[playerCardsCount]);	//Sub 1
			playerCardsCount++;	//update playersCard count
			printf("\n");*/

			
////////////////////////////////////////////////////////////////////////////////////////////////////////    
			// print cards for how many "playerCardsCount" is active in playerCards[] array
			for (int i = 0; i < playerCardsCount; i++)
			{
				//print card playerCards[0]
				printCard(playerCards[i].nVal, playerCards[i].cSuit);

				//  accumulate playerCards total
				if (playerCards[i].nVal > 10 && playerCards[i].nVal < 14)
				{
					nPlayerTotal += 10;
				}
				else if (playerCards[i].nVal == 14)
				{
					if ((nPlayerTotal + 11) > 21)
						nPlayerTotal += 1;
					else
						nPlayerTotal += 11;
				}

				nPlayerTotal += playerCards[i].nVal;
				printf("Player Card's total is %d\n", nPlayerTotal);
			}
////////////////////////////////////////////////////////////////////////////////////////////////////////            
			// print cards for how many "compterCardsCount" is active in computerCards[] array
			for (int i = 0; i < compCardsCount; i++)
			{
				//print card playerCards[0]
				printCard(computerCards[i].nVal, computerCards[i].cSuit);

				//  accumulate playerCards total
				if (computerCards[i].nVal > 10 && computerCards[i].nVal < 14)
				{
					nComputerTotal += 10;
				}
				else if (computerCards[i].nVal == 14)
				{
					if ((nComputerTotal + 11) > 21)
						nComputerTotal += 1;
					else
						nComputerTotal += 11;
				}

				nComputerTotal += computerCards[i].nVal;
				printf("Computer Card's total is %d\n", nComputerTotal);
			}
////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////
			printf("\n");
			while (playerCardsCount < 5)
			{
				hitMessage();
				scanf("%d", &hitMenu);
				if (hitMenu == 1)
				{
					dealCard(Deck, &index, &playerCards[playerCardsCount]);
					playerCardsCount++;
					nPlayerTotal = 0;
					for (int i = 0; i < playerCardsCount; i++)
					{
						//print card playerCards[i]
						printCard(playerCards[i].nVal, playerCards[i].cSuit);
						//printf("Player Card #%d is a %d%c\n", (i + 1), playerCards[i].nVal, playerCards[i].cSuit);

						//  accumulate playerCards total, not considering 11 | 1 aces yet
						if (playerCards[i].nVal > 10 && playerCards[i].nVal < 14)
						{
							nPlayerTotal += 10;
						}
						else if (playerCards[i].nVal == 14)
						{
							if ((nPlayerTotal + 11) > 21)
								nPlayerTotal += 1;
							else
								nPlayerTotal += 11;
						}

						nPlayerTotal += playerCards[i].nVal;
						printf("Player Card's total is %d\n", nPlayerTotal);

					}
				}
				else if (hitMenu == 2)
					break;
			}	// end of while, "hit loop"
////////////////////////////////////////////////////////////////////////////////////////////////////////    
			// "Hit" loop is over and print results

			// print player cards
			
			// print total for player

			// print computer cards

			// print total for computer

			// Game over?
			//////////////////////////////////////////////////////////////////////////////

			//Menu for player 
			showMenu();
			scanf("			%d", &menuChoice);
			//* 
			if (menuChoice == 1)
			{
				// reset all variables for game
				nPlayerTotal = 0;
				playerCardsCount = 0;
				nComputerTotal = 0;
				compCardsCount = 0;

				for (int i = 0; i < 5; i++)
				{
					playerCards[0].nVal = 2;
					playerCards[1].nVal = 2;
					playerCards[2].nVal = 2;
					playerCards[3].nVal = 2;
					playerCards[4].nVal = 2;
					computerCards[0].nVal = 2;
					computerCards[1].nVal = 2;
					computerCards[2].nVal = 2;
					computerCards[3].nVal = 2;
					computerCards[4].nVal = 2;

				}
			}	//*/

			else	// don't want to play again
			{
				exitMessage();
				break;	// original menu break
			}

			///////////////////////////////////////////////////////////////////////////////
		}
		else
			exitMessage();
	}	// end of menu

	return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////Functions//////////////////////////////////////////

//Function welcomeMenu
void welcomeMenu(void)
{
	printf("		   ************BLACKJACK************\n");
	printf("	           **********PLEASE CHOOSE**********\n\n");
}
//Function for dealCard
void dealCard(Card deck[], int *index, Card *card)
{
	int spot = *index;
	(*card).nVal = deck[spot].nVal;	//temp value of card = the spot in the deck
	(*card).cSuit = deck[spot].cSuit;	//temp suit = the spot in deck
	(*index)++;
}	//End of Function

//Function for int cardNumber & char suit
void printCard(int cardNumber, char cSuit)
{
	if (cardNumber <= 10)
	{
		printf("			    ------------\n");
		printf("			    |%c         |\n", cSuit);
		printf("			    |          |\n");
		printf("			    |          |\n");
		printf("			    |    %d     |\n", cardNumber);
		printf("			    |          |\n");
		printf("			    |          |\n");
		printf("			    |         %c|\n", cSuit);
		printf("			    ------------\n");
		//printf("Player card#: %d is a %c\n", cardNumber, cSuit);  // Can not store 2 digit in a char variable
	}
	else
	{
		if (cardNumber == 11)	// If nCardValue is equal to 11 change to letter
			printf("			Player card#: J of %c\n", cSuit);	// Changes cSuit to Jack
		if (cardNumber == 12)
			printf("			Player card#: Q of %c\n", cSuit);	// Changes cSuit to Queen
		if (cardNumber == 13)
			printf("			Player card#: K of %c\n", cSuit);	// Changes cSuit to King
		if (cardNumber == 14)
			printf("			Player card#: A of %c\n", cSuit);	// Changes cSuit to Ace
	}
}	//End of function

//Showmenu
void showMenu(void)
{
	printf("\n");
	printf("		   ---------------------------------\n");
	printf("   		   |	    1- Play BlackJack      |\n");	//Choice 1
	printf("   		   |	    0- Exit Program        |\n");	//Choice 2
	printf("		   ---------------------------------\n");
	printf("				Choice:");
}	//End of function
//Hit message
void hitMessage(void)
{
	printf("		   ---------------------------------\n");
	printf("		   |     Would you like to HIT!?!  |\n");
	printf("		   |  Enter 1 for yes and 2 for no.|\n");
	printf("		   ---------------------------------\n");
	printf("			    Choice:");
}

//Exit Message
void exitMessage(void)
{
	printf("		Thank you for playing Blackjack!!!\n\n");
}	//End of function

//Reset all Varaiables
void resetVariables(void)
{
}	//End of function




/////////////////////////////////////////////////////////////////////////
////////////////Working on/////////////////////////////////////


What I have tried:

I moved all my resets to bottom in hope that would clear variables but it still only sometimes works.
Posted
Updated 6-Mar-16 10:12am

You havent understood the for loop and forgot the other member of your struct
C++
//looping all members
for (int i = 0; i < 5; i++)
{
    playerCards[i].nVal = 2;
    playerCards[i].cSuit = 0;//reset
    computerCards[i].nVal = 2;
    computerCards[i].cSuit = 0;//reset
}


Clearer would be classes or a function

C++
void resetCard(Card &card)
{
   card.nval = 2;
   card.cSuit = 0;
}
//used like that
for (int i = 0; i < 5; i++)
{
    resetCard( playerCards[i] );
    resetCard( computerCards[i] );
}
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900