diff --git a/BaseOperation.cs b/BaseOperation.cs new file mode 100644 index 00000000..e46de18d --- /dev/null +++ b/BaseOperation.cs @@ -0,0 +1,48 @@ +public abstract class BaseOperation +{ + protected static readonly Random _Random = new Random(); + public int firstNumber {get; protected set;} + public int secondNumber {get; protected set;} + public abstract int result { get; } + public abstract string operatorSymbol { get; } + + public BaseOperation() + { + firstNumber = _Random.Next(1,11); + secondNumber = _Random.Next(1,11); + } + +} + +public class AdditionOperation : BaseOperation +{ + public override string operatorSymbol => "+"; + public override int result => firstNumber + secondNumber; +} + +public class SubtractionOperation : BaseOperation +{ + public override string operatorSymbol => "-"; + public override int result => firstNumber - secondNumber; +} + +public class MultiplicationOperation : BaseOperation +{ + public override string operatorSymbol => "*"; + public override int result => firstNumber * secondNumber; +} + +public class DivisionOperation : BaseOperation +{ + public override int result => firstNumber / secondNumber; + public override string operatorSymbol => "/"; + + public DivisionOperation() + { + secondNumber = _Random.Next(1,11); + int quotient = _Random.Next(1,11); + + firstNumber = secondNumber * quotient; + } + +} \ No newline at end of file diff --git a/CodeReviews.Console.MathGame.csproj b/CodeReviews.Console.MathGame.csproj new file mode 100644 index 00000000..2150e379 --- /dev/null +++ b/CodeReviews.Console.MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/GameHistory.cs b/GameHistory.cs new file mode 100644 index 00000000..8bca974c --- /dev/null +++ b/GameHistory.cs @@ -0,0 +1,25 @@ + +public interface IGameHistory +{ + List savedQuestions { get; set; } + + void SaveQuestions(List Questions); +} + +public class GameHistory : IGameHistory +{ + public List savedQuestions { get; set; } = new List(); + + public void SaveQuestions(List Questions) + { + foreach (var q in Questions) + { + savedQuestions.Add(q); + } + } + + public override string ToString() + { + return string.Join("\n", savedQuestions.Select(qstn => $"{qstn.firstNumber} {qstn.operatorSymbol} {qstn.secondNumber} = {qstn.result}")); + } +} \ No newline at end of file diff --git a/HandleUserChoice.cs b/HandleUserChoice.cs new file mode 100644 index 00000000..efc86513 --- /dev/null +++ b/HandleUserChoice.cs @@ -0,0 +1,23 @@ +public class HandleUserChoice +{ + QuestionGenerator _questionGenerator; + HandleGame _gameHandler; + IGameHistory _gameHistory; + + public HandleUserChoice(QuestionGenerator questionGenerator, HandleGame gameHandler, IGameHistory gamehistory) + { + _questionGenerator = questionGenerator; + _gameHandler = gameHandler; + _gameHistory = gamehistory; + } + public void displayHistory() + { + Console.WriteLine(_gameHistory.ToString()); + } + + public void initGame(Operation op) + { + List questions = _questionGenerator.GenerateQuestions(op); + _gameHandler.Play(questions); + } +} \ No newline at end of file diff --git a/MathQuizApp.cs b/MathQuizApp.cs new file mode 100644 index 00000000..150f1e45 --- /dev/null +++ b/MathQuizApp.cs @@ -0,0 +1,35 @@ +public class MathQuizApp +{ + public UserGameInteract _interact; + public HandleUserChoice _choiceHandler; + + public MathQuizApp(UserGameInteract interact, HandleUserChoice choiceHandler) + { + _interact = interact; + _choiceHandler = choiceHandler; + } + + public void Run() + { + Operation chosenOp; + bool endApp = false; + + while (!endApp) + { + Menu m = _interact.PromptMenu(); + switch (m) + { + case Menu.History: + _choiceHandler.displayHistory(); + break; + case Menu.Play: + chosenOp = _interact.PromptOperation(); + _choiceHandler.initGame(chosenOp); + break; + case Menu.Exit: + endApp = true; + break; + } + } + } +} \ No newline at end of file diff --git a/Menu.cs b/Menu.cs new file mode 100644 index 00000000..5fb35827 --- /dev/null +++ b/Menu.cs @@ -0,0 +1,6 @@ +public enum Menu +{ + History, + Play, + Exit +}; \ No newline at end of file diff --git a/Operation.cs b/Operation.cs new file mode 100644 index 00000000..08a9d7d5 --- /dev/null +++ b/Operation.cs @@ -0,0 +1,7 @@ +public enum Operation +{ + Addition, + Subtraction, + Multiplication, + Division +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 00000000..f2a260a9 --- /dev/null +++ b/Program.cs @@ -0,0 +1,17 @@ +public class Program +{ + public static void Main(String[] args) + { + + IGameHistory gameHistory = new GameHistory(); + + MathQuizApp App = new MathQuizApp(new UserGameInteract(), + new HandleUserChoice( + new QuestionGenerator( + gameHistory), + new HandleGame( + new ScoreHandler()), + gameHistory)); + App.Run(); + } +} \ No newline at end of file diff --git a/QuestionGenerator.cs b/QuestionGenerator.cs new file mode 100644 index 00000000..e3682257 --- /dev/null +++ b/QuestionGenerator.cs @@ -0,0 +1,33 @@ +public class QuestionGenerator +{ + private IGameHistory _questionHistory; + + public QuestionGenerator(IGameHistory questionHistory) + { + _questionHistory = questionHistory; + } + private static readonly Dictionary> operation = new Dictionary>() + { + [Operation.Addition] = () => new AdditionOperation(), + [Operation.Subtraction] = () => new SubtractionOperation(), + [Operation.Multiplication] = () => new MultiplicationOperation(), + [Operation.Division] = () => new DivisionOperation(), + }; + public List GenerateQuestions(Operation chosenOp) + { + if (!operation.TryGetValue(chosenOp, out var createOperation)) + { + throw new Exception("specified function is not there"); + } + + List questions = new List(); + for (int i = 0; i < 5; i++) + { + questions.Add(createOperation()); + } + + _questionHistory.SaveQuestions(questions); + + return questions; + } +} \ No newline at end of file diff --git a/ScoreHandler.cs b/ScoreHandler.cs new file mode 100644 index 00000000..7e3e5533 --- /dev/null +++ b/ScoreHandler.cs @@ -0,0 +1,9 @@ +public interface IScoreManager +{ + int score {get; set;} +} + +public class ScoreHandler : IScoreManager +{ + public int score { get; set;} +} \ No newline at end of file diff --git a/UserGameInteract.cs b/UserGameInteract.cs new file mode 100644 index 00000000..159708e5 --- /dev/null +++ b/UserGameInteract.cs @@ -0,0 +1,31 @@ +public class UserGameInteract +{ + public Menu PromptMenu() + { + Console.WriteLine("Welcome to Math quiz game! Please enter the choice:"); + + for(int i = 0; i < Enum.GetNames(typeof(Menu)).Length; i++) + { + Console.WriteLine($"{i}: {Enum.GetName(typeof(Menu), i)}"); + } + + + int menuChoice = int.Parse(Console.ReadLine()); + return (Menu)menuChoice; + } + + public Operation PromptOperation() + { + Console.WriteLine("Please enter which operation you would like to have: "); + + for (int i = 0; i < Enum.GetNames(typeof(Operation)).Length; i++) + { + Console.WriteLine($"{i}: {Enum.GetName(typeof(Operation), i)}"); + } + + int operationChoice = int.Parse(Console.ReadLine()); + return (Operation)operationChoice; + } + + +} \ No newline at end of file diff --git a/handleGame.cs b/handleGame.cs new file mode 100644 index 00000000..056edd3a --- /dev/null +++ b/handleGame.cs @@ -0,0 +1,41 @@ + +using System.Formats.Asn1; + +public class HandleGame +{ + private const int QUESTION_AMOUNT = 5; + private IScoreManager _scoreHandler; + + public HandleGame(IScoreManager scoreHandler) + { + _scoreHandler = scoreHandler; + } + public void Play(List questions) + { + int rounds = 0; + + while (rounds < QUESTION_AMOUNT) + { + Console.WriteLine($"{questions[rounds].firstNumber} {questions[rounds].operatorSymbol} {questions[rounds].secondNumber}"); + + if (!int.TryParse(Console.ReadLine(), out int answer)) + { + Console.WriteLine("Invalid input, please enter your answer as a number"); + } + + if (answer == questions[rounds].result) + { + Console.WriteLine("correct you get a point!"); + _scoreHandler.score++; + } + else + { + Console.WriteLine("Incorrect, you do not get a point."); + } + + rounds++; + } + + Console.WriteLine($"That's the end of the game! you scored: {_scoreHandler.score}"); + } +} \ No newline at end of file