From d970b4739db4fda800ae24b2c01c3c6040d298e8 Mon Sep 17 00:00:00 2001 From: JuanF2019 <54515731+JuanF2019@users.noreply.github.com> Date: Wed, 13 May 2026 15:10:41 -0500 Subject: [PATCH 1/4] Create base solution and project --- MathGame/MathGame.slnx | 3 +++ MathGame/MathGame/MathGame.csproj | 10 ++++++++++ MathGame/MathGame/Program.cs | 1 + 3 files changed, 14 insertions(+) create mode 100644 MathGame/MathGame.slnx create mode 100644 MathGame/MathGame/MathGame.csproj create mode 100644 MathGame/MathGame/Program.cs diff --git a/MathGame/MathGame.slnx b/MathGame/MathGame.slnx new file mode 100644 index 00000000..c91187ad --- /dev/null +++ b/MathGame/MathGame.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame/MathGame/MathGame.csproj b/MathGame/MathGame/MathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame/MathGame/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs new file mode 100644 index 00000000..1bc52a60 --- /dev/null +++ b/MathGame/MathGame/Program.cs @@ -0,0 +1 @@ +Console.WriteLine("Hello, World!"); From e00a09f24f595d88a80d9bf520f7f84d38b173ca Mon Sep 17 00:00:00 2001 From: JuanF2019 <54515731+JuanF2019@users.noreply.github.com> Date: Thu, 14 May 2026 11:11:22 -0500 Subject: [PATCH 2/4] Add main menu loop and sum game. --- MathGame/MathGame/Program.cs | 92 +++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index 1bc52a60..5ccba3e9 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -1 +1,91 @@ -Console.WriteLine("Hello, World!"); +using System.Reflection.PortableExecutable; + +bool exitGame = false; +string? input; +List scores = new List(); +const int NumberOfQuestionsPerGame = 5; +const int ScorePerQuestion = 10; +const int MaximumScore = ScorePerQuestion * NumberOfQuestionsPerGame; + +do +{ + Console.WriteLine("Main menu"); + Console.WriteLine("Please select a game mode or type \"exit\" to close the app:"); + Console.WriteLine("1. Sum game"); + + bool validSelection; + do + { + input = Console.ReadLine(); + bool inputNotEmpty = input != null && input.Length > 0; + validSelection = true; + + if (inputNotEmpty) + { + string userSelection = input.ToLower(); + switch (userSelection) + { + case "exit": + exitGame = true; + break; + case "1": + Console.Clear(); + int score = sumGame(); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + default: + Console.WriteLine("Invalid selection please try again."); + validSelection = false; + break; + } + } + } while (!validSelection); +} while (!exitGame); + +static int sumGame() +{ + int score = 0; + Random random = new Random(); + + int minNumber = 1; + int maxNumber = 9; + + for(int i = 0; i < NumberOfQuestionsPerGame; i++) + { + int number1 = random.Next(minNumber, maxNumber+1); + int number2 = random.Next(minNumber,maxNumber+1); + int expectedAnswer = number1 + number2; + + Console.WriteLine($"Question {i+1}/{NumberOfQuestionsPerGame}\nWhat is {number1} + {number2} ?"); + + string input = Console.ReadLine(); + int userAnswer; + + while (input == null || input.Length == 0 || !int.TryParse(input, out userAnswer)) + { + Console.WriteLine("Your answer must be an integer number."); + input = Console.ReadLine(); + } + + if(userAnswer == expectedAnswer) + { + score += ScorePerQuestion; + } + Console.Clear(); + } + Console.WriteLine($"Your score was: {score}/{MaximumScore}"); + return score; +} + +enum Operations +{ + ADDITION,SUBSTRACTION,MULTIPLICATION,DIVISION +} +/* + * Ciclo para la selección de la operación + * For para las preguntas + * + */ \ No newline at end of file From 550bb0754587dffa0dd84fcb44c206338930b9bf Mon Sep 17 00:00:00 2001 From: JuanF2019 <54515731+JuanF2019@users.noreply.github.com> Date: Thu, 14 May 2026 11:45:20 -0500 Subject: [PATCH 3/4] Add substraction, multiplication and division games, create enum to identify operations, create generic method for all game types. --- MathGame/MathGame/Program.cs | 113 +++++++++++++++++++++++++++-------- 1 file changed, 89 insertions(+), 24 deletions(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index 5ccba3e9..ee9d5abc 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -1,6 +1,4 @@ -using System.Reflection.PortableExecutable; - -bool exitGame = false; +bool exitGame = false; string? input; List scores = new List(); const int NumberOfQuestionsPerGame = 5; @@ -11,11 +9,14 @@ { Console.WriteLine("Main menu"); Console.WriteLine("Please select a game mode or type \"exit\" to close the app:"); - Console.WriteLine("1. Sum game"); + Console.WriteLine("1. Addition game"); + Console.WriteLine("2. Substraction game"); + Console.WriteLine("3. Multiplication game"); + Console.WriteLine("4. Division game"); bool validSelection; do - { + { input = Console.ReadLine(); bool inputNotEmpty = input != null && input.Length > 0; validSelection = true; @@ -23,6 +24,7 @@ if (inputNotEmpty) { string userSelection = input.ToLower(); + int score; switch (userSelection) { case "exit": @@ -30,7 +32,31 @@ break; case "1": Console.Clear(); - int score = sumGame(); + score = game(Operations.ADDITION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + case "2": + Console.Clear(); + score = game(Operations.SUBSTRACTION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + case "3": + Console.Clear(); + score = game(Operations.MULTIPLICATION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + case "4": + Console.Clear(); + score = game(Operations.DIVISION); scores.Add(score); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); @@ -45,21 +71,64 @@ } while (!validSelection); } while (!exitGame); -static int sumGame() +static List getFactors(int num) { - int score = 0; - Random random = new Random(); + List factors = new List(); + + for (int i = 1; i <= num; i++) + { + if (num % i == 0) + { + factors.Add(i); + } + } + return factors; +} + +static int game(Operations operation) +{ + int score = 0; + char operationSymbol = ((char)operation); int minNumber = 1; int maxNumber = 9; - for(int i = 0; i < NumberOfQuestionsPerGame; i++) + Random random = new Random(); + + for (int i = 0; i < NumberOfQuestionsPerGame; i++) { - int number1 = random.Next(minNumber, maxNumber+1); - int number2 = random.Next(minNumber,maxNumber+1); - int expectedAnswer = number1 + number2; + int number1 = random.Next(minNumber, maxNumber + 1); + int number2; + int expectedResult; + switch (operation) + { + case Operations.ADDITION: + number2 = random.Next(minNumber, maxNumber + 1); + expectedResult = number1 + number2; + break; + case Operations.SUBSTRACTION: + number2 = random.Next(minNumber, maxNumber + 1); + expectedResult = number1 - number2; + break; + case Operations.MULTIPLICATION: + number2 = random.Next(minNumber, maxNumber + 1); + expectedResult = number1 * number2; + break; + case Operations.DIVISION: + List number1Factors = getFactors(number1); + int number2PositionInFactors = random.Next(0, number1Factors.Count); + number2 = number1Factors[number2PositionInFactors]; + + expectedResult = number1 / number2; + break; + default: + number1 = 1; + number2 = 1; + expectedResult = 0; + break; + } - Console.WriteLine($"Question {i+1}/{NumberOfQuestionsPerGame}\nWhat is {number1} + {number2} ?"); + Console.WriteLine($"Question {i + 1}/{NumberOfQuestionsPerGame}\nWhat is {number1} {operationSymbol} {number2} ?"); string input = Console.ReadLine(); int userAnswer; @@ -70,22 +139,18 @@ static int sumGame() input = Console.ReadLine(); } - if(userAnswer == expectedAnswer) + if (userAnswer == expectedResult) { score += ScorePerQuestion; } Console.Clear(); } - Console.WriteLine($"Your score was: {score}/{MaximumScore}"); + + Console.WriteLine($"Your score was {score}/{MaximumScore}"); + return score; } - enum Operations { - ADDITION,SUBSTRACTION,MULTIPLICATION,DIVISION -} -/* - * Ciclo para la selección de la operación - * For para las preguntas - * - */ \ No newline at end of file + ADDITION = '+', SUBSTRACTION = '-', MULTIPLICATION = '*', DIVISION = '/' +} \ No newline at end of file From f6614bcd8b861e148c3c428a9bb9326096f9f1e3 Mon Sep 17 00:00:00 2001 From: JuanF2019 <54515731+JuanF2019@users.noreply.github.com> Date: Thu, 21 May 2026 09:51:51 -0500 Subject: [PATCH 4/4] Fix null warning. --- MathGame/MathGame/Program.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index ee9d5abc..9245f308 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -130,7 +130,7 @@ static int game(Operations operation) Console.WriteLine($"Question {i + 1}/{NumberOfQuestionsPerGame}\nWhat is {number1} {operationSymbol} {number2} ?"); - string input = Console.ReadLine(); + string? input = Console.ReadLine(); int userAnswer; while (input == null || input.Length == 0 || !int.TryParse(input, out userAnswer)) @@ -152,5 +152,8 @@ static int game(Operations operation) } enum Operations { - ADDITION = '+', SUBSTRACTION = '-', MULTIPLICATION = '*', DIVISION = '/' + ADDITION = '+', + SUBSTRACTION = '-', + MULTIPLICATION = '*', + DIVISION = '/' } \ No newline at end of file