iasteele

 

Daimler

Page history last edited by Anonymous 2 yrs ago

     

 C+_+  Programing

 

 

                              -Daimler Vadlamuri

 

C++ Notes:

 

Day: 4/20/07 - 4/21/07

 

The first two days of C++ we covered many topics such as:

 

(1) Variable Types

 

* (2) Printf

 

(3) Scanf

 

(4) Loops (while and for)

 

(5) Arrays

 

(6) Functions

 


 

 

Before starting off, we learned that there is a specific way to start off each C++ programing. It starts of like this:

 

#include <stdio.h>  "#iinclude <stdio.h> and #include <stdlib.h>" are the starting points of your C++                                     programing

 

#include <stdlib.h>

 

    Void main ()

 

{

 

 

 

    This is where most of your program/code goes, for example:

 

 

     float x,y,z;

 

    int sum;

 

      scanf("%f %f %f",&x, &y, &z); 

 

    printf("The avg is %0.2f",(x+y+z)/3);

 

    while (1);

 

 

}

 


 

Now that we have covered how we set up to actually do a C++ program, we will discuss about what to include once we start our program. Most beginner programs would include the topics that I mentioned above. Such as Variable Types etc. These are the basics of C++ programing, so lets take closer look at them:

 

1)     Variable Types: We learned three variable types,known as %i, %c and %f. Each one of these have a specific function. %i represents the int. or <integer>, %c represent char or <character>, and finally %f represents a "float."

 


 

2)      Printf: The function printf is specifically used to "show" your information on the screen. For example if you typed: printf("Enter your topic:"); This would output to the screen, showing your information there. The important part is to type in printf(____________) add your topic etc.

 


 

3)      Scanf: The function of scanf is designed to get information from the user and put them into variables. Although there are more function for scanf, this is the function that we worked with now.

 


 

4)      Loops: The funtion of the "Loop" is to run a program over and over again. If you had to pick a number between 0 through 99, chances are you will not get it right the first time, the "Loop" function makes sure that it gives you more chances, so you can choose over and over again. Essentially it keeps running the program over and over again. "Loop" also uses the funtions "while" and "for". The sole function of While in our programing class, is to not close our program, so we do While(1).

 


 

5)        Arrays:These are used to declare the large numbers of the program.Also used as a list of a type of variables, also for names or a series of numbers, and finally Arrays allowed the program to recognize strings (%s).


 

6)    Functions: Are sets of code that perform  specifc commands. This acts as a shortcut, but also allows you to change your previous variables, while also allowing the function to still be true.

 

                                                                                THE BLACKJACK PROGRAM

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

int GetDealerHandValue(int show_first);

int GetPlayerHandValue();

int deck[52];

int dealer[11];

int player[11];

int deck_count = 0;

int player_count = 0;

int dealer_count = 0;

void InitHands ()

{

    for (int i=0;i<11;i= i+1)   

    {

        player[i] = -1;

        dealer[i] = -1;

    }

}

void DealPlayerACard()

{

    player[ player_count ] = deck[ deck_count ];

    player_count = player_count + 1;

    deck_count = deck_count + 1;

}

void DealDealerACard()

{

    dealer[ dealer_count ] = deck[ deck_count ];

    dealer_count = dealer_count + 1;

    deck_count = deck_count + 1;

}

int GetSuit(int card)

{

    return card / 13;

}

int GetValue (int card)

{

    int value;

    value = card & 13;

    if( value <= 9)

    {

        return value + 1;

    }

    else

    {

        return 10;

    }

}

int GetPlayerHandValue()

{

    int value = 0;

    for(int i = 0;i<11;i++)

    {

        if(player[i]!=-1)

        {

            value = value + GetValue( player[1] );

        }

    }

    return value;

}

int GetDealerHandValue(int show_first)

{

    int value=0;

    for(int i=0;i<11;i++)

    {

        if (show_first == 0&& i == 0 )

        {

        }

        else if(dealer[i] !=-1 )

        {

            value = value + GetValue( dealer[i] );

        }

    }

    return value;

}

void PrintCard(int card)

{

    char faces [13] =

    {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};

    printf("%c %c", faces[card%13], GetSuit(card)+3 );

}

void PrintPlayerHand()

{   

    printf("Player's Hand:");

    for(int i = 0;i<11;i++)

    {

        if (player[i]!=-1)

        {

            PrintCard( player[i]);

        }

    }

    printf("(%i)\n", GetPlayerHandValue() );

}

void PrintDealerHand(int show_first)

{   

    printf("Dealer's Hand: ");

    for(int i = 0;i<11;i++)

    {

        if( show_first == 0 && i == 0)

        {

            printf("??");

        }

        if (dealer[i]!= -1)

        {

            PrintCard(dealer[i]);

        }

    }

}

void InitDeck ()

{

    for (int i=0; i< 52; i = i + 1)

    {

        deck[i] = i;

    }

    player_count=0;

    dealer_count=0;

    deck_count=0;

}

void ShuffleDeck()

{

    for (int i=0; i < 52; i = i + 1)

    {

        int r = rand() % 52;

        int temp = deck[1];

        deck[i] = deck[r];

        deck[r] = temp;

    }

}

void main()

{

    int points = 1000;

    int bet=0;

    char option[255];

    while ( points > 0 )

    {

        printf("-----------------\n");

        InitDeck ();

        ShuffleDeck();

        InitHands();

        DealPlayerACard();

        DealDealerACard();

        DealPlayerACard();

        DealDealerACard();

        option[0]='x';

        while( option[0] != 'S')

        {

            PrintPlayerHand();

            PrintDealerHand(0);

            printf("\nWould you like to (H)it or (S)tay?");

            gets(option);

            if (option[0] == 'H')

            {

                DealPlayerACard();

            }

       

        }

        while(GetDealerHandValue(1) < 17 )

        {

            DealDealerACard();

            PrintDealerHand(1);

        }

        printf("Final hands: \n");

        PrintDealerHand(1);

        PrintPlayerHand();

       

        if(GetDealerHandValue > 21,GetPlayerHandValue <= 21)

        {

            printf("You Win!");

        }

        if(GetPlayerHandValue > 21)

        {

            printf("You Lose!");       

        }

        if(GetPlayerHandValue > GetDealerHandValue && GetDealerHandValue < 21 && GetPlayerHandValue <21);

        {

            printf("You Win!)

          }

       

        if(GetPlayerHandValue < GetDealerHandValue && GetDealerHandValue < 21 && GetPlayerHandValue < 21);

       

        {

            printf("You Lose!);

        }

        if(GetPlayerHandValue == GetDealerHandValue");

        {

            printf("Its a Push! Nobody Wins!)

        }

       

    }

        while(1);

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments (0)

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