Question

In: Computer Science

2020 COS-241: DATA STRUCTURES Problem 2: Simulation: The Tortoise and the Hare DESCRIPTION This project deals...

2020 COS-241: DATA STRUCTURES

Problem 2: Simulation: The Tortoise and the Hare

DESCRIPTION

This project deals with the classic race between the tortoise and the hare. For this project, you be using random-number generation to move the creatures. To make things more interesting, the animals have to race up the side of a slippery mountain, which could cause them to lose ground. In this race either animal could win or there could be a tie with no clear winner.

The animals begin at "Square 1" of 70 squares. Each of the squares represents a position the animal can hold along the racetrack. The finish line is at Square 70. When an animal wins, a winning message of your choice should be posted. For example:

  • Yay! The rabbit won! He hops the fastest!

  • Woo-hooo! Slow and steady wins the race! Congratulations, turtle!

  • Tie score—no winner! Want to race again?

To start the race, print a message similar to:

  • Bang! Off they go!

There is a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals according to the following rules:

Animal

Move Type

Percentage of the Time

Actual Move

Tortoise

Fast Plod

50%

3 squares to the right

Tortoise

Slip

20%

6 squares to the left

Tortoise

Slow Plod

30%

1 squares to the right

Hare

Sleep

20%

No move at all

Hare

Big Hop

20%

9 squares to the right

Hare

Big Slip

10%

12 squares to the left

Hare

Small Hop

30%

1 square to the right

Hare

Small Slip

20%

2 squares to the left

Keep track of the positions of the animals by using variables. If an animal slips, the lowest it can go is back to position 1. The highest it can go is to position 70 when it wins the race.

You will work with the percentages in the table above by generating a random integer current in the range

1≤ current ≤10.

For the tortoise, a "fast plod" is when 1≤ current ≤ 5, a "slip" when 6 ≤ current ≤ 7, or a "slow plod" 8 ≤ current ≤ 10. A similar approach would be used to set up the moves for the hare.

For each tick of the clock (each repetition of the loop), print a 70-position line showing the letter T in the tortoise’s position and the letter H in the hare’s position. If the two animals land on the same square, which may happen, the animals will bump. If this happens, print BUMP! at the current position.

After you print each line, check to see if either animal has landed on Square 70. If this happens, print a winning-type message.

It may make the simulation more interesting if you have users press any key after each iteration of the loop, so that they can see the movement of the animals.

DELIVERABLES

  • Your C++ source code with any header files

  • Your executable code

  • A document detailing how you will test your program that also includes screenshots of a few sample runs of your program.

  • An overview document giving the name of each file submitted and its purpose, as well as any discussion you have on implementing this program. If there are any problems with your program or it does not run or run as it is supposed to, please indicate that as well in this document.

please be original and dont really need to worry about taking screen shots as I will test it in visual studio

Solutions

Expert Solution

here is your code for hair and turtle race .

#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 << "Woo-hooo! Slow and steady wins the race! Congratulations, turtle!" << 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 << "Yay! The rabbit won! He hops the fastest!" << 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;
}

output:-

the output of code is too large so i am giving starting and end point of output.

Thank You !!

if you face any trouble in compilation feel free to ask.


Related Solutions

Consider an initial value problem              ?′′ + 2? = ?(?) = cos? (0 ≤ ? <...
Consider an initial value problem              ?′′ + 2? = ?(?) = cos? (0 ≤ ? < ?) ,    0 (? ≥ ?) ?(0) = 0 and ?′(0) = 0 (a) Express ?(?) in terms of the unit step function. (b) Find the Laplace transform of ?(?). (c) Find ?(?) by using the Laplace transform method.
Problem 2 Statement: Let r1 = 1 + cos θ and r2 = 3 cos θ....
Problem 2 Statement: Let r1 = 1 + cos θ and r2 = 3 cos θ. (a) Graph each function in the rθ-plane. (b) Find all intersection points (both collision and non-collision). (c) Find the area common to the two graphs.
2- The PACE project at the University of Wisconsin in Madison deals with problems associated with...
2- The PACE project at the University of Wisconsin in Madison deals with problems associated with high-risk drinking on college campuses. Based on random samples, the study states that the percentage of UW students who reported bingeing at least three times within the past two weeks was 42.2% in 1999 (n = 334) and 21.2% in 2009 (n = 843). Test that the proportion of students reporting bingeing in 1999 is different from the proportion of students reporting bingeing in...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students to generate a linked list of playing card based on data read from a file and to write out the end result to a file. linkedList.h Create a header file name linkedList Include the following C header files: stdio.h stdlib.h string.h Create the following macros: TRUE 1 FACES 13 SUITS 4 Add the following function prototypes: addCard displayCards readDataFile writeDataFile Add a typedef struct...
Simulation Project Design a Seven Segment Decoder to decode and display CS and the last 2...
Simulation Project Design a Seven Segment Decoder to decode and display CS and the last 2 numbers of your #900 number (Student ID). Assume the corresponding inputs for the letters and numbers. Example: If your #900 number is 900123456 take last 2 numbers that is 56 and display 'CS56' Inputs Assumptions 00- for C 01-for S 10— Last number (6 in the given example) 11— Last but one number (5 in the given example) Write the Truth Table for the...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is a number character 1, 2, 3 or 4, which indicates whether a student is a freshman, a sophomore, junior or senior. Suppose the following characters are used to denote the majors: M: Mathematics C: Computer Science I: Information Technology Here is...
coding the following project: Setting up structures to store and retrieve data. A major requirement of...
coding the following project: Setting up structures to store and retrieve data. A major requirement of virtually all projects in the financial world involves the storage and retrieval of data. project must be properly modularized and allow for more than one way to store the data to satisfy the various needs of clients.The first manner is by storing data using hashtables (or hash maps) as the basis of storing and retrieving data.
Project 2: Micah’s Replacement Decision Project description MicahCorporation is trying to determine the initial investment required...
Project 2: Micah’s Replacement Decision Project description MicahCorporation is trying to determine the initial investment required to replace an old machine with a new one. The new machine costs $380,000, and an additional $20,000 will be necessary to install it. It will be depreciated under MACRS, using a 5-year recovery period. The 5-year MACRS depreciation rates are: Year 1 2 3 4 5 6 Depreciation 20% 32% 19% 12% 12% 5% The old machine was purchased 3 years ago at...
Problem 2 Your company, Dominant Retailer, Inc., is considering a project whose data are shown below....
Problem 2 Your company, Dominant Retailer, Inc., is considering a project whose data are shown below. Revenue and cash operating expenses are expected to be constant over the project's 5 year expected operating life; annual sales revenue is $95,000.00 and cash operating expenses are $37,500.00. The new equipment's cost and depreciable basis is $135,000.00 and it will be depreciated by MACRS as 5 year property. The new equipment replaces older equipment that is fully depreciated but can be sold for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT