Tuesday, February 8, 2011

C++: Game of War!

Wrote this ages ago.  Not sure if it works, but it took me forever.  I'm going to periodically post some of this as I go through some of my old archives of coding school-work:

/*      Rob Marsh Software     September 1997    re-write #348,898
   
    The program follows a generic path.  Within the main() division it
    follows the usual path:

        init()
        process()
        closing()

    But it also includes several other smaller calls to other functions,
    such as the card() division, used for generation of the cards,
    score(), war() {for when the players reach a draw} as well as
    other functions adding definition to the program.

    Most of the main action occurs within the process() division.  And
    it is through the closing() section that the porgram actually terminates
    out of the main() division, and finishes the application.  */

#include <stdio.h>
#include <stdlib.h>

    /* Variable Definitions */

    int comp_score;        /* the computers accumulated score.*/
    int ichart;
    int icard;
    int human_score;      /* the players score throughout the game*/
    int rounds = 1001;
    int newchar;
    int human_draw;
    int comp_draw;
    char win1;
    int war_round = 0;

    /* Below are shapes that I will use to "draw" the cards */
    #define border1 201;
    #define border2 205;
    #define border3 187; 
    #define border4 186;
    #define border5 200;
    #define border6 188; /* these top numbers are the details the card */
    #define inbord1 218;
    #define inbord2 196;
    #define inbord3 191;
    #define inbord4 179;
    #define inbord5 192;
    #define inbord6 217; /* these are the interior shapes of the card */


    /* Procedure definitions */
    void initialization(void);
    void procedure(void);
    void closing(void);
    void suit(void);
    int card(int);
    int score(int);
    void war(void);
    void aboutme(void);
    void instructions(void);
    void quit(void);
    void exit(void);

void main(void)
    {

    /* These 3 are the main 3 sections of the program.*/
    initialization();
    procedure();
    closing();



    /* These are the other procedures to be used by the program.*/
    card();
    suit();
    score();
    war();
    instructions();
    aboutme();
    quit();
    exit();

    }
void initialization (void)
    {

    /* Initialization will be the start menu, but it will also be
    looped back to by some other areas of the program.  Some menu
    options will retunr here again, for another run through.  */

    printf("\n\n\n\n\n\n\n\n\n");
        printf("\tWelcome to the game of WAR!  version 1.0");
            printf("\n\n\tI was designed by Rob Marsh during the fall of 1997.");
                printf("\n\n\tPlease select an option:");
                    printf("\t\n1) Play War.");
                printf("\t\n2) Read the game rules.");
            printf("\t\n3) About Rob Marsh Software.");
        printf("\t\n4) Quit to DOS.");
    printf("\n\n\n\n\n\n\n");
    ichart = \0;
    scanf("%d", &ichart);
        if (ichart == 2)
            {
            instructions();
            }
        else if (ichart == 3)
            {
            aboutme();
            }

        else if (ichart == 1)
            {
            printf("\n\n\n\n\n\n\n\nLet's play, shall we?");
            quit();
            }
        else
            {
            exit();
            }

    }

void procedure(void)
    {
   
    /* This is the main area of the game play.  Note that it is
    punctuated with calls to the quit() function.  I added alot of
    these to give the player the option of quitting at anytime,
    since the game of WAR can get unbearably dull after a very short
    amount of time...very similiar to rosie ODonnel.
    Random numbers are generated for the values of the cards, but
    regarding score, the actual COUNT of the cards is what will
    ultimately be tallied in the final score.
    */

    int counter;
        int hands;
        printf("\n\n\n\n\nHow many hands would you like to play?");
        scanf("%d", &hands);

        int itracker = 1;
        itracker++;
        while (itracker < hands)
            {
       
            printf("\n\n\n\nI drew a...");
            int card( human_draw );

            printf("\nYou drew a...");
            int card( comp_draw);

            /* The two statements above produce the effect of 2 random
                cards appearing on the screen.  The commands below will
                analyze which players card wins the round, or if this get's
                carried over into the WAR function.                      */

            if ( comp_draw > human_draw )
                {
                printf("\nI win, human!!!");
                int score(comp_score);
                comp_draw = (\0);
                quit();
                }
            else if ( irandom > irandom2)
                {
                printf("\nYou win, human!!!");
                int score(human_score);
                human_draw = (\0);
                quit();
                }
            else
                {
                war();
                }
            }
            quit();
    }
