From 53113b9a3a39d563a691ef1aeca6deb07183e59b Mon Sep 17 00:00:00 2001 From: Yelyzaveta Aksionova Date: Fri, 13 Nov 2026 22:58:22 +0200 Subject: [PATCH 1/4] Remove implementation --- MathGame.hakonRein/MathGame.hakonRein.slnx | 3 - .../MathGame.hakonRein.csproj | 10 -- .../MathGame.hakonRein/Program.cs | 128 ------------------ 3 files changed, 141 deletions(-) delete mode 100644 MathGame.hakonRein/MathGame.hakonRein.slnx delete mode 100644 MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj delete mode 100644 MathGame.hakonRein/MathGame.hakonRein/Program.cs diff --git a/MathGame.hakonRein/MathGame.hakonRein.slnx b/MathGame.hakonRein/MathGame.hakonRein.slnx deleted file mode 100644 index c77eeba5..00000000 --- a/MathGame.hakonRein/MathGame.hakonRein.slnx +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj b/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj deleted file mode 100644 index d20809e3..00000000 --- a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - Exe - net10.0 - enable - enable - - - diff --git a/MathGame.hakonRein/MathGame.hakonRein/Program.cs b/MathGame.hakonRein/MathGame.hakonRein/Program.cs deleted file mode 100644 index d2ab993f..00000000 --- a/MathGame.hakonRein/MathGame.hakonRein/Program.cs +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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 70cc5a29e19b03e693f1471e63ce5bfcbba62c70 Mon Sep 17 00:00:00 2001 From: Yelyzaveta Aksionova Date: Sat, 14 Nov 2026 00:57:46 +0200 Subject: [PATCH 2/4] Basic implementation --- MathGame/Game.cs | 86 ++++++++++++++++++++++++++++++++++++++++ MathGame/MathGame.csproj | 10 +++++ MathGame/Menu.cs | 45 +++++++++++++++++++++ MathGame/Operation.cs | 9 +++++ MathGame/Program.cs | 28 +++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 MathGame/Game.cs create mode 100644 MathGame/MathGame.csproj create mode 100644 MathGame/Menu.cs create mode 100644 MathGame/Operation.cs create mode 100644 MathGame/Program.cs diff --git a/MathGame/Game.cs b/MathGame/Game.cs new file mode 100644 index 00000000..d5b108e6 --- /dev/null +++ b/MathGame/Game.cs @@ -0,0 +1,86 @@ +namespace MathGame; + +public static class Game +{ + private static List games = []; + private readonly static string[] mathOperators = ["+", "-", "*", "/"]; + private static readonly Random random = new(); + + public static void PlayRound() + { + int points = 0; + + for (int i = 0; i < 5; i++) + { + Operation operation = Menu.ChooseOperation(); + SolveEquation(ref points, operation); + } + + Console.WriteLine($"\nPoints for this round: {points}"); + + games.Add(points); + } + + public static void ShowHistory() + { + if (games.Count == 0) + Console.WriteLine("No games yet"); + + for (int i = 0; i < games.Count; i++) + Console.WriteLine($"Points gained in {i + 1} game : {games[i]}"); + + Console.WriteLine(); + } + + private static (int, int) GenerateNumbers(Operation operation) + { + int num1 = random.Next(0, 10); + int num2 = random.Next(0, 10); + + if (operation == Operation.Division && (num2 == 0 || num1 % num2 != 0 || num1 > num2)) + return GenerateNumbers(operation); + + return (num1, num2); + } + + private static void SolveEquation(ref int points, Operation operation) + { + (int num1, int num2) = GenerateNumbers(operation); + + Console.Write($"{num1} {mathOperators[(int)operation - 1]} {num2} = "); + + int result = GetAnswer(); + + switch (operation) + { + case Operation.Addition: + if (result == num1 + num2) + points++; + break; + case Operation.Substraction: + if (result == num1 - num2) + points++; + break; + case Operation.Multiplication: + if (result == num1 * num2) + points++; + break; + case Operation.Division: + if (result == num1 / num2) + points++; + break; + } + } + + private static int GetAnswer() + { + bool userInput = int.TryParse(Console.ReadLine(), out int result); + + if (!userInput) + { + Console.Write("Incorrect format. Try again: "); + return GetAnswer(); + } + return result; + } +} \ No newline at end of file diff --git a/MathGame/MathGame.csproj b/MathGame/MathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame/Menu.cs b/MathGame/Menu.cs new file mode 100644 index 00000000..697ec22f --- /dev/null +++ b/MathGame/Menu.cs @@ -0,0 +1,45 @@ +namespace MathGame; + +public static class Menu +{ + public static string StartMenu() + { + string? input; + + Console.WriteLine("Play"); + Console.WriteLine("History"); + Console.WriteLine("Exit"); + + Console.Write("Enter your choise => "); + + input = Console.ReadLine(); + if (input is null) + { + Console.WriteLine("Your input is incorrect"); + return StartMenu(); + } + + return input; + } + public static Operation ChooseOperation() + { + string? input; + + Console.WriteLine(); + Console.WriteLine("Choose an operation: "); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Substraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.Write("Enter your choise => "); + + input = Console.ReadLine(); + if (input == string.Empty || !Enum.TryParse(input, out Operation operation) || !Enum.IsDefined(operation)) + { + Console.WriteLine("Your input is incorrect"); + return ChooseOperation(); + } + + return operation; + } +} \ No newline at end of file diff --git a/MathGame/Operation.cs b/MathGame/Operation.cs new file mode 100644 index 00000000..f4d57692 --- /dev/null +++ b/MathGame/Operation.cs @@ -0,0 +1,9 @@ +namespace MathGame; + +public enum Operation +{ + Addition = 1, + Substraction = 2, + Multiplication = 3, + Division = 4 +} diff --git a/MathGame/Program.cs b/MathGame/Program.cs new file mode 100644 index 00000000..47b2cebc --- /dev/null +++ b/MathGame/Program.cs @@ -0,0 +1,28 @@ +using MathGame; + +Console.WriteLine("Welcome to math game!"); + +string input; +do +{ + input = Menu.StartMenu().Trim().ToLower(); + + switch (input) + { + case "play": + Console.Clear(); + Game.PlayRound(); + break; + case "history": + Console.Clear(); + Game.ShowHistory(); + break; + case "exit": + Console.WriteLine("Thanks for the game!"); + break; + default: + Console.WriteLine("Your input is incorrect"); + break; + } + +} while (input != "exit"); From aa14a65c0d8216aba893fc307681e79b384973c1 Mon Sep 17 00:00:00 2001 From: Yelyzaveta Aksionova Date: Sat, 14 Nov 2026 01:21:57 +0200 Subject: [PATCH 3/4] Implementation of difficulty levels --- MathGame/DifficultyLevel.cs | 8 ++++++++ MathGame/Game.cs | 22 ++++++++++++++++++++-- MathGame/Menu.cs | 20 ++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 MathGame/DifficultyLevel.cs diff --git a/MathGame/DifficultyLevel.cs b/MathGame/DifficultyLevel.cs new file mode 100644 index 00000000..8c222407 --- /dev/null +++ b/MathGame/DifficultyLevel.cs @@ -0,0 +1,8 @@ +namespace MathGame; + +public enum DifficultyLevel +{ + Easy = 1, + Intermediate = 2, + Advanced = 3 +} diff --git a/MathGame/Game.cs b/MathGame/Game.cs index d5b108e6..174e6836 100644 --- a/MathGame/Game.cs +++ b/MathGame/Game.cs @@ -5,11 +5,14 @@ public static class Game private static List games = []; private readonly static string[] mathOperators = ["+", "-", "*", "/"]; private static readonly Random random = new(); + private static DifficultyLevel difficultyLevel = DifficultyLevel.Easy; public static void PlayRound() { int points = 0; + difficultyLevel = Menu.ChooseOperationDifficulty(); + for (int i = 0; i < 5; i++) { Operation operation = Menu.ChooseOperation(); @@ -34,8 +37,23 @@ public static void ShowHistory() private static (int, int) GenerateNumbers(Operation operation) { - int num1 = random.Next(0, 10); - int num2 = random.Next(0, 10); + int num1 = 0, num2 = 0; + switch (difficultyLevel) + { + case DifficultyLevel.Easy: + num1 = random.Next(0, 10); + num2 = random.Next(0, 10); + break; + case DifficultyLevel.Intermediate: + num1 = random.Next(0, 100); + num2 = random.Next(0, 100); + break; + case DifficultyLevel.Advanced: + num1 = random.Next(0, 1000); + num2 = random.Next(0, 1000); + break; + } + if (operation == Operation.Division && (num2 == 0 || num1 % num2 != 0 || num1 > num2)) return GenerateNumbers(operation); diff --git a/MathGame/Menu.cs b/MathGame/Menu.cs index 697ec22f..219db96b 100644 --- a/MathGame/Menu.cs +++ b/MathGame/Menu.cs @@ -42,4 +42,24 @@ public static Operation ChooseOperation() return operation; } + public static DifficultyLevel ChooseOperationDifficulty() + { + string? input; + + Console.Clear(); + Console.WriteLine("Choose an operation difficulty level: "); + Console.WriteLine("1. Easy"); + Console.WriteLine("2. Intermediate"); + Console.WriteLine("3. Advanced"); + Console.Write("Enter your choise => "); + + input = Console.ReadLine(); + if (input == string.Empty || !Enum.TryParse(input, out DifficultyLevel difficultyLevel) || !Enum.IsDefined(difficultyLevel)) + { + Console.WriteLine("Your input is incorrect"); + return ChooseOperationDifficulty(); + } + + return difficultyLevel; + } } \ No newline at end of file From 215a4a99fb2cb9ff2ab00cc1898111f292e97743 Mon Sep 17 00:00:00 2001 From: Yelyzaveta Aksionova Date: Sat, 14 Nov 2026 01:35:17 +0200 Subject: [PATCH 4/4] Elapsed time measurement --- MathGame/Game.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/MathGame/Game.cs b/MathGame/Game.cs index 174e6836..846b8e04 100644 --- a/MathGame/Game.cs +++ b/MathGame/Game.cs @@ -1,8 +1,10 @@ +using System.Diagnostics; + namespace MathGame; public static class Game { - private static List games = []; + private static List<(int, TimeSpan)> games = []; private readonly static string[] mathOperators = ["+", "-", "*", "/"]; private static readonly Random random = new(); private static DifficultyLevel difficultyLevel = DifficultyLevel.Easy; @@ -13,15 +15,20 @@ public static void PlayRound() difficultyLevel = Menu.ChooseOperationDifficulty(); + Stopwatch sw = Stopwatch.StartNew(); + for (int i = 0; i < 5; i++) { Operation operation = Menu.ChooseOperation(); SolveEquation(ref points, operation); } + sw.Stop(); + Console.WriteLine($"\nPoints for this round: {points}"); + Console.WriteLine($"Time spent: {sw.Elapsed:hh\\:mm\\:ss\\.fff}"); - games.Add(points); + games.Add((points, sw.Elapsed)); } public static void ShowHistory() @@ -30,8 +37,10 @@ public static void ShowHistory() Console.WriteLine("No games yet"); for (int i = 0; i < games.Count; i++) - Console.WriteLine($"Points gained in {i + 1} game : {games[i]}"); - + { + Console.WriteLine($"Points gained in {i + 1} game : {games[i].Item1}"); + Console.WriteLine($"Time spent on {i + 1} game : {games[i].Item2:hh\\:mm\\:ss\\.fff}"); + } Console.WriteLine(); }