Thread: Tortoise and Hare Race

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    1

    Tortoise and Hare Race

    I have a problem here, I don't know what should I do, can someone help me, this my my project and i dont know what to do.. this is my initialcodes

    Code:
    /*The tortoise and the hare, in this project you will recreate one of the truly great moments in history, 
    namely the classic race of the tortoise and the hare.
    
    
    Our contenders begin the race at “square 1” of 70 squares. 
    Each square represents a possible position along the race course. 
    The finish line is at square 70. 
    The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. 
    The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.
    Your program should randomly adjust the position of the animals according to the following rules:
    
    
    Animal				Move type				Random No				Actual move
    
    
    Tortoise			Fast plod				1-5						3 squares to the right
    
    
    					Slip					6-7						6 squares to the left
    
    
    					Slow plod				8-10					1 square to the right
    
    
    Hare				Sleep					1-2						No move at all
    
    
    					Big hop					3-4						9 squares to the right
    
    
    					Big slip				5						12 squares to the left
    
    
    					Small hop				6-8						1 square to the right
    
    
    					Small slip				9-10					2 squares to the left
    
    
    Keep track of the positions of the animals (values from 1 to 70).
    Start each animal at position 1 (i.e. the “starting gate”). 
    If an animal slips below position 1, move the animal back to position 1. 
    For the tortoise, perform a “fast plod” when the random number is between 1 and 5, 
    a slip when 6 to 7 or a slow plod when 8 to 10. Use the same technique to move the hare.
    
    
    Begin the race by printing “Bang!! And they’re off!!”. For each second, update the race track. 
    Occasionally the contenders will land on the same square. 
    In this case, the tortoise bites the hare and your program should print “Ouch” beginning at that position. 
    Test if either animal has reached or passed square 70. If so, then print the winner and terminate the simulation. 
    If both animals cross the finish line at the same time, you may want to print “It’s a tie”.*/
    
    
    
    
    
    
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    
    
    
    
    void main(){
    
    
    int tortoise=0;
    int hare=0;
    int random;
    int finish=70;
    
    
    printf("The Race Between The Hare and The Tortoise/n");
    printf("Bang!! And they’re off!!");
    
    
    	srand(time(NULL));
    
    
    random= rand()%10 +1;
    
    
    
    
    for(int race=1;tortoise !=finish && hare!=finish; race++)
    {
    //for tortoise
    if (random>=1 && random<=5)
    {tortoise+=3;}
    else if (random>=6 && random<=7)
    {tortoise-=6;}
    else if(random>=8 && random<=10)
    {tortoise+=1;}
    else 
    {printf(" ", race);}
    printf("T", tortoise);
    //for hare
    if (random>=1 && random<=2)
    {hare=hare;}
    else if (random>=3 && random<=4)
    {hare+=9;}
    else if(random==5)
    {hare-=12;}
    else if(random>=6 && random<=8)
    {hare+=1;}
    else if(random>=9 && random<=10)
    {hare-=2;}
    {printf(" ", race);}
    printf("H", hare);
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I will suggest that you learn about good indentation asap - It helps you organise your own code, helps us read through your code, and it is always worth a few extra marks with assignments.
    Indent style - Wikipedia, the free encyclopedia
    I use Allman, because that is what I've always done. I use Code::Blocks and there is a plugin that automatically does it for you, so there is no excuse :P
    Code:
    void main()
    {
        int tortoise=0;
        int hare=0;
        int random;
        int finish=70;
    
    
        printf("The Race Between The Hare and The Tortoise/n");
        printf("Bang!! And they’re off!!");
    
        srand(time(NULL));
    
        random= rand()%10 +1;
    
        for(int race=1; tortoise !=finish && hare!=finish; race++)
        {
            //for tortoise
            if (random>=1 && random<=5)
            {
                tortoise+=3;
            }
            else if (random>=6 && random<=7)
            {
                tortoise-=6;
            }
            else if(random>=8 && random<=10)
            {
                tortoise+=1;
            }
            else
            {
                printf(" ", race);
            }
            printf("T", tortoise);
            //for hare
            if (random>=1 && random<=2)
            {
                hare=hare;
            }
            else if (random>=3 && random<=4)
            {
                hare+=9;
            }
            else if(random==5)
            {
                hare-=12;
            }
            else if(random>=6 && random<=8)
            {
                hare+=1;
            }
            else if(random>=9 && random<=10)
            {
                hare-=2;
            }
            {
                printf(" ", race);
            }
            printf("H", hare);
    
        }
    
    }
    The next thing you will need to do is change the void main - FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    It is not good for your OS to do what you are doing.

    You need to do this:
    Code:
    int main(void)
    {
     
        return 0;
    }
    You need to get a random number between 1 and 10 for the tortoise and then one for the hare each loop.

    I think that you have the right idea with the loop and the variables.

    Do you have any specific question or problem?
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The hare and the tortoise
    By Messi 10 in forum C Programming
    Replies: 3
    Last Post: 12-08-2012, 08:56 AM
  2. Writer's Block - Tortoise & The Hare!
    By JM1082 in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2011, 03:21 PM
  3. Tortoise and Hare Project
    By fenixataris182 in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2005, 12:05 PM
  4. Race between the hare and the tortoise: not working...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-14-2002, 12:29 PM
  5. Tortoise and the Hare
    By ghettoman in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 01:26 PM

Tags for this Thread