void closing (void)
    {
    printf("\n\n\n\n\nThe final score.....");
    if (human_score > comp_score)
        {
        printf("\n\nYou win, human!");
        }
    else if (human_score < comp_score)
        {
        printf("\n\nI win, human!");
        }
    else
        {
        printf("\n\nWe tied, human!");
        }
    printf("\n\nThe final score: Me\t%d\tYou%d", human_score, comp_score);
    printf("\nThank you for playing!");

    /* this will be the final tally for the game, displayed on the screen.
        of course, this is skipped if the player complied with the
        quit() option and decides to jump out of the game.
         Sheena Easton had a great singing voice.  Where the heck
         did she vanish to?  */
    }

int score (int x)
    {
        return (x + 2);
    }

int card( int y )
    {

    int irandom = random(15);
    icard = irandom;
    y = icard
    /* the computer will draw the design of the cards below.
    while (1)
    {   
    if (icard == 11)  /* this prints up a jack card 
         I proceeded with possibly some unnecessary
           repetition in this area, but this is due to the
            fact that I had difficulty with initial printing
            of the cards during some earlier versions of the
            program.  I couldn't quite pinpoint the problem,
            so I redesigned the way ther cards were assembled,
            and there is a greater focus on details.  */
        {
        printf("%c%c%c%c%c",border1,border2,border2,border2, border3);
        printf("\n");
        printf("%c%J  %c",border4, border4);
        /* thank goodness for cut and paste */
        printf("%c%c%c%c%c",border4, inbord1,inbord2,inbord3,border4);
        printf("%c%c%c%c%c",border4, inbord4,"$",inbord4,border4);
        printf("%c%c%c%c%c",border4, inbord5,inbord2,inbord6,border4);
        printf("%c%  J%c",border4, border4);
        printf("%c%c%c%c%c",border5,border2,border2,border2,border6);
        suit();
        return (y);
        break;
        }

    else if ( icard == 12)  /* This is the Queen card */
        {
        printf("%c%c%c%c%c",border1,border2,border2,border2, border3);
        printf("\n");
        printf("%c%Q  %c",border4, border4);
        /* thank goodness for cut and paste */
        printf("%c%c%c%c%c",border4,inbord1,inbord2,inbord3,border4);
        printf("%c%c%c%c%c",border4,inbord4,"@",inbord4,border4);
        printf("%c%c%c%c%c",border4,inbord5,inbord2,inbord6,border4);
        printf("%c%  Q%c",border4,border4);
        printf("%c%c%c%c%c",border5,border2,border2,border2,border6);
        suit();
            return (y);
        break;
        }

    else if (icard == 13)  /* This prints up the King card...
                  Once again, thank goodness for cut n' paste  */
        {
        printf("%c%c%c%c%c",border1,border2,border2,border2, border3);
        printf("\n");
        printf("%c%K  %c",border4, border4);
        /* thank goodness for cut and paste */
        printf("%c%c%c%c%c",border4,inbord1,inbord2,inbord3,border4);
        printf("%c%c%c%c%c",border4,inbord4,"!",inbord4,border4);
        printf("%c%c%c%c%c",border4,inbord5,inbord2,inbord6,border4);
        printf("%c%  K%c",border4, border4);
        printf("%c%c%c%c%c",border5,border2,border2,border2,border6);
        suit();
        return (y);
        break;
        }
    else if (icard == 14) /* This prints up the ACE card */
        {
        printf("%c%c%c%c%c",border1,border2,border2,border2, border3);
        printf("\n");
        printf("%cACE%c",border4, border4);
        /* thank goodness for cut and paste */
        printf("%c%c%c%c%c",border4,inbord1,inbord2,inbord3,border4);
        printf("%c%c%c%c%c",border4,inbord4,"+",inbord4,border4);
        printf("%c%c%c%c%c",border4,inbord5,inbord2,inbord6,border4);
        printf("%c%ACE%c",border4, border4);
        printf("%c%c%c%c%c",border5,border2,border2,border2,border6);
        suit();
        return (y);
        break;
        }
    else if ((icard > 1) && (icard < 11))
        {
        printf("%c%c%c%c%c",border1,border2,border2,border2, border3);
        printf("\n");
        printf("%c%3d%c",border4, icard, border4);
        /* thank goodness for cut and paste */
        printf("%c%c%c%c%c",border4,inbord1,inbord2,inbord3,border4);
        printf("%c%c%c%c%c",border4,inbord4,"*",inbord4,border4);
        printf("%c%c%c%c%c",border4,inbord5,inbord2,inbord6,border4);
        printf("%c%3d%c",border4,icard, border4);
        printf("%c%c%c%c%c",border5,border2,border2,border2,border6);
        suit();
            return (y);
        break;
        }

    else
        {
        /* The condition met was a one or a zero, and ....
        "I can't go for that! no can do!"

        this condition should only be met if the random number
        is a 1 or a zero.  The while loop will continue without
        stopping, and hopefully it will re-value the icard with a
        new number that fits into one of the above categories.*/

        }
    }
    }
