<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>deciacco.com blog &#187; C/C++</title>
	<atom:link href="http://www.deciacco.com/blog/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deciacco.com/blog</link>
	<description>my stuff online</description>
	<lastBuildDate>Tue, 29 Jun 2010 18:38:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Blackjack Game in C Lang!</title>
		<link>http://www.deciacco.com/blog/c/blackjack-game-in-c-lang/</link>
		<comments>http://www.deciacco.com/blog/c/blackjack-game-in-c-lang/#comments</comments>
		<pubDate>Tue, 04 Sep 2007 23:43:00 +0000</pubDate>
		<dc:creator>bojanm</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://www.deciacco.com/blog/archives/104</guid>
		<description><![CDATA[This is the code for a blackjack game that deciacco and I worked on for a project for a class that I had. Documentation is in the code itself. Update: By deciacco: I uploaded a zip file with the c file and a compiled exe so you can give it a try! You should be [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Blackjack Image" href="http://www.deciacco.com/blog/wp-content/uploads/blackjack.gif"><img title="Blackjack Image" src="http://www.deciacco.com/blog/wp-content/uploads/blackjack.thumbnail.gif" alt="Blackjack Image" align="right" /></a></p>
<p>This is the code for a blackjack game that deciacco and I worked on for a project for a class that I had. Documentation is in the code itself.</p>
<p>Update:</p>
<ul>
<li>By deciacco: I uploaded a zip file with the c file and a compiled exe so you can give it a try! You should be able to run the exe in Windows without a problem.</li>
</ul>
<pre class="brush: cpp;">
/*
    Copyright (C) 2007  Bojan M.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .

*/
/**********************************************************************************/
/**  Bojan M.  *****************************************************************/
/**  blackjack.c  *****************************************************************/
/**  This will play a game of blackjack with the house, and 1 - 3 players for a  **/
/**  max of 10 hands. Since this game will not employ a wager, the scoring  *******/
/**  will be as follows: When the player or house get blackjack --&amp;gt; 2pts.  ********/
/**  If the player or house bust --&amp;gt; 0pts. If the player or house win --&amp;gt; 1pt.  ***/
/**  Pressing zero will exit the game.  *******************************************/
/**********************************************************************************/

#define _CRT_SECURE_NO_DEPRECATE = 1 //Avoid Visual Studio warning
#include
#include

#include
#include
	//Required for system()
#include 		//Required for rand() and srand()

//Value defenitions
#define TRUE 1
#define FALSE 0
#define BUFFSIZE 10
#define NOCARDS 52    //Will need to gen deck later
#define MAXPLAYERS 3
#define MAXHANDS 10
#define RANDSHUF 100
#define PLAYERSIZE 12
#define NEWCARDPOS 11
#define SCORE 0
#define SUM 1

//Function prototypes
void init_cards(int cards[]);
int get_card(int cards[], int *);
void shuf_cards(int cards[], int *);
void show_deck(int cards[]);
int random_int(int, int);
void show_p_hand(int player[][PLAYERSIZE], int);
void show_h_hand(int house[]);
void calc_p_sum(int player[][PLAYERSIZE], int);
void calc_h_sum(int house[]);
void summary(int house[], int player[][PLAYERSIZE], int);
void score(int house[], int player[][PLAYERSIZE], int);

/******************************  START main  *********************************/
void main()
{
	int i, j, no_players, no_hands, cur_hand = 0;

	int cards[NOCARDS];						//Deck of cards
	int player[MAXPLAYERS][PLAYERSIZE];		//Holds player stats during game
	int house[PLAYERSIZE];					//Holds house stats during game
	int play, ask, playhand;				//Flags
	int nextplayer;							//Flag
	int deckpos;							//Keeps track of where to store next card

	char yesno[BUFFSIZE];					//Input buffer
	char hitstay[BUFFSIZE];

	play = FALSE;
	ask = playhand = TRUE;

	house[SCORE] = 0;  //Initializing the house total score

	init_cards(cards);  //Calling two functions to generate cards and shuffle them
	shuf_cards(cards, &amp;amp;deckpos);

	printf(&quot;BBBB       LL          AAA      CCCCCC   KK      KK n&quot;);
	printf(&quot;BB  BB     LL         AA AA    CC    CC  KK     KK n&quot;);
	printf(&quot;BB    BB   LL         AA AA    CC    CC  KK    KK   n&quot;);
	printf(&quot;BB     BB  LL         AA AA    CC        KK   KK    n&quot;);
	printf(&quot;BB     BB  LL         AA AA    CC        KK  KK     n&quot;);
	printf(&quot;BB    BB   LL        AA   AA   CC        KK KK      n&quot;);
	printf(&quot;BB  BB     LL        AA   AA   CC        KKK       n&quot;);
	printf(&quot;BBBB       LL        AA   AA   CC        KK KK      n&quot;);
	printf(&quot;BB  BB     LL        AA   AA   CC        KK  KK     n&quot;);
	printf(&quot;BB    BB   LL       AAAAAAAAA  CC        KK   KK    n&quot;);
	printf(&quot;BB     BB  LL       AAAAAAAAA  CC        KK    KK   n&quot;);
	printf(&quot;BB     BB  LL       AA     AA  CC        KK     KK  n&quot;);
	printf(&quot;BB    BB   LL       AA     AA  CC    CC  KK      KKn&quot;);
	printf(&quot;BB  BB     LLLLLLL  AA     AA  CC    CC  KK       KKn&quot;);
	printf(&quot;BBBB       LLLLLLL  AA     AA   CCCCCC   KK        KKnnn&quot;);

	printf(&quot;	   JJ     AAA      CCCCCC   KK      KKn&quot;);
	printf(&quot;	   JJ    AA AA    CC    CC  KK     KKn&quot;);
	printf(&quot;	   JJ    AA AA    CC    CC  KK    KKn&quot;);
	printf(&quot;	   JJ    AA AA    CC        KK   KKn&quot;);
	printf(&quot;	   JJ    AA AA    CC        KK  KKn&quot;);
	printf(&quot;	   JJ   AA   AA   CC        KK KKn&quot;);
	printf(&quot;	   JJ   AA   AA   CC        KKKn&quot;);
	printf(&quot;	   JJ   AA   AA   CC        KK KKn&quot;);
	printf(&quot;	   JJ   AA   AA   CC        KK  KKn&quot;);
	printf(&quot;	   JJ  AAAAAAAAA  CC        KK   KKn&quot;);
	printf(&quot;	   JJ  AAAAAAAAA  CC        KK    KKn&quot;);
	printf(&quot;    JJ     JJ  AA     AA  CC        KK     KKn&quot;);
	printf(&quot;    JJ     JJ  AA     AA  CC    CC  KK      KKn&quot;);
	printf(&quot;    JJ     JJ  AA     AA  CC    CC  KK       KKn&quot;);
	printf(&quot;     JJJJJJJ   AA     AA   CCCCCC   KK        KKnnnnn&quot;);
	printf(&quot;Let's play some BlackJack!!!nn&quot;);
	printf(&quot;Please enter '1' - '%d' for the number of players, or enter '0' to quit.n&quot;, MAXPLAYERS);

	//If the user enters '0' ask will equal FALSE and will break out of loop
	//Else it will take the input and go on to ask for how many hands
	while(ask == TRUE)
	{
		printf(&quot;Enter choice: &quot;);
		scanf(&quot;%d&quot;, &amp;amp;no_players);

		if(no_players == 0)
		{
			ask = FALSE;
		}
		else if(no_players &amp;gt;= 1 &amp;amp;&amp;amp; no_players &amp;lt;= MAXPLAYERS)
		{
			play = TRUE;
			ask = FALSE;
		}
	}

	ask = TRUE;
	//system(&quot;cls&quot;);

	//If the user enters '0' ask will equal FALSE and will break out of loop
	//Else it will take the input and go on to play the game
	if(play == TRUE)
	{
		play = FALSE;
		printf(&quot;How many hands do you want to play (max %d hands, '0' to quit.n&quot;, MAXHANDS);

		while(ask == TRUE)
		{
			printf(&quot;Enter choice: &quot;);
			scanf(&quot;%d&quot;, &amp;amp;no_hands);

			if(no_hands == 0)
			{
				ask = FALSE;
			}
			else if(no_hands &amp;gt;= 1 &amp;amp;&amp;amp; no_hands &amp;lt;= MAXHANDS)
			{
				play = TRUE;
				ask = FALSE;
			}
		}

		//Initializing player score
		for(i=0; i
		{
			player[i][SCORE] = 0;
		}

		//If the user entered 1 - 3 players play will equal to TRUE so the game starts
		if(play == TRUE)
		{
			//system(&quot;cls&quot;);
			printf(&quot;OK, let's play %d hands with %d players!!!nn&quot;, no_hands, no_players);

			while(playhand == TRUE &amp;amp;&amp;amp; cur_hand &amp;lt; no_hands)
			{
				//Give out current hand to each player
				for(i = 0; i &amp;lt; no_players; i++)
				{
					for(j = 2; j &amp;lt; 4; j++)
					{
						player[i][j] = get_card(cards, &amp;amp;deckpos);
					}
					player[i][NEWCARDPOS] = 4; //Next available empty slot in player array
					//must always be called after assigning empty slot
					calc_p_sum(player, i);

				}

				//Take out two cards for house
				for(j = 2; j &amp;lt; 4; j++)
				{
					house[j] = get_card(cards, &amp;amp;deckpos);
				}

				house[NEWCARDPOS] = 4;//Next available empty slot in house array
				//must always be called after assigning empty slot
				calc_h_sum(house);

				//Go through every player for hit or stay
				for(i = 0; i &amp;lt; no_players; i++)
				{
					nextplayer = FALSE;

					while(nextplayer == FALSE)
					{
						printf(&quot;Player %dn&quot;, i+1);
						show_p_hand(player,i);  //Will show player X hand

						if(player[i][SUM] == 21)  //If it's 21, player will get
						{					//blackjack and go on to next player
							printf(&quot;Player %d got BLACKJACK!!!nn&quot;, i+1);
							nextplayer = TRUE;
						}
						else
						{
							if(player[i][SUM] &amp;lt; 21)
							{
								//Must enter h or s to hit or stay, anything else
								//will go to next playesr
								printf(&quot;Player %d, hit or stay (h or s)?: &quot;, i+1);
								scanf(&quot;%s&quot;, hitstay);

								if(hitstay[0] == 'h' || hitstay[0] == 'H')
								{
									player[i][player[i][NEWCARDPOS]] = get_card(cards, &amp;amp;deckpos);
									player[i][NEWCARDPOS] += 1;
									calc_p_sum(player, i);

								}
								else
									nextplayer = TRUE;
							}
							else
							{  //If a player get more then 21 it's a Bust and go to
								//next player
								printf(&quot;BUST!!!!nn&quot;);
								nextplayer = TRUE;
							}
						}
					}
				}

				//Process house and display it's hand
				//if house has 17 or less, draw another card then calc sum
				while(house[SUM] &amp;lt; 17)
				{
					house[house[NEWCARDPOS]] = get_card(cards, &amp;amp;deckpos);
					house[NEWCARDPOS] += 1;
					calc_h_sum(house);
				}

				show_h_hand(house);  //Shows the house hand
				summary(house, player, no_players);  //Prints the current hand summary

				//Next hand?
				//If no, game quits
				if (cur_hand &amp;lt; no_hands-1)
				{
					printf(&quot;Next hand? (y,n): &quot;);
					scanf(&quot;%s&quot;, yesno);
					printf(&quot;n&quot;);

					if(yesno[0] == 'y' || yesno[0] == 'Y')
					{
						playhand = TRUE;
						shuf_cards(cards, &amp;amp;deckpos);
					}
					else
					{
						playhand = FALSE;
					}
				}
				else
					playhand = FALSE;

				cur_hand += 1;//Increase the hand counter
			}
		}
	}
	score(house, player, no_players);  //Score at the end of all hands

	printf(&quot;nnThank you for playing BlackJack.n&quot;);

	system(&quot;PAUSE&quot;);
}
/********************************  END main  *********************************/

/****************************  START init_cards  *****************************/
//Will fill the cards array (can change the number of decks to play
void init_cards(int cards[])
{
	int base_cards[13];
	int i,j;

	//Fill base cards array
	for(i = 0; i &amp;lt; 13; i++)
	{
		if (i &amp;lt;= 9 )
			base_cards[i] = i+1;
		else
			base_cards[i] = 10;
	}

	//Fill deck with NOCARDS
	i = 0;
	for(j = 0; j &amp;lt; NOCARDS; j++)
	{
		cards[j] = base_cards[i];
		if(i &amp;lt; 12)
			i += 1;
		else
			i = 0;
	}
}
/****************************  END init_cards  *******************************/

/****************************  START show_deck  ******************************/
//This will show the deck after the cards shuffled, can be used for cheating (kind of)
void show_deck(int cards[])
{
	int i;
	for(i = 0; i &amp;lt; NOCARDS; i++)
	{
		printf(&quot;%dn&quot;,cards[i]);
	}
}
/*****************************  END show_deck  *******************************/

/****************************  START shuf_cards  *****************************/
//Shuffles the cards 100 times in the cards array
//Basically it takes one card and swithes it with another in the array
void shuf_cards(int cards[], int *deckpos)
{
	int i, tempvalue, randpos, x;

	for(i = 0; i &amp;lt;= RANDSHUF; i++)
	{
		for(x = 0; x &amp;lt; NOCARDS; x++)
		{
			tempvalue = cards[x];
			randpos = random_int(x,NOCARDS-1);

			cards[x] = cards[randpos];
			cards[randpos] = tempvalue;
		}
	}
	*deckpos = 0; //Reset the card that we are on
	//Keeps track of the current array position so all cards that have been
	//taken out cannot be taken out anymore
}
/****************************  END shuf_cards  *******************************/

/****************************  START random_int  *****************************/
//Random function to pick random numbers to pick out of array and swap
int random_int(int min, int max)
{
	int rand_int, i;

	time_t seconds;
	time(&amp;amp;seconds);
	srand((unsigned int) seconds); //Seed generator

	for (i=0; i
		rand_int = rand() % (max - min + 1) + min;

	return rand_int;
}
/****************************  END random_int  *******************************/

/****************************  START get_card  *******************************/
//If user says hit, this function gets the card out of the shuffled array and increases
//the deckpos counter to the next slot in array
int get_card(int cards[], int *deckpos)
{
	int cardval;
	cardval = cards[*deckpos];
	*deckpos += 1;
	return cardval;
}
/*****************************  END get_hand  ********************************/

/***************************  START show_p_hand  *****************************/
//Shows player hand
void show_p_hand(int player[][PLAYERSIZE], int p)
{
	int i;
	for(i = 2; i &amp;lt; player[p][NEWCARDPOS]; i++)
		printf(&quot;%d &quot;, player[p][i]);

	printf(&quot; = %dn&quot;, player[p][SUM]);
}
/****************************  END show_p_hand  ******************************/

/***************************  START show_h_hand  *****************************/
//Shows house hand
void show_h_hand(int house[])
{
	int i;

	printf(&quot;Housen&quot;);
	for(i = 2; i &amp;lt; house[NEWCARDPOS]; i++)
		printf(&quot;%d &quot;, house[i]);

	printf(&quot; = %dnn&quot;, house[SUM]);
}
/****************************  END show_h_hand  ******************************/

/***************************  START calc_p_sum  ******************************/
//Calculates the player sum, determines if an Ace is 1 or 11
void calc_p_sum(int player[][PLAYERSIZE], int p)
{
	int i;
	int acepos = 1;
	player[p][SUM] = 0; //Reset sum to calc

	//Raw sum for player
	for(i = 2; i &amp;lt; player[p][NEWCARDPOS]; i++)
	{
		player[p][SUM] += player[p][i];
	}
	//Lets check for 1 or 11
	for(i = 2; i &amp;lt; player[p][NEWCARDPOS]; i++)
		if(player[p][i] == 1)
			acepos = i;

	if(acepos &amp;gt; 1) //We have either have a 1 or an 11
	{
		if((player[p][SUM] - 1 + 11) &amp;lt;= 21){
			player[p][acepos] = 11;
			//New sum for player
			player[p][SUM] = 0; //Reset sum to calc
			for(i = 2; i &amp;lt; player[p][NEWCARDPOS]; i++)
			{
				player[p][SUM] += player[p][i];
			}
		}
	}
}
/****************************  END calc_p_sum  *******************************/

/**************************  START calc_h_sum  *******************************/
//Calculates the house sum, determines if an Ace is 1 or 11
void calc_h_sum(int house[])
{
	int i;
	int acepos = 1;
	house[SUM] = 0; //Reset sum to calc

	//Raw sum for player
	for(i = 2; i &amp;lt; house[NEWCARDPOS]; i++)
	{
		house[SUM] += house[i];
	}
	//Lets check for 1 or 11
	for(i = 2; i &amp;lt; house[NEWCARDPOS]; i++)
		if(house[i] == 1)
			acepos = i;

	if(acepos &amp;gt; 1) //We have either a 1 or an 11
	{
		if((house[SUM] - 1 + 11) &amp;lt;= 21){
			house[acepos] = 11;
			//New sum for player
			house[SUM] = 0; //Reset sum to calc
			for(i = 2; i &amp;lt; house[NEWCARDPOS]; i++)
			{
				house[SUM] += house[i];
			}
		}
	}
}
/****************************  END calc_h_sum  *******************************/

/*****************************  START summary  *******************************/
//Goes through all scenarios to determine if it's a Bust, Blackjack, Win, Loss, Push/Tie
void summary(int house[], int player[][PLAYERSIZE], int no_players)
{
	int i;
	//Go through every player and check score for hand summary
	printf(&quot;Current Hand summary:n&quot;);
	for(i = 0; i &amp;lt; no_players; i++)
	{
		if(house[SUM] &amp;gt; 21 &amp;amp;&amp;amp; player[i][SUM] &amp;lt; 21)
		{
			player[i][SCORE] += 1;
			printf(&quot;Player %d - Winsn&quot;,i+1);
		}
		else if (player[i][SUM] == 21 &amp;amp;&amp;amp; house[SUM] != 21)
		{
			player[i][SCORE] += 2;
			printf(&quot;Player %d - BlackJack!n&quot;,i+1);
		}
		else if(player[i][SUM] &amp;lt; house[1] &amp;amp;&amp;amp; house[SUM] &amp;lt;= 21)
		{
			house[SCORE] += 1;
			printf(&quot;Player %d - Loosesn&quot;,i+1);
		}
		else if(player[i][SUM] &amp;gt; house[SUM] &amp;amp;&amp;amp; player[i][SUM] &amp;lt;= 21)
		{
			player[i][SCORE] += 1;
			printf(&quot;Player %d - Winsn&quot;,i+1);
		}
		else if(player[i][SUM] &amp;gt; 21)
		{
			printf(&quot;Player %d - Bustn&quot;,i+1);
		}
		else
		{
			printf(&quot;Player %d - Push/Tien&quot;,i+1);
		}
	}
}
/*****************************  END summary  *********************************/

/*****************************  START score  *********************************/
//Displays the final overall score for players and house
void score(int house[], int player[][PLAYERSIZE], int no_players)
{
	int i;
	//Go through every player for hit or stay
	printf(&quot;nnOverall game scores:n&quot;);
	for(i = 0; i &amp;lt; no_players; i++)
	{
		printf(&quot;Player %d score: %dn&quot;, i+1, player[i][SCORE]);
	}
	printf(&quot;House score: %dn&quot;, house[SCORE]);
}
/*****************************  END score  ************************************/
</pre>
<p><a title="Download the source file and compiled binary here" href="http://www.deciacco.com/blog/wp-content/uploads/blackjack_v1.zip">Download the source file and compiled binary here</a></p>
<p>If you want to compile it yourself, take a look at this page: <a href="http://www.mingw.org/">MinGW</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.deciacco.com/blog/c/blackjack-game-in-c-lang/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
