C++ Program to Solve Tortoise and Hare Problem Using Algorithm Full Project For Beginners

  • Post author:
  • Post category:c++
  • Post comments:0 Comments

 

 

main.cpp

 

 

/* Introduction to C++ Tortoise and Hare Problem by Dillon Morse
 * =============================================================
 * Simulation of a race. Both the tortoise and the hare begin at square 1 of
 * 70. Each square represents a possible position along the race track. The
 * finish is square 70. You win when you reach or pass square 70.
 *
 * There is a clock that ticks once per second. With each tick your program
 * should adjust the position of the racers according to the table below.
 *
 * Use variables to keep track of the positions. A racer cannot slip past
 * square 1 into a square <= 0.
 *
 * Generate the table by producing a random integer i, 1 <= i <= 10. For the
 * tortoise, perform a fast plod when 1 <= i <= 5, a slip when 6 <= i <= 7 or a
 * slow plod when 8 <= i <= 10. Similarly for the hare.
 *
 * Begin the race by printing:
   * BANG !!!
   * AND THEY'RE OFF !!!!!
 *
 * For each tick of the clock, print a 70-position line showing the letter T
 * for the Tortoise's position and H for the hare. If the racers land on the
 * same square, the tortoise bites the hare and you must print
 *  OUCH !!!
 * beginning at that position. All positions other than the T, the H, or the
 * OUCH !!! should be a blank.
 *
 * After printing each line, whether either animal has reached or passed square
 * 70. If so, print the winner and terminate the simulation. If the tortoise
 * wins, print TORTOISE WINS!!!, otherwise print HARE WINS? If both win on the
 * same clock tick, print TIE.
 */

#include <iostream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <ctime>

using namespace std;

class tortoise {
  public:
    int square;

    tortoise();
    bool victor();
    void win();
    void fastPlod();
    void slip();
    void slowPlod();
} turtle;

tortoise::tortoise() {
  square = 1;
}

bool tortoise::victor() {
  bool victory;

  if ( square < 70 )
    victory = false;
  else
    victory = true;

  return victory;
}

void tortoise::win() {
  cout << "TORTOISE WINS!!!" << endl;
}

void tortoise::fastPlod() {
  square += 3;
  if ( square > 70 )
    square = 70;
}

void tortoise::slip() {
  square -= 6;
  if ( square < 1 )
    square = 1;
}

void tortoise::slowPlod() {
  square += 1;
}

class hare {
  public:
    int square;

    hare();
    // The sleep function is absent here because it does nothing, and is
    // therfore handled in tick() instead.
    bool victor();
    void win();
    void bigHop();
    void bigSlip();
    void smallHop();
    void smallSlip();
} rabbit;

hare::hare() {
  square = 1;
}

bool hare::victor() {
  bool victory;

  // Constructs like this will be found in any functions related to movement.
  // They are necessary for proper output.
  if ( square < 70 )
    victory = false;
  else
    victory = true;

  return victory;
}

void hare::win() {
  cout << "HARE WINS?" << endl;
}

void hare::bigHop() {
  square += 9;
  if ( square > 70 )
    square = 70;
}

void hare::bigSlip() {
  square -= 12;
  if ( square < 1 )
    square = 1;
}

void hare::smallHop() {
  square += 1;
}

void hare::smallSlip() {
  square -= 2;
  if ( square < 1 )
    square = 1;
}

void tick();
void displayOutput();

int main()
{
  cout << "BANG !!!" << endl
       << "AND THEY'RE OFF !!!!!" << endl;

  while ( !( turtle.victor() || rabbit.victor() ) ) {
    // This line tells the thread to pause for 1 second.
    this_thread::sleep_for ( chrono::seconds(1) );
    tick();
  }

  if ( turtle.victor() && rabbit.victor() )
    cout << "TIE";
  else if ( turtle.victor() )
    turtle.win();
  else
    rabbit.win();

  return 0;
}

void tick() {
  srand ( time(0) );

  int random = rand() % 11;

  if ( random < 5 )
    turtle.fastPlod();
  else if ( random < 7 )
    turtle.slip();
  else
    turtle.slowPlod();

  // A sleep action is also absent here, since it would just be empty
  if ( ( random >= 2 ) && ( random < 4 ) )
    rabbit.bigHop();
  else if ( random < 5 )
    rabbit.bigSlip();
  else if ( random < 8 )
    rabbit.smallHop();
  else if ( random < 10 )
    rabbit.smallSlip();

  displayOutput();
}

void displayOutput() {
  cout << endl;

  if ( turtle.square < rabbit.square ) {
    for ( int i = 1; i < turtle.square; i++ )
      cout << '=';
    cout << 'T';

    for ( int i = 1; i < ( rabbit.square - turtle.square ); i++ )
      cout << '=';

    cout << 'H';

    for ( int i = 1; i < ( 70 - rabbit.square ); i++ )
      cout << '=';

    cout << endl;
  }
  else if ( rabbit.square < turtle.square ) {
    for ( int i = 1; i < rabbit.square; i++ )
      cout << '=';
    cout << 'H';

    for ( int i = 1; i < ( turtle.square - rabbit.square ); i++ )
      cout << '=';

    cout << 'T';

    for ( int i = 1; i < ( 70 - turtle.square ); i++ )
      cout << '=';

    cout << endl;
  }
  else {
    for ( int i = 1; i < rabbit.square; i++ )
      cout << '=';
    cout << 'B';

    for ( int i = 1; i < ( 70 - rabbit.square ); i++ )
      cout << '=';

    cout << endl << "OUCH !!!" << endl;
  }

  cout << "T = Tortoise" << endl
       << "H = Hare"     << endl
       << "B = Both"     << endl;
}

Leave a Reply