From 3327bdd68c7847678f1c77ead162f11e3b9485d2 Mon Sep 17 00:00:00 2001 From: Edwin Tan Date: Thu, 12 Mar 2026 15:48:08 +0800 Subject: [PATCH 1/3] project finished! --- Math Game | 1 + 1 file changed, 1 insertion(+) create mode 160000 Math Game diff --git a/Math Game b/Math Game new file mode 160000 index 00000000..a9897b04 --- /dev/null +++ b/Math Game @@ -0,0 +1 @@ +Subproject commit a9897b04bdcdf36552f74c2dddadfcc852b9a61b From c34266490ff5794560b10db057b1899738b95b37 Mon Sep 17 00:00:00 2001 From: Edwin Tan Date: Fri, 13 Mar 2026 13:12:06 +0800 Subject: [PATCH 2/3] removed .git --- Math Game | 1 - Math Game/Math Game.slnx | 3 + Math Game/Math Game/Math Game.csproj | 11 ++ Math Game/Math Game/Program.cs | 281 +++++++++++++++++++++++++++ 4 files changed, 295 insertions(+), 1 deletion(-) delete mode 160000 Math Game create mode 100644 Math Game/Math Game.slnx create mode 100644 Math Game/Math Game/Math Game.csproj create mode 100644 Math Game/Math Game/Program.cs diff --git a/Math Game b/Math Game deleted file mode 160000 index a9897b04..00000000 --- a/Math Game +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a9897b04bdcdf36552f74c2dddadfcc852b9a61b diff --git a/Math Game/Math Game.slnx b/Math Game/Math Game.slnx new file mode 100644 index 00000000..6c0c858d --- /dev/null +++ b/Math Game/Math Game.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Math Game/Math Game/Math Game.csproj b/Math Game/Math Game/Math Game.csproj new file mode 100644 index 00000000..50a3b8c4 --- /dev/null +++ b/Math Game/Math Game/Math Game.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + Math_Game + enable + enable + + + diff --git a/Math Game/Math Game/Program.cs b/Math Game/Math Game/Program.cs new file mode 100644 index 00000000..ffc7b754 --- /dev/null +++ b/Math Game/Math Game/Program.cs @@ -0,0 +1,281 @@ +using System.Diagnostics; +using System.Linq.Expressions; +using System.Net; +using System.Numerics; +using System.Reflection.Metadata.Ecma335; +using System.Security.Cryptography; + +namespace Math_Game +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("testing 123!"); + Menu(); + } + + static void Menu() + { + List history = new List(); + var gameParemeters = ("", 0); + int userOption = 0; + bool gameParameterChosen = false; + string menu = "Welcome to the math game! Please select a game mode: \n" + + "1) Select game options \n" + + "2) Play \n" + + "3) View history \n" + + "4) Exit"; + + while (true) + { + Console.WriteLine(menu); + + if (!int.TryParse(Console.ReadLine(), out userOption)) + { + Console.WriteLine("Please enter a valid number (1 - 4): "); + continue; + } + + + if (userOption == 1) + { + gameParemeters = getGameParameters(); + gameParameterChosen = true; + continue; + } + else if (4 > userOption && userOption > 1 && gameParameterChosen == false) + { + Console.WriteLine("Please select game parameters first"); + continue; + } + else if (userOption == 2) + { + while (true) + { + Console.WriteLine("do you want a random game? (yes/no)"); + string userInputForRandomGame = Console.ReadLine(); + userInputForRandomGame = userInputForRandomGame.ToLower(); + + if (userInputForRandomGame == "yes") + { + history.AddRange(playGame(gameParemeters.Item1, gameParemeters.Item2, true, 0)); + break; + } + else if (userInputForRandomGame == "no") + { + Console.WriteLine("please choose an operator \n" + + "1) +\n" + + "2) -\n" + + "3) *\n" + + "4) /\n"); + + if (!int.TryParse(Console.ReadLine(), out int userChosenOperator)) + { + Console.WriteLine("please enter 1 - 4 only"); + continue; + } + else + { + history.AddRange(playGame(gameParemeters.Item1, gameParemeters.Item2, false, userChosenOperator - 1)); + } + break; + } + else + { + Console.WriteLine("Please enter yes or no only"); + } + } + + + + } + else if (userOption == 3) + { + if(history.Count == 0) + { + Console.WriteLine("No games played yet!"); + continue; + } + foreach (string item in history) + { + Console.WriteLine(item); + } + } + else if (userOption == 4) + { + Console.WriteLine("Thanks for playing!"); + break; + } + else + { + Console.WriteLine("Please enter a valid number ( 1 - 4 ): "); + continue; + + } + } + } + + static (string difficulty, int numberOfQuestions) getGameParameters() + { + + string difficulty = ""; + int numberOfQuestions = 0; + + while (true) + { + Console.WriteLine("please enter a valid difficulty: easy, normal or hard"); + difficulty = Console.ReadLine(); + + difficulty = difficulty.ToLower(); + if (difficulty != "easy" && difficulty != "normal" && difficulty != "hard") + { + continue; + } + else + { + break; + } + } + + while (true) + { + Console.WriteLine("please enter the amount of questions you want: (5 - 99)"); + if (!int.TryParse(Console.ReadLine(), out numberOfQuestions) || numberOfQuestions < 5 || numberOfQuestions > 99) + { + Console.WriteLine("Please enter a valid number between 5 and 99"); + continue; + } + else + { + break; + } + + } + return (difficulty, numberOfQuestions); + + } + + + + static List playGame(string difficulty, int numberOfQuestions, bool randomGameBool, int userChosenOperator) + { + List history = new List(); + Random rand = new Random(); + Stopwatch sw = new Stopwatch(); + int correctCtr = 0; + sw.Start(); + for (int i = 0; i < numberOfQuestions; i++) + { + + int firstNumber = 0; + int secondNumber = 0; + if (difficulty == "easy") + { + firstNumber = rand.Next(1, 11); + secondNumber = rand.Next(1, 11); + } + else if (difficulty == "normal") + { + firstNumber = rand.Next(10, 100); + secondNumber = rand.Next(10, 100); + } + else if (difficulty == "hard") + { + firstNumber = rand.Next(100, 1000); + secondNumber = rand.Next(100, 1000); + } + + if (randomGameBool) + { + int randomOperator = rand.Next(0, 4); + history.Add(AskQuestion(firstNumber, secondNumber, randomOperator)); + } + else + { + history.Add(AskQuestion(firstNumber, secondNumber, userChosenOperator)); + } + if (history.Last().Contains("Correct!")) + { + correctCtr++; + } + } + sw.Stop(); + Console.WriteLine("Your score is: " + correctCtr + "/" + numberOfQuestions); + Console.WriteLine("The total time taken(seconds) is: " + sw.Elapsed.TotalSeconds); + Console.WriteLine("press enter to continue"); + Console.ReadLine(); + return history; + } + static string AskQuestion(int firstNumber, int secondNumber, int randomOperator) + { + + String operatorSymbol = ""; + int realAnswer = 0; + if (randomOperator == 0) + { + operatorSymbol = "+"; + realAnswer = firstNumber + secondNumber; + } + else if (randomOperator == 1) + { + operatorSymbol = "-"; + realAnswer = firstNumber - secondNumber; + } + else if (randomOperator == 2) + { + operatorSymbol = "*"; + realAnswer = firstNumber * secondNumber; + } + else if (randomOperator == 3) + { + operatorSymbol = "/"; + while (true) + { + if (firstNumber % secondNumber == 0) + { + realAnswer = firstNumber / secondNumber; + break; + } + else + { + if (firstNumber < secondNumber) + { + int temp = secondNumber; + secondNumber = firstNumber; + firstNumber = temp; + } + firstNumber++; + + } + + } + + } + Console.WriteLine($"What is {firstNumber} {operatorSymbol} {secondNumber} ?"); + int userAnswerInt = 0; + while (true) + { + if (!int.TryParse(Console.ReadLine(), out userAnswerInt)) + { + Console.WriteLine("Please enter a valid number"); + continue; + + + } + else + { + break; + } + + } + + string result = realAnswer == userAnswerInt ? "Correct!" : $"Wrong! The correct answer is {realAnswer}"; + Console.WriteLine(result); + return $"Question: {firstNumber} {operatorSymbol} {secondNumber}\t | Your answer: {userAnswerInt}\t | Result: {result}"; + + } + + + } +} From 1d10c215c67e6b0d4a85eeed1813d99cd7aae33f Mon Sep 17 00:00:00 2001 From: Edwin Tan Date: Fri, 13 Mar 2026 14:00:29 +0800 Subject: [PATCH 3/3] rectified codacy analysis issues and fixed warnings --- Math Game/Math Game/Program.cs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/Math Game/Math Game/Program.cs b/Math Game/Math Game/Program.cs index ffc7b754..76b1994b 100644 --- a/Math Game/Math Game/Program.cs +++ b/Math Game/Math Game/Program.cs @@ -1,9 +1,7 @@ -using System.Diagnostics; -using System.Linq.Expressions; -using System.Net; -using System.Numerics; -using System.Reflection.Metadata.Ecma335; -using System.Security.Cryptography; + + + +using System.Diagnostics; namespace Math_Game { @@ -40,7 +38,7 @@ static void Menu() if (userOption == 1) { - gameParemeters = getGameParameters(); + gameParemeters = GetGameParameters(); gameParameterChosen = true; continue; } @@ -54,12 +52,12 @@ static void Menu() while (true) { Console.WriteLine("do you want a random game? (yes/no)"); - string userInputForRandomGame = Console.ReadLine(); - userInputForRandomGame = userInputForRandomGame.ToLower(); + + string userInputForRandomGame = (Console.ReadLine() ?? "").ToLower(); if (userInputForRandomGame == "yes") { - history.AddRange(playGame(gameParemeters.Item1, gameParemeters.Item2, true, 0)); + history.AddRange(PlayGame(gameParemeters.Item1, gameParemeters.Item2, true, 0)); break; } else if (userInputForRandomGame == "no") @@ -77,7 +75,7 @@ static void Menu() } else { - history.AddRange(playGame(gameParemeters.Item1, gameParemeters.Item2, false, userChosenOperator - 1)); + history.AddRange(PlayGame(gameParemeters.Item1, gameParemeters.Item2, false, userChosenOperator - 1)); } break; } @@ -116,18 +114,17 @@ static void Menu() } } - static (string difficulty, int numberOfQuestions) getGameParameters() + static (string difficulty, int numberOfQuestions) GetGameParameters() { - string difficulty = ""; + string? difficulty = ""; int numberOfQuestions = 0; while (true) { Console.WriteLine("please enter a valid difficulty: easy, normal or hard"); - difficulty = Console.ReadLine(); - difficulty = difficulty.ToLower(); + difficulty = (Console.ReadLine() ?? "").ToLower(); if (difficulty != "easy" && difficulty != "normal" && difficulty != "hard") { continue; @@ -158,7 +155,7 @@ static void Menu() - static List playGame(string difficulty, int numberOfQuestions, bool randomGameBool, int userChosenOperator) + static List PlayGame(string difficulty, int numberOfQuestions, bool randomGameBool, int userChosenOperator) { List history = new List(); Random rand = new Random();