In: Computer Science
Please code in C# (C-Sharp)
Assignment Description
A pirate needs to do some accounting and has asked for your help. Write a program that will accept a
pirate’s starting amount of treasure in number of gold pieces. The program will then run one of two
simulations, indicated by the user:
1) The first simulation runs indefinitely, until one of two conditions is met: the pirate’s treasure
falls to 0 or below, or the pirate’s treasure grows to 1000 or above.
2) The second simulation runs for a number of years set by the user.
For both simulations, each year the pirate has an equal chance to either gain or lose 50 gold pieces. At
the end of each year, the pirate’s total gold and the year is displayed to the user. Validate all user input.
Tasks
1) The program needs to contain the following
a.
A comment header containing your name and a brief description of the program
b. At least 5 comments besides the comment header throughout your code
c.
A prompt for the starting treasure amount
d. A prompt to choose a simulation
i. The first simulation continues until the treasure amount becomes 1000 or more
or 0 or less
ii. The second simulation will prompt the user for number of years, then simulate
that many years. The gold amount can go below zero
e. For both simulations, treasure amount has an equal chance to increase by 50 or
decrease by 50 each year
f.
Output the year and treasure amount after each year
g.
“Press enter to continue” and Console.ReadLine(); at the end of your code
h. Validate all user input. Either through exception handling or boolean logic.
2) Upload a completed .cs file onto the Assignment 5 submission folder and a word document
containing the following six (6) screenshots:
a.
One run of the first simulation, starting amount 500
i. Your screenshot only needs to show up to the last 10 years
b. Two runs of the second simulation, starting amount 300 for 10 years and starting
amount 500 for 20 years
c.
Three test runs with invalid input for the following: starting treasure amount, simulation
choice, number of years
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
using System;
using System.Collections.Generic;
public class Program
{
//Random object for random number
generation
static Random random=new
Random();
//method to run the first type simulation,
given the number of starting
//pieces of gold
public static void
runSimulation1(int pieces){
int
year=0;
//displaying gold
at initial year
Console.WriteLine("Year:
"+year+", Gold: "+pieces);
//looping until
pieces becomes 0 or negative or 1000 or above
while(pieces>0
&& pieces<1000){
//generating
an integer between 0 and 99
if(random.Next(100)<50){
//if
the number is less than 50 (50%), subtracting 50 pieces
pieces-=50;
}else{
//otherwise,
adding 50 pieces
pieces+=50;
}
//updating
year and displaying current values
year++;
Console.WriteLine("Year:
"+year+", Gold: "+pieces);
}
}
//method to run the second type simulation,
given initial gold value and
//number of years to simulate
public static void
runSimulation2(int pieces,
int years){
//displaying gold
at initial year
Console.WriteLine("Year:
0, Gold: "+pieces);
//looping for years
number of times
for(int
year=1;year<=years;year++){
//repeating
the same process as the below method
if(random.Next(100)<50){
pieces-=50;
}else{
pieces+=50;
}
Console.WriteLine("Year:
"+year+", Gold: "+pieces);
}
}
//helper method to read a valid positive
integer input from user
static int
getInt(string prompt){
bool
done=false;
int
num=0;
//looping until
done is true
while(!done){
//displaying
prompt
Console.Write(prompt);
//checking
if next line contains a valid number, if yes, reading
//to
num
if(int.TryParse(Console.ReadLine(),out
num)){
if(num>0){
//done,
end of loop
done=true;
}else{
//negative
value
Console.WriteLine("Value
must be positive.");
}
}else{
//non
numeric input
Console.WriteLine("Value
must be an integer");
}
}
//returning the
value
return num;
}
public static void
Main(){
//reading initial
number of gold
int
gold=getInt("Enter a starting amount of gold:
");
//displaying
simulation options
Console.WriteLine("1.
Infinite simulation until gold reaches 0 or 1000");
Console.WriteLine("2.
Simulation for specified number of years");
//reading
choice
int
choice=getInt("Your choice: ");
//identifying
choice
if(choice==1){
//running
simulation 1
runSimulation1(gold);
}else
if(choice==2){
//reading
years and running simulation 2
int
years=getInt("Please enter the number of years:
");
runSimulation2(gold,years);
}else{
//no
other choices are accepted
Console.WriteLine("That's
an invalid option!");
}
Console.WriteLine("\nPress
enter to continue");
Console.ReadLine();
}
}
/*OUTPUT (multiple runs)*/