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
39 changes: 39 additions & 0 deletions MathGame.CSamu7/Logic/Contest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using MathGame.Utils;
using MathGame.Views;

namespace MathGame.Logic
{
public class Contest
{
private readonly int _noQuestions = 5;
private int points = 0;
private readonly Question _question;
public ContestConfig Config { get; init; }
public Contest(ContestConfig config)
{
Config = config;

IIntervalFactory interval = Config.Difficulty switch
{
Difficulty.Easy => new EasyIntervalFactory(),
Difficulty.Medium => new MediumIntervalFactory(),
_ => new HardIntervalFactory()
};

_question = new Question(new OperationGenerator(interval));
}
public int Play()
{
for (int i = 0; i < _noQuestions; i++)
{
string op = GetOperator();

int point = _question.Prompt(op);
points += point;
}

return points;
}
protected virtual string GetOperator() => Config.Operator;
}
}
20 changes: 20 additions & 0 deletions MathGame.CSamu7/Logic/ContestConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using MathGame.Views;

namespace MathGame.Logic
{
public record ContestConfig
{
public Difficulty Difficulty { get; init; }
public string Operator { get; init; }
private ContestConfig(Difficulty difficulty, string op)
{
Difficulty = difficulty;
Operator = op;
}
public static ContestConfig RandomContest(Difficulty difficulty) =>
new ContestConfig(difficulty, String.Empty);
public static ContestConfig NormalContest(Difficulty difficulty, string op) =>
new ContestConfig(difficulty, op);
}
public enum ContestType { Normal, Random };
}
19 changes: 19 additions & 0 deletions MathGame.CSamu7/Logic/ContestRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace MathGame.Logic
{
public class ContestRandom : Contest
{
public ContestRandom(ContestConfig config) : base(config)
{

}
protected override string GetOperator()
{
List<string> operators = ["+", "-", "*", "/"];
Random rnd = new Random();

int rndIndex = rnd.Next(operators.Count);

return operators[rndIndex];
}
}
}
14 changes: 14 additions & 0 deletions MathGame.CSamu7/Logic/DivisionValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MathGame.Logic
{
public class DivisionValidation()
{
public bool IsValid(int n1, int n2)
{
bool areDividendsNotEqual = n1 != n2;
bool isQuotientAnInteger = n1 % n2 == 0;

return areDividendsNotEqual
&& isQuotientAnInteger;
}
}
}
4 changes: 4 additions & 0 deletions MathGame.CSamu7/Logic/HistoryRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace MathGame.Logic
{
public record HistoryRecord(string Difficulty, string Operation, int Points, TimeSpan Time);
}
46 changes: 46 additions & 0 deletions MathGame.CSamu7/Logic/IIntervalGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace MathGame.Logic
{
public interface IIntervalFactory
{
public Interval GetInterval(string op);
}
public class EasyIntervalFactory : IIntervalFactory
{
public Interval GetInterval(string op)
{
return op switch
{
"+" or "-" => new Interval(0, 10),
"*" => new Interval(0, 10),
"/" => new Interval(1, 60),
_ => new Interval(0, 10)
};
}
};
public class MediumIntervalFactory : IIntervalFactory
{
public Interval GetInterval(string op)
{
return op switch
{
"+" or "-" => new Interval(0, 50),
"*" => new Interval(0, 15),
"/" => new Interval(3, 120),
_ => new Interval(0, 50)
};
}
}
public class HardIntervalFactory : IIntervalFactory
{
public Interval GetInterval(string op)
{
return op switch
{
"+" or "-" => new Interval(0, 100),
"*" => new Interval(0, 20),
"/" => new Interval(4, 240),
_ => new Interval(0, 100)
};
}
}
}
15 changes: 15 additions & 0 deletions MathGame.CSamu7/Logic/Interval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MathGame.Logic
{
public record Interval
{
public int Min { get; init; }
public int Max { get; init; }
public Interval(int min, int max)
{
if (min > max) throw new ArgumentException();

Min = min;
Max = max;
}
}
}
25 changes: 25 additions & 0 deletions MathGame.CSamu7/Logic/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace MathGame.Logic
{
public record Operation
{
public int N1 { get; private set; }
public int N2 { get; private set; }
public string Operator { get; private set; }
public Operation(int n1, int n2, string op)
{
N1 = n1;
N2 = n2;
Operator = op;
}
public int Calculate()
{
return Operator switch
{
"+" => N1 + N2,
"-" => N1 - N2,
"*" => N1 * N2,
"/" => N1 / N2,
};
}
}
}
25 changes: 25 additions & 0 deletions MathGame.CSamu7/Logic/OperationGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace MathGame.Logic
{

public class OperationGenerator(IIntervalFactory interval)
{
private readonly Random _rnd = new();
private readonly IIntervalFactory _interval = interval;
private readonly DivisionValidation _divValidation = new();
public Operation Generate(string op)
{
while (true)
{
Interval interval = _interval.GetInterval(op);

int n1 = _rnd.Next(interval.Min, interval.Max);
int n2 = _rnd.Next(interval.Min, interval.Max);

if (op == "/" && !_divValidation.IsValid(n1, n2))
continue;

return new Operation(n1, n2, op);
}
}
}
}
33 changes: 33 additions & 0 deletions MathGame.CSamu7/Logic/Question.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using MathGame.Utils;

