iasteele

 

Lies

Page history last edited by Anonymous 2 yrs ago
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:
  1. First ask the user for a number between 10 and 20 using scanf and printf.
  2. 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:
 
I created these notes during class  it's a little unorganized
 
 
#include <stdio.h>
#include <stdlib.h>
 
void main()
{
      printf("Hello Kitty!!!!\n") ;
 
      while (1) ;
}
 
Include standard input output
Include standard library
 
Has to have a main function
 
Void: no type
 
Without the while it goes away immediately
 
Printf
F stands for function
Anything between the “s is printed exactly as it’s typed
\n stands for new line
It’s like <br> in html
You can put in as many \ns as you want
A double backslash = one backslash on the screen
\t = tab
Print f can also print variables
 
Int = integer
 
Put a ; at the end of each particular statement
 
After you’ve named the integer assign the variable by doing ; after the name and then doing [name of the integer] = [number]
 
In order to make it show random numbers do [name]= rand()
 
In order to print a variable in the brackets of print f put % and then identify what kind of variable you want it to print so in this case %i
 
The first parameter is the writing printf(“[first parameter]”,my_first_var)
 
Always keep the order of the parameters
%i indicates where the number is going to show up, not the position of the second parameter
EX: printf(“%i is the value of my_first_var”,my_first_var)
This would print 7 is the value of my_first_var if you indicate that my_first_var = 7
The second parameter indicates what %i is so of course you have to make an integer first and then name it and indicate what number it is
 
If you have two %is indicate what they stand for twice. C uses it in the order you put in
 
EX: printf(“%i blablabla %i”,my_first_var,my_first_var)
This would print 7 blablabla 7
 
How to ask something from the user:
[worst function ever written] lol
Scanf
Scanf(“%i”,&my_first_var)
First you indicate what you’re asking for (an integer in this case)
 
// = anything after these two slashes is considered a comment and not used in the program
 
Always declare all your variables on the top of your function in C
 
If you get errors [many of them] look for the first one and ignore the ones after because one error usually brings more errors
 
If = always asks a question
true or false
 
      if( guess < target)
      {
            printf("sorry, your guess is too low")
      }
 
      if( guess > target)
      {
            printf("sorry, your guess is too high")
      }
 
In this case you say that if the guess is lower than the target >sorry, your guess is too low< pops up and if guess is higher than the target >sorry your guess is too high<
 
To ask is something equal to another do two equal signs
 
While() = whatever in the ()s is is true keep going
 
Char = character
 
%c
 
Scanf reads whatever key you type including enter
 
While(1) is an infinite loop
While(0) = never do this loop
 
Loops have bodies
 
Include ALL statements
 
The symbol for not = is a !=
Time.h gives us different time functions
 
You can change the name of the main function, but then the program will not have a main function, which a c program ALWAYS needs
Once you change the name of main you must create a new main
 
The new main is going to be empty
In order for the function to work call the function in the new main function
 
As many times as you call the function in main as many times you will have to play
 
In order to control this you need to put while
While(1)= the user has to play an infinite amt. Of time
 
 
Create two new characters: throw_away (you need to do this again) and choice (the answer to the question you’re asking, which in this case would be either yes or no represented by y and n [you can only have 1 character answers otherwise the system gets confused])
 
Characters are identified in c with single quotation marks
 
In the body of while, under the called function (in this case pick_a_number) ask the question and make sure to identify in the question that the user must put in y or n not yes or no
Then put in your scanf which is going to be two characters:choice and throw_away (“%c%c”,&choice,&throw_away)
 
Instead of 1 in the while parenthesis put choice == ‘y’ this makes the game go on if the user types in y in order for this to work identify theat the char choice = ‘y’
 
void main()
      {
            char throw_away;
            char choice='y';
            while(choice == 'y')
      {
            pick_a_number();
            printf("Do you want to play again? (y/n)");
            scanf("%c%c",&choice,&throw_away);
      }
      //while(1);
 
      }
In order to tell the user how many tries it took him/her to guess the right number add a new integer called score and make it equal to zero
Then, under your scanf type score=score+1. this way, the program will count how many tries the user needed.
 
Print the score inside the if (guess == target) {brackets
printf("\n\n You got the right number in %i guesses!!!!!",score)
 
Next you want to let the user decide what the maximum value is
 
Make a new integer called max_value create this integer under your libraries so that it will be able to be used by all functions you enter in your text.
 
In your main function ask the user to enter the maximum value he/she wants to guess. Add in the scan f
printf("What is the maximum number you want to guess?");
            scanf ("%i%c",&max_value,&throw_away);
 
under srand change your code so that it looks like this
 
srand((unsigned int)time(0));
      target = rand ()%max_value;
      target = rand ()%max_value;
 
 
 
create a new integer called temp
identify that temp=max_value in case you want to play again
 
 
      temp=max_value;
      while (temp > 1)
      {
            temp = temp/2;
            par = par +1;    
      }
You have to substitute temp for max_value in this part of the code
 

Comments (0)

You don't have permission to comment on this page.