Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,108 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,720 people online right now. Registration is fast and FREE... Join Now!




Tortoise and Hare win condition is outputting wrong

 
Reply to this topicStart new topic

Tortoise and Hare win condition is outputting wrong

Smarf
15 Oct, 2007 - 01:18 PM
Post #1

D.I.C Head
**

Joined: 21 Sep, 2007
Posts: 80



Thanked: 2 times
My Contributions
Ok the assigment is as follows:

Re-create the classic Tortoise and Hare race. A clock ticks every second (loop) and a random number is generated for each animal that then determines whether they move left or right and how many spaces. Each cylcle of the loop it shows the position of the tortoise and the hare on a 70 space line using H and T with blanks between. Once either animal reaches square 70, the race is over and it should say "Tortoise won" or "Hare won" etc.

Read the full problem here

I have the program figured out but for some reason when the Hare wins it says "The tortoise won", and vice versa when the tortoise wins. I could cheat and just change the output statements so they print properly but I'd like to know what I'm doing wrong.

Here is the code:

CODE
#include <iostream>
using std::cout;
using std::endl;

#include <cstdlib>

#include <ctime>

#include <iomanip>
using std::setw;

// set a constant for race end
const int RACE_END = 70;

// Function prototype for moveTortoise
void moveTortoise (int *);

// Function prototype for moveHare
void moveHare(int *);

// Function prototype for showPosition
void showPosition(int *,int *);

int main()
{
   int tortoise = 1;
   int hare = 1;
   int timer = 0;

   srand(time(0));

   cout << "BANG !!!!" << endl;
   cout << "AND THEY’RE OFF !!!!" << endl;
   cout << endl;
  
   // While the race isn't over, keep getting moves and showing positions
   while (tortoise != RACE_END && hare != RACE_END)
    {
      // get the tortoises move
       moveTortoise(&tortoise);
      
      // get the hares move
       moveHare(&hare);
      
      // show their positions with function and keep track of time
       showPosition(&tortoise,&hare);
      ++timer;

   } // end while

   // determine winner
   if (tortoise > hare)
     {
      cout << endl;
      cout << "TORTOISE WINS!!! YAY!!!" << endl;
     }
   else if (hare > tortoise)
     {
       cout << "Hare wins. Yuch." << endl;
     }
   else if (hare == tortoise)
     {
        cout << "It's a tie, but we'll say the Tortoise won!" << endl;        
     }
    
   cout << "TIME ELAPSED = " << timer << " seconds" << endl;

   cout << endl;
   system("pause");
   return 0;

  
} // end main


// function definition for moveTortoise
void moveTortoise(int *tortoisePtr)
{
   int x = 1 + rand() % 10;

   // determine which move to make
   if (x >= 1 && x <= 5)        // fast plod
      *tortoisePtr += 3;

   else if (x == 6 || x == 7)   // slip
      *tortoisePtr -= 6;

   else                         // slow plod
      ++(*tortoisePtr);
   if (*tortoisePtr < 1)
      *tortoisePtr = 1;

   else if (*tortoisePtr > RACE_END)
      *tortoisePtr = RACE_END;

} // end function moveTortoise


// function definition for moveHare
void moveHare(int *harePtr)
{
   int y = 1 + rand() % 10;

   // determine which move to make
   if (y == 1 || y == 2)        // 20% chance to sleep
     {
      *harePtr += 0;             // move zero spaces
     }
    
   else if (y == 3 || y == 4)   // 20% chance to do big hop
     {
       *harePtr += 9;            // move 9 squares right
     }  

   else if (y == 5)             // 10% chance to big slip
     {
       *harePtr -= 12;          // move 12 squares left
     }
  
   else if (y >= 6 && y <= 8)   // 30% chance to small hop  
     {
       *harePtr += 1;           // move 1 square right
     }  
  
   else if (y == 9 || y == 10)  // 20% chance to small slip
     {
       *harePtr -= 2;           // move 2 squares left
     }  
    
  
   // Test if hare is before beginning of race
// and don't let it go outside array bounds
   if (*harePtr < 1)
      {
        *harePtr = 1;
      }  

   else if (*harePtr > RACE_END)
      *harePtr = RACE_END;
} // end function moveHare


// function definition for showPosition
void showPosition(int *bunnyPtr,int *snapperPtr)
{
   if (*bunnyPtr == *snapperPtr)
      cout << setw( *bunnyPtr ) << "OUCH!!!";      

   else if (*bunnyPtr < *snapperPtr)
      cout << setw( *bunnyPtr ) << "H"
           << setw( *snapperPtr - *bunnyPtr) << "T";

   else
      cout << setw(*snapperPtr) << "T"
           << setw(*bunnyPtr - *snapperPtr) << "H";

   cout << endl;

} // end function showPosition


This post has been edited by Smarf: 15 Oct, 2007 - 01:24 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Tortoise And Hare Win Condition Is Outputting Wrong
15 Oct, 2007 - 01:36 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Your problem is not with the determination of the winner - that code is executing properly. The problem lies in your call to the showPosition function:
CODE

void showPosition(int *, int *);        //prototype
showPosition(&tortoise, &hare);      //function call
void showPosition(int *bunnyPtr, int *snapperPtr)    //function header


You're swapping the tortoise and hare arguments when you pass them to the function, so the output displayed on the screen is wrong. The actual positions are right (throw in a cout << hare << " " << tortoise << endl statement instead of the display function to check), as is the output of who won.

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

Smarf
RE: Tortoise And Hare Win Condition Is Outputting Wrong
15 Oct, 2007 - 04:28 PM
Post #3

D.I.C Head
**

Joined: 21 Sep, 2007
Posts: 80



Thanked: 2 times
My Contributions
QUOTE(jjhaag @ 15 Oct, 2007 - 02:36 PM) *

Your problem is not with the determination of the winner - that code is executing properly. The problem lies in your call to the showPosition function:
CODE

void showPosition(int *, int *);        //prototype
showPosition(&tortoise, &hare);      //function call
void showPosition(int *bunnyPtr, int *snapperPtr)    //function header


You're swapping the tortoise and hare arguments when you pass them to the function, so the output displayed on the screen is wrong. The actual positions are right (throw in a cout << hare << " " << tortoise << endl statement instead of the display function to check), as is the output of who won.

Hope that helps,

-jjh


Ah duh, I feel stupid. Thanks for pointing that out!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month