Question: I need a main function for my code im kind of stuck Tourtise and hare race code below using System; public abstract class RaceAnimal {
I need a main function for my code im kind of stuck Tourtise and hare race
code below
using System;
public abstract class RaceAnimal { protected string track; protected char symbol; protected int position; protected string name;
public RaceAnimal(string name, char symbol) { this.name = name; this.symbol = symbol; track = new string('-', 70); track = track.Substring(0, position) + symbol + track.Substring(position + 1); }
public override string ToString() { return track; }
public void ChangePos(int steps) { int newPosition = position + steps; if (newPosition < 0) { newPosition = 0; } else if (newPosition > 69) { newPosition = 69; } track = track.Substring(0, position) + "-" + track.Substring(position + 1); position = newPosition; track = track.Substring(0, position) + symbol + track.Substring(position + 1); }
public abstract void Move(); }
public class Hare : RaceAnimal { public Hare(string name, char symbol) : base(name, symbol) { }
public override void Move() { int moveType = new Random().Next(10);
if (moveType < 2) { ChangePos(-12); } else if (moveType < 5) { ChangePos(-2); } else if (moveType < 8) { ChangePos(9); } else { ChangePos(1); } } }
public class Tortoise : RaceAnimal { public Tortoise(string name, char symbol) : base(name, symbol) { }
public override void Move() { int moveType = new Random().Next(10);
if (moveType < 5) { ChangePos(3); } else if (moveType < 7) { ChangePos(-6); } else { ChangePos(1); } } }
public class Race { private RaceAnimal animal1; private RaceAnimal animal2; private RaceAnimal winner; private bool over;
public Race(RaceAnimal animal1, RaceAnimal animal2) { this.animal1 = animal1; this.animal2 = animal2; winner = null; over = false; }
public void UpdateStatus() { if (animal1.position >= 69 && animal2.position >= 69) { over = true; winner = null; } else if (animal1.position >= 69) { over = true; winner = animal1; } else if (animal2.position >= 69) { over = true; winner = animal2; } }
public void Simulate() { while (!over) { Console.WriteLine(animal1.ToString()); Console.WriteLine(animal2.ToString()); animal1.Move(); animal2.Move(); System.Threading.Thread.Sleep(1000); Console.Clear(); UpdateStatus(); }
Console.WriteLine(animal1.ToString()); Console.WriteLine(animal2.ToString());
if (winner == null) { Console.WriteLine("The race was a tie!"); } else { Console.WriteLine(winner.name + " wins the race!"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
