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
9 changes: 9 additions & 0 deletions AdditionOperation.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions CodeReviews.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>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
3 changes: 3 additions & 0 deletions CodeReviews.MathGame.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="CodeReviews.MathGame.csproj" />
</Solution>
66 changes: 66 additions & 0 deletions ConsoleUserInteraction.cs
Original file line number Diff line number Diff line change
@@ -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<string, List<int>> 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();
}

}
}
15 changes: 15 additions & 0 deletions DivisionOperation.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
15 changes: 15 additions & 0 deletions EnumExtension.cs
Original file line number Diff line number Diff line change
@@ -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<DescriptionAttribute>();
return attribute?.Description ?? value.ToString();
}
}
}
26 changes: 26 additions & 0 deletions GameLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace CodeReviews.MathGame
{
public class GameLog
{
List<String> log;

public GameLog() {
this.log = new List<String>();
}

public void AddLog(string questionAndResult)
{
this.log.Add(questionAndResult);
}

public void ShowLog()
{
Console.WriteLine("Game Log:");
foreach (var entry in log)
{
Console.WriteLine(entry);
}
}

}
}
10 changes: 10 additions & 0 deletions IMathOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace CodeReviews.MathGame
{
public interface IMathOperation
{
Operation Type { get; }
string Symbol { get; }
int Calculate(int a, int b);
}
}
10 changes: 10 additions & 0 deletions MultiplicationOperation.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions Operation.cs
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
63 changes: 63 additions & 0 deletions QuestionGenerator.cs
Original file line number Diff line number Diff line change
@@ -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<Operation, IMathOperation> _operations = new()
{
{ Operation.Addition, new AdditionOperation() },
{ Operation.Subtraction, new SubtractionOperation() },
{ Operation.Division, new DivisionOperation() },
{ Operation.Multiplication, new MultiplicationOperation() },
};

public int Calculate(ConsoleUserInteraction consoleUserInteraction)
{
Dictionary<string, List<int>> mapOfQuestion = RandomNumberAndQuestion();
IMathOperation op = _operations[operation];


foreach (KeyValuePair<string, List<int>> 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<string, List<int>> RandomNumberAndQuestion()
{
Dictionary<string, List<int>> questions = new Dictionary<string, List<int>>();
Random random = new Random();

for (int i = 0; i < NUMBER_OF_QUESTION; i++)
{
questions.Add($"Question {i + 1}", new List<int> { random.Next(0, 100), random.Next(1, 100) });
}

return questions;
}

}
10 changes: 10 additions & 0 deletions SubtractionOperation.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}