I learned alot of things today so let me list them.While(0) = never do this loop.While(1) is an infinite loop.Has to have a main function.Void: no type.F stands for function.\n stands for new line.t = tab.If = always asks a question. = anything after these two slashes is considered a comment and not used in the program.Int = integer.
scanf works like this
you need a
int x
and a
char throw_away
and you set up the scanf like this
scanf("%i%c",&x,&throw_away);
the %i means that we are asking for one interger
the %c is there becuase scanf also includes the enter button so we have a charater named throw_away
the printf function works like this
you need nothing else
and you set up the printf like this
printf("whatever you want");
however you can make the printf say the value of a interger like this
you need a int
int x=0;
and you set up like this
printf("%i",x);
and now it says the value of x.
Things to remember:
Always put a while(1) at the end of your code, so that the program doesn’t exit immediately when you press enter.
The Debugger allows you to go through the program by line in order to see if your program is functioning correctly.
You can start at a specific line by going to the of your line and pessing F9 and debugging. When you press F9 the program automatically that line.
We already covered the while loop, but there also another loop: the for
It has 3 sections
The initialization, the condition, the increment
in order to stop a while loop all you have to do is put in "break;" without the quotation marks.
The complete coding for blackjack is:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int deck[52];
int hand[2][11];
void InitDeck()
{
int count;
for(count=0; count<52; count=count+1)
{
deck[count] = count;
}
}
void InitHand(int hand_number)
{
int card;
for(card=0; card < 11; card = card + 1)
{
hand[ hand_number ][ card ] = -1;
}
}
void PrintCard(int card_number)
{
int suit = deck[card_number]/13;
int value = deck[card_number]%13 +1;
if( value == 1 )
printf("A%c ",3+suit);
else if( value == 10 )
printf("T%c ",3+suit);
else if( value == 11 )
printf("J%c ",3+suit);
else if( value == 12 )
printf("Q%c ",3+suit);
else if( value == 13 )
printf("K%c ",3+suit);
else
printf("%i%c ",value,3+suit);
}
int GetValue(int card_number)
{
int value = deck[card_number]%13 +1;
if( value > 10 )
{
value = 10;
}
return value;
}
int GetHandValue(int hand_number, int show_dealer = 0)
{
int c;
int ace = 0;
int hand_value = 0;
for(c=0;c<11;c++)
{
if( hand[hand_number][c] >= 0
&& hand[hand_number][c] <=51 )
{
if( GetValue( hand[hand_number][c] ) == 1)
{
ace = ace + 1;
}
if( !show_dealer && hand_number == 0 && c == 0)
{
}
else
{
hand_value =
hand_value
+ GetValue( hand[hand_number][c] );
}
}
}
if( ace > 0 && hand_value + 10 <= 21 )
{
hand_value = hand_value + 10;
}
return hand_value;
}
void PrintHand(int hand_number, int show_dealer = 0)
{
int c;
int hand_value;
hand_value = 0;
for(c=0;c<11;c++)
{
if( hand[hand_number][c] >= 0 && hand[hand_number][c] <=51 )
{
if( !show_dealer && hand_number == 0 && c == 0)
{
printf("?? ");
}
else
{
PrintCard( hand[hand_number][c] );
hand_value = hand_value + GetValue( hand[hand_number][c] );
}
}
}
printf(" (%i)\n",GetHandValue(hand_number));
}
int random(int min, int max)
{
int range;
int rval;
range = max - min +1;
rval = rand() % range;
rval = rval + min;
return rval;
}
int PlayAHand()
{
char option[64];
int player_count=0;
int dealer_count=0;
srand( (unsigned int) time(0));
InitDeck();
InitHand(0);
InitHand(1);
hand[1][0] = random(0,51);
player_count = player_count + 1;
hand[0][0] = random(0,51);
hand[1][1] = random(0,51);
player_count = player_count + 1;
hand[0][1] = random(0,51);
printf("Dealer:\n");
PrintHand(0);
printf("\n");
printf("Player:\n");
PrintHand(1);
printf("Would you like to (H)it or (S)tand: ");
gets(option);
while( option[0] == 'H' || option[0] == 'h')
{
hand[1][player_count] = random(0,51);
player_count = player_count + 1;
PrintHand(1);
if( GetHandValue(1) < 21 )
{
printf("Would you like to (H)it or (S)tand: ");
gets(option);
printf("\n");
}
else
{
option[0]='S';
}
}
// dealer gets to play
while( GetHandValue(0) < 17 )
{
hand[0][dealer_count]=random(0,51);
dealer_count++;
printf("Dealer: \n");
PrintHand(0,1);
}
if( GetHandValue(1) > 21)
{
printf("Dealer wins,wow you suck.\n\n");
return 0;
}
else if( GetHandValue(0) > 21)
{
printf("holy crap you won.\n\n");
return 1;
}
else if( GetHandValue(1) == GetHandValue(0) )
{
printf("No one wins so everybody sucks :D\n\n");
return -1;
}
else if( GetHandValue(1) > GetHandValue(0) )
{
printf("holy crap you won.\n\n");
return 1;
}
else
{
printf("Dealer wins u suck at life.\n\n");
return 0;
}
}
void main()
{
int wager=0;
int chips=100;
int winner;
char throw_away;
while(1)
{
// ask for a wager
// play the hand
// adjust the score
if(chips > 1)
{
printf("You have this many chips ");
printf("%i\n",chips);
printf("How much do you wager\n");
scanf("%i%c",&wager,&throw_away);
printf("\n");
if(wager > chips)
{
printf("wow stop sucking at life\n\n");
break;
}
winner=PlayAHand();
}
if(chips < 1)
{
printf("sorry but u suck at blackjack and at life and so cannot wager anymore :D\n\n");
break;
}
if( winner == 1 )
{
chips = chips + wager;
}
if( winner == 0)
{
chips = chips - wager;
}
if(winner == -1)
{
chips = chips;
}
}
while(1);
}
remeber never actually gamble.
Comments (0)
You don't have permission to comment on this page.