From 10b6b87e16a6ffa321fbee1aa7adebb8d97c6c3e Mon Sep 17 00:00:00 2001 From: David Date: Wed, 14 Oct 2026 19:16:22 +0300 Subject: [PATCH 1/2] Mathgame --- .../.vscode/c_cpp_properties.json | 18 +++ MathGame.whoami3222/.vscode/launch.json | 24 ++++ MathGame.whoami3222/.vscode/settings.json | 59 ++++++++ .../CodeReviews.Console.MathGame | 1 + MathGame.whoami3222/MathGame.csproj | 10 ++ MathGame.whoami3222/Program.cs | 133 ++++++++++++++++++ 6 files changed, 245 insertions(+) create mode 100644 MathGame.whoami3222/.vscode/c_cpp_properties.json create mode 100644 MathGame.whoami3222/.vscode/launch.json create mode 100644 MathGame.whoami3222/.vscode/settings.json create mode 160000 MathGame.whoami3222/CodeReviews.Console.MathGame create mode 100644 MathGame.whoami3222/MathGame.csproj create mode 100644 MathGame.whoami3222/Program.cs diff --git a/MathGame.whoami3222/.vscode/c_cpp_properties.json b/MathGame.whoami3222/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..cea4d3f4 --- /dev/null +++ b/MathGame.whoami3222/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/MathGame.whoami3222/.vscode/launch.json b/MathGame.whoami3222/.vscode/launch.json new file mode 100644 index 00000000..d8855e02 --- /dev/null +++ b/MathGame.whoami3222/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/cSharpProjects/MathGame", + "program": "c:/cSharpProjects/MathGame/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/MathGame.whoami3222/.vscode/settings.json b/MathGame.whoami3222/.vscode/settings.json new file mode 100644 index 00000000..bb879da5 --- /dev/null +++ b/MathGame.whoami3222/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/MathGame.whoami3222/CodeReviews.Console.MathGame b/MathGame.whoami3222/CodeReviews.Console.MathGame new file mode 160000 index 00000000..2625a623 --- /dev/null +++ b/MathGame.whoami3222/CodeReviews.Console.MathGame @@ -0,0 +1 @@ +Subproject commit 2625a623bf32d6aae5604217158059dfd4429c04 diff --git a/MathGame.whoami3222/MathGame.csproj b/MathGame.whoami3222/MathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame.whoami3222/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.whoami3222/Program.cs b/MathGame.whoami3222/Program.cs new file mode 100644 index 00000000..6ccc9a91 --- /dev/null +++ b/MathGame.whoami3222/Program.cs @@ -0,0 +1,133 @@ +using System.Diagnostics; +int points = 0; +List history = new List(); +MathGame(); + + +void MathGame() +{ + DisplayMenu(); + string? input = Console.ReadLine(); + HandleMenuChoice(input); + +} + +void DisplayMenu() +{ + Console.WriteLine("Choose from the menu below:"); + Console.WriteLine("1. Start Game"); + Console.WriteLine("2. Display History"); + Console.WriteLine("3. Exit"); +} + +void HandleMenuChoice(string? choice) +{ + switch (choice) + { + case "1": + StartGame(); + break; + case "2": + DisplayList(); + break; + case "3": + Console.WriteLine("Exiting the game. Goodbye!"); + Environment.Exit(0); + break; + default: + Console.WriteLine("Invalid choice. Please try again."); + break; + } +} + +void StartGame() +{ + Stopwatch stopwatch = Stopwatch.StartNew(); + + for (int i = 0; i < 5; i++) + { + int number1 = new Random().Next(1, 9); + int number2 = new Random().Next(1, 9); + MathOperation operation = (MathOperation)new Random().Next(0, 4); + + string operationSymbol = operation switch + { + MathOperation.Addition => "+", + MathOperation.Subtraction => "-", + MathOperation.Multiplication => "*", + MathOperation.Division => "/", + _ => throw new ArgumentOutOfRangeException() + }; + + Console.WriteLine($"Question {i + 1}: What is {number1} {operationSymbol} {number2}?"); + string? input = Console.ReadLine(); + if (CheckAnswer(number1, number2, operation, input)) + { + points++; + } + } + stopwatch.Stop(); + Console.WriteLine($"You scored {points} out of 5."); + Console.WriteLine($"Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds."); + history.Add($"Game played on {DateTime.Now}: Scored {points}/5, Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds."); + PlayAgainPrompt(); +} + +void PlayAgainPrompt() +{ + Console.WriteLine("Do you want to return to the menu? (yes/no)"); + string? playAgain = Console.ReadLine(); + if (playAgain?.ToLower() == "yes") + { + points = 0; + MathGame(); + } + else if(playAgain?.ToLower() == "no") + { + Console.WriteLine("Thank you for playing!"); + Environment.Exit(0); + } + else + { + Console.WriteLine("Invalid input, try again."); + PlayAgainPrompt(); + } +} + +void DisplayList() +{ + Console.WriteLine("History of previous games:"); + foreach (string item in history) + { + Console.WriteLine($"- {item}"); + } + + PlayAgainPrompt(); +} + +bool CheckAnswer(int number1, int number2, MathOperation operation, string? result) +{ + int correctAnswer = operation switch + { + MathOperation.Addition => number1 + number2, + MathOperation.Subtraction => number1 - number2, + MathOperation.Multiplication => number1 * number2, + MathOperation.Division => number1 / number2, + _ => throw new ArgumentException("Invalid operation") + }; + + if (int.TryParse(result, out int parsedValue) && parsedValue == correctAnswer) + { + return true; + } + + return false; +} + +public enum MathOperation +{ + Addition, + Subtraction, + Multiplication, + Division +} \ No newline at end of file From 01a7d2898b3644b520be0df085f0a0b5157af3ad Mon Sep 17 00:00:00 2001 From: David Date: Thu, 15 Oct 2026 13:13:44 +0300 Subject: [PATCH 2/2] fix division, menu to choose operations, random operation mode --- MathGame.whoami3222/Program.cs | 56 ++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/MathGame.whoami3222/Program.cs b/MathGame.whoami3222/Program.cs index 6ccc9a91..8eace0e0 100644 --- a/MathGame.whoami3222/Program.cs +++ b/MathGame.whoami3222/Program.cs @@ -15,9 +15,13 @@ void MathGame() void DisplayMenu() { Console.WriteLine("Choose from the menu below:"); - Console.WriteLine("1. Start Game"); - Console.WriteLine("2. Display History"); - Console.WriteLine("3. Exit"); + Console.WriteLine("1. Summation"); + Console.WriteLine("2. Subtraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.WriteLine("5. Random Operation"); + Console.WriteLine("6. Display History"); + Console.WriteLine("7. Exit"); } void HandleMenuChoice(string? choice) @@ -25,12 +29,24 @@ void HandleMenuChoice(string? choice) switch (choice) { case "1": - StartGame(); + StartGame(MathOperation.Addition); break; case "2": - DisplayList(); + StartGame(MathOperation.Subtraction); break; case "3": + StartGame(MathOperation.Multiplication); + break; + case "4": + StartGame(MathOperation.Division); + break; + case "5": + StartGame(MathOperation.Random); + break; + case "6": + DisplayList(); + break; + case "7": Console.WriteLine("Exiting the game. Goodbye!"); Environment.Exit(0); break; @@ -40,17 +56,32 @@ void HandleMenuChoice(string? choice) } } -void StartGame() +void StartGame(MathOperation operation) { Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 0; i < 5; i++) { - int number1 = new Random().Next(1, 9); - int number2 = new Random().Next(1, 9); - MathOperation operation = (MathOperation)new Random().Next(0, 4); + MathOperation questionOperation = operation == MathOperation.Random + ? (MathOperation)Random.Shared.Next(0, 4) + : operation; + int number1; + int number2; + + if (questionOperation == MathOperation.Division) + { + number2 = Random.Shared.Next(1, 11); + int maxQuotient = 100 / number2; + int quotient = Random.Shared.Next(0, maxQuotient + 1); + number1 = number2 * quotient; + } + else + { + number1 = Random.Shared.Next(1, 9); + number2 = Random.Shared.Next(1, 9); + } - string operationSymbol = operation switch + string operationSymbol = questionOperation switch { MathOperation.Addition => "+", MathOperation.Subtraction => "-", @@ -61,7 +92,7 @@ void StartGame() Console.WriteLine($"Question {i + 1}: What is {number1} {operationSymbol} {number2}?"); string? input = Console.ReadLine(); - if (CheckAnswer(number1, number2, operation, input)) + if (CheckAnswer(number1, number2, questionOperation, input)) { points++; } @@ -129,5 +160,6 @@ public enum MathOperation Addition, Subtraction, Multiplication, - Division + Division, + Random } \ No newline at end of file