Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions BaseOperation.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
10 changes: 10 additions & 0 deletions CodeReviews.Console.MathGame.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions GameHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

public interface IGameHistory
{
List<BaseOperation> savedQuestions { get; set; }

void SaveQuestions(List<BaseOperation> Questions);
}

public class GameHistory : IGameHistory
{
public List<BaseOperation> savedQuestions { get; set; } = new List<BaseOperation>();

public void SaveQuestions(List<BaseOperation> 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}"));
}
}
23 changes: 23 additions & 0 deletions HandleUserChoice.cs
Original file line number Diff line number Diff line change
@@ -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<BaseOperation> questions = _questionGenerator.GenerateQuestions(op);
_gameHandler.Play(questions);
}
}
35 changes: 35 additions & 0 deletions MathQuizApp.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
6 changes: 6 additions & 0 deletions Menu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public enum Menu
{
History,
Play,
Exit
};
7 changes: 7 additions & 0 deletions Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public enum Operation
{
Addition,
Subtraction,
Multiplication,
Division
}
17 changes: 17 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
33 changes: 33 additions & 0 deletions QuestionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class QuestionGenerator
{
private IGameHistory _questionHistory;

public QuestionGenerator(IGameHistory questionHistory)
{
_questionHistory = questionHistory;
}
private static readonly Dictionary<Operation, Func<BaseOperation>> operation = new Dictionary<Operation, Func<BaseOperation>>()
{
[Operation.Addition] = () => new AdditionOperation(),
[Operation.Subtraction] = () => new SubtractionOperation(),
[Operation.Multiplication] = () => new MultiplicationOperation(),
[Operation.Division] = () => new DivisionOperation(),
};
public List<BaseOperation> GenerateQuestions(Operation chosenOp)
{
if (!operation.TryGetValue(chosenOp, out var createOperation))
{
throw new Exception("specified function is not there");
}

List<BaseOperation> questions = new List<BaseOperation>();
for (int i = 0; i < 5; i++)
{
questions.Add(createOperation());
}

_questionHistory.SaveQuestions(questions);

return questions;
}
}
9 changes: 9 additions & 0 deletions ScoreHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public interface IScoreManager
{
int score {get; set;}
}

public class ScoreHandler : IScoreManager
{
public int score { get; set;}
}
31 changes: 31 additions & 0 deletions UserGameInteract.cs
Original file line number Diff line number Diff line change
@@ -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;
}


}
41 changes: 41 additions & 0 deletions handleGame.cs
Original file line number Diff line number Diff line change
@@ -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<BaseOperation> 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}");
}
}