From 9a012bcc08b73698fab6f7aa992687bb7353082e Mon Sep 17 00:00:00 2001 From: Jackson Cmelak Date: Thu, 26 Feb 2026 11:10:24 -0800 Subject: [PATCH 1/3] Fixed git submodule --- ConsoleMathGame/ConsoleMathGame.csproj | 10 ++ ConsoleMathGame/ConsoleMathGame.sln | 24 ++++ ConsoleMathGame/GameLogic.cs | 164 +++++++++++++++++++++++++ ConsoleMathGame/Program.cs | 55 +++++++++ 4 files changed, 253 insertions(+) create mode 100644 ConsoleMathGame/ConsoleMathGame.csproj create mode 100644 ConsoleMathGame/ConsoleMathGame.sln create mode 100644 ConsoleMathGame/GameLogic.cs create mode 100644 ConsoleMathGame/Program.cs diff --git a/ConsoleMathGame/ConsoleMathGame.csproj b/ConsoleMathGame/ConsoleMathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/ConsoleMathGame/ConsoleMathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/ConsoleMathGame/ConsoleMathGame.sln b/ConsoleMathGame/ConsoleMathGame.sln new file mode 100644 index 00000000..f3ea9aef --- /dev/null +++ b/ConsoleMathGame/ConsoleMathGame.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}") = "ConsoleMathGame", "ConsoleMathGame.csproj", "{98C51DDF-AF71-1511-B745-A8B42D2494AA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {98C51DDF-AF71-1511-B745-A8B42D2494AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98C51DDF-AF71-1511-B745-A8B42D2494AA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98C51DDF-AF71-1511-B745-A8B42D2494AA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {98C51DDF-AF71-1511-B745-A8B42D2494AA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {794E7496-4DFE-4778-AA51-94AD03C5EFD4} + EndGlobalSection +EndGlobal diff --git a/ConsoleMathGame/GameLogic.cs b/ConsoleMathGame/GameLogic.cs new file mode 100644 index 00000000..9d1db686 --- /dev/null +++ b/ConsoleMathGame/GameLogic.cs @@ -0,0 +1,164 @@ +namespace MathGame { + public class GameLogic { + public List GameHistory { get; set; } = new List(); + + public enum MathOperation + { + Addition, + Subtraction, + Multiplication, + Division + } + + public enum Difficulty + { + Easy, + Medium, + Hard + } + + private int[] GenerateNumbers(Difficulty difficulty, MathOperation operation) + { + var rng = new Random(); + int maxValue; + int[] operands = new int[2]; + + switch (difficulty) + { + case Difficulty.Easy: + maxValue = 20; + break; + case Difficulty.Medium: + maxValue = 50; + break; + case Difficulty.Hard: + maxValue = 100; + break; + default: + return operands; + } + + if (operation == MathOperation.Division) + { + operands[1] = (int)rng.NextInt64(100) + 1; + operands[0] = (int)((rng.NextInt64(maxValue)+1) * operands[1]); + } + else + { + operands[0] = (int)rng.NextInt64(maxValue+1); + operands[1] = (int)rng.NextInt64(maxValue+1); + } + + return operands; + } + + public void NewGame() + { + Console.Clear(); + //Get difficulty input + Console.WriteLine("Please select a difficulty:"); + Console.WriteLine("[1] Easy"); + Console.WriteLine("[2] Medium"); + Console.WriteLine("[3] Hard"); + Console.WriteLine("[anykey] Exit Current Game"); + Difficulty difficulty; + var input = Console.ReadKey(); + int score = 0; + Console.WriteLine(); + + switch (input.KeyChar) + { + case '1': + difficulty = Difficulty.Easy; + break; + case '2': + difficulty = Difficulty.Medium; + break; + case '3': + difficulty = Difficulty.Hard; + break; + default: + return; + } + + Console.Clear(); + //Get operation input + Console.WriteLine("Please select an operation:"); + Console.WriteLine("[1] Addition"); + Console.WriteLine("[2] Subtraction"); + Console.WriteLine("[3] Multiplication"); + Console.WriteLine("[4] Division"); + Console.WriteLine("[anykey] Exit Current Game"); + MathOperation operation; + input = Console.ReadKey(); + Console.WriteLine(); + + switch (input.KeyChar) + { + case '1': + operation = MathOperation.Addition; + break; + case '2': + operation = MathOperation.Subtraction; + break; + case '3': + operation = MathOperation.Multiplication; + break; + case '4': + operation = MathOperation.Division; + break; + default: + return; + }; + + var opSign = operation switch + { + MathOperation.Addition => "+", + MathOperation.Subtraction => "-", + MathOperation.Multiplication => "*", + MathOperation.Division => "/", + _ => throw new ArgumentException("Invalid Operation") + }; + + for(int i = 0; i < 5; i++) + { + int[] operands = GenerateNumbers(difficulty, operation); + + string question = $"{operands[0]} {opSign} {operands[1]}"; + Console.WriteLine(question); + var response = Console.ReadLine(); + + int answer = operation switch + { + MathOperation.Addition => operands[0] + operands[1], + MathOperation.Subtraction => operands[0] - operands[1], + MathOperation.Multiplication => operands[0] * operands[1], + MathOperation.Division => operands[0] / operands[1], + _ => throw new ArgumentException("Invalid operation") + }; + + if (int.Parse(response) == answer) + { + score++; + Console.WriteLine($"Correct! Your score is now {score}/{i+1}"); + GameHistory.Add(question + $" = {answer}"); + } else + { + Console.WriteLine($"Whoops! The correct answer was {answer}. Your score is now {score}/{i+1}"); + GameHistory.Add(question + $" =/= {response}\tINCORRECT"); + } + + if(i != 4) { + for(int j = 3; j > 0; j--) + { + Console.Write($"\rNext question in {j}..."); + Thread.Sleep(700); + } + } + Console.Clear(); + } + + Console.WriteLine($"You got {score}/5 questions correct."); + } + } +} \ No newline at end of file diff --git a/ConsoleMathGame/Program.cs b/ConsoleMathGame/Program.cs new file mode 100644 index 00000000..84dafade --- /dev/null +++ b/ConsoleMathGame/Program.cs @@ -0,0 +1,55 @@ +/* +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 in 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 deleted. +*/ + +using MathGame; +var game = new GameLogic(); + +bool playing = true; + +while(playing){ + Console.Clear(); + Console.WriteLine("Start a new game? y/n"); + var input = Console.ReadKey(); + + if(input.KeyChar != 'y') + { + playing = false; + } + else + { + game.NewGame(); + } + + if(playing) + { + Console.WriteLine("View Game History? y/n"); + input = Console.ReadKey(); + if(input.KeyChar != 'y') + { + continue; + } + else + { + Console.Clear(); + foreach (string question in game.GameHistory) + { + Console.WriteLine(question); + } + + Console.WriteLine("Questions answered: " + game.GameHistory.Count); + Console.WriteLine("Press any key to continue..."); + Console.ReadKey(); + } + } +} From 65d7e92ca861bae99ec60685294aa113774daa3a Mon Sep 17 00:00:00 2001 From: Jackson Cmelak Date: Thu, 26 Feb 2026 12:03:32 -0800 Subject: [PATCH 2/3] Fixed null parse warning --- ConsoleMathGame/GameLogic.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ConsoleMathGame/GameLogic.cs b/ConsoleMathGame/GameLogic.cs index 9d1db686..b89d54bd 100644 --- a/ConsoleMathGame/GameLogic.cs +++ b/ConsoleMathGame/GameLogic.cs @@ -127,6 +127,7 @@ public void NewGame() string question = $"{operands[0]} {opSign} {operands[1]}"; Console.WriteLine(question); var response = Console.ReadLine(); + int userAnswer; int answer = operation switch { @@ -137,7 +138,8 @@ public void NewGame() _ => throw new ArgumentException("Invalid operation") }; - if (int.Parse(response) == answer) + bool validInput = int.TryParse(response, out userAnswer); + if (validInput && userAnswer == answer) { score++; Console.WriteLine($"Correct! Your score is now {score}/{i+1}"); From 918d6a1c0c5de50687b74df33347f2cdf0406cd5 Mon Sep 17 00:00:00 2001 From: Jackson Cmelak Date: Thu, 26 Feb 2026 12:09:04 -0800 Subject: [PATCH 3/3] Remove empty statement --- ConsoleMathGame/GameLogic.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConsoleMathGame/GameLogic.cs b/ConsoleMathGame/GameLogic.cs index b89d54bd..86e227e5 100644 --- a/ConsoleMathGame/GameLogic.cs +++ b/ConsoleMathGame/GameLogic.cs @@ -109,7 +109,7 @@ public void NewGame() break; default: return; - }; + } var opSign = operation switch {