From 58bcee6fb30ffffb2019bedc632dad6938efbf4a Mon Sep 17 00:00:00 2001 From: raonieqr Date: Mon, 13 Jul 2026 21:38:57 -0300 Subject: [PATCH 1/2] feat: implementation of the game's core logic --- AdditionOperation.cs | 9 ++++++ CodeReviews.MathGame.csproj | 10 ++++++ CodeReviews.MathGame.slnx | 3 ++ ConsoleUserInteraction.cs | 46 ++++++++++++++++++++++++++ DivisionOperation.cs | 15 +++++++++ EnumExtension.cs | 15 +++++++++ IMathOperation.cs | 10 ++++++ MultiplicationOperation.cs | 10 ++++++ Operation.cs | 16 ++++++++++ Program.cs | 41 ++++++++++++++++++++++++ QuestionGenerator.cs | 64 +++++++++++++++++++++++++++++++++++++ SubtractionOperation.cs | 10 ++++++ 12 files changed, 249 insertions(+) create mode 100644 AdditionOperation.cs create mode 100644 CodeReviews.MathGame.csproj create mode 100644 CodeReviews.MathGame.slnx create mode 100644 ConsoleUserInteraction.cs create mode 100644 DivisionOperation.cs create mode 100644 EnumExtension.cs create mode 100644 IMathOperation.cs create mode 100644 MultiplicationOperation.cs create mode 100644 Operation.cs create mode 100644 Program.cs create mode 100644 QuestionGenerator.cs create mode 100644 SubtractionOperation.cs diff --git a/AdditionOperation.cs b/AdditionOperation.cs new file mode 100644 index 00000000..81fae9d9 --- /dev/null +++ b/AdditionOperation.cs @@ -0,0 +1,9 @@ +namespace CodeReviews.MathGame +{ + public class AdditionOperation : IMathOperation + { + public Operation Type => Operation.Addition; + public string Symbol => "+"; + public int Calculate(int a, int b) => a + b; + } +} diff --git a/CodeReviews.MathGame.csproj b/CodeReviews.MathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/CodeReviews.MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/CodeReviews.MathGame.slnx b/CodeReviews.MathGame.slnx new file mode 100644 index 00000000..b1e545f8 --- /dev/null +++ b/CodeReviews.MathGame.slnx @@ -0,0 +1,3 @@ + + + diff --git a/ConsoleUserInteraction.cs b/ConsoleUserInteraction.cs new file mode 100644 index 00000000..44bd645c --- /dev/null +++ b/ConsoleUserInteraction.cs @@ -0,0 +1,46 @@ +namespace CodeReviews.MathGame +{ + internal class ConsoleUserInteraction + { + int userPoints = 0; + + private int GetUserInput() + { + while (true) + { + Console.WriteLine("Enter your answer: "); + var input = Console.ReadLine(); + if (input != null && int.TryParse(input, out var value)) + { + return value; + } + } + + throw new ArgumentException("Invalid input. Please enter a valid integer."); + + } + + public void ShowQuestion(KeyValuePair> item, int result, string symbol) + { + + Console.WriteLine($"Question: {item.Key} - What is {item.Value[0]} {symbol} {item.Value[1]}?"); + + int resultOfQuestion = GetUserInput(); + if (resultOfQuestion == result) + { + Console.WriteLine("Correct!"); + userPoints++; + } + else + { + Console.WriteLine($"Incorrect! The correct answer is {result}."); + } + } + + public int GetUserPoints() + { + return userPoints; + } + + } +} diff --git a/DivisionOperation.cs b/DivisionOperation.cs new file mode 100644 index 00000000..6035682d --- /dev/null +++ b/DivisionOperation.cs @@ -0,0 +1,15 @@ + +namespace CodeReviews.MathGame +{ + public class DivisionOperation : IMathOperation + { + public Operation Type => Operation.Division; + public string Symbol => "/"; + public int Calculate(int a, int b) + { + if (b == 0) + throw new DivideByZeroException(); + return a / b; + } + } +} diff --git a/EnumExtension.cs b/EnumExtension.cs new file mode 100644 index 00000000..c70e1441 --- /dev/null +++ b/EnumExtension.cs @@ -0,0 +1,15 @@ +using System.ComponentModel; +using System.Reflection; + +namespace CodeReviews.MathGame +{ + public static class EnumExtension + { + public static string GetDescription(this Enum value) + { + FieldInfo? field = value.GetType().GetField(value.ToString()); + DescriptionAttribute? attribute = field?.GetCustomAttribute(); + return attribute?.Description ?? value.ToString(); + } + } +} diff --git a/IMathOperation.cs b/IMathOperation.cs new file mode 100644 index 00000000..6473e6c7 --- /dev/null +++ b/IMathOperation.cs @@ -0,0 +1,10 @@ + +namespace CodeReviews.MathGame +{ + public interface IMathOperation + { + Operation Type { get; } + string Symbol { get; } + int Calculate(int a, int b); + } +} diff --git a/MultiplicationOperation.cs b/MultiplicationOperation.cs new file mode 100644 index 00000000..97e8ddc5 --- /dev/null +++ b/MultiplicationOperation.cs @@ -0,0 +1,10 @@ + +namespace CodeReviews.MathGame +{ + public class MultiplicationOperation : IMathOperation + { + public Operation Type => Operation.Multiplication; + public string Symbol => "*"; + public int Calculate(int a, int b) => a * b; + } +} diff --git a/Operation.cs b/Operation.cs new file mode 100644 index 00000000..f96fa216 --- /dev/null +++ b/Operation.cs @@ -0,0 +1,16 @@ +using System.ComponentModel; + +public enum Operation +{ + [Description("Addition")] + Addition = 1, + + [Description("Subtraction")] + Subtraction = 2, + + [Description("Division")] + Division = 3, + + [Description("Multiplication")] + Multiplication = 4 +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 00000000..3b99b93b --- /dev/null +++ b/Program.cs @@ -0,0 +1,41 @@ +namespace CodeReviews.MathGame +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Welcome to the Math Game!"); + var operation = PromptOperationChoice(); + + if (operation == null) + { + Console.WriteLine("Invalid operation selected."); + return; + } + + try + { + var questionGenerator = new QuestionGenerator(operation.Value); + int result = questionGenerator.calculate(); + Console.WriteLine($"Your total points: {result} points"); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } + + static Operation? PromptOperationChoice() + { + Console.WriteLine("Choose a type of operation: "); + foreach (Operation op in Enum.GetValues(typeof(Operation))) + Console.WriteLine($"{(int)op}. {op.GetDescription()}"); + + string? input = Console.ReadLine(); + if (int.TryParse(input, out var value) && Enum.IsDefined(typeof(Operation), value)) + return (Operation)value; + + return null; + } + } +} \ No newline at end of file diff --git a/QuestionGenerator.cs b/QuestionGenerator.cs new file mode 100644 index 00000000..f12bb54a --- /dev/null +++ b/QuestionGenerator.cs @@ -0,0 +1,64 @@ +using CodeReviews.MathGame; + +public class QuestionGenerator +{ + private const int NUMBER_OF_QUESTION = 5; + + Operation operation; + + public QuestionGenerator(Operation operation) + { + this.operation = operation; + } + + private readonly Dictionary _operations = new() + { + { Operation.Addition, new AdditionOperation() }, + { Operation.Subtraction, new SubtractionOperation() }, + { Operation.Division, new DivisionOperation() }, + { Operation.Multiplication, new MultiplicationOperation() }, + }; + + public int calculate() + { + Dictionary> mapOfQuestion = RandomNumberAndQuestion(); + IMathOperation op = _operations[operation]; + + ConsoleUserInteraction consoleUserInteraction = new ConsoleUserInteraction(); + + foreach (KeyValuePair> item in mapOfQuestion) + { + int numberOne = item.Value[0]; + int numberTwo = item.Value[1]; + try + { + int result = op.Calculate(numberOne, numberTwo); + consoleUserInteraction.ShowQuestion(item, result, op.Symbol); + } + catch (DivideByZeroException) + { + Console.WriteLine("Skipping division by zero question."); + continue; + } + + } + + return consoleUserInteraction.GetUserPoints(); + + } + + + public Dictionary> RandomNumberAndQuestion() + { + Dictionary> questions = new Dictionary>(); + Random random = new Random(); + + for (int i = 0; i < NUMBER_OF_QUESTION; i++) + { + questions.Add($"Question {i + 1}", new List { random.Next(0, 100), random.Next(1, 100) }); + } + + return questions; + } + +} diff --git a/SubtractionOperation.cs b/SubtractionOperation.cs new file mode 100644 index 00000000..af9cff5a --- /dev/null +++ b/SubtractionOperation.cs @@ -0,0 +1,10 @@ + +namespace CodeReviews.MathGame +{ + public class SubtractionOperation : IMathOperation + { + public Operation Type => Operation.Subtraction; + public string Symbol => "-"; + public int Calculate(int a, int b) => a - b; + } +} From 19da6d1c085fac2966941ef433535b65b37c5993 Mon Sep 17 00:00:00 2001 From: raonieqr Date: Wed, 15 Jul 2026 22:45:43 -0300 Subject: [PATCH 2/2] feat: implement log --- ConsoleUserInteraction.cs | 26 +++++++++++++++-- GameLog.cs | 26 +++++++++++++++++ Program.cs | 59 ++++++++++++++++++++++++++------------- QuestionGenerator.cs | 9 +++--- 4 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 GameLog.cs diff --git a/ConsoleUserInteraction.cs b/ConsoleUserInteraction.cs index 44bd645c..2a0b77cf 100644 --- a/ConsoleUserInteraction.cs +++ b/ConsoleUserInteraction.cs @@ -1,8 +1,14 @@ namespace CodeReviews.MathGame { - internal class ConsoleUserInteraction + public class ConsoleUserInteraction { int userPoints = 0; + private GameLog log; + + public ConsoleUserInteraction() + { + log = new GameLog(); + } private int GetUserInput() { @@ -25,8 +31,10 @@ public void ShowQuestion(KeyValuePair> item, int result, strin Console.WriteLine($"Question: {item.Key} - What is {item.Value[0]} {symbol} {item.Value[1]}?"); - int resultOfQuestion = GetUserInput(); - if (resultOfQuestion == result) + int userAnswer = GetUserInput(); + bool isCorrect = userAnswer == result; + + if (isCorrect) { Console.WriteLine("Correct!"); userPoints++; @@ -35,6 +43,8 @@ public void ShowQuestion(KeyValuePair> item, int result, strin { Console.WriteLine($"Incorrect! The correct answer is {result}."); } + + log.AddLog($"{item.Value[0]} {symbol} {item.Value[1]} = {result} | your answer: {userAnswer}"); } public int GetUserPoints() @@ -42,5 +52,15 @@ public int GetUserPoints() return userPoints; } + public void SetGameLog(GameLog gameLog) + { + this.log = gameLog; + } + + public void ShowGameLog() + { + log.ShowLog(); + } + } } diff --git a/GameLog.cs b/GameLog.cs new file mode 100644 index 00000000..17578837 --- /dev/null +++ b/GameLog.cs @@ -0,0 +1,26 @@ +namespace CodeReviews.MathGame +{ + public class GameLog + { + List log; + + public GameLog() { + this.log = new List(); + } + + public void AddLog(string questionAndResult) + { + this.log.Add(questionAndResult); + } + + public void ShowLog() + { + Console.WriteLine("Game Log:"); + foreach (var entry in log) + { + Console.WriteLine(entry); + } + } + + } +} diff --git a/Program.cs b/Program.cs index 3b99b93b..7ea74d98 100644 --- a/Program.cs +++ b/Program.cs @@ -4,38 +4,59 @@ class Program { static void Main(string[] args) { + int result = 0; Console.WriteLine("Welcome to the Math Game!"); - var operation = PromptOperationChoice(); + var consoleUserInteraction = new ConsoleUserInteraction(); - if (operation == null) + while (true) { - Console.WriteLine("Invalid operation selected."); - return; - } + int choice = PromptMenuChoice(); - try - { - var questionGenerator = new QuestionGenerator(operation.Value); - int result = questionGenerator.calculate(); - Console.WriteLine($"Your total points: {result} points"); - } - catch (Exception ex) - { - Console.WriteLine($"An error occurred: {ex.Message}"); + if (choice == 9) + { + Console.WriteLine("Exiting the game. Goodbye!"); + break; + } + + if (choice == 8) + { + consoleUserInteraction.ShowGameLog(); + continue; + } + + if (!Enum.IsDefined(typeof(Operation), choice)) + { + Console.WriteLine("Invalid operation selected. Try again."); + continue; + } + + var operation = (Operation)choice; + + try + { + var questionGenerator = new QuestionGenerator(operation); + result += questionGenerator.Calculate(consoleUserInteraction); + Console.WriteLine($"Your total points: {result} points"); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } } } - static Operation? PromptOperationChoice() + static int PromptMenuChoice() { Console.WriteLine("Choose a type of operation: "); foreach (Operation op in Enum.GetValues(typeof(Operation))) Console.WriteLine($"{(int)op}. {op.GetDescription()}"); - string? input = Console.ReadLine(); - if (int.TryParse(input, out var value) && Enum.IsDefined(typeof(Operation), value)) - return (Operation)value; + Console.WriteLine("8. View logs"); + Console.WriteLine("9. Exit"); - return null; + string? input = Console.ReadLine(); + int.TryParse(input, out var value); + return value; } } } \ No newline at end of file diff --git a/QuestionGenerator.cs b/QuestionGenerator.cs index f12bb54a..f3ab33cb 100644 --- a/QuestionGenerator.cs +++ b/QuestionGenerator.cs @@ -4,7 +4,7 @@ public class QuestionGenerator { private const int NUMBER_OF_QUESTION = 5; - Operation operation; + private Operation operation; public QuestionGenerator(Operation operation) { @@ -19,12 +19,11 @@ public QuestionGenerator(Operation operation) { Operation.Multiplication, new MultiplicationOperation() }, }; - public int calculate() + public int Calculate(ConsoleUserInteraction consoleUserInteraction) { Dictionary> mapOfQuestion = RandomNumberAndQuestion(); IMathOperation op = _operations[operation]; - ConsoleUserInteraction consoleUserInteraction = new ConsoleUserInteraction(); foreach (KeyValuePair> item in mapOfQuestion) { @@ -40,14 +39,14 @@ public int calculate() Console.WriteLine("Skipping division by zero question."); continue; } - + } return consoleUserInteraction.GetUserPoints(); } - + public Dictionary> RandomNumberAndQuestion() { Dictionary> questions = new Dictionary>();