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..2a0b77cf --- /dev/null +++ b/ConsoleUserInteraction.cs @@ -0,0 +1,66 @@ +namespace CodeReviews.MathGame +{ + public class ConsoleUserInteraction + { + int userPoints = 0; + private GameLog log; + + public ConsoleUserInteraction() + { + log = new GameLog(); + } + + 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 userAnswer = GetUserInput(); + bool isCorrect = userAnswer == result; + + if (isCorrect) + { + Console.WriteLine("Correct!"); + userPoints++; + } + else + { + Console.WriteLine($"Incorrect! The correct answer is {result}."); + } + + log.AddLog($"{item.Value[0]} {symbol} {item.Value[1]} = {result} | your answer: {userAnswer}"); + } + + public int GetUserPoints() + { + return userPoints; + } + + public void SetGameLog(GameLog gameLog) + { + this.log = gameLog; + } + + public void ShowGameLog() + { + log.ShowLog(); + } + + } +} 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/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/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..7ea74d98 --- /dev/null +++ b/Program.cs @@ -0,0 +1,62 @@ +namespace CodeReviews.MathGame +{ + class Program + { + static void Main(string[] args) + { + int result = 0; + Console.WriteLine("Welcome to the Math Game!"); + var consoleUserInteraction = new ConsoleUserInteraction(); + + while (true) + { + int choice = PromptMenuChoice(); + + 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 int PromptMenuChoice() + { + Console.WriteLine("Choose a type of operation: "); + foreach (Operation op in Enum.GetValues(typeof(Operation))) + Console.WriteLine($"{(int)op}. {op.GetDescription()}"); + + Console.WriteLine("8. View logs"); + Console.WriteLine("9. Exit"); + + 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 new file mode 100644 index 00000000..f3ab33cb --- /dev/null +++ b/QuestionGenerator.cs @@ -0,0 +1,63 @@ +using CodeReviews.MathGame; + +public class QuestionGenerator +{ + private const int NUMBER_OF_QUESTION = 5; + + private 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(ConsoleUserInteraction consoleUserInteraction) + { + Dictionary> mapOfQuestion = RandomNumberAndQuestion(); + IMathOperation op = _operations[operation]; + + + 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; + } +}