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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "MathGame.sln"
}
53 changes: 53 additions & 0 deletions MathGame.rcraig14/Controllers/GameController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using MathGame.rcraig14.Models;

namespace MathGame.rcraig14.Controllers;

public class GameController : IGameController
{

private Problem? CurrentProblem { get; set; }
private List<SubmittedAnswer> AnswerHistory { get; set; }

public GameController()
{
AnswerHistory = new List<SubmittedAnswer>();
}

public Problem GenerateProblem(Operation operation)
{
CurrentProblem = operation switch
{
Operation.Addition => Addition.GenerateRandom(),
Operation.Subtraction => Subtraction.GenerateRandom(),
Operation.Multiplication => Multiplication.GenerateRandom(),
Operation.Division => Division.GenerateRandom(),
_ => throw new ArgumentOutOfRangeException(nameof(operation), $"Unexpected operation value: {operation}")
};

return CurrentProblem;
}


public SubmittedAnswer SubmitAnswer(int answer, TimeSpan timeToComplete)
{
if (CurrentProblem is null)
{
throw new ArgumentNullException("Problem must be generated before submitting answer");
}

var submittedAnswer = new SubmittedAnswer(CurrentProblem, answer, timeToComplete);
AnswerHistory.Add(submittedAnswer);

return submittedAnswer;
}

public void DisplayProblem()
{
Console.WriteLine(CurrentProblem?.ToString() ?? "Problem needs to be generated");
}

public List<SubmittedAnswer> GetAnswerHistory()
{
return AnswerHistory;
}
}
12 changes: 12 additions & 0 deletions MathGame.rcraig14/Controllers/IGameController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MathGame.rcraig14.Models;

namespace MathGame.rcraig14.Controllers;

public interface IGameController
{
public Problem GenerateProblem(Operation operation);
public void DisplayProblem();
public SubmittedAnswer SubmitAnswer(int answer, TimeSpan timeToComplete);
public List<SubmittedAnswer> GetAnswerHistory();

}
14 changes: 14 additions & 0 deletions MathGame.rcraig14/Controllers/IUserInteractionsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MathGame.rcraig14.Models;

namespace MathGame.rcraig14.Controllers;

public interface IUserInteractionsController
{
public void DisplayWelcome();
public NextStep GetNextStep();
public Operation GetNextOperationType();
public int GetAnswer(Problem problem);
public void DisplayResults(SubmittedAnswer submittedAnswer);
public void DisplayAnswerHistory(List<SubmittedAnswer> submittedAnswers);

}
85 changes: 85 additions & 0 deletions MathGame.rcraig14/Controllers/UserInteractionsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using MathGame.rcraig14.Models;

namespace MathGame.rcraig14.Controllers;

