diff --git a/CodeReviews.Console.MathGame.sln b/CodeReviews.Console.MathGame.sln new file mode 100644 index 00000000..344bf09b --- /dev/null +++ b/CodeReviews.Console.MathGame.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}") = "Tenebris-06.MathGame", "Tenebris-06.MathGame\Tenebris-06.MathGame.csproj", "{3E3BF72D-9C95-C189-A25A-77F3C1042C60}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1F3971B0-9543-463C-8B64-17441F96B5FB} + EndGlobalSection +EndGlobal diff --git a/Tenebris-06.MathGame/Game.cs b/Tenebris-06.MathGame/Game.cs new file mode 100644 index 00000000..56881b42 --- /dev/null +++ b/Tenebris-06.MathGame/Game.cs @@ -0,0 +1,10 @@ +public class Game +{ + public int score; + public List problemSet; + public Game(int score, List problemSet) + { + this.score = score; + this.problemSet = problemSet; + } +} \ No newline at end of file diff --git a/Tenebris-06.MathGame/MenuOptionEnum.cs b/Tenebris-06.MathGame/MenuOptionEnum.cs new file mode 100644 index 00000000..cfd90813 --- /dev/null +++ b/Tenebris-06.MathGame/MenuOptionEnum.cs @@ -0,0 +1,9 @@ +public enum MenuOption +{ + Exit = 0, + Add = 1, + Subtract, + Multiply, + Divide, + History +} \ No newline at end of file diff --git a/Tenebris-06.MathGame/Problem.cs b/Tenebris-06.MathGame/Problem.cs new file mode 100644 index 00000000..ac374f74 --- /dev/null +++ b/Tenebris-06.MathGame/Problem.cs @@ -0,0 +1,61 @@ +public class Problem +{ + public string Text; + public int Answer; + public int UserAnswer; + public bool CorrectlyAnswered; + + + public Problem(string Text, int Answer) + { + this.Text = Text; + this.Answer = Answer; + } + + public void SetText(string Text) + { + this.Text = Text; + } + + public void SetAnswer(int Answer) + { + this.Answer = Answer; + } + + public string GetText() + { + return Text; + } + + public int GetAnswer() + { + return Answer; + } + + public void SetUserAnswer(int userAnswer) + { + this.UserAnswer = userAnswer; + } + + public int GetUserAnswer() + { + return UserAnswer; + } + + public override string ToString() + { + string AnswerState; + + switch (CorrectlyAnswered) + { + case true: + AnswerState = "✔"; + break; + + case false: + AnswerState = "✘"; + break; + } + return $"{Text.PadRight(12)}{UserAnswer.ToString().PadRight(12)}{Answer.ToString().PadRight(12)}{AnswerState.PadLeft(10)}"; + } +} \ No newline at end of file diff --git a/Tenebris-06.MathGame/ProblemGenerator.cs b/Tenebris-06.MathGame/ProblemGenerator.cs new file mode 100644 index 00000000..4d7358d9 --- /dev/null +++ b/Tenebris-06.MathGame/ProblemGenerator.cs @@ -0,0 +1,66 @@ +public static class ProblemGenerator +{ + static Random randomizer = new Random(); + static int rand1; + static int rand2; + public static Problem GenerateProblem(MenuOption operatorValue) + { + do + { + rand1 = randomizer.Next(10); + rand2 = randomizer.Next(10); + + if (!(operatorValue == MenuOption.Divide)) break; + + } while (rand2 == 0 || rand1 % rand2 != 0); + + string problemText = $"{rand1} {GetOperator(operatorValue)} {rand2} = ?"; + int problemAnswer = -1; + + switch (operatorValue) + { + case MenuOption.Add: + problemAnswer = rand1 + rand2; + break; + + case MenuOption.Subtract: + problemAnswer = rand1 - rand2; + break; + + case MenuOption.Multiply: + problemAnswer = rand1 * rand2; + break; + + case MenuOption.Divide: + problemAnswer = rand1 / rand2; + break; + + default: + break; + } + + return new Problem(problemText, problemAnswer); + + } + + public static string GetOperator(MenuOption operatorValue) + { + switch (operatorValue) + { + case MenuOption.Add: + return "+"; + + case MenuOption.Subtract: + return "-"; + + case MenuOption.Multiply: + return "*"; + + case MenuOption.Divide: + return "/"; + + default: + return ""; + } + } +} \ No newline at end of file diff --git a/Tenebris-06.MathGame/Program.cs b/Tenebris-06.MathGame/Program.cs new file mode 100644 index 00000000..5f3aaec3 --- /dev/null +++ b/Tenebris-06.MathGame/Program.cs @@ -0,0 +1,78 @@ +class Program +{ + static void Main(string[] args) + { + + List gameHistory = new List(); + + while (true) { + + Console.WriteLine("\t====== Math Game ======\nPick an Option:"); + Console.WriteLine("[1] Addition \n[2] Subtraction \n[3] Multiplication \n[4] Division \n[5] History \n[0] Exit"); + Console.Write("=> "); + + int userChoice; + + if (!(int.TryParse(Console.ReadLine(), out userChoice))) + { + Console.WriteLine("Invalid Input!"); + } + + if ((MenuOption)userChoice == MenuOption.History) + { + Console.WriteLine("\t=== Game History ==="); + + for (int i = 0; i <= gameHistory.Count - 1; i++) + { + Console.WriteLine($"\n[Game {i + 1}] (Score: {gameHistory[i].score})"); + Console.WriteLine($"{"Problem".PadRight(12)}{"Your Answer".PadRight(12)}{"Correct Answer".PadRight(12)}{"Status".PadLeft(10)}"); + foreach (Problem p in gameHistory[i].problemSet) + { + Console.WriteLine(p.ToString()); + } + } + + Console.WriteLine("Press any key to go back to main menu => "); + Console.ReadKey(); + continue; + + } else if ((MenuOption)userChoice == MenuOption.Exit) + { + Console.WriteLine("Goodbye!"); + Environment.Exit(0); + } + + int score = 0; + List ProblemSet = new List(); + + for (int i = 0; i <= 4; i++) + { + // cast the userChoice to it's corresponding enum option + + Problem generatedProblem = ProblemGenerator.GenerateProblem((MenuOption)userChoice); + ProblemSet.Add(generatedProblem); + Console.WriteLine($"\t == Problem {i}: =="); + Console.WriteLine($"{generatedProblem.Text}"); + Console.Write("=> "); + int.TryParse(Console.ReadLine(), out int userAnswer); + + generatedProblem.SetUserAnswer(userAnswer); + + if (userAnswer == generatedProblem.Answer) + { + Console.WriteLine("Correct!"); + generatedProblem.CorrectlyAnswered = true; + score++; + } else + { + Console.WriteLine("Wrong!"); + generatedProblem.CorrectlyAnswered = false; + } + } + + Game currentGame = new Game(score, ProblemSet); + gameHistory.Add(currentGame); + + } +} +} diff --git a/Tenebris-06.MathGame/Tenebris-06.MathGame.csproj b/Tenebris-06.MathGame/Tenebris-06.MathGame.csproj new file mode 100644 index 00000000..2f23d3c0 --- /dev/null +++ b/Tenebris-06.MathGame/Tenebris-06.MathGame.csproj @@ -0,0 +1,11 @@ + + + + Exe + net9.0 + Tenebris_06.MathGame + enable + enable + + +