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..86e227e5 --- /dev/null +++ b/ConsoleMathGame/GameLogic.cs @@ -0,0 +1,166 @@ +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 userAnswer; + + 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") + }; + + bool validInput = int.TryParse(response, out userAnswer); + if (validInput && userAnswer == 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(); + } + } +}