Skip to content
Closed
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"
}
34 changes: 34 additions & 0 deletions MathGame.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MathGame.vitorvelozo", "MathGame.vitorvelozo\MathGame.vitorvelozo.csproj", "{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Debug|x64.ActiveCfg = Debug|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Debug|x64.Build.0 = Debug|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Debug|x86.ActiveCfg = Debug|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Debug|x86.Build.0 = Debug|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Release|Any CPU.Build.0 = Release|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Release|x64.ActiveCfg = Release|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Release|x64.Build.0 = Release|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Release|x86.ActiveCfg = Release|Any CPU
{285CA9B0-CBE7-49C5-840F-AA8F7EAD0CD8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions MathGame.vitorvelozo/MathGame.vitorvelozo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.57.2" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions MathGame.vitorvelozo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using CodeReviews.Console.MathGame.Controllers;
using CodeReviews.Console.MathGame.Models;
using CodeReviews.Console.MathGame.Views;

Game RunGame()
{
GameDifficulty difficulty = MainMenu.GetDifficulty();
bool randomMode = MainMenu.GetRandomMode();

Game game = new Game(difficulty, randomMode: randomMode);

for (int i = 0; i < game.GameLength; i++)
{
QuestionGenerator randomGenerator;
if (!randomMode)
{
randomGenerator = new QuestionGenerator(difficulty, QuestionMenu.GetOperation()
);
} else
{
randomGenerator = new QuestionGenerator(difficulty);
}
Question question = randomGenerator.GenerateQuestion();
game.Questions.Add(question);

question.StartTime = TimeOnly.FromDateTime(DateTime.Now);
question.ProvidedAnswer = QuestionMenu.GetQuestionAnswer(question);
question.FinishTime = TimeOnly.FromDateTime(DateTime.Now);
game.PlayerScore += QuestionEvaluator.Evaluate(question);
}
return game;
}

void Main()
{
MainMenu.DisplayWelcome();
MathSet mathSet = new MathSet();

bool keepPlaying;
do
{
mathSet.AddGame(RunGame());
keepPlaying = GameMenu.Display(mathSet);
} while (keepPlaying);

ResultScreen.Display(mathSet);
}

Main();
41 changes: 41 additions & 0 deletions MathGame.vitorvelozo/controllers/Evaluation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using CodeReviews.Console.MathGame.Models;

namespace CodeReviews.Console.MathGame.Controllers;

public static class QuestionEvaluator
{
private static int CalculateResult(int firstNumber, int secondNumber, MathOperation operation)
{
int result;
switch (operation)
{
case MathOperation.Addition:
result = firstNumber + secondNumber;
break;
case MathOperation.Subtraction:
result = firstNumber - secondNumber;
break;
case MathOperation.Multiplication:
result = firstNumber * secondNumber;
break;
case MathOperation.Division:
result = firstNumber / secondNumber;
break;
default:
throw new ArgumentOutOfRangeException(nameof(operation), operation, null);
}
return result;
}

public static int Evaluate(Question question)
{
question.Result = CalculateResult(question.FirstNumber, question.SecondNumber, question.Operation);
int answer;
if (int.TryParse(question.ProvidedAnswer, out answer))
{
question.IsAnswerCorrect = answer == question.Result;
return 1;
}
return 0;
}
}
78 changes: 78 additions & 0 deletions MathGame.vitorvelozo/controllers/QuestionGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CodeReviews.Console.MathGame.Models;

namespace CodeReviews.Console.MathGame.Controllers;

public class QuestionGenerator
{
public int FirstNumber { get; init; }
public int SecondNumber { get; set; }
public GameDifficulty GameDifficulty {get; set;}
public MathOperation Operation { get; init; }
private int GeneratedNumberCeiling
{
get
{
if (GameDifficulty == GameDifficulty.Easy)
{
return 11;
}
else if (GameDifficulty == GameDifficulty.Normal)
{
return 51;
}
return 101;
}
}

private static readonly Array OperationsList = Enum.GetValues(typeof(MathOperation));

public QuestionGenerator(GameDifficulty difficulty)
{
int randomIndex = Random.Shared.Next(OperationsList.Length);
Operation = (MathOperation)OperationsList.GetValue(randomIndex)!;

GameDifficulty = difficulty;
FirstNumber = Random.Shared.Next(1, GeneratedNumberCeiling);
SecondNumber = GenerateSecondNumber();
}

public QuestionGenerator(GameDifficulty difficulty, MathOperation operation)
{
Operation = operation;

GameDifficulty = difficulty;
FirstNumber = Random.Shared.Next(1, GeneratedNumberCeiling);
SecondNumber = GenerateSecondNumber();
}

private int[] GetIntDivisors()
{
int[] divisors = Enumerable.Range(1, FirstNumber)
.Where(d => FirstNumber % d == 0)
.ToArray();
return divisors;
}

private int GenerateSecondNumber()
{
switch (Operation)
{
case MathOperation.Subtraction:
SecondNumber = Random.Shared.Next(1, FirstNumber);
break;
case MathOperation.Division:
int[] divisors = GetIntDivisors();
SecondNumber = divisors[Random.Shared.Next(divisors.Length)];
break;
default:
SecondNumber = Random.Shared.Next(1, GeneratedNumberCeiling);
break;
}
return SecondNumber;
}

public Question GenerateQuestion()
{
return new Question(FirstNumber, SecondNumber, Operation);
}
}
24 changes: 24 additions & 0 deletions MathGame.vitorvelozo/models/MathGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace CodeReviews.Console.MathGame.Models;

public enum GameDifficulty : int
{
Easy = 0,
Normal = 1,
Hard = 2,
}
public class Game(GameDifficulty difficulty = GameDifficulty.Normal, int length = 5, bool randomMode = true)
{
public List<Question> Questions { get; set; } = [];
public int PlayerScore { get; set; } = 0;
public int GameLength { get; } = length;
public GameDifficulty Difficulty { get; } = difficulty;
public bool IsRandomMode { get; } = randomMode;
public TimeSpan Duration
{
get
{
long totalTicks = Questions.Sum(question => question.Duration.Ticks);
return TimeSpan.FromTicks(totalTicks);
}
}
}
36 changes: 36 additions & 0 deletions MathGame.vitorvelozo/models/MathQuestion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace CodeReviews.Console.MathGame.Models;

public enum MathOperation : ushort
{
Multiplication = '*',
Division = '/',
Addition = '+',
Subtraction = '-',
}

public class Question
{
public int FirstNumber { get; }
public int SecondNumber { get; }
public MathOperation Operation { get; }
public int Result { get; set; }
public string? ProvidedAnswer { get; set; }
public bool IsAnswerCorrect { get; set; } = false;
public TimeOnly StartTime {get; set; }
public TimeOnly FinishTime {get; set; }
public TimeSpan Duration
{
get
{
return FinishTime - StartTime;
}
}


public Question(int firstNumber, int secondNumber, MathOperation operation)
{
FirstNumber = firstNumber;
SecondNumber = secondNumber;
Operation = operation;
}
}
36 changes: 36 additions & 0 deletions MathGame.vitorvelozo/models/MathSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace CodeReviews.Console.MathGame.Models;

public class MathSet
{
public List<Game> Games { get; set; } = [];

public int totalPoints
{
get
{
return Games.Sum(game => game.PlayerScore);
}
}

public int totalQuestions
{
get
{
return Games.Sum(game => game.GameLength);
}
}

public TimeSpan Duration
{
get
{
long totalTicks = Games.Sum(game => game.Duration.Ticks);
return TimeSpan.FromTicks(totalTicks);
}
}

public void AddGame(Game game)
{
Games.Add(game);
}
}
43 changes: 43 additions & 0 deletions MathGame.vitorvelozo/views/GameScreen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CodeReviews.Console.MathGame.Models;
using Spectre.Console;

namespace CodeReviews.Console.MathGame.Views;

public static class GameMenu
{
private static MenuChoices GetOperation()
{
return AnsiConsole.Prompt(
new SelectionPrompt<MenuChoices>()
.Title("Select an operation")
.AddChoices(Enum.GetValues<MenuChoices>())
);
}

public static bool Display(MathSet mathSet)
{
bool keepPlaying = false;
MenuChoices operation;
do
{
operation = GetOperation();
switch (operation)
{
case MenuChoices.NewGame:
keepPlaying = true;
break;
case MenuChoices.ShowHistory:
ResultScreen.Display(mathSet);
break;
}
} while (operation == MenuChoices.ShowHistory);
return keepPlaying;
}

private enum MenuChoices
{
NewGame,
ShowHistory,
ExitProgram,
}
}
Loading