From 642ba2e0584905996f12a619817bcc650ec2a7e2 Mon Sep 17 00:00:00 2001 From: Sumanth Reddy Date: Mon, 12 Jan 2026 15:04:52 -0500 Subject: [PATCH 1/5] Add initial project files and implement game logic for math quiz --- .vscode/launch.json | 23 +++++++ .vscode/tasks.json | 14 +++++ GameRound.cs | 35 +++++++++++ GameSession.cs | 116 +++++++++++++++++++++++++++++++++++ Program.cs | 2 + mathGame.rumanstheddy.csproj | 10 +++ mathGame.rumanstheddy.sln | 24 ++++++++ 7 files changed, 224 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 GameRound.cs create mode 100644 GameSession.cs create mode 100644 Program.cs create mode 100644 mathGame.rumanstheddy.csproj create mode 100644 mathGame.rumanstheddy.sln 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..20e0865a --- /dev/null +++ b/GameRound.cs @@ -0,0 +1,35 @@ +class GameRound +{ + public (int, int)[] Questions { get; set; } + public char Operation { get; } + public int TotalScore { get; set; } + private readonly int[] _answers; + public IReadOnlyList Answers + { + get { return _answers; } + } + 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, (int, int)[] questions) + { + Operation = operation; + _answers = [.. new int[questions.Length]]; + Questions = questions; + ComputeAnswersForOperation(); + } +} \ No newline at end of file diff --git a/GameSession.cs b/GameSession.cs new file mode 100644 index 00000000..52159bca --- /dev/null +++ b/GameSession.cs @@ -0,0 +1,116 @@ +using System.Collections.Immutable; + +class GameSession +{ + public List History { get; set; } + private bool _isFirstRound; + public ImmutableDictionary OptionsDictionary = new Dictionary + { + {1, '+'}, + {2, '-'}, + {3, '*'}, + {4, '/'}, + }.ToImmutableDictionary(); + + // TODO: Move away from static questions (maybe from a csv file?) + public static readonly ImmutableDictionary QuestionsDictionary = new Dictionary + { + {'+', [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12), (13, 14), (15, 16), (17, 18), (19, 20)]}, + {'-', [(10, 2), (15, 5), (20, 7), (18, 9), (12, 3), (22, 11), (30, 10), (25, 8), (40, 20), (50, 25)]}, + {'*', [(2, 3), (4, 5), (6, 2), (7, 3), (8, 4), (5, 5), (9, 2), (3, 6), (10, 3), (4, 7)]}, + {'/', [(8, 2), (15, 3), (18, 6), (20, 4), (21, 7), (24, 6), (30, 5), (36, 6), (40, 8), (27, 3)]}, + }.ToImmutableDictionary(); + + public void ShowMenu() + { + 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"); + + int selectedOption = int.Parse(Console.ReadLine()!); + switch (selectedOption) + { + case 1: + StartGame(); + break; + case 2: + ShowHistory(); + break; + case 3: + Environment.Exit(0); + break; + default: + ShowMenu(); + break; + } + + Console.WriteLine(); + } + + 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]}"); + } + int option = int.Parse(Console.ReadLine()!); + char operation = OptionsDictionary[option]; + + GameRound gameRound = new(operation, QuestionsDictionary[operation]); + + Console.WriteLine(); + Console.WriteLine("You've selected: " + operation); + for (int i = 0; i < gameRound.Questions.Length; i++) + { + (int, int) q = gameRound.Questions[i]; + Console.WriteLine(); + Console.WriteLine($"What is {q.Item1} {operation} {q.Item2}?"); + int response = int.Parse(Console.ReadLine()!); + 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); + + ShowMenu(); + } + + 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 From d815ad2e615ce75632c1275e4c0ff77133389879 Mon Sep 17 00:00:00 2001 From: Sumanth Reddy Date: Mon, 19 Jan 2026 21:24:14 -0500 Subject: [PATCH 2/5] Refactor GameRound and GameSession to generate questions dynamically and remove static question dictionary --- GameRound.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++------ GameSession.cs | 13 ++----------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/GameRound.cs b/GameRound.cs index 20e0865a..f0d5bdcd 100644 --- a/GameRound.cs +++ b/GameRound.cs @@ -1,18 +1,57 @@ class GameRound { - public (int, int)[] Questions { get; set; } 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++) + for (int i = 0; i < _questions.Length; i++) { - (int, int) question = Questions[i]; + (int, int) question = _questions[i]; int answer = Operation switch { '+' => question.Item1 + question.Item2, @@ -25,11 +64,12 @@ private void ComputeAnswersForOperation() _answers[i] = answer; } } - public GameRound(char operation, (int, int)[] questions) + public GameRound(char operation) { Operation = operation; - _answers = [.. new int[questions.Length]]; - Questions = questions; + _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 index 52159bca..5beb3dd9 100644 --- a/GameSession.cs +++ b/GameSession.cs @@ -12,15 +12,6 @@ class GameSession {4, '/'}, }.ToImmutableDictionary(); - // TODO: Move away from static questions (maybe from a csv file?) - public static readonly ImmutableDictionary QuestionsDictionary = new Dictionary - { - {'+', [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12), (13, 14), (15, 16), (17, 18), (19, 20)]}, - {'-', [(10, 2), (15, 5), (20, 7), (18, 9), (12, 3), (22, 11), (30, 10), (25, 8), (40, 20), (50, 25)]}, - {'*', [(2, 3), (4, 5), (6, 2), (7, 3), (8, 4), (5, 5), (9, 2), (3, 6), (10, 3), (4, 7)]}, - {'/', [(8, 2), (15, 3), (18, 6), (20, 4), (21, 7), (24, 6), (30, 5), (36, 6), (40, 8), (27, 3)]}, - }.ToImmutableDictionary(); - public void ShowMenu() { if (_isFirstRound) @@ -68,11 +59,11 @@ public void StartGame() int option = int.Parse(Console.ReadLine()!); char operation = OptionsDictionary[option]; - GameRound gameRound = new(operation, QuestionsDictionary[operation]); + GameRound gameRound = new(operation); Console.WriteLine(); Console.WriteLine("You've selected: " + operation); - for (int i = 0; i < gameRound.Questions.Length; i++) + for (int i = 0; i < gameRound.Questions.Count; i++) { (int, int) q = gameRound.Questions[i]; Console.WriteLine(); From 27bbcc9c51d477e8ce70b3758f73549f2d3e53f6 Mon Sep 17 00:00:00 2001 From: Sumanth Reddy Date: Tue, 20 Jan 2026 11:56:47 -0500 Subject: [PATCH 3/5] Refactor ShowMenu and StartGame methods for improved input validation and flow control --- GameSession.cs | 89 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/GameSession.cs b/GameSession.cs index 5beb3dd9..8cfedc93 100644 --- a/GameSession.cs +++ b/GameSession.cs @@ -14,36 +14,41 @@ class GameSession public void ShowMenu() { - if (_isFirstRound) - { - _isFirstRound = false; - Console.WriteLine("WELCOME TO THE MATH QUIZ!"); - StartGame(); - } + int selectedOption = -1; - Console.WriteLine("Choose an option:"); - Console.WriteLine("1. Start another round"); - Console.WriteLine("2. Check history of scores"); - Console.WriteLine("3. Quit"); - - int selectedOption = int.Parse(Console.ReadLine()!); - switch (selectedOption) + while (selectedOption != 3) { - case 1: + if (_isFirstRound) + { + _isFirstRound = false; + Console.WriteLine("WELCOME TO THE MATH QUIZ!"); StartGame(); - break; - case 2: - ShowHistory(); - break; - case 3: - Environment.Exit(0); - break; - default: - ShowMenu(); - break; + } + + 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(); } - Console.WriteLine(); + Environment.Exit(0); } public void StartGame() @@ -56,7 +61,20 @@ public void StartGame() { Console.WriteLine($"{o} : {OptionsDictionary[o]}"); } - int option = int.Parse(Console.ReadLine()!); + + 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); @@ -68,7 +86,22 @@ public void StartGame() (int, int) q = gameRound.Questions[i]; Console.WriteLine(); Console.WriteLine($"What is {q.Item1} {operation} {q.Item2}?"); - int response = int.Parse(Console.ReadLine()!); + + 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; @@ -81,8 +114,6 @@ public void StartGame() Console.WriteLine("******************************* ROUND ENDED *******************************"); Console.WriteLine(); History.Add(gameRound); - - ShowMenu(); } public void ShowHistory() From 62677495f8cc3b957539fe34293e91ab2c413da1 Mon Sep 17 00:00:00 2001 From: Sumanth Reddy Date: Tue, 20 Jan 2026 12:42:26 -0500 Subject: [PATCH 4/5] Remove unnecessary exit call in ShowMenu method --- GameSession.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GameSession.cs b/GameSession.cs index 8cfedc93..169a6639 100644 --- a/GameSession.cs +++ b/GameSession.cs @@ -31,7 +31,7 @@ public void ShowMenu() Console.WriteLine("3. Quit"); Console.WriteLine(); - var menuOptionsSet = new HashSet(){1, 2, 3}; + var menuOptionsSet = new HashSet() { 1, 2, 3 }; _ = int.TryParse(Console.ReadLine()!, out selectedOption); while (selectedOption == 0 || !menuOptionsSet.Contains(selectedOption)) @@ -47,8 +47,6 @@ public void ShowMenu() if (selectedOption == 1) StartGame(); else if (selectedOption == 2) ShowHistory(); } - - Environment.Exit(0); } public void StartGame() From 498a0e05369e91811096c3b755911cd99c3392fa Mon Sep 17 00:00:00 2001 From: Sumanth Reddy Date: Sat, 24 Jan 2026 10:01:09 -0500 Subject: [PATCH 5/5] Refactor question generation logic for division operation to ensure valid results --- GameRound.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/GameRound.cs b/GameRound.cs index f0d5bdcd..74384a5d 100644 --- a/GameRound.cs +++ b/GameRound.cs @@ -33,10 +33,9 @@ private void GenerateQuestionsForOperation() } else if (Operation == '/') { - while (a % b != 0) - { - b = rnd.Next(2, a); - } + int k = rnd.Next(1, 21); + b = rnd.Next(2, 21); + a = b * k; } var question = (a, b);