public class UserInteractionsController : IUserInteractionsController
{
public void DisplayResults(SubmittedAnswer submittedAnswer)
{
Console.WriteLine(submittedAnswer.ToString());
Console.WriteLine($"It took {submittedAnswer.TimeToComplete} to complete");
}

public void DisplayWelcome()
{
Console.WriteLine("Welcome to the new math game");
}

public int GetAnswer(Problem problem)
{
Console.WriteLine(problem.ToString());
Console.Write("Answer: ");
string? strAnswer = Console.ReadLine();

if (int.TryParse(strAnswer, out int intOut))
{
return intOut;
}
else
{
Console.WriteLine("Only integers can be submitted for answers");
return GetAnswer(problem);
}
}

public NextStep GetNextStep()
{
while (true)
{
Console.WriteLine("Get next problem (n), history of problems (h), or quit (q)");
string? input = Console.ReadLine();

switch (input?.ToLower() ?? "")
{
case "q":
return NextStep.Quit;
case "n":
return NextStep.NewProblem;
case "h":
return NextStep.History;
default:
continue;
}
}

}

public Operation GetNextOperationType()
{
Console.WriteLine(@"
Select a type of problem
- Additon (a)
- Subtraction (s)
- Multipication (m)
- Division (d)");

string? problemType = Console.ReadLine();

if (problemType is null)
return GetNextOperationType();

return OperationConverter.StringToOperation(problemType);
}

public void DisplayAnswerHistory(List<SubmittedAnswer> submittedAnswers)
{
int correct = 0;
submittedAnswers.ForEach(answer =>
{
Console.WriteLine($"{answer.Problem} {answer.UserAnswer} Correct: {answer.IsCorrect()}");
correct += answer.IsCorrect() ? 1 : 0;
});

Console.WriteLine($"Total Correct {correct} / {submittedAnswers.Count()}");
}
}
10 changes: 10 additions & 0 deletions MathGame.rcraig14/MathGame.rcraig14.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>
63 changes: 63 additions & 0 deletions MathGame.rcraig14/MathGameRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using MathGame.rcraig14.Controllers;
using MathGame.rcraig14.Models;

namespace MathGame.rcraig14;

public class MathGameRunner
{
private UserInteractionsController UserInteractionController { get; }
private GameController GameController { get; }
private bool NewProblem { get; set; }

public MathGameRunner(
UserInteractionsController userInteractionsController,
GameController gameController)
{
UserInteractionController = userInteractionsController;
GameController = gameController;
NewProblem = true;
}

private void NextProblem()
{
Operation op = UserInteractionController.GetNextOperationType();
Problem problem = GameController.GenerateProblem(op);

DateTime startTime = DateTime.UtcNow;
int userAnswer = UserInteractionController.GetAnswer(problem);
DateTime endTime = DateTime.UtcNow;

SubmittedAnswer submittedAnswer = GameController.SubmitAnswer(userAnswer, endTime - startTime);
UserInteractionController.DisplayResults(submittedAnswer);
}

private void History()
{
var history = GameController.GetAnswerHistory();
UserInteractionController.DisplayAnswerHistory(history);
}

public void Start()
{
UserInteractionController.DisplayWelcome();

while (NewProblem)
{
NextStep next = UserInteractionController.GetNextStep();

switch (next)
{
case NextStep.Quit:
NewProblem = false;
break;
case NextStep.History:
this.History();
break;
default:
this.NextProblem();
break;
}

}
}
}
16 changes: 16 additions & 0 deletions MathGame.rcraig14/Models/Addition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace MathGame.rcraig14.Models;

public class Addition(int pLeft, int pRight) : Problem(pLeft, pRight, Operation.Addition, "+")
{

public static Addition GenerateRandom()
{
var random = new Random();
return new Addition(random.Next(0, 100), random.Next(0, 100));
}

public override int Answer()
{
return Left + Right;
}
}
23 changes: 23 additions & 0 deletions MathGame.rcraig14/Models/Division.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace MathGame.rcraig14.Models;

public class Division(int pLeft, int pRight) : Problem(pLeft, pRight, Operation.Division, "/")
{

public static Division GenerateRandom()
{
var random = new Random();
int left = 1, right = 1;

while (left <= right || (left % right) != 0)
{
left = random.Next(1, 100);
right = random.Next(1, 99);
}

return new Division(left, right);
}
public override int Answer()
{
return Left / Right;
}
}
15 changes: 15 additions & 0 deletions MathGame.rcraig14/Models/Multiplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MathGame.rcraig14.Models;

public class Multiplication(int pLeft, int pRight) : Problem(pLeft, pRight, Operation.Multiplication, "*")
{
public override int Answer()
{
return Left * Right;
}

public static Multiplication GenerateRandom()
{
var random = new Random();
return new Multiplication(random.Next(0, 100), random.Next(0, 100));
}
}
24 changes: 24 additions & 0 deletions MathGame.rcraig14/Models/Problem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace MathGame.rcraig14.Models;

public abstract class Problem
{
protected int Left { get; }
protected int Right { get; }
public Operation Operation { get; }
string OperationString { get; }

public Problem(int pLeft, int pRight, Operation pOperation, string pOperationString)
{
Left = pLeft;
Right = pRight;
Operation = pOperation;
OperationString = pOperationString;
}

public abstract int Answer();

public override string ToString()
{
return $"{Left} {OperationString} {Right} =";
}
}
23 changes: 23 additions & 0 deletions MathGame.rcraig14/Models/SubmittedAnswer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace MathGame.rcraig14.Models;

public class SubmittedAnswer
{
public Problem Problem { get; }
public int UserAnswer { get; }
public TimeSpan TimeToComplete { get; }


public SubmittedAnswer(Problem problem, int answer, TimeSpan timeToComplete)
{
Problem = problem;
UserAnswer = answer;
TimeToComplete = timeToComplete;
}

public bool IsCorrect() => Problem.Answer() == UserAnswer;

public override string ToString()
{
return IsCorrect() ? $"Correct the answer is {Problem.Answer()}" : $"Incorrect the answer is {Problem.Answer()}";
}
}
23 changes: 23 additions & 0 deletions MathGame.rcraig14/Models/Subtraction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace MathGame.rcraig14.Models;

public class Subtraction(int pLeft, int pRight) : Problem(pLeft, pRight, Operation.Subtraction, "-")
{
public override int Answer()
{
return Left - Right;
}

public static Subtraction GenerateRandom()
{
var random = new Random();
int left = 0, right = 0;

while (left <= right)
{
left = random.Next(1, 100);
right = random.Next(0, 100);
}

return new Subtraction(left, right);
}
}
8 changes: 8 additions & 0 deletions MathGame.rcraig14/NextStep.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MathGame.rcraig14;

public enum NextStep
{
Quit,
NewProblem,
History
}
Loading