Question
In C#, use the following code to make a new version of the program that meets the following guidelines: 1. Make sure the project runs.
In C#, use the following code to make a new version of the program that meets the following guidelines:
1. Make sure the project runs. The Caterpillars are red and the plants are green. The Caterpillars keep eating any plant that they land on until it is gone.
2. Make each type use a different character. Currently, both use *. Try to make this change in the parent class (SimObject).
3. Make the plants reproduce if their amount is over a certain value like 12. When a plant does reproduce, reduce it's amount to a lower value since reproducing takes away from the original plant.
4. Make the Caterpillars able to reproduce.
5. Make the Caterpillars able to starve.
With some tweaking of the numbers for reproducing and starving, you might get a simulation of predator and prey scenerios. For some extra street cred, add a cool feature to your sim.
Below are the separate folders that contain the given code for Version 1. There are 2 folders, SimulationObj.cs and Program.cs. Remember that this is to be done in C#. Thank you in advance for your help!!!
SimulationObj.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1 { // ---------------------------------------------------------------- // Everything should be column, row // ---------------------------------------------------------------- public class SimObject { private int mRow, mColumn; public int Column { get { return mColumn; } set { if (value >= 0) mColumn = value; else throw new ArgumentOutOfRangeException("Column cannot be negative"); } } public int Row { get { return mRow; } set { if (value >= 0 && value < Console.WindowHeight) mRow = value; else throw new ArgumentOutOfRangeException("Row cannot be negative"); } }
public SimObject(int aColumn, int aRow, ConsoleColor aColor = ConsoleColor.White) { Column = aColumn; Row = aRow; Color = aColor; } // Allow each child to have a unique color public ConsoleColor Color { get; set; }
public override string ToString() { return "SimObject at " + Column + ", " + Row; }
public void Draw() { // Draw the character to the screen in the correct position Console.SetCursorPosition(Column, Row); Console.ForegroundColor = Color; Console.Write("*"); // hard-coded the character - its the same for each child :( }
public virtual void Turn(List
public class Plant : SimObject { private int mAmount; public int Amount { get { return mAmount; } set { if (value >= 0) mAmount = value; else mAmount = 0;//throw new ArgumentOutOfRangeException("Amount of plant cannot be negative"); } }
public Plant(int aColumn, int aRow, int anAmount = 5) : base(aColumn, aRow, ConsoleColor.DarkGreen) // Use DarkGreen for plants { Amount = anAmount; }
public override string ToString() { return "Plant at " + Column + ", " + Row + " with amount = " + Amount; }
public override void Turn(List
// Add a new plant to theList when amount > 12 }
public void Eat() { if (Amount > 0) Amount = Amount - 2; } }
public class Caterpillar : SimObject { private static Random aRan = new Random();
public Caterpillar(int aColumn, int aRow, int aSpeed = 1) : base(aColumn, aRow, ConsoleColor.Red) { Speed = aSpeed; }
private int mSpeed; public int Speed { get { return mSpeed; } set { if (value >= 0) mSpeed = value; else throw new ArgumentOutOfRangeException("Speed of caterpillar cannot be negative"); } }
public override void Turn(List
if (so is Plant) // it's a plant, so don't move the caterpillar away from it { Plant p = (Plant)so; if (p.Amount > 0) // eat some of the plant if any is left p.Eat(); else if (p.Amount == 0) // if no plant is left, remove the plant from theList (this is the simulation's list) theList.Remove(p); } else //if (so == null) // no other object in this space, so try moving to a new space without another caterpillar { // Move randomly but not off the screen's edges int newCol = Column + aRan.Next(-2, 3); int newRow = Row + aRan.Next(-2, 3); while (newCol < 0 || newCol >= Console.WindowWidth || newRow < 0 || newRow >= Console.WindowHeight) { newCol = Column + aRan.Next(-2, 3); newRow = Row + aRan.Next(-2,3); }
// make sure the new space is empty or has a plant (e.g. don't move on top of another caterpillar) so = theList.Find(x => this != x && x.Row == newRow && x.Column == newCol && x.Color != Color); if (so == null || (so is Plant)) { Column = newCol; Row = newRow; } } }
public override string ToString() { return "Caterpillar at " + Row + ", " + Column + " with speed = " + Speed; } }
}
Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // Create the list of parent objects List
// Adds some plants const int NUM_PLANTS = 250; for (int i = 0; i < NUM_PLANTS; i++) theObjects.Add(new Plant(aRan.Next(80), aRan.Next(24))); // Add some caterpillars const int NUM_CATS = 25; for (int i = 0; i < NUM_CATS; i++) theObjects.Add(new Caterpillar(aRan.Next(78), aRan.Next(22)));
// Run the sim until the user presses a key while (Console.KeyAvailable == false) { Console.Clear(); foreach (var x in theObjects) x.Draw();
// Call each sim object's turn method for (int i = 0; i < theObjects.Count; i++) theObjects[i].Turn(theObjects);
Thread.Sleep(600); }
Console.SetCursorPosition(0, 24); Console.Write("PAK ..."); Console.ReadKey(); Console.ReadKey(); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started