DAY FOUR:
Today we finished up on our Black Jack Game. We gave the player a score and allowed him/her to place a bet or a wager. We also made sure that Ace could equal 11 or 1 depending on the hand number of the player.
We also let the computer hide one of the dealer's cards.
Here's the final coding for the Black Jack Game:
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int
deck[52];
int
hand[2][11];
void
{
{
deck[count]=count;
}
}
InitDeck()int count;for (count=0;count<52;count=count+1)
void
{
printf("A%c ",3+suit);
printf("T%c ",3+suit);
printf("J%c ",3+suit);
printf("Q%c ",3+suit);
printf("K%c ",3+suit);
printf("%i%c ",value,3+suit);
}
PrintCard(int card_number)int suit = deck[card_number]/13;int value = deck[card_number]%13 +1;if (value == 1)elseif (value == 10)elseif (value == 11)elseif (value == 12)elseif (value == 13)else
int
{
GetValue(int card_number)//deck[card_index];
{
value=10;
}
}
int value = deck[card_number]%13 +1;if(value > 10)return value;
int
{
{
{
{
ace = ace + 1;
}
{
}
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)if( !show_dealer && hand_number == 0 && c == 0)else
{
hand_value = hand_value + GetValue(hand[hand_number] [c] );
}
}
}
{
hand_value = hand_value + 10;
}
}
if ( ace> 0 && hand_value + 10 <= 21)return hand_value;
void
{
hand_value=0;
{
{
{
printf(" ?? ");
}
{
PrintCard( hand[hand_number][c] );
hand_value = hand_value + GetValue (hand[hand_number][c]);
}
}
}
printf("(%i)\n", GetHandValue(hand_number,show_dealer));
}
PrintHand(int hand_number, int show_dealer = 0)int c;int hand_value;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 )else
int
{
range = max - min +1;
rval = rand() % range;
rval = rval + min;
}
random(int min, int max)int range;int rval;return rval;
void
{
{
hand[hand_number][card] = -1;
}
}
InitHand(int hand_number)int card;for(card = 0 ; card < 11 ; card = card + 1)
int
{
srand( (
player_count = 0;
dealer_count = 2;
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("\nPlayer:\n");
PrintHand(1);
PlayAHand()int player_count;char option[64];int dealer_count;unsignedint) time(0));///while(hand[1][0]== hand[1][1])
//{
// hand[1][1] = random (0,51);
//}
//PrintHand(1);
printf("\n\n Would you like to (h)it or (s)tand?\n\n");
//scanf("%c%c",&s,&throw_away);
gets(option);
{
GetHandValue(1);
hand[1][player_count]= random(0,51);
player_count = player_count + 1;
printf("\nDealer:\n");
PrintHand(0);
printf("\nPlayer:\n");
PrintHand(1);
{
option[0]='s';
printf("\nSorry you loooooooose... :( \nyour hand is higher than 21.");
}
while (option[0] == 'h')if (GetHandValue(1)> 21)return 0;else
{
printf("\n\n Would you like to (h)it or (s)tand?\n\n");
gets(option);
}
}
{
printf("Dealer: \n");
hand[0][dealer_count]= random(0,51);
dealer_count = dealer_count + 1;
while (GetHandValue(0,1) <= 17)//printf("Dealer: \n");
PrintHand(0,1);
}
{
option[0]='s';
printf("\nYou win!!! - the dealer's hand is higher than 21...");
printf("\nFinal Hands:");
printf("\nDealer:\n");
PrintHand(0,1);
printf("\n\nPlayer:\n");
PrintHand(1);
}
{
printf("\n\nYOU WINNNN!!!!!!!!!!!");
printf("\nFinal Hands:");
printf("\nDealer:\n");
PrintHand(0,1);
printf("\n\nPlayer:\n");
PrintHand(1);
}
{
printf("\n\nPush... nobody wins :|");
printf("\nFinal Hands:");
printf("\nDealer:\n");
PrintHand(0,1);
printf("\n\nPlayer:\n");
PrintHand(1);
}
}
if(GetHandValue(0)>21 && GetHandValue(1) <= 21)return 1;if (GetHandValue(1) <= 21 && GetHandValue(1)>GetHandValue(0))return 1;if (GetHandValue(1) == 21 && GetHandValue(0) == 21 || GetHandValue(1)== GetHandValue(0))return -1;while(1);
void
{
InitDeck();
srand((
card1 = random(0,51);
card2 = random(0,51);
card3 = random(0,51);
PrintCard(card1);
printf("\n+\n");
PrintCard(card2);
printf("\n+\n");
PrintCard(card3);
a=GetValue(card1)+GetValue(card2)+GetValue(card3);
printf("\n=%i",a);
{
card2 = random(0,51);
}
{
card3= random(0,51);
}
main2()int i;int card1;int card2;int card3;int a;unsignedint) time (0));while(card1 == card2)while(card1 == card3|| card2 == card3)//while(card1 == card3 || card2 == card3);
//this is so that it doesn't print the same thing twice
{
printf("deck[%2i] = ",deck[i]);
PrintCard(i);
printf("%i",deck[i]);
printf("(%i)\n",GetValue(i));
}
}
for (i=0;i<0;i++)while (1);
Then, we were recquired to make a program by ourselves, that asked the player for his/her name, asked them for two test grades and 5 quiz/hw grades and then calculated the final grade.
We also made the program say whether the grade was an A, B, or C etc.
In order to do this, I used seperate functions for the seperate math (averages etc.) , gets, scanf, printf, and if- statements.
Here's my coding:
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
float
{
result = (x+y)/2;
}
test_average(float x, float y)float result;return result;
float
{
result = (a+b+c+d+e)/5;
}
quiz_average(float a, float b,float c,float d,float e)float result;return result;
float
{
result = (test_average + quiz_average)/2;
}
grade(float test_average,float quiz_average)float result;return result;
void
{
printf("What's your name?\n");
gets(name);
printf("Hello!\n");
printf("please enter two of your test scores (in percentages)\n");
scanf("%f %f%c",&x,&y,&throw_away);
printf("Your test average is %f",test_average(x,y));
printf("\nNow enter 5 quiz scores\n");
scanf("%f%f%f%f%f%c",&a,&b,&c,&d,&e,&throw_away);
printf("Your quiz average is %f",quiz_average(a,d,c,d,e));
printf("\n\nAccording to our calculations your final grade is %f",grade(test_average(x,y),quiz_average(a,d,c,d,e)));
g = grade(test_average(x,y),quiz_average(a,d,c,d,e));
{
printf("\nYou have an A+! \nGood Job :D");
}
{
printf("\nYou have an A\n nice!");
}
{
printf("\n That would be a B+! \n...so close to an A!");
}
{
printf("\nYou have a B");
}
{
printf("\nThat's a C... Study HARDER! XD");
}
{
printf("A C+! You almost made it to a B... almost...");
}
{
printf("\n WHAT DID YOU DO IN CLASS???? Sleep?");
}
}
This last program we made was very challenging, because we had to do it (pretty much) by ourselves and use the things we learned. I liked it though =D
main()char name[64];char throw_away;float x;float y;float a;float b;float c;float d;float e;float g;if (g>= 100)if (g>=90)if (g<90 && g>=85)if (g>=80 && g<=85)if (g>=70 && g<=75)if (g>=75 && g<80)if (g<70)while(1);
--------------------------------------------------------------------------------
DAY THREE:
Today we worked on our Black Jack program. Last time we made the program show the deck, but the values were incorrect, so today we fixed that.
We also created two players, the player who plays the game and the dealer, who is played by the computer.
We used a lot of 'if - statements' to outline the rules of the game.
We also let the program ask the player whether he wants to hit or stand. Once the player decides to stand the computer automatically gives the dealer more cards if his hand value is lower or equal to 17. Then, the game decides who wins if it's not decided until then.
Another thing I learned today was how to say 'or' in c: pressing Shift + \ twice. It makes this sign: ||, which means 'or'.
Here's the code of the game from today:
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int
deck[52];
int
hand[2][11];
void
{
{
deck[count]=count;
}
}
InitDeck()int count;for (count=0;count<52;count=count+1)
void
{
printf("A%c ",3+suit);
printf("T%c ",3+suit);
printf("J%c ",3+suit);
printf("Q%c ",3+suit);
printf("K%c ",3+suit);
printf("%i%c ",value,3+suit);
}
PrintCard(int card_number)int suit = deck[card_number]/13;int value = deck[card_number]%13 +1;if (value == 1)elseif (value == 10)elseif (value == 11)elseif (value == 12)elseif (value == 13)else
int
{
GetValue(int card_number)//deck[card_index];
{
value=10;
}
}
int value = deck[card_number]%13 +1;if(value > 10)return value;
int
{
{
{
hand_value = hand_value + GetValue(hand[hand_number] [c] );
}
}
}
GetHandValue(int hand_number)int c;int hand_value = 0;for(c=0;c<11;c++)if( hand[hand_number] [c] >= 0 && hand[hand_number] [c] <=51)return hand_value;
void
{
hand_value=0;
{
{
PrintCard( hand[hand_number][c] );
hand_value = hand_value + GetValue (hand[hand_number][c]);
}
}
printf("%i",hand_value);
}
PrintHand(int hand_number)int c;int hand_value;for(c=0;c<11;c++)if (hand[hand_number][c]>= 0 && hand[hand_number][c] <= 51)
int
{
range = max - min +1;
rval = rand() % range;
rval = rval + min;
}
{
{
hand[hand_number][card] = -1;
}
}
random(int min, int max)int range;int rval;return rval;void InitHand(int hand_number)int card;for(card = 0 ; card < 11 ; card = card + 1)
void
{
main()
// char throw_away;
//char s;
srand( (
player_count = 0;
dealer_count = 2;
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("\nPlayer:\n");
PrintHand(1);
int player_count;char option[64];int dealer_count;unsignedint) time(0));///while(hand[1][0]== hand[1][1])
//{
// hand[1][1] = random (0,51);
//}
//PrintHand(1);
printf("\n\n Would you like to (h)it or (s)tand?\n\n");
//scanf("%c%c",&s,&throw_away);
gets(option);
{
GetHandValue(1);
hand[1][player_count]= random(0,51);
player_count = player_count + 1;
printf("\nDealer:\n");
PrintHand(0);
printf("\nPlayer:\n");
PrintHand(1);
{
option[0]='s';
printf("\nSorry you loose... :( \nyour hand is higher than 21.");
}
while (option[0] == 'h')if (GetHandValue(1)> 21)else
{
printf("\n\n Would you like to (h)it or (s)tand?\n\n");
gets(option);
}
{
hand[0][dealer_count]= random(0,51);
dealer_count = dealer_count ++;
if(GetHandValue(0) <= 17)//printf("Dealer: \n");
//PrintHand(0);
}
{
option[0]='s';
printf("\nYou win!!! - the dealer's hand is higher than 21...");
}
{
printf("\n\nYOU WINNNN!!!!!!!!!!!");
}
{
printf("\n\nPush... nobody wins :|");
}
if(GetHandValue(0)>21 && GetHandValue(1) <= 21)if (GetHandValue(1) <= 21 && GetHandValue(1)>GetHandValue(0))if (GetHandValue(1) == 21 && GetHandValue(0) == 21)//now the dealer gets to play
}
}
while(1);
void
{
InitDeck();
srand((
card1 = random(0,51);
card2 = random(0,51);
card3 = random(0,51);
PrintCard(card1);
printf("\n+\n");
PrintCard(card2);
printf("\n+\n");
PrintCard(card3);
a=GetValue(card1)+GetValue(card2)+GetValue(card3);
printf("\n=%i",a);
{
card2 = random(0,51);
}
{
card3= random(0,51);
}
main2()int i;int card1;int card2;int card3;int a;unsignedint) time (0));while(card1 == card2)while(card1 == card3|| card2 == card3)//while(card1 == card3 || card2 == card3);
//this is so that it doesn't print the same thing twice
{
printf("deck[%2i] = ",deck[i]);
PrintCard(i);
printf("%i",deck[i]);
printf("(%i)\n",GetValue(i));
}
}
for (i=0;i<0;i++)while (1);
-------------------------------------------------------------------------------------
DAY TWO:
Create a new project
Create a new source file [right click; add new item; etc.]
Instructions: Create a program that asks the user for a number between 10 and 20
Print to the screen a count from 1 to that number
#include <stdio.h>
#include <stdlib.h>
void main ()
{
int number;
char throw_away;
int choice;
choice=0;
printf("chose a number between 10 and 20 \n");
scanf("%i,%c",&number, &throw_away);
while (number != choice)
{
printf("%i ", choice+1);
choice=choice+1;
}
while(1);
}
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.
Step by step:
- First ask the user for a number between 10 and 20 using scanf and printf.
- Then create a while that specifies what to do after the user has typed in the number. In this case, count up to that number
Debugging
The Debugger allows you to go through the program line by line in order to see if your program is functioning correctly.
You can start at a specific line by going to the beginning of your line and pressing F9 and debugging. When you press F10 the program automatically executes that line.
We already covered the while loop, but there is also another loop: the for loop
It has 3 sections
The initialization, the condition, the increment
for (choice=0; choice<number; count = count+1)
{
printf("%i", count+1);
}
Put this into every program you write:
#include <stdio.h>
#include <stdlib.h>
void main ()
{
}
Create a new program
Ask the user to pick a number
Then ask them what they want to do with them.(+ - * /)
If you’re comparing a single character put single quotation marks
#include <stdio.h>
#include <stdlib.h>
void main ()
{
int choice;
int choice_2;
char throw_away;
char function;
printf("Please enter two numbers \n");
scanf("%i%i%c", &choice, &choice_2, &throw_away);
printf("what function would you like to use ? (+ - / *)\n");
scanf("%c%c", &function, &throw_away);
if (function == '+')
{
printf("%i%c%i=%i", choice, function, choice_2, choice+choice_2);
}
if (function == '-')
{
printf("%i%c%i=%i ",choice, function, choice_2, choice-choice_2);
}
if (function == '*')
{
printf("%i%c%i=%i", choice, function, choice_2, choice*choice_2);
}
if (function == '/')
{
printf("%i%c%i=%i", choice, function, choice_2, choice/choice_2);
}
while (1);
}
In order to say ‘and’ in c type &&
Create a new function
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
}
void main ()
{
int choice;
int choice_2;
char throw_away;
char function;
//printf("Please enter two numbers \n");
//scanf("%i%i%c", &choice, &choice_2, &throw_away);
//printf("what function would you like to use ? (+ - / *)\n");
//scanf("%c%c", &function, &throw_away);
printf("Please enter an equation: \n");
scanf ("%i%c%i%c", &choice,&function, &choice_2, &throw_away);
if (function == '+' )
{
printf("%i%c%i=%i", choice, function, choice_2, choice+choice_2);
}
if (function == '-' )
{
printf("%i%c%i=%i ",choice, function, choice_2, choice-choice_2);
}
if (function == '*' )
{
printf("%i%c%i=%i", choice, function, choice_2, choice*choice_2);
}
if (function == '/' )
{
if (choice_2 != 0)
{
printf("%i%c%i=%i", choice, function, choice_2, choice/choice_2);
}
else
{
printf ("sorry, you can't divide by 0 \n\n");
}
}
while (1);
}
The final equation
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
int result;
result = x+y;
return result;
}
int subt(int x, int y)
{
int result;
result = x-y;
return result;
}
int mult(int x, int y)
{
int result;
result = x*y;
return result;
}
int divd(int x, int y)
{
int result;
result = x/y;
return result;
}
void main ()
{
int choice;
int choice_2;
char throw_away;
char function;
int a;
int b;
//printf("Please enter two numbers \n");
//scanf("%i%i%c", &choice, &choice_2, &throw_away);
//printf("what function would you like to use ? (+ - / *)\n");
//scanf("%c%c", &function, &throw_away);
printf("Please enter an equation: \n");
scanf ("%i%c%i%c", &choice,&function, &choice_2, &throw_away);
a=choice;
b=choice_2;
if (function == '+' )
{
printf("=%i", add (a, b));
}
if (function == '-' )
{
printf("=%i ", subt (a,b));
}
if (function == '*' )
{
printf("=%i", mult(a, b));
}
if (function == '/' )
{
if (choice_2 != 0)
{
printf("%=%i", divd(a,b));
}
else
{
printf ("sorry, you can't divide by 0 \n\n");
}
}
while (1);
}
Change your integers to floats, so that they can have decimal points.
You can also tell printf how many spaces it should take up
Example %5.2f
In order to create an array of variables put the number in brackets after the name of the variable.
Replace scanf with gets [get string] (initial);
%s = string
Integers aren’t strings
Atoi = array to integer
Strlen returns the length of the string length
Create a new program
------------------------------------------------------
DAY ONE:
Comments (0)
You don't have permission to comment on this page.