From 2fac0e5b1cd0acc0ec2a82c43ff7a8a820fe97c5 Mon Sep 17 00:00:00 2001 From: Vitor Carvalho Date: Sat, 24 Oct 2026 02:24:51 -0300 Subject: [PATCH 1/4] Single commit because stupid me did this in another repo --- MathGame.sln | 34 ++++++++ .../MathGame.vitorvelozo.csproj | 10 +++ MathGame.vitorvelozo/Program.cs | 54 +++++++++++++ .../controllers/Evaluation.cs | 41 ++++++++++ .../controllers/QuestionGeneration.cs | 79 +++++++++++++++++++ MathGame.vitorvelozo/models/MathGame.cs | 26 ++++++ MathGame.vitorvelozo/models/MathQuestion.cs | 36 +++++++++ MathGame.vitorvelozo/models/MathSet.cs | 38 +++++++++ MathGame.vitorvelozo/views/GameScreen.cs | 45 +++++++++++ MathGame.vitorvelozo/views/QuestionScreen.cs | 64 +++++++++++++++ MathGame.vitorvelozo/views/ResultScreen.cs | 27 +++++++ MathGame.vitorvelozo/views/StartScreen.cs | 53 +++++++++++++ 12 files changed, 507 insertions(+) create mode 100644 MathGame.sln create mode 100644 MathGame.vitorvelozo/MathGame.vitorvelozo.csproj create mode 100644 MathGame.vitorvelozo/Program.cs create mode 100644 MathGame.vitorvelozo/controllers/Evaluation.cs create mode 100644 MathGame.vitorvelozo/controllers/QuestionGeneration.cs create mode 100644 MathGame.vitorvelozo/models/MathGame.cs create mode 100644 MathGame.vitorvelozo/models/MathQuestion.cs create mode 100644 MathGame.vitorvelozo/models/MathSet.cs create mode 100644 MathGame.vitorvelozo/views/GameScreen.cs create mode 100644 MathGame.vitorvelozo/views/QuestionScreen.cs create mode 100644 MathGame.vitorvelozo/views/ResultScreen.cs create mode 100644 MathGame.vitorvelozo/views/StartScreen.cs diff --git a/MathGame.sln b/MathGame.sln new file mode 100644 index 00000000..6bc6d048 --- /dev/null +++ b/MathGame.sln @@ -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 diff --git a/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj b/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.vitorvelozo/Program.cs b/MathGame.vitorvelozo/Program.cs new file mode 100644 index 00000000..20726843 --- /dev/null +++ b/MathGame.vitorvelozo/Program.cs @@ -0,0 +1,54 @@ +using Controllers.Evaluation; +using Controllers.QuestionGeneration; +using Models.MathGame; +using Models.MathQuestion; +using Models.MathSet; +using Views.GameMenu; +using Views.QuestionDisplay; +using Views.StartScreen; + +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(); diff --git a/MathGame.vitorvelozo/controllers/Evaluation.cs b/MathGame.vitorvelozo/controllers/Evaluation.cs new file mode 100644 index 00000000..6a4f2907 --- /dev/null +++ b/MathGame.vitorvelozo/controllers/Evaluation.cs @@ -0,0 +1,41 @@ +using Models.MathQuestion; + +namespace Controllers.Evaluation; + +public 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; + } +} diff --git a/MathGame.vitorvelozo/controllers/QuestionGeneration.cs b/MathGame.vitorvelozo/controllers/QuestionGeneration.cs new file mode 100644 index 00000000..66eed832 --- /dev/null +++ b/MathGame.vitorvelozo/controllers/QuestionGeneration.cs @@ -0,0 +1,79 @@ +using Models.MathGame; +using Models.MathQuestion; + +namespace Controllers.QuestionGeneration; + +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); + } +} diff --git a/MathGame.vitorvelozo/models/MathGame.cs b/MathGame.vitorvelozo/models/MathGame.cs new file mode 100644 index 00000000..b3fc8184 --- /dev/null +++ b/MathGame.vitorvelozo/models/MathGame.cs @@ -0,0 +1,26 @@ +using Models.MathQuestion; + +namespace Models.MathGame; + +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 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); + } + } +} diff --git a/MathGame.vitorvelozo/models/MathQuestion.cs b/MathGame.vitorvelozo/models/MathQuestion.cs new file mode 100644 index 00000000..abf4db09 --- /dev/null +++ b/MathGame.vitorvelozo/models/MathQuestion.cs @@ -0,0 +1,36 @@ +namespace Models.MathQuestion; + +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; + } +} diff --git a/MathGame.vitorvelozo/models/MathSet.cs b/MathGame.vitorvelozo/models/MathSet.cs new file mode 100644 index 00000000..faf05a4f --- /dev/null +++ b/MathGame.vitorvelozo/models/MathSet.cs @@ -0,0 +1,38 @@ +using Models.MathGame; + +namespace Models.MathSet; + +public class MathSet +{ + public List 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); + } +} diff --git a/MathGame.vitorvelozo/views/GameScreen.cs b/MathGame.vitorvelozo/views/GameScreen.cs new file mode 100644 index 00000000..140e025b --- /dev/null +++ b/MathGame.vitorvelozo/views/GameScreen.cs @@ -0,0 +1,45 @@ +using Models.MathSet; +using Views.StartScreen; + +namespace Views.GameMenu; + +public class GameMenu +{ + private static void DisplayOptions() + { + Console.WriteLine("\nSelect an operation:"); + Console.WriteLine("New game = 0"); + Console.WriteLine("Show history = 1"); + Console.WriteLine("End the program = 2"); + Console.Write("=> "); + } + + public static bool Display(MathSet mathSet) + { + bool leaveMenu = false; + bool keepPlaying = false; + string? operation; + string[] validOperations = {"0", "1", "2"}; + do { + DisplayOptions(); + operation = Console.ReadLine(); + switch (operation) + { + case "0": + leaveMenu = true; + keepPlaying = true; + break; + case "1": + ResultScreen.Display(mathSet); + break; + case "2": + leaveMenu = true; + break; + default: + Console.WriteLine($"Invalid input: {operation}. Valid options are {string.Join(", ", validOperations)}"); + break; + } + } while (!leaveMenu); + return keepPlaying; + } +} \ No newline at end of file diff --git a/MathGame.vitorvelozo/views/QuestionScreen.cs b/MathGame.vitorvelozo/views/QuestionScreen.cs new file mode 100644 index 00000000..8f621219 --- /dev/null +++ b/MathGame.vitorvelozo/views/QuestionScreen.cs @@ -0,0 +1,64 @@ + +using Models.MathQuestion; + +namespace Views.QuestionDisplay; + +public class QuestionMenu +{ + public static MathOperation GetOperation() + { + while (true) + { + DisplayOperationMenu(); + + string? input = Console.ReadLine()?.Trim(); + + if (TryParseOperation(input, out MathOperation operation)) + return operation; + + Console.WriteLine("Invalid selection. Please enter +, -, *, or /."); + } + } + + public static string? GetQuestionAnswer(Question question) + { + Console.Write($"{question.FirstNumber} {GetOperationSymbol(question.Operation)} {question.SecondNumber} = "); + return Console.ReadLine(); + } + + private static void DisplayOperationMenu() + { + Console.WriteLine("\nSelect an operation:"); + Console.WriteLine("Addition = +"); + Console.WriteLine("Subtraction = -"); + Console.WriteLine("Multiplication = *"); + Console.WriteLine("Division = /"); + Console.Write("=> "); + } + + private static bool TryParseOperation( + string? input, + out MathOperation operation) + { + operation = input switch + { + "+" => MathOperation.Addition, + "-" => MathOperation.Subtraction, + "*" => MathOperation.Multiplication, + "/" => MathOperation.Division, + _ => default + }; + + return input is "+" or "-" or "*" or "/"; + } + + private static char GetOperationSymbol(MathOperation operation) => + operation switch + { + MathOperation.Addition => '+', + MathOperation.Subtraction => '-', + MathOperation.Multiplication => '*', + MathOperation.Division => '/', + _ => '?' + }; +} diff --git a/MathGame.vitorvelozo/views/ResultScreen.cs b/MathGame.vitorvelozo/views/ResultScreen.cs new file mode 100644 index 00000000..3de0471b --- /dev/null +++ b/MathGame.vitorvelozo/views/ResultScreen.cs @@ -0,0 +1,27 @@ +using Models.MathGame; +using Models.MathQuestion; +using Models.MathSet; + +namespace Views.StartScreen; + +public class ResultScreen +{ + public static void Display(MathSet mathSet) + { + Console.WriteLine("Results..."); + for (int i = 0; i < mathSet.Games.Count; i++) + { + Game game = mathSet.Games[i]; + Console.WriteLine($"==== Game {i+1} ({game.Difficulty}) | Score: ({game.PlayerScore}/{game.GameLength})===="); + foreach (Question question in mathSet.Games[i].Questions) + { + string questionText = $"{question.Result} {question.ProvidedAnswer}"; + string symbol = question.IsAnswerCorrect ? "✔" : "✘"; + Console.WriteLine($"{questionText,-10} {symbol}"); + } + Console.WriteLine(); + } + string durationFormatted = mathSet.Duration.ToString(@"mm'm 'ss's'"); + Console.WriteLine($"==== RESULT: {mathSet.totalPoints}/{mathSet.totalQuestions} in {durationFormatted} ===="); + } +} diff --git a/MathGame.vitorvelozo/views/StartScreen.cs b/MathGame.vitorvelozo/views/StartScreen.cs new file mode 100644 index 00000000..5364cc18 --- /dev/null +++ b/MathGame.vitorvelozo/views/StartScreen.cs @@ -0,0 +1,53 @@ +using Models.MathGame; + +namespace Views.StartScreen; + +public class MainMenu +{ + public static void DisplayWelcome() + { + Console.WriteLine("Hello!"); + } + + public static GameDifficulty GetDifficulty() + { + GameDifficulty validSelection = default; + bool validInput = false; + do + { + Console.WriteLine("\nSelect a difficulty:"); + Console.WriteLine("Easy = 0"); + Console.WriteLine("Normal = 1"); + Console.WriteLine("Hard = 2"); + Console.Write("=> "); + string? input = Console.ReadLine(); + + if (int.TryParse(input, out int selection) && Enum.IsDefined(typeof(GameDifficulty), selection)) + { + validInput = true; + validSelection = (GameDifficulty)selection; + } + else + { + Console.WriteLine("Invalid selection. Please enter 0, 1, or 2."); + } + } while (!validInput); + return validSelection; + } + + public static bool GetRandomMode() + { + Console.WriteLine("Do you prefer to manually select operations?"); + Console.Write("(y/any) => "); + string? input = Console.ReadLine(); + if (input == null) + { + return true; + } + if (input.Equals("Y") || input.Equals("y")) + { + return false; + } + return true; + } +} From 4beb3e18a44d36c27338d162afb0c040f9b93eea Mon Sep 17 00:00:00 2001 From: Vitor Carvalho Date: Sat, 24 Oct 2026 22:38:28 -0300 Subject: [PATCH 2/4] refactor to use spectre --- .../MathGame.vitorvelozo.csproj | 4 ++ MathGame.vitorvelozo/views/GameScreen.cs | 43 +++++++++--------- MathGame.vitorvelozo/views/QuestionScreen.cs | 45 +++---------------- MathGame.vitorvelozo/views/ResultScreen.cs | 29 ++++++++---- MathGame.vitorvelozo/views/StartScreen.cs | 45 +++++-------------- 5 files changed, 63 insertions(+), 103 deletions(-) diff --git a/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj b/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj index ed9781c2..452a0af1 100644 --- a/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj +++ b/MathGame.vitorvelozo/MathGame.vitorvelozo.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/MathGame.vitorvelozo/views/GameScreen.cs b/MathGame.vitorvelozo/views/GameScreen.cs index 140e025b..69a71be8 100644 --- a/MathGame.vitorvelozo/views/GameScreen.cs +++ b/MathGame.vitorvelozo/views/GameScreen.cs @@ -1,45 +1,44 @@ using Models.MathSet; +using Spectre.Console; using Views.StartScreen; namespace Views.GameMenu; public class GameMenu { - private static void DisplayOptions() + private static MenuChoices GetOperation() { - Console.WriteLine("\nSelect an operation:"); - Console.WriteLine("New game = 0"); - Console.WriteLine("Show history = 1"); - Console.WriteLine("End the program = 2"); - Console.Write("=> "); + return AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Select an operation") + .AddChoices(Enum.GetValues()) + ); } public static bool Display(MathSet mathSet) { - bool leaveMenu = false; bool keepPlaying = false; - string? operation; - string[] validOperations = {"0", "1", "2"}; - do { - DisplayOptions(); - operation = Console.ReadLine(); + MenuChoices operation; + do + { + operation = GetOperation(); switch (operation) { - case "0": - leaveMenu = true; + case MenuChoices.NewGame: keepPlaying = true; break; - case "1": + case MenuChoices.ShowHistory: ResultScreen.Display(mathSet); break; - case "2": - leaveMenu = true; - break; - default: - Console.WriteLine($"Invalid input: {operation}. Valid options are {string.Join(", ", validOperations)}"); - break; } - } while (!leaveMenu); + } while (operation == MenuChoices.ShowHistory); return keepPlaying; } + + private enum MenuChoices + { + NewGame, + ShowHistory, + ExitProgram, + } } \ No newline at end of file diff --git a/MathGame.vitorvelozo/views/QuestionScreen.cs b/MathGame.vitorvelozo/views/QuestionScreen.cs index 8f621219..df069625 100644 --- a/MathGame.vitorvelozo/views/QuestionScreen.cs +++ b/MathGame.vitorvelozo/views/QuestionScreen.cs @@ -1,5 +1,6 @@ using Models.MathQuestion; +using Spectre.Console; namespace Views.QuestionDisplay; @@ -7,51 +8,19 @@ public class QuestionMenu { public static MathOperation GetOperation() { - while (true) - { - DisplayOperationMenu(); - - string? input = Console.ReadLine()?.Trim(); - - if (TryParseOperation(input, out MathOperation operation)) - return operation; - - Console.WriteLine("Invalid selection. Please enter +, -, *, or /."); - } + return AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Select an operation") + .AddChoices(Enum.GetValues()) + ); } public static string? GetQuestionAnswer(Question question) { - Console.Write($"{question.FirstNumber} {GetOperationSymbol(question.Operation)} {question.SecondNumber} = "); + AnsiConsole.Markup($"{question.FirstNumber} {GetOperationSymbol(question.Operation)} {question.SecondNumber} = "); return Console.ReadLine(); } - private static void DisplayOperationMenu() - { - Console.WriteLine("\nSelect an operation:"); - Console.WriteLine("Addition = +"); - Console.WriteLine("Subtraction = -"); - Console.WriteLine("Multiplication = *"); - Console.WriteLine("Division = /"); - Console.Write("=> "); - } - - private static bool TryParseOperation( - string? input, - out MathOperation operation) - { - operation = input switch - { - "+" => MathOperation.Addition, - "-" => MathOperation.Subtraction, - "*" => MathOperation.Multiplication, - "/" => MathOperation.Division, - _ => default - }; - - return input is "+" or "-" or "*" or "/"; - } - private static char GetOperationSymbol(MathOperation operation) => operation switch { diff --git a/MathGame.vitorvelozo/views/ResultScreen.cs b/MathGame.vitorvelozo/views/ResultScreen.cs index 3de0471b..b08a7233 100644 --- a/MathGame.vitorvelozo/views/ResultScreen.cs +++ b/MathGame.vitorvelozo/views/ResultScreen.cs @@ -1,6 +1,7 @@ using Models.MathGame; using Models.MathQuestion; using Models.MathSet; +using Spectre.Console; namespace Views.StartScreen; @@ -8,20 +9,32 @@ public class ResultScreen { public static void Display(MathSet mathSet) { - Console.WriteLine("Results..."); + Console.Clear(); for (int i = 0; i < mathSet.Games.Count; i++) { Game game = mathSet.Games[i]; - Console.WriteLine($"==== Game {i+1} ({game.Difficulty}) | Score: ({game.PlayerScore}/{game.GameLength})===="); - foreach (Question question in mathSet.Games[i].Questions) + Table table = new Table() + .Border(TableBorder.Rounded) + .Title($"Game {i + 1} ({Markup.Escape(game.Difficulty.ToString())}) | Score: {game.PlayerScore}/{game.GameLength}") + .AddColumn("Question") + .AddColumn("Answer") + .AddColumn("Result"); + + foreach (Question question in game.Questions) { - string questionText = $"{question.Result} {question.ProvidedAnswer}"; - string symbol = question.IsAnswerCorrect ? "✔" : "✘"; - Console.WriteLine($"{questionText,-10} {symbol}"); + string result = question.IsAnswerCorrect ? "[green]✔ Correct[/]" : "[red]✘ Incorrect[/]"; + table.AddRow( + Markup.Escape(question.Result.ToString()), + Markup.Escape(question.ProvidedAnswer.ToString()), + result); } - Console.WriteLine(); + + AnsiConsole.Write(table); } + string durationFormatted = mathSet.Duration.ToString(@"mm'm 'ss's'"); - Console.WriteLine($"==== RESULT: {mathSet.totalPoints}/{mathSet.totalQuestions} in {durationFormatted} ===="); + AnsiConsole.Write(new Panel($"[bold]{mathSet.totalPoints}/{mathSet.totalQuestions}[/] in {durationFormatted}") + .Header("SET RESULT", Justify.Center) + .Border(BoxBorder.Double)); } } diff --git a/MathGame.vitorvelozo/views/StartScreen.cs b/MathGame.vitorvelozo/views/StartScreen.cs index 5364cc18..f4f15187 100644 --- a/MathGame.vitorvelozo/views/StartScreen.cs +++ b/MathGame.vitorvelozo/views/StartScreen.cs @@ -1,4 +1,5 @@ using Models.MathGame; +using Spectre.Console; namespace Views.StartScreen; @@ -6,48 +7,22 @@ public class MainMenu { public static void DisplayWelcome() { - Console.WriteLine("Hello!"); + AnsiConsole.MarkupLine("Hello! Welcome to the [cyan]math game[/]!"); } public static GameDifficulty GetDifficulty() { - GameDifficulty validSelection = default; - bool validInput = false; - do - { - Console.WriteLine("\nSelect a difficulty:"); - Console.WriteLine("Easy = 0"); - Console.WriteLine("Normal = 1"); - Console.WriteLine("Hard = 2"); - Console.Write("=> "); - string? input = Console.ReadLine(); - - if (int.TryParse(input, out int selection) && Enum.IsDefined(typeof(GameDifficulty), selection)) - { - validInput = true; - validSelection = (GameDifficulty)selection; - } - else - { - Console.WriteLine("Invalid selection. Please enter 0, 1, or 2."); - } - } while (!validInput); - return validSelection; + return AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Select a [red]difficulty[/]") + .AddChoices(Enum.GetValues()) + ); } public static bool GetRandomMode() { - Console.WriteLine("Do you prefer to manually select operations?"); - Console.Write("(y/any) => "); - string? input = Console.ReadLine(); - if (input == null) - { - return true; - } - if (input.Equals("Y") || input.Equals("y")) - { - return false; - } - return true; + bool randomMode = AnsiConsole.Confirm("Do you prefer to [red]manually select[/] operations?"); + Console.Clear(); + return !randomMode; } } From d530eec821c0520a30a21fb8c7092bee62847f71 Mon Sep 17 00:00:00 2001 From: Vitor Carvalho Date: Sun, 25 Oct 2026 12:40:10 -0300 Subject: [PATCH 3/4] add static keyword --- .vscode/settings.json | 3 +++ MathGame.vitorvelozo/controllers/Evaluation.cs | 2 +- MathGame.vitorvelozo/views/GameScreen.cs | 2 +- MathGame.vitorvelozo/views/QuestionScreen.cs | 2 +- MathGame.vitorvelozo/views/ResultScreen.cs | 2 +- MathGame.vitorvelozo/views/StartScreen.cs | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..2efc0762 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "MathGame.sln" +} \ No newline at end of file diff --git a/MathGame.vitorvelozo/controllers/Evaluation.cs b/MathGame.vitorvelozo/controllers/Evaluation.cs index 6a4f2907..b849add0 100644 --- a/MathGame.vitorvelozo/controllers/Evaluation.cs +++ b/MathGame.vitorvelozo/controllers/Evaluation.cs @@ -2,7 +2,7 @@ namespace Controllers.Evaluation; -public class QuestionEvaluator() +public static class QuestionEvaluator { private static int CalculateResult(int firstNumber, int secondNumber, MathOperation operation) { diff --git a/MathGame.vitorvelozo/views/GameScreen.cs b/MathGame.vitorvelozo/views/GameScreen.cs index 69a71be8..b79ea368 100644 --- a/MathGame.vitorvelozo/views/GameScreen.cs +++ b/MathGame.vitorvelozo/views/GameScreen.cs @@ -4,7 +4,7 @@ namespace Views.GameMenu; -public class GameMenu +public static class GameMenu { private static MenuChoices GetOperation() { diff --git a/MathGame.vitorvelozo/views/QuestionScreen.cs b/MathGame.vitorvelozo/views/QuestionScreen.cs index df069625..3b84ee0e 100644 --- a/MathGame.vitorvelozo/views/QuestionScreen.cs +++ b/MathGame.vitorvelozo/views/QuestionScreen.cs @@ -4,7 +4,7 @@ namespace Views.QuestionDisplay; -public class QuestionMenu +public static class QuestionMenu { public static MathOperation GetOperation() { diff --git a/MathGame.vitorvelozo/views/ResultScreen.cs b/MathGame.vitorvelozo/views/ResultScreen.cs index b08a7233..ed370fed 100644 --- a/MathGame.vitorvelozo/views/ResultScreen.cs +++ b/MathGame.vitorvelozo/views/ResultScreen.cs @@ -5,7 +5,7 @@ namespace Views.StartScreen; -public class ResultScreen +public static class ResultScreen { public static void Display(MathSet mathSet) { diff --git a/MathGame.vitorvelozo/views/StartScreen.cs b/MathGame.vitorvelozo/views/StartScreen.cs index f4f15187..c2e70f71 100644 --- a/MathGame.vitorvelozo/views/StartScreen.cs +++ b/MathGame.vitorvelozo/views/StartScreen.cs @@ -3,7 +3,7 @@ namespace Views.StartScreen; -public class MainMenu +public static class MainMenu { public static void DisplayWelcome() { From fd2f3f2250811a97b0309308e6af39c49c5000de Mon Sep 17 00:00:00 2001 From: Vitor Carvalho Date: Sun, 25 Oct 2026 13:23:53 -0300 Subject: [PATCH 4/4] reorganize imports --- MathGame.vitorvelozo/Program.cs | 11 +++-------- MathGame.vitorvelozo/controllers/Evaluation.cs | 4 ++-- .../controllers/QuestionGeneration.cs | 5 ++--- MathGame.vitorvelozo/models/MathGame.cs | 4 +--- MathGame.vitorvelozo/models/MathQuestion.cs | 2 +- MathGame.vitorvelozo/models/MathSet.cs | 4 +--- MathGame.vitorvelozo/views/GameScreen.cs | 5 ++--- MathGame.vitorvelozo/views/QuestionScreen.cs | 6 +++--- MathGame.vitorvelozo/views/ResultScreen.cs | 8 +++----- MathGame.vitorvelozo/views/StartScreen.cs | 6 +++--- 10 files changed, 21 insertions(+), 34 deletions(-) diff --git a/MathGame.vitorvelozo/Program.cs b/MathGame.vitorvelozo/Program.cs index 20726843..5faa2a04 100644 --- a/MathGame.vitorvelozo/Program.cs +++ b/MathGame.vitorvelozo/Program.cs @@ -1,11 +1,6 @@ -using Controllers.Evaluation; -using Controllers.QuestionGeneration; -using Models.MathGame; -using Models.MathQuestion; -using Models.MathSet; -using Views.GameMenu; -using Views.QuestionDisplay; -using Views.StartScreen; +using CodeReviews.Console.MathGame.Controllers; +using CodeReviews.Console.MathGame.Models; +using CodeReviews.Console.MathGame.Views; Game RunGame() { diff --git a/MathGame.vitorvelozo/controllers/Evaluation.cs b/MathGame.vitorvelozo/controllers/Evaluation.cs index b849add0..1bd1b474 100644 --- a/MathGame.vitorvelozo/controllers/Evaluation.cs +++ b/MathGame.vitorvelozo/controllers/Evaluation.cs @@ -1,6 +1,6 @@ -using Models.MathQuestion; +using CodeReviews.Console.MathGame.Models; -namespace Controllers.Evaluation; +namespace CodeReviews.Console.MathGame.Controllers; public static class QuestionEvaluator { diff --git a/MathGame.vitorvelozo/controllers/QuestionGeneration.cs b/MathGame.vitorvelozo/controllers/QuestionGeneration.cs index 66eed832..609c01cb 100644 --- a/MathGame.vitorvelozo/controllers/QuestionGeneration.cs +++ b/MathGame.vitorvelozo/controllers/QuestionGeneration.cs @@ -1,7 +1,6 @@ -using Models.MathGame; -using Models.MathQuestion; +using CodeReviews.Console.MathGame.Models; -namespace Controllers.QuestionGeneration; +namespace CodeReviews.Console.MathGame.Controllers; public class QuestionGenerator { diff --git a/MathGame.vitorvelozo/models/MathGame.cs b/MathGame.vitorvelozo/models/MathGame.cs index b3fc8184..c02ddfc4 100644 --- a/MathGame.vitorvelozo/models/MathGame.cs +++ b/MathGame.vitorvelozo/models/MathGame.cs @@ -1,6 +1,4 @@ -using Models.MathQuestion; - -namespace Models.MathGame; +namespace CodeReviews.Console.MathGame.Models; public enum GameDifficulty : int { diff --git a/MathGame.vitorvelozo/models/MathQuestion.cs b/MathGame.vitorvelozo/models/MathQuestion.cs index abf4db09..6ce5ad47 100644 --- a/MathGame.vitorvelozo/models/MathQuestion.cs +++ b/MathGame.vitorvelozo/models/MathQuestion.cs @@ -1,4 +1,4 @@ -namespace Models.MathQuestion; +namespace CodeReviews.Console.MathGame.Models; public enum MathOperation : ushort { diff --git a/MathGame.vitorvelozo/models/MathSet.cs b/MathGame.vitorvelozo/models/MathSet.cs index faf05a4f..b6f6cbfd 100644 --- a/MathGame.vitorvelozo/models/MathSet.cs +++ b/MathGame.vitorvelozo/models/MathSet.cs @@ -1,6 +1,4 @@ -using Models.MathGame; - -namespace Models.MathSet; +namespace CodeReviews.Console.MathGame.Models; public class MathSet { diff --git a/MathGame.vitorvelozo/views/GameScreen.cs b/MathGame.vitorvelozo/views/GameScreen.cs index b79ea368..0a619894 100644 --- a/MathGame.vitorvelozo/views/GameScreen.cs +++ b/MathGame.vitorvelozo/views/GameScreen.cs @@ -1,8 +1,7 @@ -using Models.MathSet; +using CodeReviews.Console.MathGame.Models; using Spectre.Console; -using Views.StartScreen; -namespace Views.GameMenu; +namespace CodeReviews.Console.MathGame.Views; public static class GameMenu { diff --git a/MathGame.vitorvelozo/views/QuestionScreen.cs b/MathGame.vitorvelozo/views/QuestionScreen.cs index 3b84ee0e..9d071d26 100644 --- a/MathGame.vitorvelozo/views/QuestionScreen.cs +++ b/MathGame.vitorvelozo/views/QuestionScreen.cs @@ -1,8 +1,8 @@ -using Models.MathQuestion; +using CodeReviews.Console.MathGame.Models; using Spectre.Console; -namespace Views.QuestionDisplay; +namespace CodeReviews.Console.MathGame.Views; public static class QuestionMenu { @@ -18,7 +18,7 @@ public static MathOperation GetOperation() public static string? GetQuestionAnswer(Question question) { AnsiConsole.Markup($"{question.FirstNumber} {GetOperationSymbol(question.Operation)} {question.SecondNumber} = "); - return Console.ReadLine(); + return System.Console.ReadLine(); } private static char GetOperationSymbol(MathOperation operation) => diff --git a/MathGame.vitorvelozo/views/ResultScreen.cs b/MathGame.vitorvelozo/views/ResultScreen.cs index ed370fed..d9b0997d 100644 --- a/MathGame.vitorvelozo/views/ResultScreen.cs +++ b/MathGame.vitorvelozo/views/ResultScreen.cs @@ -1,15 +1,13 @@ -using Models.MathGame; -using Models.MathQuestion; -using Models.MathSet; +using CodeReviews.Console.MathGame.Models; using Spectre.Console; -namespace Views.StartScreen; +namespace CodeReviews.Console.MathGame.Views; public static class ResultScreen { public static void Display(MathSet mathSet) { - Console.Clear(); + System.Console.Clear(); for (int i = 0; i < mathSet.Games.Count; i++) { Game game = mathSet.Games[i]; diff --git a/MathGame.vitorvelozo/views/StartScreen.cs b/MathGame.vitorvelozo/views/StartScreen.cs index c2e70f71..f24dc7e8 100644 --- a/MathGame.vitorvelozo/views/StartScreen.cs +++ b/MathGame.vitorvelozo/views/StartScreen.cs @@ -1,7 +1,7 @@ -using Models.MathGame; +using CodeReviews.Console.MathGame.Models; using Spectre.Console; -namespace Views.StartScreen; +namespace CodeReviews.Console.MathGame.Views; public static class MainMenu { @@ -22,7 +22,7 @@ public static GameDifficulty GetDifficulty() public static bool GetRandomMode() { bool randomMode = AnsiConsole.Confirm("Do you prefer to [red]manually select[/] operations?"); - Console.Clear(); + System.Console.Clear(); return !randomMode; } }