From 4dabbfcba79ea764e8af88a2d2c27e6d6f745719 Mon Sep 17 00:00:00 2001 From: ianmcnerlin Date: Sun, 30 Aug 2026 17:34:09 +0100 Subject: [PATCH] Add MathGame console app --- MathGame.paddymcn/MathGame.csproj | 10 ++++ MathGame.paddymcn/MathGameSession.cs | 88 +++++++++++++++++++++++++++ MathGame.paddymcn/Program.cs | 90 ++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 MathGame.paddymcn/MathGame.csproj create mode 100644 MathGame.paddymcn/MathGameSession.cs create mode 100644 MathGame.paddymcn/Program.cs diff --git a/MathGame.paddymcn/MathGame.csproj b/MathGame.paddymcn/MathGame.csproj new file mode 100644 index 00000000..6c1dc922 --- /dev/null +++ b/MathGame.paddymcn/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.paddymcn/MathGameSession.cs b/MathGame.paddymcn/MathGameSession.cs new file mode 100644 index 00000000..4d1e50d0 --- /dev/null +++ b/MathGame.paddymcn/MathGameSession.cs @@ -0,0 +1,88 @@ +namespace MathGame; + +public class MathGameSession +{ + public int Score { get; set; } = 0; + public int Answer { get; set; } = 0; + public int GameNumber { get; set; } = 0; + + public List GameHistory = new List(); + + // Constructor + public MathGameSession() + { + + } + + // Method + public string AskQuestion(string type) + { + int rand1; + int rand2; + switch (type) + { + case "addition": + rand1 = randomNumber(0, 100); + rand2 = randomNumber(0, 100); + Answer = rand1 + rand2; + return $"What is {rand1} + {rand2}"; + break; + + case "substraction": + rand1 = randomNumber(0, 100); + rand2 = randomNumber(0, 100); + Answer = rand1 - rand2; + return $"What is {rand1} - {rand2}"; + break; + + case "multiplication": + rand1 = randomNumber(0, 100); + rand2 = randomNumber(0, 100); + Answer = rand1 * rand2; + return $"What is {rand1} * {rand2}"; + break; + + case "division": + int divisor = randomNumber(1, 10); + int quotient = randomNumber(1, 10); + int dividend = divisor * quotient; + Answer = quotient; + return $"What is {dividend} / {divisor}"; + break; + default: + Console.WriteLine("No type returned."); + break; + } + return type; + } + + public void CheckAnswer() + { + Console.WriteLine("Enter your answer:"); + string input = Console.ReadLine(); + int userAnswer; + + while (!int.TryParse(input, out userAnswer)) + { + Console.WriteLine("That's not a valid number. Please enter your answer again:"); + input = Console.ReadLine(); + } + + if (userAnswer == Answer) + { + Console.WriteLine("Answer is correct."); + Score++; + } + else + { + Console.WriteLine("Wrong answer."); + } + } + int randomNumber(int min, int max) + { + var random = new Random(); + var rNum = random.Next(min, max); + return rNum; + } +} + \ No newline at end of file diff --git a/MathGame.paddymcn/Program.cs b/MathGame.paddymcn/Program.cs new file mode 100644 index 00000000..8d27bbb6 --- /dev/null +++ b/MathGame.paddymcn/Program.cs @@ -0,0 +1,90 @@ +using MathGame; +MathGameSession gameSession = new(); +DisplayMenu(); +void DisplayMenu() +{ + int userAnswer=0; + string menuChoice = ""; + while (menuChoice != "6") + { + Console.WriteLine("Menu\n===="); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Subtraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.WriteLine("5. View History"); + Console.WriteLine("6. Exit"); + menuChoice = Console.ReadLine(); + MenuChoice(menuChoice); + } + void MenuChoice(string menuChoice) + { + + switch (menuChoice) + { + case "1": + // Addition game + for (int i = 0; i < 5; i++) + { + Console.WriteLine(gameSession.AskQuestion("addition")); + gameSession.CheckAnswer(); + } + gameSession.GameNumber++; + gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}"); + gameSession.Score = 0; + break; + + case "2": + // Subtraction game + for (int i = 0; i < 5; i++) + { + Console.WriteLine(gameSession.AskQuestion("substraction")); + gameSession.CheckAnswer(); + } + gameSession.GameNumber++; + gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}"); + gameSession.Score = 0; + break; + + case "3": + // Multiplication game + for (int i = 0; i < 5; i++) + { + Console.WriteLine(gameSession.AskQuestion("multiplication")); + gameSession.CheckAnswer(); + } + gameSession.GameNumber++; + gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}"); + gameSession.Score = 0; + break; + + case "4": + // Division + for (int i = 0; i < 5; i++) + { + Console.WriteLine(gameSession.AskQuestion("division")); + gameSession.CheckAnswer(); + } + gameSession.GameNumber++; + gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}"); + gameSession.Score = 0; + break; + + case "5": + foreach (var list in gameSession.GameHistory) + { + Console.WriteLine(list); + } + break; + + case "6": + Console.WriteLine("Goodbye!"); + break; + + default: + Console.WriteLine("Invalid choice, try again."); + break; + } + } +} +