Simplify Train (C#) folder structure

This commit is contained in:
Zev Spitz
2022-01-17 08:44:09 +02:00
parent 79c04373f4
commit 9deb45db96
5 changed files with 7 additions and 7 deletions

View File

@@ -0,0 +1,93 @@
using System;
using System.Linq;
namespace Train
{
public class TrainGame
{
private Random Rnd { get; } = new Random();
private readonly int ALLOWED_PERCENTAGE_DIFFERENCE = 5;
static void Main()
{
TrainGame train = new TrainGame();
train.GameLoop();
}
public void GameLoop()
{
DisplayIntroText();
do
{
PlayGame();
} while (TryAgain());
}
private void PlayGame()
{
int carSpeed = (int)GenerateRandomNumber(40, 25);
int timeDifference = (int)GenerateRandomNumber(5, 15);
int trainSpeed = (int)GenerateRandomNumber(20, 19);
Console.WriteLine($"A CAR TRAVELING {carSpeed} MPH CAN MAKE A CERTAIN TRIP IN");
Console.WriteLine($"{timeDifference} HOURS LESS THAN A TRAIN TRAVELING AT {trainSpeed} MPH");
Console.WriteLine("HOW LONG DOES THE TRIP TAKE BY CAR?");
double userInputCarJourneyDuration = double.Parse(Console.ReadLine());
double actualCarJourneyDuration = CalculateCarJourneyDuration(carSpeed, timeDifference, trainSpeed);
int percentageDifference = CalculatePercentageDifference(userInputCarJourneyDuration, actualCarJourneyDuration);
if (IsWithinAllowedDifference(percentageDifference, ALLOWED_PERCENTAGE_DIFFERENCE))
{
Console.WriteLine($"GOOD! ANSWER WITHIN {percentageDifference} PERCENT.");
}
else
{
Console.WriteLine($"SORRY. YOU WERE OFF BY {percentageDifference} PERCENT.");
}
Console.WriteLine($"CORRECT ANSWER IS {actualCarJourneyDuration} HOURS.");
}
public static bool IsWithinAllowedDifference(int percentageDifference, int allowedDifference)
{
return percentageDifference <= allowedDifference;
}
private static int CalculatePercentageDifference(double userInputCarJourneyDuration, double carJourneyDuration)
{
return (int)(Math.Abs((carJourneyDuration - userInputCarJourneyDuration) * 100 / userInputCarJourneyDuration) + .5);
}
public static double CalculateCarJourneyDuration(double carSpeed, double timeDifference, double trainSpeed)
{
return timeDifference * trainSpeed / (carSpeed - trainSpeed);
}
public double GenerateRandomNumber(int baseSpeed, int multiplier)
{
return multiplier * Rnd.NextDouble() + baseSpeed;
}
private bool TryAgain()
{
Console.WriteLine("ANOTHER PROBLEM (YES OR NO)? ");
return IsInputYes(Console.ReadLine());
}
public static bool IsInputYes(string consoleInput)
{
var options = new string[] { "Y", "YES" };
return options.Any(o => o.Equals(consoleInput, StringComparison.CurrentCultureIgnoreCase));
}
private void DisplayIntroText()
{
Console.WriteLine("TRAIN");
Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
Console.WriteLine();
Console.WriteLine("TIME - SPEED DISTANCE EXERCISE");
Console.WriteLine();
}
}
}