diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..612c8cf6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,23 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (mathGame)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "dotnet: build", + "program": "${workspaceFolder}/bin/Debug/net10.0/mathGame.dll", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + "console": "internalConsole", + "internalConsoleOptions": "openOnSessionStart", + "launchBrowser": { + "enabled": false + } + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..b58eb205 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,14 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "dotnet: build", + "type": "shell", + "command": "dotnet build", + "problemMatcher": [ + "$msCompile" + ], + "group": "build" + } + ] +} \ No newline at end of file diff --git a/GameRound.cs b/GameRound.cs new file mode 100644 index 00000000..f0d5bdcd --- /dev/null +++ b/GameRound.cs @@ -0,0 +1,75 @@ +class GameRound +{ + public char Operation { get; } + public int TotalScore { get; set; } + private readonly int _questionsPerRound = 10; + public int QuestionsPerRound + { + get { return _questionsPerRound; } + } + private readonly (int, int)[] _questions; + public IReadOnlyList<(int, int)> Questions + { + get { return _questions; } + } + private readonly int[] _answers; + public IReadOnlyList Answers + { + get { return _answers; } + } + private void GenerateQuestionsForOperation() + { + Random rnd = new(); + var uniqueQuestions = new HashSet<(int, int)>(); + + int i = 0; + while (i < _questionsPerRound) + { + int a = rnd.Next(1, 101); + int b = rnd.Next(1, 101); + if (Operation == '-') + { + b = rnd.Next(1, a + 1); + } + else if (Operation == '/') + { + while (a % b != 0) + { + b = rnd.Next(2, a); + } + } + + var question = (a, b); + if (uniqueQuestions.Add(question)) + { + _questions[i] = question; + i += 1; + } + } + } + private void ComputeAnswersForOperation() + { + for (int i = 0; i < _questions.Length; i++) + { + (int, int) question = _questions[i]; + int answer = Operation switch + { + '+' => question.Item1 + question.Item2, + '*' => question.Item1 * question.Item2, + '-' => question.Item1 - question.Item2, + '/' => question.Item1 / question.Item2, + _ => throw new InvalidOperationException("Unknown Operator.") + }; + + _answers[i] = answer; + } + } + public GameRound(char operation) + { + Operation = operation; + _questions = [.. new (int, int)[_questionsPerRound]]; + GenerateQuestionsForOperation(); + _answers = [.. new int[_questionsPerRound]]; + ComputeAnswersForOperation(); + } +} \ No newline at end of file diff --git a/GameSession.cs b/GameSession.cs new file mode 100644 index 00000000..169a6639 --- /dev/null +++ b/GameSession.cs @@ -0,0 +1,136 @@ +using System.Collections.Immutable; + +class GameSession +{ + public List History { get; set; } + private bool _isFirstRound; + public ImmutableDictionary OptionsDictionary = new Dictionary + { + {1, '+'}, + {2, '-'}, + {3, '*'}, + {4, '/'}, + }.ToImmutableDictionary(); + + public void ShowMenu() + { + int selectedOption = -1; + + while (selectedOption != 3) + { + if (_isFirstRound) + { + _isFirstRound = false; + Console.WriteLine("WELCOME TO THE MATH QUIZ!"); + StartGame(); + } + + Console.WriteLine("Choose an option:"); + Console.WriteLine("1. Start another round"); + Console.WriteLine("2. Check history of scores"); + Console.WriteLine("3. Quit"); + Console.WriteLine(); + + var menuOptionsSet = new HashSet() { 1, 2, 3 }; + + _ = int.TryParse(Console.ReadLine()!, out selectedOption); + while (selectedOption == 0 || !menuOptionsSet.Contains(selectedOption)) + { + Console.WriteLine("Invalid option selected. Please try again."); + Console.WriteLine("1. Start another round"); + Console.WriteLine("2. Check history of scores"); + Console.WriteLine("3. Quit"); + Console.WriteLine(); + _ = int.TryParse(Console.ReadLine()!, out selectedOption); + } + + if (selectedOption == 1) StartGame(); + else if (selectedOption == 2) ShowHistory(); + } + } + + public void StartGame() + { + Console.WriteLine(); + Console.WriteLine("******************************* ROUND STARTED *******************************"); + Console.WriteLine(); + Console.WriteLine("Choose an operation to get started: "); + foreach (int o in OptionsDictionary.Keys) + { + Console.WriteLine($"{o} : {OptionsDictionary[o]}"); + } + + var optionsSet = new HashSet(OptionsDictionary.Keys); + _ = int.TryParse(Console.ReadLine()!, out int option); + + while (option == 0 || !optionsSet.Contains(option)) + { + Console.WriteLine("Invalid option selected. Please try again."); + foreach (int o in OptionsDictionary.Keys) + { + Console.WriteLine($"{o} : {OptionsDictionary[o]}"); + } + _ = int.TryParse(Console.ReadLine()!, out option); + } + + char operation = OptionsDictionary[option]; + + GameRound gameRound = new(operation); + + Console.WriteLine(); + Console.WriteLine("You've selected: " + operation); + for (int i = 0; i < gameRound.Questions.Count; i++) + { + (int, int) q = gameRound.Questions[i]; + Console.WriteLine(); + Console.WriteLine($"What is {q.Item1} {operation} {q.Item2}?"); + + if (!int.TryParse(Console.ReadLine()!, out int response)) + { + response = -1; + } + + while (response == -1) + { + Console.WriteLine(); + Console.WriteLine($"Invalid response. Please enter a valid number."); + if (!int.TryParse(Console.ReadLine()!, out response)) + { + response = -1; + } + } + + if (response == gameRound.Answers[i]) + { + gameRound.TotalScore += 1; + } + } + + Console.WriteLine(); + Console.WriteLine($"YOU SCORED: {gameRound.TotalScore} POINT(S)!"); + Console.WriteLine(); + Console.WriteLine("******************************* ROUND ENDED *******************************"); + Console.WriteLine(); + History.Add(gameRound); + } + + public void ShowHistory() + { + Console.WriteLine(); + Console.WriteLine("History of scores for this session:"); + for (int i = 0; i < History.Count; i++) + { + Console.WriteLine($"Game {i}: {History[i].TotalScore} POINT(S)"); + } + + Console.WriteLine(); + Console.WriteLine("Press any key to exit the history section..."); + Console.ReadLine(); + } + + public GameSession() + { + History = []; + _isFirstRound = true; + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 00000000..c2c4df07 --- /dev/null +++ b/Program.cs @@ -0,0 +1,2 @@ +GameSession gameSession = new(); +gameSession.ShowMenu(); \ No newline at end of file diff --git a/mathGame.rumanstheddy.csproj b/mathGame.rumanstheddy.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/mathGame.rumanstheddy.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/mathGame.rumanstheddy.sln b/mathGame.rumanstheddy.sln new file mode 100644 index 00000000..8b134cbc --- /dev/null +++ b/mathGame.rumanstheddy.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.rumanstheddy", "mathGame.rumanstheddy.csproj", "{8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8A0099C2-3CCB-42B6-B9C9-7389D2867AA2} + EndGlobalSection +EndGlobal