Thread: Developing a C program - Pokemon

  1. #16
    Registered User
    Join Date
    Nov 2016
    Posts
    10
    Thanks

  2. #17
    Registered User chappo's Avatar
    Join Date
    Nov 2016
    Posts
    2
    I saw this while browsing the forums and thought it was a pretty cool project. This is what I was able to come up with yesterday. There are still many bugs/issues with the code, but I'll leave that for you to fix! Please post your results here so that we can compare our progress.

    Code:
    /*
    - Author: chappo
    - Name: Safari Zone
    - Desc: A simple safari zone clone
    */
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    struct Pokemon
    {
        char name[20];
        int id;
        int catchRate;
        int encounterRate;
        int timesCaptured;
        
    };
    
    
    struct Bag
    {
        char itemName[20];
        int itemAmount;
    };
    
    
    int main()
    {
        // initial setup of pokemon and bag
        srand(time(NULL));
        int pokemonRand = rand() %5;
        
        // setup pokemon 1
        struct Pokemon Pokemon[4];
        strcpy(Pokemon[0].name, "Blastoise");
        Pokemon[0].id = 9;
        Pokemon[0].catchRate = (rand() % 100);
        Pokemon[0]. timesCaptured = 0;
        
        // setup pokemon 2
        strcpy(Pokemon[1].name, "Golduck");
        Pokemon[1].id = 55;
        Pokemon[1].catchRate = (rand() % 100);
        Pokemon[1]. timesCaptured = 0;
        
        // setup pokemon 3
        strcpy(Pokemon[2].name, "Weezing");
        Pokemon[2].id = 110;
        Pokemon[2].catchRate = (rand() % 100);
        Pokemon[2]. timesCaptured = 0;
        
        // setup pokemon 4
        strcpy(Pokemon[3].name, "Golem");
        Pokemon[3].id = 76;
        Pokemon[3].catchRate = (rand() % 100);
        Pokemon[3]. timesCaptured = 0;
        
        // setup pokemon 5
        strcpy(Pokemon[4].name, "Butterfree");
        Pokemon[4].id = 12;
        Pokemon[4].catchRate = (rand() % 100);
        Pokemon[4]. timesCaptured = 0;
        
        // setup item 1 
        struct Bag Item[4];
        strcpy(Item[0].itemName, "Safariball");
        Item[0].itemAmount = 10;
        
        // setup item 2
        strcpy(Item[1].itemName, "Pokeball");
        Item[1].itemAmount = 5;
        
        // setup item 3
        strcpy(Item[2].itemName, "Good Bait");
        Item[2].itemAmount = 5;
        
        // setup item 4
        strcpy(Item[3].itemName, "Bait");
        Item[3].itemAmount = 5;
        
        // setup item 5
        strcpy(Item[4].itemName, "Rock");
        Item[4].itemAmount = 5;
        
        int choice, stop, currentID, currentCatchRate, currentTimesCaptured, leave, currentBalls, currentBait, currentGoodBait, currentRocks, misses;
        int realcatch = rand()%100+1;
        char currentName[20], isCaught[20];
        label:
        printf("\n** Welcome to the Safari Zone**\n\n1)Enter the Safari Zone\n2)Information\n3)Exit\n\nEnter your selection: ");
        scanf("%d", &choice);
        
        
        switch(choice)
        {
            case 1:
            printf("\nYou entered the Safari Zone.\nYou walk around and see lush gardens and ample space for pokemon to roam.");
            currentTimesCaptured = Pokemon[pokemonRand].timesCaptured;
            currentBalls = Item[0].itemAmount+Item[1].itemAmount;
            currentRocks = Item[4].itemAmount;
            currentBait = Item[3].itemAmount;
            currentGoodBait = Item[2].itemAmount;
        label1:
        srand(time(NULL));
            strcpy(currentName, Pokemon[pokemonRand].name);
            currentID = Pokemon[pokemonRand].id;
            currentCatchRate = Pokemon[pokemonRand].catchRate;
        label2:
            printf("\nAll of a sudden you hear a bush move...\n");
            printf("\nA wild %s appeared!", currentName);
        label3:
            printf("\n\nWhat would you like to do?\n1)Throw ball [%d/%d]\t2)Throw Rock [%d/%d]\n3)Bait [%d/%d]\t4)Good Bait [%d/%d]\nPokemon Caught: [%d]\n\nEnter your selection: ", currentBalls, Item[0].itemAmount+Item[1].itemAmount, currentRocks, Item[4].itemAmount, currentBait, Item[3].itemAmount, currentGoodBait, Item[2].itemAmount, currentTimesCaptured);
            scanf("%d", &choice);
            switch(choice)
            {
                case 1:
                // throw a ball
                // if out of balls, you leave
                if (currentBalls == 0)
                {
                    printf("You are out of balls! You have to leave now!");
                    goto label;
                }
                // if caught
                if (currentCatchRate >= realcatch)
                {
                    strcpy(isCaught, "\n!The Pokemon WAS caught!");
                    leave = 1;
                    currentTimesCaptured++;
                    currentBalls--;
                    misses = 0;
                } else {
                    // if not caught -1 ball, decrease catch chance -1
                    strcpy(isCaught,"\n!The Pokemon was NOT caught!");
                    currentBalls--;
                    realcatch--;
                    // if you miss three times Pokemon runs away. New Pokemon comes.
                    if(misses >=3) {
                        printf("\n!The Pokemon has ran away!\n");
                        goto label1;
                    }
                    // if miss, add to counter
                    misses++;
                }
                printf("\nYou threw a Safari Ball at %s...\n%s has a catch rate of %d! Did he get caught?....%s\n", currentName, currentName, currentCatchRate, isCaught);
                // if caught, you encounter a new pokemon. goto label1
                if (leave == 1)
                {
                    goto label1;
                    leave == 0;
                }
                // else goto options again
                goto label3;
                case 2:
                // throw a rock
                // if you have rocks make it harder to catch+1, rocks-1
                if (currentRocks != 0)
                {
                    printf("You hit %s with a rock! They're angry now!", currentName);
                    realcatch++;
                    currentRocks--;
                    goto label3;
                }
                // if out or rocks, you cannot use this option
                printf("You are out of rocks! You cannot use this option any longer");
                goto label3;
                
                case 3:
                // throw bait
                // if you have bait make it easier to catch-1, bait-1
                if (currentBait != 0)
                {
                    printf("You threw some bait at %s! They're less aware now!", currentName);
                    realcatch--;
                    currentBait--;
                    goto label3;
                }
                // if out or bait, you cannot use this option
                printf("You are out of bait! You cannot use this option any longer");
                goto label3;
                
                case 4:
                // throw good bait
                // if you have bait make it easier to catch-1, bait-1
                if (currentGoodBait != 0)
                {
                    printf("You threw some good bait at %s! They're less aware now!", currentName);
                    realcatch = realcatch-5;
                    currentGoodBait--;
                    goto label3;
                }
                // if out or good bait, you cannot use this option
                printf("You are out of good bait! You cannot use this option any longer");
                goto label3;
    
    
                default:
                goto label3;
                
            }
    
    
            break;
            
            case 2:
            printf("\nIn the Safari Zone you are given Safariballs and some other items to help catch Pokemon!\nThere are currently 5 Pokemon in our Park: %s, %s, %s, %s, and %s. ", Pokemon[0].name, Pokemon[1].name, Pokemon[2].name, Pokemon[3].name, Pokemon[4].name);
            printf("Would you like to enter?\n1)Yes\n2)No\n\nEnter your selection: ");
            scanf("%d", &choice);
            if(choice != 1)    {break;}
            goto label;
            break;
            
            case 3:
            
            default:
            printf("\nInvalid option.");
            break;
        }
    }

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @chappo: I strongly suggest you stop using goto; at least start using label names that have meaning instead of ones like label3.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #19
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Also - there is never any real need to have so much code stuffed within function main. Such a large program can easily be split into functions with each performing a set task. Like stahta01 said, it would also relieve the usage of goto statements. Or, instead of goto use loops where possible.
    Double Helix STL

  5. #20
    Registered User chappo's Avatar
    Join Date
    Nov 2016
    Posts
    2
    You're all absolutely right. The program is not at all as optimized as it could be. I aimed to write it as a novice would while in school, and like i stated in my previous post, the program was left imperfect for the guy to fix.

    Did any of you by chance create this program? I'd love to see what you came up with.

    Quote Originally Posted by chappo View Post
    I saw this while browsing the forums and thought it was a pretty cool project. This is what I was able to come up with yesterday. There are still many bugs/issues with the code, but I'll leave that for you to fix! Please post your results here so that we can compare our progress.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Developing program for solving logic equations
    By Mariano Sosto in forum C Programming
    Replies: 1
    Last Post: 05-01-2015, 11:00 PM
  2. Pokemon MMORPG?
    By FiftyNine50 in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 07-25-2007, 04:05 PM
  3. [WinAPI] Developing a color customizable program
    By Templario in forum Windows Programming
    Replies: 6
    Last Post: 02-04-2003, 06:12 PM
  4. Pokemon talk and a couple useless stories
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-01-2003, 03:49 PM
  5. help me in developing a good anti virus program
    By sadat in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2002, 02:09 PM

Tags for this Thread