Visual C# 8.28 (Simulation: Tortoise and the Hare)

This Solution is for the “Simulation: Tortoise and the Hare” of chapter 8 in the textbook; Visual c# How to program 6th edition by Deitel.

//Author: Alexander Anaya Ashe
//Date: 6/6/2017

/*Description:
Program begins the race by displaying the specified message “On your Mark…” then, for each repetition of a loop displays the letter “T” in the position of the tortoise
and the letter “H” in the position of the hare. when the contenders land on the same square the program displays “ouch!!!” while all other output
positions display a blank. after each line is displayed program tests for whether either variable has reached or passed square 70
if so program displays the winner and terminates the simulation. If the tortoise wins, program prints “tortoise wins!!! Yay!!!
and if the hare wins prints “Hare Wins. If both animals win on the same loop prints “It’s a tie.”
*/

using System;

class Program
{

static int tortoise = 1;
static int hare = 1;
const int finishline = 70;
const int startingGate = 1;

//main method calls the race method
static void Main()
{ race(); }

 

//method manages the race
static void race()
{

Console.WriteLine(” ON YOUR MARK,\n GET SET,\n BANG !!!!!\n AND THEY’RE OFF !!!!!”);
Console.WriteLine();

//runs at least one time then statement loops the race until either variables tortoise or //hare (or both) equal to variable finishline
do
{

//calls tortoisemethod to determine variable tortoises value after each loop
tortoisemethod();

haresmethod();//calls haresmethod to determine variable hares value after each loop

showrace();//prints the race

// Slows down race
// This is a brute force delay in the code
//allows users to view the race at a readable speed
for (int slowdown = 0; slowdown < 100000000; slowdown++) ;

} while (tortoise < finishline && hare < finishline);

//if and if else statements on lines 37-50 print the conclusion of the race
if (tortoise == hare)
{
Console.WriteLine(” \n It’s a TIE!”);
}

else if (tortoise > hare)
{
Console.WriteLine(” \n TORTOISE WINS!!! YAY!!!”);
}

else if (hare > tortoise)
{
Console.WriteLine(” \n Hare Wins.”);
}
Console.WriteLine();
}

 

//method to move the tortoise
static void tortoisemethod()
{
//generates random number 1 thru 10 to use for variable move
Random randomNumbers = new Random();
int move = randomNumbers.Next(1, 11);
/*
fast plod: 50% (1-5) = 3 forward

slow plod: 30% (6-8) = 1 forward

slip: 20% (9 or 10) = 6 back
*/

//fast plod
//if generated number is (1-5) moves tortoise 3 squares to the right (forward)
if (move >= 1 && move <= 5)
{
tortoise += 3;
}

//slow plod
//if generated number is (6-8) moves toroise 1 square to the right (forward)
else if (move >= 6 && move <= 8)
{
++tortoise;
}

//slip
//if generated number is (9 or 10) moves tortoise 6 squares to the left (backward)
else if (move == 9 || move == 10)
{
tortoise -= 6;
}

//ensures tortoise does not slip back farther than the starting gate
if (tortoise < startingGate)
{
tortoise = startingGate;
}

//ensures if tortoise goes past finishline loop still ends
else if (tortoise > finishline)
{
tortoise = finishline;
}

}

 

//method to move hare
static void haresmethod()
{
//generates random number 1 thru 10 to use for variable move
Random randomNumbers = new Random();
int move = randomNumbers.Next(1, 11);
/*
sleep : 20% (1 or 2) = no move

big hop: 20% (3 or 4) = 9 forward

big slip: 10% (5) = 12 back

small hop: 30% (6-8) = 1 forward

small slip: 20% (9 or 10) = 2 back
*/

//sleep
//if generated number is (1 or 2) no change is made to variable hare
//this statement could be ommited, but I already wrote it, so here it stays
if (move == 1 || move == 2)
{
hare += 0;
}

//big hop
//if generated number is (3 or 4) moves hare 9 squares to the right (forward)
else if (move == 3 || move == 4)
{
hare += 9;
}

//big slip
//if generated number is (5) moves hare 12 squares to the left (backward)
else if (move == 5)
{
hare -= 12;
}

//small hop
//if generated number is (6-8) moves hare 1 squares to the left (forward)
else if (move >= 5 && move <= 8)
{
++hare;
}

//small slip
//if generated number is (9 or 10) moves hare 2 squares to the left (backward)
else if (move == 9 || move == 10)
{
hare -= 2;
}

//ensures hare does not slip back farther than the starting gate
if (hare < startingGate)
{
hare = startingGate;
}

//ensures if hare goes past finishline, loop still ends
else if (hare > finishline)
{
hare = finishline;
}
}

 

//method to print the race
static void showrace()
{
//increments variable _move by 1 each loop, when _move is equal to either tortoise or //hare (or both) for said loop, prints an output
for (int _move = 1; _move <= finishline; ++_move)
{

//if variable _move is equal to both variables tortoise & hare; prints “OUCH!!!”
if (_move == tortoise && _move == hare)
{
Console.Write(“OUCH!!!”);
}

//if variable _move is equal to variable tortoise, prints “T”
else if (_move == tortoise)
{
Console.Write(“T”);
}

//if variable _move is equal to variable hare prints, “H”
else if (_move == hare)
{
Console.Write(“H”);
}

//if variable _move does not meet any if statments prints a blank
else
{
Console.Write(‘ ‘);
}
}
}
}

Program Output:
Visual C# 8.28 (Simulation: Tortoise and the Hare

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s