void suit(void)
    /* suit actually has no real bearing on the game regarding how
        points are accumulated, but it helps make the cards
        seem like cards.  I couldn't get the symbols to print
        as of the latest re-write of this program.  According to the
        ANSI symbol list, it should simply be 'alt-001 through 004'
        but for some reason it wouldn't accept these.
        I would prefer to simply have these symbols, but since
        I dont, the sentences below will have to suffice.  */

    {
    while(1)
        {
        int irandom3 = random(5);
        if (irandom3 == 1)
            {
            printf("\nSuit of Hearts");
            break;
            }
        else if (irandom3 == 2)
            {
            printf("\nSuit of Clubs");
            break;
            }
        else if (irandom3 == 3)
            {
            printf("\nSuit of Spades");
            break;
            }
        else if (irandom3 == 4)
            {
            printf("\nSuit of Diamonds");
            break;
            }
        else
            {
            /* returns to loop to retrieve another value */
            }
           
        }
    }
void war(void)
    {
    printf("\n\nWar is declared, human!");
    quit();
    int irounds = (\0);
    while (1)

        {
        int irounds = irounds + 6;
        printf("\nI drew 3 cards, the face card is...");
        int card (comp_draw);
       
        printf("\nYou drew 3 cards, the face card is...");
        int card(human_draw);

        if (comp_draw > human_draw)
            {
            comp_score = (comp_score + irounds);
            break;
            }
        else if (human_draw > comp_draw)
            {
            human_score = (human_score + irounds);
            break;
            }
        else
            {
            printf("\nWe tied, so we play again!");
            quit();
            }
        }

    }
void quit(void)
    {
    printf("\n\nHit any key to continue, or CTRL-C to quit");
    getchar();
    }

void instructions(void)
    {
    printf("\n\n\n\n\n\n\n\n");
    printf("\nInstructions");
    printf("\n\nThe game of War!");
    printf("\n\nIn this game, the two players will be you");
    printf("\nversus the computer.  The computer has been programmed by me to have a ");
    printf("\nremarkable, unrivaled artificial intelligence.  He will be, quit possibly,");
    printf("\nthe most brilliant opponent to ever face you in the card game of war!");
    printf("\n\nIn War, 2 players draw a card from the deck.  The player who draws the");
    printf("\nhigher card wins both cards.  If both cards contain the same face value");
    printf("\nthen WAR is declared!  The game then goes into a war round.  Both players");
    printf("\ndraw 3 cards, 1 face up and 2 face down.  The player who holds the higher");
    printf("\nface value of the card wins all the cards!  If both cards are the same,");
    printf("\nwell, then, the war round continues, the sequence continues until one of the");
    printf("\nplayers has a higher card than the other.  When a winner is determined, he");
    printf("\ntakes ALL the cards of the war round!");
    printf("\n\n");
    printf("The game is easy to play, and easy to master.");
    printf("Get ready to face the computer...a master of WAR!");
    quit();
    initialization();
   
    }
void aboutme(void)
    {
    printf("\n\n\n\n\n\n\n\n");
    printf("\nAbout me");
    printf("\n\nRob Marsh is a struggling computer student, who is too stupid to admit");
    printf("\nThat he is probably taking more classes than he can handle. ");
    quit();
    initialization();
    }

void exit(void)
    {
    exit(0);
    }

No comments:

Post a Comment