Click here to Skip to main content
16,023,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program should display "Ouch" when the tortoise and hare are on the same block (during a certain phase). However it displays it incorrectly. Plus I have a problem with displaying the last phase of the race and the result. Thanks for anyone's help. Here's the code:

C#
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int END=70;

int tortoise = 1;
int hare = 1;


int y;
int randNum1= static_cast<int>((1+rand()+time(0))%10);
int moveTortoise=0;

int x;
int randNum2= static_cast<int>((1+rand()+time(0))%10);
int moveHare= 0;




void moveTortoise1(int *T)
{
    if (*T !=END)
  {
      randNum1=static_cast<int>((1+rand()+time(0))%10);
      *T++;

   switch (randNum1)
       {
           case 1: case 2: case 3: case 4: case 5:
               moveTortoise=3;
               break;
           case 6: case 7:
               moveTortoise=-6;
               break;
           case 8: case 9: case 10:
               moveTortoise=1;
               break;

       }
  *T+=moveTortoise;
    if ( *T<1 )
      *T=1;

   else if (*T>END )
   {
       *T=END;

   }

    }

    for (y=0;y<=*T;y++)
    {
        if (y== *T)
        {
            cout<<"T";
        }
        else cout<<"-";
    }
    cout<<endl;

}

void moveHare1(int *H)
{


     if (*H !=END)
  {
      randNum2=static_cast<int>((1+rand()+time(0))%10);
      *H++;




   switch (randNum2)
       {
           case 1: case 2:
               moveHare=0;
               break;
           case 3: case 4:
               moveHare=9;
               break;
           case 5:
               moveHare=-12;
               break;
           case 6: case 7: case 8:
               moveHare=1;
               break;
           case 9: case 10:
               moveHare=-2;
               break;
        }
     *H+=moveHare;
   if ( *H<1 )
      *H=1;

   else if (*H>END )
   {
       *H=END;

   }

   for (x=0;x<=*H;x++)
   {
       if (x==*H)
       {
        cout<<"H";
       }
       else cout<<"-";
   }

   cout<<endl<<endl;
  }


}

int main()
{

   cout<<" BANG!!!\n AND THEY'RE OFF !!!"<<endl<<endl;
   while (tortoise != END && hare !=END)
   {
       moveTortoise1(&tortoise);
       moveHare1(&hare);
       if (tortoise==hare)
           cout<<"OUCH!!!"<<endl;
   }

   if (tortoise<hare)

       cout<< "Hare wins. Yuch.";
   else if (tortoise==hare)
       cout<< "It's a tie";
   else
       cout<< "TORTOISE WINS!!! YAY!!!";

   cout<<endl;
}
Posted

1 solution

A nice subtle problem which had me stumped for a while. You have two statements whose results are not what you expect, due to the fact that postfix increment occurs before indirection. In your moveXXX functions you need to make the following changes:
// in moveTortoise1
(*T)++;        // was *T++

// in moveHare1
(*H)++;        // was *H++

See this page[^] for details of operator precedence and associativity.

I think you may need to look at your randomisation as the Tortoise seems to be winning all the time.
 
Share this answer
 
Comments
7mesho 21-May-10 11:14am    
THANKS RICHARD! IT WORKED

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900