namespace MathGame.Logic
{
public class Question
{
private readonly OperationGenerator _operationGenerator;
public Question(OperationGenerator operationGenerator)
{
_operationGenerator = operationGenerator;
}
public int Prompt(string operation)
{
Console.Clear();
Operation op = _operationGenerator.Generate(operation);

Console.WriteLine($"What's the result of {op.N1} {op.Operator} {op.N2}?");
int answer = GetAnswer();

return answer == op.Calculate() ? 1 : 0;
}
private int GetAnswer()
{
while (true)
{
if (int.TryParse(Console.ReadLine(), out int answer))
return answer;

ConsoleUtils.Error("You need to enter a number");
}
}
}
}
10 changes: 10 additions & 0 deletions MathGame.CSamu7/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 MathGame.CSamu7/MathGame.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36603.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MathGame", "MathGame.csproj", "{FE7A7814-A57D-4D15-BE98-0B48968094AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FE7A7814-A57D-4D15-BE98-0B48968094AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE7A7814-A57D-4D15-BE98-0B48968094AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE7A7814-A57D-4D15-BE98-0B48968094AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE7A7814-A57D-4D15-BE98-0B48968094AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2CFE25C1-050F-4262-B678-DC967B7CA3E5}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions MathGame.CSamu7/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MathGame.Views;

namespace MathGame
{
public class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.Start();
}
}
}
39 changes: 39 additions & 0 deletions MathGame.CSamu7/Utils/ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace MathGame.Utils
{
public static class ConsoleUtils
{
public static void Error(string message)
{
WriteLine(message, ConsoleColor.Red);
}
public static void WriteLine(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
}
public static void DisplayList(List<string> list)
{
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine($"{i + 1}. {list[i]}");
}
}
public static bool ReadOption(out int option, Predicate<int> extraValidation)
{
if (!int.TryParse(Console.ReadLine(), out option))
{
ConsoleUtils.Error($"You have to enter a number");
return false;
}

if (!extraValidation(option))
{
ConsoleUtils.Error($"This isn't a valid option");
return false;
}

return true;
}
}
}
12 changes: 12 additions & 0 deletions MathGame.CSamu7/Utils/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Globalization;

namespace MathGame.Utils
{
public static class TimeSpanExtensions
{
public static string ToMinutesSeconds(this TimeSpan time)
{
return time.ToString("mm':'ss");
}
}
}
31 changes: 31 additions & 0 deletions MathGame.CSamu7/Views/ContestConfigView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using MathGame.Logic;

namespace MathGame.Views
{
public class ContestConfigView
{
private readonly OperationMenu _menuOperation = new();
private readonly DifficultyMenu _difficultyView = new();
private readonly ContestTypeMenu _contestType = new();
public Contest GetContest()
{
Difficulty difficulty = _difficultyView.GetDifficulty();
ContestType type = _contestType.GetGameType();
ContestConfig config;
Contest contest;

if (type.Equals(Logic.ContestType.Random))
{
config = ContestConfig.RandomContest(difficulty);
contest = new ContestRandom(config);
} else
{
string operation = _menuOperation.GetOperation();
config = ContestConfig.NormalContest(difficulty, operation);
contest = new Contest(config);
}

return contest;
}
}
}
Loading