From 7d54e0c53fbb6d7157ada8593710656c5fc43fe1 Mon Sep 17 00:00:00 2001 From: AidanLDev Date: Sat, 8 Aug 2026 09:35:23 +0100 Subject: [PATCH 1/3] AidanLDevs Maths Game submission --- MathGame.AidanLDev/MathGame.AidanLDev.csproj | 10 + MathGame.AidanLDev/Program.cs | 207 +++++++++++++++++++ MathGame.AidanLDev/README.md | 10 + 3 files changed, 227 insertions(+) create mode 100644 MathGame.AidanLDev/MathGame.AidanLDev.csproj create mode 100644 MathGame.AidanLDev/Program.cs create mode 100644 MathGame.AidanLDev/README.md diff --git a/MathGame.AidanLDev/MathGame.AidanLDev.csproj b/MathGame.AidanLDev/MathGame.AidanLDev.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame.AidanLDev/MathGame.AidanLDev.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.AidanLDev/Program.cs b/MathGame.AidanLDev/Program.cs new file mode 100644 index 00000000..64915111 --- /dev/null +++ b/MathGame.AidanLDev/Program.cs @@ -0,0 +1,207 @@ +Random rng = new(); + +bool stillPlaying = true; +int curGame = 1; +int curScore = 0; +List gameHistory = []; + +void menuOptions() +{ + Console.WriteLine("1: Play new game"); + if (curGame > 1) + { + Console.WriteLine("2: View game history"); + } + + Console.WriteLine("(e) to exit..."); +} + +void printMainMenu() +{ + Console.WriteLine("Welcome to the Maths Game! Please select from one of the options:\n"); + menuOptions(); +} + +Dictionary operators = new() +{ + { 1, '+' }, + { 2, '-' }, + { 3, '*' }, + { 4, '/' } +}; + + +void additionQuestion() +{ + int num1 = rng.Next(1, 100000); + int num2 = rng.Next(1, 100000); + Console.WriteLine($"What is {num1:N0} + {num2:N0}"); + int res = num1 + num2; + string? usersResStr = Console.ReadLine(); + if (usersResStr != null) + { + int.TryParse(usersResStr, out int usersRes); + if (usersRes != res) + { + Console.WriteLine($"Unlucky... the result was {res}"); + } + else + { + Console.WriteLine($"Right you are! The answer was {res}"); + curScore++; + } + gameHistory.Add($"{num1} + {num2} = {res} - your answer {usersRes}. Current score {curScore}"); + } +} + +void subtractionQuestion() +{ + int num1 = rng.Next(1, 100000); + int num2 = rng.Next(1, 100000); + Console.WriteLine($"What is {num1:N0} - {num2:N0}"); + int res = num1 - num2; + string? usersResStr = Console.ReadLine(); + if (usersResStr != null) + { + int.TryParse(usersResStr, out int usersRes); + if (usersRes != res) + { + Console.WriteLine($"Unlucky... the result was {res}"); + } + else + { + Console.WriteLine($"Right you are! The answer was {res}"); + curScore++; + } + gameHistory.Add($"{num1} - {num2} = {res} - your answer {usersRes}. Current score {curScore}"); + } +} + +void multiplicationQuestion() +{ + int num1 = rng.Next(1, 100); + int num2 = rng.Next(1, 100); + Console.WriteLine($"What is {num1:N0} * {num2:N0}"); + int res = num1 * num2; + string? usersResStr = Console.ReadLine(); + if (usersResStr != null) + { + int.TryParse(usersResStr, out int usersRes); + if (usersRes != res) + { + Console.WriteLine($"Unlucky... the result was {res}"); + } + else + { + Console.WriteLine($"Right you are! The answer was {res}"); + curScore++; + } + gameHistory.Add($"{num1} * {num2} = {res} - your answer {usersRes}. Current score {curScore}"); + } +} + +void divisionQuestion() +{ + int divisor = rng.Next(1, 11); + int quotient = rng.Next(1, 11); + int divided = divisor * quotient; + Console.WriteLine($"What is {divided:N0}/{quotient:N0}"); + int res = divided / quotient; + string? usersResStr = Console.ReadLine(); + if (usersResStr != null) + { + int.TryParse(usersResStr, out int usersRes); + if (usersRes != res) + { + Console.WriteLine($"Unlucky... the result was {res}"); + } + else + { + Console.WriteLine($"Right you are! The answer was {res}"); + curScore++; + } + gameHistory.Add($"{divided}/{quotient} = {res} - your answer {usersRes}. Current score {curScore}"); + } +} + +void playGame() +{ + for (int i = 1; i < 6; i++) + { + int operatorRng = rng.Next(1, 5); + char mathOperator = operators[operatorRng]; + switch (mathOperator) + { + case '+': + additionQuestion(); + break; + case '-': + subtractionQuestion(); + break; + case '*': + multiplicationQuestion(); + break; + case '/': + divisionQuestion(); + break; + default: + additionQuestion(); + break; + } + } + Console.WriteLine($"You got {curScore}/5"); + curGame++; + curScore = 0; +} + +void displayHistory() +{ + for (int i = 0; i < gameHistory.Count; i++) + { + if (i % 5 == 0) + { + int gameNumber = (i / 5) + 1; + Console.WriteLine($"== Game {gameNumber} =="); + } + + Console.WriteLine(gameHistory[i]); + + } +} + +while (stillPlaying) +{ + + printMainMenu(); + string? userInput = Console.ReadLine(); + if (userInput != null) + { + userInput = userInput.Trim().ToLower(); + if (userInput != "e" || userInput != "1" || userInput != "2" || (userInput == "2" && curGame == 1)) + { + Console.WriteLine($"You inputted... {userInput}, please choose one of the options provided"); + menuOptions(); + } + + if (userInput == "e") + { + Console.WriteLine("Thank you for playing :)"); + stillPlaying = false; + break; + } + + if (userInput == "1") + { + playGame(); + } + + Console.WriteLine($"User inputted {userInput}"); + if (userInput == "2") + { + Console.WriteLine("Display history"); + displayHistory(); + } + + } + +} \ No newline at end of file diff --git a/MathGame.AidanLDev/README.md b/MathGame.AidanLDev/README.md new file mode 100644 index 00000000..d1a67044 --- /dev/null +++ b/MathGame.AidanLDev/README.md @@ -0,0 +1,10 @@ +# The Maths Game rules + +You need to create a game that consists of asking the player what's the result of a math question (i.e. 9 x 9 = ?), collecting the input and adding a point in case of a correct answer. + + +- A game needs to have at least 5 questions. +- The divisions should result on INTEGERS ONLY and dividends should go from 0 to 100. Example: Your app shouldn't present the division 7/2 to the user, since it doesn't result in an integer. +- Users should be presented with a menu to choose an operation +- You should record previous games in a List and there should be an option in the menu for the user to visualize a history of previous games. +- You don't need to record results on a database. Once the program is closed the results will be \ No newline at end of file From 49b303a004d61eb82cf92b4d36c53d3e006d2efa Mon Sep 17 00:00:00 2001 From: AidanLDev Date: Wed, 26 Aug 2026 22:08:10 +0100 Subject: [PATCH 2/3] Using a proper menu --- MathGame.AidanLDev/MathGame.AidanLDev.csproj | 4 + MathGame.AidanLDev/Program.cs | 131 ++++++++++--------- 2 files changed, 70 insertions(+), 65 deletions(-) diff --git a/MathGame.AidanLDev/MathGame.AidanLDev.csproj b/MathGame.AidanLDev/MathGame.AidanLDev.csproj index ed9781c2..452a0af1 100644 --- a/MathGame.AidanLDev/MathGame.AidanLDev.csproj +++ b/MathGame.AidanLDev/MathGame.AidanLDev.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/MathGame.AidanLDev/Program.cs b/MathGame.AidanLDev/Program.cs index 64915111..bf4df940 100644 --- a/MathGame.AidanLDev/Program.cs +++ b/MathGame.AidanLDev/Program.cs @@ -1,26 +1,13 @@ -Random rng = new(); +using Spectre.Console; + +Random rng = new(); bool stillPlaying = true; int curGame = 1; int curScore = 0; +int totalScore = 0; List gameHistory = []; -void menuOptions() -{ - Console.WriteLine("1: Play new game"); - if (curGame > 1) - { - Console.WriteLine("2: View game history"); - } - - Console.WriteLine("(e) to exit..."); -} - -void printMainMenu() -{ - Console.WriteLine("Welcome to the Maths Game! Please select from one of the options:\n"); - menuOptions(); -} Dictionary operators = new() { @@ -30,11 +17,27 @@ void printMainMenu() { 4, '/' } }; +void addToGameHistory(int num1, int num2, int res, int usersRes, int curScore, char mathSymbol) +{ + gameHistory.Add($"{num1} {mathSymbol} {num2} = {res} - your answer {usersRes}. Current score {curScore}"); +} + +void displayWrongAnswer(int res) +{ + AnsiConsole.MarkupLine($"[red]Unlucky... the result was {res}[/]"); +} + +void displayCorrectAnswer(int res) +{ + AnsiConsole.MarkupLine($"[green]Right you are! The answer was {res}[/]"); + curScore++; +} + void additionQuestion() { - int num1 = rng.Next(1, 100000); - int num2 = rng.Next(1, 100000); + int num1 = rng.Next(1, 100); + int num2 = rng.Next(1, 100); Console.WriteLine($"What is {num1:N0} + {num2:N0}"); int res = num1 + num2; string? usersResStr = Console.ReadLine(); @@ -43,21 +46,20 @@ void additionQuestion() int.TryParse(usersResStr, out int usersRes); if (usersRes != res) { - Console.WriteLine($"Unlucky... the result was {res}"); + displayWrongAnswer(res); } else { - Console.WriteLine($"Right you are! The answer was {res}"); - curScore++; + displayCorrectAnswer(res); } - gameHistory.Add($"{num1} + {num2} = {res} - your answer {usersRes}. Current score {curScore}"); + addToGameHistory(num1, num2, res, usersRes, curScore, '+'); } } void subtractionQuestion() { - int num1 = rng.Next(1, 100000); - int num2 = rng.Next(1, 100000); + int num1 = rng.Next(1, 100); + int num2 = rng.Next(1, 100); Console.WriteLine($"What is {num1:N0} - {num2:N0}"); int res = num1 - num2; string? usersResStr = Console.ReadLine(); @@ -66,21 +68,20 @@ void subtractionQuestion() int.TryParse(usersResStr, out int usersRes); if (usersRes != res) { - Console.WriteLine($"Unlucky... the result was {res}"); + displayWrongAnswer(res); } else { - Console.WriteLine($"Right you are! The answer was {res}"); - curScore++; + displayCorrectAnswer(res); } - gameHistory.Add($"{num1} - {num2} = {res} - your answer {usersRes}. Current score {curScore}"); + addToGameHistory(num1, num2, res, usersRes, curScore, '-'); } } void multiplicationQuestion() { - int num1 = rng.Next(1, 100); - int num2 = rng.Next(1, 100); + int num1 = rng.Next(1, 10); + int num2 = rng.Next(1, 10); Console.WriteLine($"What is {num1:N0} * {num2:N0}"); int res = num1 * num2; string? usersResStr = Console.ReadLine(); @@ -89,14 +90,13 @@ void multiplicationQuestion() int.TryParse(usersResStr, out int usersRes); if (usersRes != res) { - Console.WriteLine($"Unlucky... the result was {res}"); + displayWrongAnswer(res); } else { - Console.WriteLine($"Right you are! The answer was {res}"); - curScore++; + displayCorrectAnswer(res); } - gameHistory.Add($"{num1} * {num2} = {res} - your answer {usersRes}. Current score {curScore}"); + addToGameHistory(num1, num2, res, usersRes, curScore, '*'); } } @@ -113,14 +113,13 @@ void divisionQuestion() int.TryParse(usersResStr, out int usersRes); if (usersRes != res) { - Console.WriteLine($"Unlucky... the result was {res}"); + displayWrongAnswer(res); } else { - Console.WriteLine($"Right you are! The answer was {res}"); - curScore++; + displayCorrectAnswer(res); } - gameHistory.Add($"{divided}/{quotient} = {res} - your answer {usersRes}. Current score {curScore}"); + addToGameHistory(divided, quotient, res, usersRes, curScore, '/'); } } @@ -151,6 +150,7 @@ void playGame() } Console.WriteLine($"You got {curScore}/5"); curGame++; + totalScore += curScore; curScore = 0; } @@ -163,45 +163,46 @@ void displayHistory() int gameNumber = (i / 5) + 1; Console.WriteLine($"== Game {gameNumber} =="); } - Console.WriteLine(gameHistory[i]); + } + AnsiConsole.MarkupLine($"Total score [green]{totalScore}[/]"); + Console.WriteLine("\n"); +} +List BuildMenuChoices() +{ + List menuChoices = ["Start a new game"]; + + if (curGame > 1) + { + menuChoices.Add("View previous game history"); } + menuChoices.Add("Exit game"); + return menuChoices; } while (stillPlaying) { + string userChoice = AnsiConsole.Prompt( + new SelectionPrompt(). + Title("Please select from the following...") + .AddChoices(BuildMenuChoices()) + ).ToLower().Trim(); - printMainMenu(); - string? userInput = Console.ReadLine(); - if (userInput != null) + switch (userChoice) { - userInput = userInput.Trim().ToLower(); - if (userInput != "e" || userInput != "1" || userInput != "2" || (userInput == "2" && curGame == 1)) - { - Console.WriteLine($"You inputted... {userInput}, please choose one of the options provided"); - menuOptions(); - } - - if (userInput == "e") - { + case "exit game": Console.WriteLine("Thank you for playing :)"); stillPlaying = false; break; - } - - if (userInput == "1") - { + case "start a new game": playGame(); - } - - Console.WriteLine($"User inputted {userInput}"); - if (userInput == "2") - { - Console.WriteLine("Display history"); + break; + case "view previous game history": displayHistory(); - } - + break; + default: + playGame(); + break; } - } \ No newline at end of file From 4ac8b74eadf096a8879719dfb862df41e7e208d6 Mon Sep 17 00:00:00 2001 From: AidanLDev Date: Wed, 26 Aug 2026 22:11:53 +0100 Subject: [PATCH 3/3] Let user pick the operation from a menu before each game Previously the operator was chosen randomly inside playGame, so the top-level menu never actually controlled which operation was played. --- MathGame.AidanLDev/Program.cs | 66 ++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/MathGame.AidanLDev/Program.cs b/MathGame.AidanLDev/Program.cs index bf4df940..7335ffbf 100644 --- a/MathGame.AidanLDev/Program.cs +++ b/MathGame.AidanLDev/Program.cs @@ -123,30 +123,56 @@ void divisionQuestion() } } +char chooseOperation() +{ + Dictionary operationChoices = new() + { + { "Addition (+)", '+' }, + { "Subtraction (-)", '-' }, + { "Multiplication (*)", '*' }, + { "Division (/)", '/' }, + { "Mixed (random)", '?' } + }; + + string selection = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Which operation would you like to practice?") + .AddChoices(operationChoices.Keys) + ); + + return operationChoices[selection]; +} + +void askQuestion(char mathOperator) +{ + char resolvedOperator = mathOperator == '?' + ? operators[rng.Next(1, 5)] + : mathOperator; + + switch (resolvedOperator) + { + case '+': + additionQuestion(); + break; + case '-': + subtractionQuestion(); + break; + case '*': + multiplicationQuestion(); + break; + case '/': + divisionQuestion(); + break; + } +} + void playGame() { + char mathOperator = chooseOperation(); + for (int i = 1; i < 6; i++) { - int operatorRng = rng.Next(1, 5); - char mathOperator = operators[operatorRng]; - switch (mathOperator) - { - case '+': - additionQuestion(); - break; - case '-': - subtractionQuestion(); - break; - case '*': - multiplicationQuestion(); - break; - case '/': - divisionQuestion(); - break; - default: - additionQuestion(); - break; - } + askQuestion(mathOperator); } Console.WriteLine($"You got {curScore}/5"); curGame++;