From b5498e40c5e27e02b277726c44465756d8778da6 Mon Sep 17 00:00:00 2001 From: Pham Minh Date: Sat, 31 Jan 2026 21:13:51 +0700 Subject: [PATCH 1/3] Upload code --- Program.cs | 253 ++++++++++++++++++++++++++++++++++++++++ mathGame.m1nh4ke.csproj | 10 ++ mathGame.m1nh4ke.sln | 24 ++++ 3 files changed, 287 insertions(+) create mode 100644 Program.cs create mode 100644 mathGame.m1nh4ke.csproj create mode 100644 mathGame.m1nh4ke.sln diff --git a/Program.cs b/Program.cs new file mode 100644 index 00000000..06922815 --- /dev/null +++ b/Program.cs @@ -0,0 +1,253 @@ +using System.Collections; +using System.Diagnostics; + +// Boolean to determine whether to exit the program +bool exit = false; + +// Array to save user's results +List gameHistory = new List(); + +// Difficulty +int difficulty = 10; + +// Show menu to player +void ShowMenu() +{ + Console.WriteLine("Please enter a number to select the operation you want to practice:"); + Console.WriteLine("\t1. Summation"); + Console.WriteLine("\t2. Subtraction"); + Console.WriteLine("\t3. Multiplication"); + Console.WriteLine("\t4. Division"); + Console.WriteLine("\t5. Random"); + Console.WriteLine("\t6. Show history"); + Console.WriteLine("\t7. Change difficulty"); + Console.WriteLine("\t8. Exit"); +} + +// Get user's input and return an integer to choose operation +int GetUserInput() +{ + string input = Console.ReadLine().Trim().ToLower(); + int choice; + + try + { + choice = int.Parse(input); + if (0 < choice && choice < 9) + return choice; + else + { + Console.WriteLine("Please enter a valid option."); + return GetUserInput(); + } + } + catch + { + Console.WriteLine("Please enter a valid option."); + return GetUserInput(); + } +} + +void ShowHistory() +{ + for(int i = 0; i < gameHistory.Count; i++) + { + Console.WriteLine($"Q{i+1}: {gameHistory[i]}"); + } + Console.Write("\n"); +} + +string getDifficulty() +{ + // Get current difficulty + string currentDifficulty = ""; + if(difficulty == 1) + currentDifficulty = "Easy"; + else if(difficulty == 10) + currentDifficulty = "Normal"; + else if(difficulty == 100) + currentDifficulty = "Hard"; + else + currentDifficulty = "ASIAN!"; + return currentDifficulty; +} + +void ChangeDifficulty() +{ + Console.WriteLine("Choose your difficulty:"); + Console.WriteLine("\t1. Easy"); + Console.WriteLine("\t2. Normal"); + Console.WriteLine("\t3. Hard"); + Console.WriteLine("\t4. ASIAN!"); + Console.WriteLine($"Current difficulty: {getDifficulty()}"); + + string userChoice = Console.ReadLine().Trim().ToLower(); + int newDifficulty; + bool getUserChoice = int.TryParse(userChoice, out newDifficulty); + + // Return if cannot get valid input + if(getUserChoice == false) + { + Console.WriteLine("Invalid input! Cannot change your difficulty!"); + return; + } + // Change difficulty + switch (newDifficulty) + { + case 1: + difficulty = 1; + break; + case 2: + difficulty = 10; + break; + case 3: + difficulty = 100; + break; + case 4: + difficulty = 10000; + break; + } + Console.WriteLine($"Your new difficulty is {getDifficulty()}\n"); +} + +int MathQuestion(int firstNumber, int secondNumber, char operation) +{ + int result = 0; + + switch (operation) + { + case '+': + result = firstNumber + secondNumber; + break; + case '-': + result = firstNumber - secondNumber; + break; + case '*': + result = firstNumber * secondNumber; + break; + case '/': + result = firstNumber / secondNumber; + break; + } + gameHistory.Add($"{firstNumber} {operation} {secondNumber} = {result}"); + return result; +} + +void startGame(char operation) +{ + // Variables to generate questions + Random generateNumber = new Random(); + int first = 0, second = 0; + bool isRandom = operation == 'r'; + + // User variables + int userPoint = 0; + bool correctAnswer; + + // Start timer + Stopwatch stopwatch = Stopwatch.StartNew(); + + for(int i = 0; i < 5; i++) + { + if (isRandom) + { + // Generate random operation + char[] operations = {'+', '-', '*', '/'}; + Random random = new Random(); + operation = operations[random.Next(0, 4)]; + } + if(operation == '+') + { + first = generateNumber.Next(0, 10 * difficulty); + second = generateNumber.Next(0, 10 * difficulty); + } + else if (operation == '-') + { + first = generateNumber.Next(1, 10 * difficulty); + second = generateNumber.Next(0, first); + } + else if(operation == '*') + { + first = generateNumber.Next(1, 3 * difficulty); + second = generateNumber.Next(1, 3 * difficulty); + } + else + { + second = generateNumber.Next(1, 3 * difficulty); + int tmp = generateNumber.Next(1, 3 * difficulty); + first = tmp * second; + } + Console.Write($"Question {i+1}:\t"); + int res = MathQuestion(first, second, operation); + Console.Write($"{first} {operation} {second} = "); + string? answer = Console.ReadLine(); + try + { + int userAnswer = int.Parse(answer); + correctAnswer = userAnswer.Equals(res); + } + catch + { + Console.WriteLine("Invalid input.\nYou won't have any point for this answer!\n"); + correctAnswer = false; + } + if(correctAnswer == true) + { + userPoint += 1; + Console.WriteLine($"Correct! The answer is {res}\n"); + } + else + { + Console.WriteLine($"Wrong! The correct answer is {res}\n"); + } + } + // Get the total seconds as a double + double exactSeconds = stopwatch.Elapsed.TotalSeconds; + // Round up total seconds + int roundedUpSeconds = (int)Math.Ceiling(exactSeconds); + + // Display game result + Console.WriteLine($"You have successfully answered {userPoint}/5 questions!"); + Console.WriteLine($"Total time: {roundedUpSeconds} seconds.\n"); +} + +while(exit == false) +{ + ShowMenu(); + int userChoice = GetUserInput(); + char operation; + + switch (userChoice) + { + case 1: + operation = '+'; + startGame(operation); + break; + case 2: + operation = '-'; + startGame(operation); + break; + case 3: + operation = '*'; + startGame(operation); + break; + case 4: + operation = '/'; + startGame(operation); + break; + case 5: + operation = 'r'; + startGame(operation); + break; + case 6: + ShowHistory(); + break; + case 7: + ChangeDifficulty(); + break; + default: + Console.WriteLine("Game closed!"); + exit = true; + break; + } +} \ No newline at end of file diff --git a/mathGame.m1nh4ke.csproj b/mathGame.m1nh4ke.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/mathGame.m1nh4ke.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/mathGame.m1nh4ke.sln b/mathGame.m1nh4ke.sln new file mode 100644 index 00000000..85a03ae3 --- /dev/null +++ b/mathGame.m1nh4ke.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mathGame.m1nh4ke", "mathGame.m1nh4ke.csproj", "{A24CAC86-24D6-8CA4-38B9-D1A4B8476AEA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A24CAC86-24D6-8CA4-38B9-D1A4B8476AEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A24CAC86-24D6-8CA4-38B9-D1A4B8476AEA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A24CAC86-24D6-8CA4-38B9-D1A4B8476AEA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A24CAC86-24D6-8CA4-38B9-D1A4B8476AEA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4B21904C-AF95-4055-853B-D5449545C2FE} + EndGlobalSection +EndGlobal From 98466dc8080c2d12ecc96c72396ae04bb541adc3 Mon Sep 17 00:00:00 2001 From: m1nh4ke <85721118+m1nh4ke@users.noreply.github.com> Date: Sun, 1 Feb 2026 09:14:47 +0700 Subject: [PATCH 2/3] Improve user input handling and validation Refactor input handling to prevent null values and improve error checking. --- Program.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Program.cs b/Program.cs index 06922815..15249e84 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using System.Diagnostics; // Boolean to determine whether to exit the program @@ -27,7 +27,8 @@ void ShowMenu() // Get user's input and return an integer to choose operation int GetUserInput() { - string input = Console.ReadLine().Trim().ToLower(); + string input = Console.ReadLine() ?? ""; + input = input.Trim().ToLower(); int choice; try @@ -81,7 +82,8 @@ void ChangeDifficulty() Console.WriteLine("\t4. ASIAN!"); Console.WriteLine($"Current difficulty: {getDifficulty()}"); - string userChoice = Console.ReadLine().Trim().ToLower(); + string userChoice = Console.ReadLine() ?? ""; + userChoice = userChoice.Trim().ToLower(); int newDifficulty; bool getUserChoice = int.TryParse(userChoice, out newDifficulty); @@ -181,16 +183,17 @@ void startGame(char operation) int res = MathQuestion(first, second, operation); Console.Write($"{first} {operation} {second} = "); string? answer = Console.ReadLine(); - try - { - int userAnswer = int.Parse(answer); - correctAnswer = userAnswer.Equals(res); - } - catch + + int userAnswer; + bool getUserAnswer = int.TryParse(answer, out userAnswer); + correctAnswer = userAnswer.Equals(res); + + if (getUserAnswer == false) { Console.WriteLine("Invalid input.\nYou won't have any point for this answer!\n"); correctAnswer = false; } + if(correctAnswer == true) { userPoint += 1; @@ -250,4 +253,4 @@ void startGame(char operation) exit = true; break; } -} \ No newline at end of file +} From aabbfbb7fd6246538afbf8c5b32c6c2a5e84825d Mon Sep 17 00:00:00 2001 From: M1nhake <85721118+m1nh4ke@users.noreply.github.com> Date: Sun, 1 Feb 2026 15:33:48 +0700 Subject: [PATCH 3/3] Rename methods for consistent casing --- Program.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Program.cs b/Program.cs index 15249e84..6dde455e 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,3 @@ -using System.Collections; using System.Diagnostics; // Boolean to determine whether to exit the program @@ -58,7 +57,7 @@ void ShowHistory() Console.Write("\n"); } -string getDifficulty() +string GetDifficulty() { // Get current difficulty string currentDifficulty = ""; @@ -80,7 +79,7 @@ void ChangeDifficulty() Console.WriteLine("\t2. Normal"); Console.WriteLine("\t3. Hard"); Console.WriteLine("\t4. ASIAN!"); - Console.WriteLine($"Current difficulty: {getDifficulty()}"); + Console.WriteLine($"Current difficulty: {GetDifficulty()}"); string userChoice = Console.ReadLine() ?? ""; userChoice = userChoice.Trim().ToLower(); @@ -109,7 +108,7 @@ void ChangeDifficulty() difficulty = 10000; break; } - Console.WriteLine($"Your new difficulty is {getDifficulty()}\n"); + Console.WriteLine($"Your new difficulty is {GetDifficulty()}\n"); } int MathQuestion(int firstNumber, int secondNumber, char operation) @@ -135,7 +134,7 @@ int MathQuestion(int firstNumber, int secondNumber, char operation) return result; } -void startGame(char operation) +void StartGame(char operation) { // Variables to generate questions Random generateNumber = new Random(); @@ -224,23 +223,23 @@ void startGame(char operation) { case 1: operation = '+'; - startGame(operation); + StartGame(operation); break; case 2: operation = '-'; - startGame(operation); + StartGame(operation); break; case 3: operation = '*'; - startGame(operation); + StartGame(operation); break; case 4: operation = '/'; - startGame(operation); + StartGame(operation); break; case 5: operation = 'r'; - startGame(operation); + StartGame(operation); break; case 6: ShowHistory();