How to print a line for Hare Tortoise race
Mar 18, 2016 at 3:36am UTC
I have the program for the simulation of the race between Hare and Tortoise. The tortoise and the hare move randomly until they reach position 70. It works fine, but I need to make the output look like this "______H_____". I don't know how to make the line print. Now it prints like that " H ". I need to add it to the positions(), but I don't know how.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int FINISH_LINE = 70;
void Tortoise(int *);
void Hare(int *);
void positions( int ,int );
int main()
{
int T = 1;
int H = 1;
int timer = 0;
srand( time( 0 ) );
cout << "Bang! Off they go!\n" ;
while ( T != FINISH_LINE && H != FINISH_LINE )
{
Tortoise(&T);
Hare(&H);
positions(T,H);
++timer;
}
if (T>H)
{
cout << "\nWoo-hooo! Slow and steady wins the race! Congratulations, turtle!\n" ;
}
else if (T<H)
{
cout << "\nYay! The rabbit won! He hops the fastest!\n" ;
}
else if (T=H)
{
cout << "Tie score--no winner! Want to race again?\n" ;
}
return 0;
}
void Tortoise(int *tortoisePtr)
{
int x = 1 + rand() % 10;
if ( x >= 1 && x <= 5 )
*tortoisePtr += 3;
else if ( x == 6 || x == 7 )
*tortoisePtr -= 6;
else
++( *tortoisePtr );
if ( *tortoisePtr < 1 )
*tortoisePtr = 1;
else if ( *tortoisePtr > FINISH_LINE )
*tortoisePtr = FINISH_LINE;
}
void Hare(int *harePtr)
{
int y;
y = rand() % 10;
if (y >= 1 && y <= 2)
{
*harePtr = *harePtr;
}
else if (y >= 3 && y <= 4)
{
*harePtr += 9;
}
else if (y == 5)
{
*harePtr -= 12;
}
else if (y >= 6 && y<= 8)
{
*harePtr += 1;
}
else
{
*harePtr -= 2;
}
if (*harePtr < 1)
*harePtr = 1;
else if ( *harePtr > FINISH_LINE )
*harePtr = FINISH_LINE;
}
void positions(int T,int H)
{
if ( H == T )
cout << setw( H ) << "BUMP!" ;
else if ( H < T )
cout << setw( H ) << "H"
<< setw( T - H ) << "T" ;
else
cout << setw( T ) << "T"
<< setw( H - T ) << "H" ;
cout << "\n" ;
}
Topic archived. No new replies allowed.