diff --git a/Math-Game/Math-Game.slnx b/Math-Game/Math-Game.slnx new file mode 100644 index 00000000..afd3844c --- /dev/null +++ b/Math-Game/Math-Game.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Math-Game/Math-Game/Classes.cs b/Math-Game/Math-Game/Classes.cs new file mode 100644 index 00000000..056143dc --- /dev/null +++ b/Math-Game/Math-Game/Classes.cs @@ -0,0 +1,194 @@ +using System; + + +namespace Math_Game +{ + internal class Addition + { + public void Start(List games) + { + Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)"); + int numberOfQuestions = int.Parse(Console.ReadLine()); + if(numberOfQuestions < 5) + { + Console.WriteLine("You must enter a number that is 5 or greater than 5."); + ConsoleHelper.Pause(); + return; + } + Random random = new Random(); + int score = 0; + for (int i = 0; i < numberOfQuestions; i++) + { + int num1 = random.Next(1, 100); + int num2 = random.Next(1, 100); + int answer = num1 + num2; + Console.WriteLine($"What is {num1} + {num2}?"); + int userAnswer = int.Parse(Console.ReadLine()); + if (userAnswer == answer) + { + Console.WriteLine("Correct!"); + score++; + } + else + { + Console.WriteLine($"Incorrect. The correct answer is {answer}."); + } + } + Console.WriteLine($"Your score is {score} out of {numberOfQuestions}."); + games.Add($"Addition - score is {score}/{numberOfQuestions}"); + ConsoleHelper.Pause(); + + } + + } + + internal class Subtraction + { + public void Start(List games) + { + Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)"); + int numberOfQuestions = int.Parse(Console.ReadLine()); + if (numberOfQuestions < 5) + { + Console.WriteLine("You must enter a number that is 5 or greater than 5."); + ConsoleHelper.Pause(); + return; + } + Random random = new Random(); + int score = 0; + for (int i = 0; i < numberOfQuestions; i++) + { + int num1 = random.Next(1, 100); + int num2 = random.Next(1, 100); + int answer = (num1 - num2); + Console.WriteLine($"What is {num1} - {num2}?"); + int userAnswer = int.Parse(Console.ReadLine()); + if (userAnswer == answer) + { + Console.WriteLine("Correct!"); + score++; + } + else + { + Console.WriteLine($"Incorrect. The correct answer is {answer}."); + } + } + Console.WriteLine($"Your score is {score} out of {numberOfQuestions}."); + games.Add($"Subtraction - score is {score}/{numberOfQuestions}"); + ConsoleHelper.Pause(); + } + } + + internal class Multiplication + { + public void Start(List games) + { + Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)"); + int numberOfQuestions = int.Parse(Console.ReadLine()); + if (numberOfQuestions < 5) + { + Console.WriteLine("You must enter a number that is 5 or greater than 5."); + ConsoleHelper.Pause(); + return; + } + Random random = new Random(); + int score = 0; + for (int i = 0; i < numberOfQuestions; i++) + { + int num1 = random.Next(1, 100); + int num2 = random.Next(1, 100); + int answer = num1 * num2; + Console.WriteLine($"What is {num1} * {num2}?"); + int userAnswer = int.Parse(Console.ReadLine()); + if (userAnswer == answer) + { + Console.WriteLine("Correct!"); + score++; + } + else + { + Console.WriteLine($"Incorrect. The correct answer is {answer}."); + } + } + Console.WriteLine($"Your score is {score} out of {numberOfQuestions}."); + games.Add($"Multiplication - score is {score}/{numberOfQuestions}"); + ConsoleHelper.Pause(); + } + } + + internal class Division + { + public void Start(List games) + { + Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)"); + int numberOfQuestions = int.Parse(Console.ReadLine()); + if (numberOfQuestions < 5) + { + Console.WriteLine("You must enter a number that is 5 or greater than 5."); + ConsoleHelper.Pause(); + return; + } + Random random = new Random(); + int score = 0; + for (int i = 0; i < numberOfQuestions; i++) + { + int num1, num2; + do + { + num1 = random.Next(1, 101); + num2 = random.Next(1, 101); + } while (num1 % num2 != 0); + + int answer = num1 / num2; + Console.WriteLine($"What is {num1} / {num2}?"); + int userAnswer = int.Parse(Console.ReadLine()); + if (userAnswer == answer) + { + Console.WriteLine("Correct!"); + score++; + } + else + { + Console.WriteLine($"Incorrect. The correct answer is {answer}."); + } + } + Console.WriteLine($"Your score is {score} out of {numberOfQuestions}."); + games.Add($" Division - score is {score}/{numberOfQuestions}"); + ConsoleHelper.Pause(); + } + } + + internal class PreviousGameScore + { + public void Start(List games) + { + Console.WriteLine("Here are your previous game scores:"); + if (games.Count == 0) + { + Console.WriteLine("No games have been played yet."); + } + else + { + foreach (var game in games) + { + Console.WriteLine(game); + } + } + ConsoleHelper.Pause(); + + } + + } + + internal static class ConsoleHelper + { + public static void Pause() + { + Console.WriteLine(); + Console.WriteLine("Press any key to return to the main menu..."); + Console.ReadKey(true); + Console.Clear(); + } + } + +} diff --git a/Math-Game/Math-Game/Math-Game.csproj b/Math-Game/Math-Game/Math-Game.csproj new file mode 100644 index 00000000..50a3b8c4 --- /dev/null +++ b/Math-Game/Math-Game/Math-Game.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + Math_Game + enable + enable + + + diff --git a/Math-Game/Math-Game/Program.cs b/Math-Game/Math-Game/Program.cs new file mode 100644 index 00000000..796b11b4 --- /dev/null +++ b/Math-Game/Math-Game/Program.cs @@ -0,0 +1,87 @@ +using System; +using Math_Game; + + +Console.WriteLine(@" __ __ _ _ _ _ __ __ _ _ ____ _ + \ \ / /__| | ___ ___ _ __ ___ ___ | |_ ___ | |_| |__ ___ | \/ | __ _| |_| |__ / ___| __ _ _ __ ___ ___ | | + \ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \ | __/ _ \ | __| '_ \ / _ \ | |\/| |/ _` | __| '_ \| | _ / _` | '_ ` _ \ / _ \ | | + \ V V / __/ | (_| (_) | | | | | | __/ | || (_) | | |_| | | | __/ | | | | (_| | |_| | | | |_| | (_| | | | | | | __/ |_| + \_/\_/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ \__|_| |_|\___| |_| |_|\__,_|\__|_| |_|\____|\__,_|_| |_| |_|\___| (_) +"); + + + + + + +Console.WriteLine("Please Enter your Name"); +string name = Console.ReadLine(); +bool isPlaying = true; + +var games = new List(); + +while (isPlaying) +{ + + Console.WriteLine($"Hello, {name}! Let's start the game."); + Console.WriteLine("What Game would you like to play?"); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Subtraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.WriteLine("5. Previous Game Score"); + Console.WriteLine("6. Exit"); + + int choice; + while (true) + { + string rawInput = Console.ReadLine()?.Trim(); + if (string.IsNullOrWhiteSpace(rawInput)) + { + Console.WriteLine("Input was empty. Please enter a number between 1 and 6."); + continue; + } + if (!int.TryParse(rawInput, out choice) || choice < 1 || choice > 6) + { + Console.WriteLine("Invalid selection. Please enter a number between 1 and 6."); + continue; + } + break; + } + switch (choice) + { + case 1: + Addition addition = new Addition(); + addition.Start(games); + break; + case 2: + Subtraction subtraction = new Subtraction(); + subtraction.Start(games); + break; + case 3: + Multiplication multiplication = new Multiplication(); + multiplication.Start(games); + break; + case 4: + Division division = new Division(); + division.Start(games); + break; + + case 5: + PreviousGameScore previousGameScore = new PreviousGameScore(); + previousGameScore.Start(games); + break; + + case 6: + Console.WriteLine("Thank you for playing!"); + isPlaying = false; + break; + + + + default: + Console.WriteLine("Invalid choice. Please try again."); + ConsoleHelper.Pause(); + break; + } +}