diff --git a/MathGame.AidanLDev/MathGame.AidanLDev.csproj b/MathGame.AidanLDev/MathGame.AidanLDev.csproj new file mode 100644 index 00000000..452a0af1 --- /dev/null +++ b/MathGame.AidanLDev/MathGame.AidanLDev.csproj @@ -0,0 +1,14 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + diff --git a/MathGame.AidanLDev/Program.cs b/MathGame.AidanLDev/Program.cs new file mode 100644 index 00000000..7335ffbf --- /dev/null +++ b/MathGame.AidanLDev/Program.cs @@ -0,0 +1,234 @@ +using Spectre.Console; + +Random rng = new(); + +bool stillPlaying = true; +int curGame = 1; +int curScore = 0; +int totalScore = 0; +List gameHistory = []; + + +Dictionary operators = new() +{ + { 1, '+' }, + { 2, '-' }, + { 3, '*' }, + { 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, 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) + { + displayWrongAnswer(res); + } + else + { + displayCorrectAnswer(res); + } + addToGameHistory(num1, num2, res, usersRes, curScore, '+'); + } +} + +void subtractionQuestion() +{ + 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) + { + displayWrongAnswer(res); + } + else + { + displayCorrectAnswer(res); + } + addToGameHistory(num1, num2, res, usersRes, curScore, '-'); + } +} + +void multiplicationQuestion() +{ + 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(); + if (usersResStr != null) + { + int.TryParse(usersResStr, out int usersRes); + if (usersRes != res) + { + displayWrongAnswer(res); + } + else + { + displayCorrectAnswer(res); + } + addToGameHistory(num1, num2, res, usersRes, 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) + { + displayWrongAnswer(res); + } + else + { + displayCorrectAnswer(res); + } + addToGameHistory(divided, quotient, res, usersRes, curScore, '/'); + } +} + +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++) + { + askQuestion(mathOperator); + } + Console.WriteLine($"You got {curScore}/5"); + curGame++; + totalScore += curScore; + 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]); + } + 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(); + + switch (userChoice) + { + case "exit game": + Console.WriteLine("Thank you for playing :)"); + stillPlaying = false; + break; + case "start a new game": + playGame(); + break; + case "view previous game history": + displayHistory(); + break; + default: + playGame(); + break; + } +} \ 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