From 11d3b0a16d46105e8af3b11a541a215d4dbd8b71 Mon Sep 17 00:00:00 2001 From: hakonRein Date: Thu, 29 Oct 2026 01:51:54 +0100 Subject: [PATCH 1/2] Finished project --- MathGame.hakonRein/MathGame.hakonRein.slnx | 3 + .../MathGame.hakonRein.csproj | 10 ++ .../MathGame.hakonRein/Program.cs | 128 ++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 MathGame.hakonRein/MathGame.hakonRein.slnx create mode 100644 MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj create mode 100644 MathGame.hakonRein/MathGame.hakonRein/Program.cs diff --git a/MathGame.hakonRein/MathGame.hakonRein.slnx b/MathGame.hakonRein/MathGame.hakonRein.slnx new file mode 100644 index 00000000..c77eeba5 --- /dev/null +++ b/MathGame.hakonRein/MathGame.hakonRein.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj b/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj new file mode 100644 index 00000000..d20809e3 --- /dev/null +++ b/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.hakonRein/MathGame.hakonRein/Program.cs b/MathGame.hakonRein/MathGame.hakonRein/Program.cs new file mode 100644 index 00000000..d2ab993f --- /dev/null +++ b/MathGame.hakonRein/MathGame.hakonRein/Program.cs @@ -0,0 +1,128 @@ +/* + * A simple math game that allows the user to select an operation and perform calculations. + */ + +int menuChoice = 0; +int totalPoints = 0; +List history = new List(); + +string difficulty = "easy"; +const int EASY_RANGE = 10; +const int DIFFICULT_RANGE = 100; + +// Used to store operations for the random operator option +string[] operations = { "+", "-", "*", "/" }; + +Random numberGenerator = new Random(); + +do +{ + DisplayMenu(); + while (!int.TryParse(Console.ReadLine(), out menuChoice) || menuChoice < 1 || menuChoice > 8) + { + Console.Write("Invalid input. Please enter a number between 1 and 8: "); + } + + int num1 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + int num2 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + int correctAnswer; + string operation = ""; + + switch (menuChoice) + { + case 1: + operation = "+"; + break; + case 2: + operation = "-"; + break; + case 3: + operation = "*"; + break; + case 4: + operation = "/"; + break; + case 5: + // Handle random operator + operation = operations[numberGenerator.Next(0, operations.Length)]; + break; + case 6: + Console.WriteLine("\nHistory of calculations:"); + foreach (var record in history) + { + Console.WriteLine(record); + } + break; + case 7: + difficulty = difficulty == "easy" ? "difficult" : "easy"; + Console.WriteLine($"Changed difficulty to {difficulty}"); + break; + case 8: + Console.WriteLine("Exiting the game. Thank you for playing!"); + break; + } + + // Ensure that division problems are valid (no division by zero and integer results) + if (operation == "/") + { + num2 = numberGenerator.Next(1, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + num1 = num2 * numberGenerator.Next(0, 10); + } + + // Calculate the correct answer and check this against the user's guess + if (menuChoice >= 1 && menuChoice <= 5) + { + correctAnswer = Calculate(num1, num2, operation); + Console.Write($"\n{num1} {operation} {num2} = "); + int userAnswer; + while (!int.TryParse(Console.ReadLine(), out userAnswer)) + { + Console.Write("Invalid input. Please enter a valid integer: "); + } + if (userAnswer == correctAnswer) + { + Console.WriteLine("Correct!"); + totalPoints++; + history.Add($"{num1} {operation} {num2} = {correctAnswer} (Correct)"); + } + else + { + Console.WriteLine($"Incorrect. The correct answer is {correctAnswer}."); + history.Add($"{num1} {operation} {num2} = {userAnswer} (Incorrect - Correct answer: {correctAnswer})"); + } + Console.WriteLine($"Total points: {totalPoints}"); + } + +} while (menuChoice != 8); + + +int Calculate(int num1, int num2, string operation) +{ + switch (operation) + { + case "+": + return num1 + num2; + case "-": + return num1 - num2; + case "*": + return num1 * num2; + case "/": + return num1 / num2; + default: + throw new ArgumentException("Invalid operator"); + } +} + +void DisplayMenu() +{ + Console.WriteLine("\nSelect operation:"); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Subtraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.WriteLine("5. Random operator"); + Console.WriteLine("6. Show history"); + Console.WriteLine("7. Change difficulty to " + (difficulty == "easy" ? "difficult" : "easy")); + Console.WriteLine("8. Exit"); + Console.Write("Your choice (1-8): "); +} \ No newline at end of file From 4485aae9cd568dc3654c413638a4b31978c6abcc Mon Sep 17 00:00:00 2001 From: hakonRein Date: Thu, 29 Oct 2026 11:47:27 +0100 Subject: [PATCH 2/2] Add support for 5 questions per game --- .../MathGame.hakonRein/Program.cs | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/MathGame.hakonRein/MathGame.hakonRein/Program.cs b/MathGame.hakonRein/MathGame.hakonRein/Program.cs index d2ab993f..1c9e28be 100644 --- a/MathGame.hakonRein/MathGame.hakonRein/Program.cs +++ b/MathGame.hakonRein/MathGame.hakonRein/Program.cs @@ -9,6 +9,7 @@ string difficulty = "easy"; const int EASY_RANGE = 10; const int DIFFICULT_RANGE = 100; +const int QUESTIONS_PER_GAME = 5; // Used to store operations for the random operator option string[] operations = { "+", "-", "*", "/" }; @@ -23,8 +24,8 @@ Console.Write("Invalid input. Please enter a number between 1 and 8: "); } - int num1 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); - int num2 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + int num1; + int num2; int correctAnswer; string operation = ""; @@ -62,35 +63,42 @@ break; } - // Ensure that division problems are valid (no division by zero and integer results) - if (operation == "/") - { - num2 = numberGenerator.Next(1, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); - num1 = num2 * numberGenerator.Next(0, 10); - } - - // Calculate the correct answer and check this against the user's guess + // Do calculations only if the user selected a calculation operation (1-5) if (menuChoice >= 1 && menuChoice <= 5) - { - correctAnswer = Calculate(num1, num2, operation); - Console.Write($"\n{num1} {operation} {num2} = "); - int userAnswer; - while (!int.TryParse(Console.ReadLine(), out userAnswer)) + { + for (int i = 1; i <= QUESTIONS_PER_GAME; i++) { - Console.Write("Invalid input. Please enter a valid integer: "); - } - if (userAnswer == correctAnswer) - { - Console.WriteLine("Correct!"); - totalPoints++; - history.Add($"{num1} {operation} {num2} = {correctAnswer} (Correct)"); - } - else - { - Console.WriteLine($"Incorrect. The correct answer is {correctAnswer}."); - history.Add($"{num1} {operation} {num2} = {userAnswer} (Incorrect - Correct answer: {correctAnswer})"); + num1 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + num2 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + + // Ensure that division problems are valid (no division by zero and integer results) + if (operation == "/") + { + num2 = numberGenerator.Next(1, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); + num1 = num2 * numberGenerator.Next(0, 10); + } + + // Calculate the correct answer and check this against the user's guess + correctAnswer = Calculate(num1, num2, operation); + Console.Write($"\nQ{i}: {num1} {operation} {num2} = "); + int userAnswer; + while (!int.TryParse(Console.ReadLine(), out userAnswer)) + { + Console.Write("Invalid input. Please enter a valid integer: "); + } + if (userAnswer == correctAnswer) + { + Console.WriteLine("Correct!"); + totalPoints++; + history.Add($"{num1} {operation} {num2} = {correctAnswer} (Correct)"); + } + else + { + Console.WriteLine($"Incorrect. The correct answer is {correctAnswer}."); + history.Add($"{num1} {operation} {num2} = {userAnswer} (Incorrect - Correct answer: {correctAnswer})"); + } + Console.WriteLine($"Total points: {totalPoints}"); } - Console.WriteLine($"Total points: {totalPoints}"); } } while (menuChoice != 8);