I'm trying to write a program to simulate a tortoise & hare program. They begin at square 1 of 70. There is a clock that ticks once per second. With each tick, the program should adjust the position of the animals according to a chart I've been given to follow. Variables keep track of the animal's positions. Position numbers are 1-70. For each tick of the clock, I need to print a 70 position line showing the letter T for tortoise & H for hare. If they land in the same square, OUCH needs to be printed. Besides the potential OUCH, all print positions besides the T & H should be blank.

I'm not begging for code. I'm just drawing a total blank atm. I need something to help get me started. It just seems like so much in this program that I can't figure out how to even start it all up.


-Kahaj

Recommended Answers

All 5 Replies

Work on just one animal at a time. Using pen and paper develop an equation that will calculate the position of that animal at any given point in time given the original starting position of the animal and the number of positions per unit time the animal advances. I'd probably round down if the animal was less than half way to the next position and round up if more than half way, but that's up to you, or your instructions. Then use a loop to represent the position. Clear the screen between each update to the positions. You might want to write a delay in the display feature so it doesn't whip by you, faster than the average Bear, I mean tortoise.

Then do the same for the other animal.

Lastly, compare the position of both animals at each time frame and display the apropriate output.

Aaargh!

I thought I had this one figured out, thought I had it all done. I tried to compile the program and got a ton of error messages. I ended up wiping the whole crazy thing and starting over again. Here is what I have so far. I know the srand functions (I think I should be using srand and not just rand *shrugs*) still need defined. I just have them in there so that I don't mess around and forget to put them in.

//Clifton Copeland
//Excercise 8.17

#include <iostream>
using namespace std;
#include <cstdlib>
using std::rand;

#define TORTOISE 'T'
#define HARE 'H'

main()
{
	int tort=0;
	int hare=0;
	int i=0;
	int b=0;
	char positions[70];
	
	cout << "BANG !!!!!" << endl;
	cout << "AND THEY'RE OFF !!!!!" << endl;
      
    
    if (tort >= 70)
       cout << "TORTOISE WINS!!! YAY!!!" << endl;
    else
        srand; 
        
    if (hare >= 70)
       cout << "Hare wins.  Yuch." << endl;
    else 
         srand;
   
      
      
return 0;

}

Any ideas on where to head with this thing? I am just totally hit-my-head-against-the-desk at a loss on this one!

You need to learn how to call a function (among many other things). Back up in your text a bit.

[edit]Call srand only once in your program, before you call rand . Use rand to obtain a random number between 0 and RAND_MAX . If you want to narrow the range, a typical newb method is to use the % operator with the value returned by rand to narrow the range. And an easy start is to generally seed srand with time(NULL) .

Okay, I've wiped the whole program and started it over again. This is what I've come up with so far. Am I headed in the right direction now?

I seem somewhat stuck as to how to get the output going in the right direction though. :/

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


#define TORTOISE 'T'
#define HARE 'H'

main()
{
	int tort=0;
	int hare=0;
	int tPosition = 0;
	int hPosition = 0;
	int animalCtr = 1;
	int spaces=0;

	char positions[70];
	for(int a =0;a<70;a++)
		positions[a]='_';
	
	cout << "BANG !!!!!" << endl;
	cout << "AND THEY'RE OFF !!!!!" << endl;
      
    
    srand(time(0));

    int num = 1 + rand() % 10;
    cout << "animalCtr: " << animalCtr << endl;
	if(animalCtr == 1)
	{
     if(num > 0 && num < 3)
		spaces -= 6;
    if(num > 2 && num < 6)
		spaces += 1;
    if(num > 5 && num < 11)
		spaces += 3;
     
	if(spaces < 0)
		spaces = 0;
	for(int b = 0;b<100;b++)
	{
    
 
    tPosition += spaces;
	spaces = 0;

    cout << "tPosition: " << tPosition << endl;
	 for(int i = 0;i<70;i++)
		 if(i== tPosition)
			 positions[i] = 'T';

	 for(int j =0;j< 70;j++)
		 cout << positions[j];
	}

    }
    if (tort >= 70)
       cout << "TORTOISE WINS!!! YAY!!!" << endl;
    else
            
        
    if (hare >= 70)
       cout << "Hare wins.  Yuch." << endl;
    else 
   
      
      
return 0;

}

I might start more like this:

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

void show(int pos, char who)
{
   for ( int i = 0; i < 70; ++i )
   {
      if (i == pos)
      {
         cout << who;
      }
      else
      {
         cout << '_';
      }
   }
   cout << endl;
}

int jump()
{
   int num = 1 + rand() % 10;
   if ( num > 0 && num < 3 )
      return -6;
   else if ( num < 6 )
      return 1;
   return 3;
}

int main()
{
   int tort = 0, hare = 0;

   cout << "BANG !!!!!\n" 
           "AND THEY'RE OFF !!!!!" << endl;

   srand(time(0));

   int num = 1 + rand() % 10;
   while ( tort < 70 && hare < 70 )
   {
      tort += jump();
      show(tort, 'T');
      hare += jump();
      show(hare, 'H');
      cout << endl;
   }
   if ( tort >= 70 )
      cout << "TORTOISE WINS!!! YAY!!!" << endl;
   else 
      cout << "Hare wins.  Yuch." << endl;
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.