From 0447f2cd01117ef47fe146f013d2455dfab22f86 Mon Sep 17 00:00:00 2001 From: Lucas Correia Date: Mon, 31 Aug 2026 10:51:27 -0300 Subject: [PATCH 1/4] My finished challenge --- LucasCorreia.MathGame/math-game/Libary.cs | 108 ++++++++++++++++++ LucasCorreia.MathGame/math-game/Program.cs | 53 +++++++++ LucasCorreia.MathGame/math-game/README.md | 16 +++ LucasCorreia.MathGame/math-game/Teste.cs | 9 ++ LucasCorreia.MathGame/math-game/Time.cs | 15 +++ .../math-game/math-game.csproj | 11 ++ 6 files changed, 212 insertions(+) create mode 100644 LucasCorreia.MathGame/math-game/Libary.cs create mode 100644 LucasCorreia.MathGame/math-game/Program.cs create mode 100644 LucasCorreia.MathGame/math-game/README.md create mode 100644 LucasCorreia.MathGame/math-game/Teste.cs create mode 100644 LucasCorreia.MathGame/math-game/Time.cs create mode 100644 LucasCorreia.MathGame/math-game/math-game.csproj diff --git a/LucasCorreia.MathGame/math-game/Libary.cs b/LucasCorreia.MathGame/math-game/Libary.cs new file mode 100644 index 00000000..fb6de24c --- /dev/null +++ b/LucasCorreia.MathGame/math-game/Libary.cs @@ -0,0 +1,108 @@ +public class Libary +{ + private Random randNum = new(); + + private string[] operadores = {"+", "-", "x", "/"}; + public int Easy() + { + int ponto = 0; + for (int i = 0; i < 5; i++) + { + string operador = operadores[randNum.Next(operadores.Length)]; + int num1 = randNum.Next(11); + int num2 = randNum.Next(11); + NovasPerguntas(num1,num2,operador); + int resposta = Convert.ToInt32(Console.ReadLine()); + bool resultado = Resposta(num1,num2,operador,resposta); + if (resultado) + { + ponto++; + } + } + + return ponto; + } + + public int Inter() + { + int ponto = 0; + for (int i = 0; i < 5; i++) + { + string operador = operadores[randNum.Next(operadores.Length)]; + int num1 = randNum.Next(51); + int num2 = randNum.Next(51); + NovasPerguntas(num1,num2,operador); + int resposta = Convert.ToInt32(Console.ReadLine()); + bool resultado = Resposta(num1,num2,operador,resposta); + if (resultado) + { + ponto++; + } + } + + return ponto; + } + + public int Dificult() + { + int ponto = 0; + for (int i = 0; i < 5; i++) + { + string operador = operadores[randNum.Next(operadores.Length)]; + int num1 = randNum.Next(101); + int num2 = randNum.Next(101); + NovasPerguntas(num1,num2,operador); + int resposta = Convert.ToInt32(Console.ReadLine()); + bool resultado = Resposta(num1,num2,operador,resposta); + if (resultado) + { + ponto++; + } + } + + return ponto; + } + + public void NovasPerguntas(int x, int y, string operador) + { + if (operador == "/" && x % y != 0 ) + { + operador = operadores[randNum.Next(3)]; + } + Console.WriteLine("Qual é resultado da conta abaixo ?"); + Console.WriteLine($"{x} {operador} {y}"); + } + + public bool Resposta(int x, int y, string operador, int z) + { + switch (operador) + { + case "+": + if ((x + y) == z) + { + return true; + } + break; + case "-": + if ((x - y) == z) + { + return true; + } + break; + case "/": + if ((x / y) == z) + { + return true; + } + break; + case "x": + if ((x * y) == z) + { + return true; + } + break; + } + + return false; + } +} \ No newline at end of file diff --git a/LucasCorreia.MathGame/math-game/Program.cs b/LucasCorreia.MathGame/math-game/Program.cs new file mode 100644 index 00000000..d1895d67 --- /dev/null +++ b/LucasCorreia.MathGame/math-game/Program.cs @@ -0,0 +1,53 @@ +using System.Diagnostics; + +public class Program +{ + public static void Main() + { + int op = 0; + int pontos = 0; + Libary lb = new(); + double tempo_jogo = 0; + List rodadas = []; + List tempo_rodadas = []; + do + { + Console.WriteLine("===============================\nBem vindo ao Jogo Matématico.\n==============================="); + Console.WriteLine("\nEscolha uma opção abaixo:"); + Console.WriteLine("1- Jogo fácil\n2- Jogo intermédiario\n3- Jogo Difícil\n4- Listar Pontuações\n0- Sair do jogo"); + op = Convert.ToInt32(Console.ReadLine()); + Console.Clear(); + switch (op) + { + case 1: + tempo_jogo = Time.MedirTempo(() => pontos = lb.Easy()); + rodadas.Add(pontos); + tempo_rodadas.Add(tempo_jogo); + break; + case 2: + pontos = lb.Inter(); + rodadas.Add(pontos); + break; + case 3: + pontos = lb.Dificult(); + rodadas.Add(pontos); + break; + case 4: + Console.WriteLine("=====================\n=PONTUAÇÃO DOS JOGOS=\n====================="); + for (int i = 0; i < rodadas.Count; i++) + { + Console.WriteLine($"Rodada {i + 1}° -> {rodadas[i]} pontos | Tempo -> {(int)tempo_rodadas[i]} segundos"); + } + Console.ReadLine(); + Console.WriteLine(); + break; + case 0: + break; + default: + Console.WriteLine("Opção inválida"); + break; + } + }while(op != 0); + + } +} \ No newline at end of file diff --git a/LucasCorreia.MathGame/math-game/README.md b/LucasCorreia.MathGame/math-game/README.md new file mode 100644 index 00000000..6e5a3bb6 --- /dev/null +++ b/LucasCorreia.MathGame/math-game/README.md @@ -0,0 +1,16 @@ +# Desafio Jogo da Matemática + +Criar um jogo que consiste em perguntar ao jogador qual é resultado de uma pergunta de matemática (ou seja, 9 x 9 = ?), coletar a entrada e adiconar um ponto em caso de uma resposta coreta. + +## Requisitos do projeto + +- [X] Ter pelo menos 5 perguntas. +- [X] As divisões resultam apenas em INTEGERS e os dividendos devem passar de 0 a 100. Além disso não deve apresentar a divisão 7/2 para o usuário, já que não resulta em um número inteiro. +- [X] Os usuários devem receber um menu para escolher uma operação. +- [X] Deve gravar jogos anteriores em uma lista e deve haver uma opção no menu para o usuário visualizar um histórico de jogos anteriores. + +## Desafios +- [X] Implementar níveis de dificuldade. +- [X] Adicione um tempororizador para acompanhar quanto o usuário leva para terminar o jogo. +- [X] Crie uma opção de 'jogo aleátorio' onde os jogadores serão apresentados com perguntas de operações aleatórios. (* Praticamente todos que eu fiz são aleátorios.) +- [X] Tentar usar apenas um método para todos os jogos. Utilizar o Princípio DRY. ( Não achei algo !) \ No newline at end of file diff --git a/LucasCorreia.MathGame/math-game/Teste.cs b/LucasCorreia.MathGame/math-game/Teste.cs new file mode 100644 index 00000000..7eb3573c --- /dev/null +++ b/LucasCorreia.MathGame/math-game/Teste.cs @@ -0,0 +1,9 @@ +public class Teste +{ + public void Test() + { + Libary lb = new(); + + lb.NovasPerguntas(7,2,"/"); + } +} \ No newline at end of file diff --git a/LucasCorreia.MathGame/math-game/Time.cs b/LucasCorreia.MathGame/math-game/Time.cs new file mode 100644 index 00000000..49a4a339 --- /dev/null +++ b/LucasCorreia.MathGame/math-game/Time.cs @@ -0,0 +1,15 @@ +using System.Diagnostics; + +public class Time +{ + public static double MedirTempo(Action acao) + { + Stopwatch tempo = Stopwatch.StartNew(); + + acao(); + + tempo.Stop(); + + return tempo.Elapsed.TotalSeconds; + } +} \ No newline at end of file diff --git a/LucasCorreia.MathGame/math-game/math-game.csproj b/LucasCorreia.MathGame/math-game/math-game.csproj new file mode 100644 index 00000000..d4b756a1 --- /dev/null +++ b/LucasCorreia.MathGame/math-game/math-game.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + math_game + enable + enable + + + From 57c02540319c42de656407d573307d9157b47620 Mon Sep 17 00:00:00 2001 From: Lucas Correia <164992636+lucascorreia-tech@users.noreply.github.com> Date: Fri, 4 Sep 2026 16:40:28 -0300 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20Modificando=20a=20l=C3=B3gica?= =?UTF-8?q?=20por=20tras=20dos=20jogos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LucasCorreia.MathGame/math-game/Libary.cs | 108 --------------------- LucasCorreia.MathGame/math-game/Library.cs | 94 ++++++++++++++++++ 2 files changed, 94 insertions(+), 108 deletions(-) delete mode 100644 LucasCorreia.MathGame/math-game/Libary.cs create mode 100644 LucasCorreia.MathGame/math-game/Library.cs diff --git a/LucasCorreia.MathGame/math-game/Libary.cs b/LucasCorreia.MathGame/math-game/Libary.cs deleted file mode 100644 index fb6de24c..00000000 --- a/LucasCorreia.MathGame/math-game/Libary.cs +++ /dev/null @@ -1,108 +0,0 @@ -public class Libary -{ - private Random randNum = new(); - - private string[] operadores = {"+", "-", "x", "/"}; - public int Easy() - { - int ponto = 0; - for (int i = 0; i < 5; i++) - { - string operador = operadores[randNum.Next(operadores.Length)]; - int num1 = randNum.Next(11); - int num2 = randNum.Next(11); - NovasPerguntas(num1,num2,operador); - int resposta = Convert.ToInt32(Console.ReadLine()); - bool resultado = Resposta(num1,num2,operador,resposta); - if (resultado) - { - ponto++; - } - } - - return ponto; - } - - public int Inter() - { - int ponto = 0; - for (int i = 0; i < 5; i++) - { - string operador = operadores[randNum.Next(operadores.Length)]; - int num1 = randNum.Next(51); - int num2 = randNum.Next(51); - NovasPerguntas(num1,num2,operador); - int resposta = Convert.ToInt32(Console.ReadLine()); - bool resultado = Resposta(num1,num2,operador,resposta); - if (resultado) - { - ponto++; - } - } - - return ponto; - } - - public int Dificult() - { - int ponto = 0; - for (int i = 0; i < 5; i++) - { - string operador = operadores[randNum.Next(operadores.Length)]; - int num1 = randNum.Next(101); - int num2 = randNum.Next(101); - NovasPerguntas(num1,num2,operador); - int resposta = Convert.ToInt32(Console.ReadLine()); - bool resultado = Resposta(num1,num2,operador,resposta); - if (resultado) - { - ponto++; - } - } - - return ponto; - } - - public void NovasPerguntas(int x, int y, string operador) - { - if (operador == "/" && x % y != 0 ) - { - operador = operadores[randNum.Next(3)]; - } - Console.WriteLine("Qual é resultado da conta abaixo ?"); - Console.WriteLine($"{x} {operador} {y}"); - } - - public bool Resposta(int x, int y, string operador, int z) - { - switch (operador) - { - case "+": - if ((x + y) == z) - { - return true; - } - break; - case "-": - if ((x - y) == z) - { - return true; - } - break; - case "/": - if ((x / y) == z) - { - return true; - } - break; - case "x": - if ((x * y) == z) - { - return true; - } - break; - } - - return false; - } -} \ No newline at end of file diff --git a/LucasCorreia.MathGame/math-game/Library.cs b/LucasCorreia.MathGame/math-game/Library.cs new file mode 100644 index 00000000..6ce011a7 --- /dev/null +++ b/LucasCorreia.MathGame/math-game/Library.cs @@ -0,0 +1,94 @@ +public class Library +{ + private Random randNum = new(); + private string[] operadores = { "+", "-", "x", "/" }; + + public int Jogar(string operador, int maxNum1, int maxNum2) + { + int ponto = 0; + for (int i = 0; i < 5; i++) + { + int num1 = 0; + int num2 = 0; + + string opAtual = operador == "aleatorio" ? operadores[randNum.Next(operadores.Length)] : operador; + + if (opAtual == "/") + { + num2 = randNum.Next(1, maxNum2 + 1); + int multiplicador = randNum.Next(0, maxNum1 + 1); + num1 = num2 * multiplicador; + } + + NovasPerguntas(num1, num2, opAtual); + + if (int.TryParse(Console.ReadLine(), out int resposta)) + { + if (Resposta(num1, num2, opAtual, resposta)) + { + ponto++; + } + } + else + { + Console.Clear(); + Console.WriteLine("Por favor, digite apenas números!\n"); + i--; + } + } + return ponto; + } + + public void NovasPerguntas(int x, int y, string operador) + { + Console.WriteLine("Qual é resultado da conta abaixo ?"); + Console.WriteLine($"{x} {operador} {y}"); + } + + public bool Resposta(int x, int y, string operador, int z) + { + return operador switch + { + "+" => (x + y) == z, + "-" => (x - y) == z, + "/" => (x / y) == z, + "x" => (x * y) == z, + _ => false + }; + } + + public string EscolhaOperador() + { + int op = 0; + bool entradaValida = false; + + while(!entradaValida) + { + Console.WriteLine("Escolha a operação que quer fazer:"); + Console.WriteLine("1- Somar\n2- Subtrair\n3- Multiplicar\n4- Dividir\n5- Aleatório"); + + if (int.TryParse(Console.ReadLine(), out op) && op >= 1 && op <= 5) + { + entradaValida = true; + } + else + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("Opção inválida! Por favor, digite um número de 1 a 5.\n"); + Console.ResetColor(); + } + } + + + return op switch + { + 1 => "+", + 2 => "-", + 3 => "x", + 4 => "/", + 5 => "aleatorio", + _ => throw new ArgumentException("Número escolhido errado!") + }; + } +} From b64eae719aaeab40f653488eb82f1a33f877331b Mon Sep 17 00:00:00 2001 From: Lucas Correia <164992636+lucascorreia-tech@users.noreply.github.com> Date: Fri, 4 Sep 2026 16:41:17 -0300 Subject: [PATCH 3/4] refactor: Refatorando os cases --- LucasCorreia.MathGame/math-game/Program.cs | 112 +++++++++++++++------ 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/LucasCorreia.MathGame/math-game/Program.cs b/LucasCorreia.MathGame/math-game/Program.cs index d1895d67..52065fb3 100644 --- a/LucasCorreia.MathGame/math-game/Program.cs +++ b/LucasCorreia.MathGame/math-game/Program.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using System.Diagnostics; public class Program { @@ -6,48 +6,94 @@ public static void Main() { int op = 0; int pontos = 0; - Libary lb = new(); + Library lb = new(); double tempo_jogo = 0; List rodadas = []; List tempo_rodadas = []; + string operador = ""; + do { Console.WriteLine("===============================\nBem vindo ao Jogo Matématico.\n==============================="); Console.WriteLine("\nEscolha uma opção abaixo:"); - Console.WriteLine("1- Jogo fácil\n2- Jogo intermédiario\n3- Jogo Difícil\n4- Listar Pontuações\n0- Sair do jogo"); - op = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("1- Jogo fácil\n2- Jogo intermédiario\n3- Jogo Difícil\n4- Jogo Aleátorio\n5- Listar Pontuações\n0- Sair do jogo"); + + if (!int.TryParse(Console.ReadLine(), out op)) + { + Console.Clear(); + Console.WriteLine("Por favor, digite apenas números!\n"); + Console.WriteLine("Pressione qualquer tecla para continuar..."); + Console.ReadKey(); + // PARA O C# PARA DE CONVERTE PARA 0 + op = -1; + Console.Clear(); + continue; + } + Console.Clear(); - switch (op) + + try + { + switch (op) + { + case 1: + operador = lb.EscolhaOperador(); + tempo_jogo = Time.MedirTempo(() => pontos = lb.Jogar(operador, 10, 10)); + rodadas.Add(pontos); + tempo_rodadas.Add(tempo_jogo); + break; + case 2: + operador = lb.EscolhaOperador(); + tempo_jogo = Time.MedirTempo(() => pontos = lb.Jogar(operador, 10, 50)); + rodadas.Add(pontos); + tempo_rodadas.Add(tempo_jogo); + break; + case 3: + operador = lb.EscolhaOperador(); + tempo_jogo = Time.MedirTempo(() => pontos = lb.Jogar(operador, 50, 100)); + rodadas.Add(pontos); + tempo_rodadas.Add(tempo_jogo); + break; + case 4: + tempo_jogo = Time.MedirTempo(() => pontos = lb.Jogar("aleatorio", 100, 100)); + rodadas.Add(pontos); + tempo_rodadas.Add(tempo_jogo); + break; + case 5: + Console.WriteLine("=====================\n=PONTUAÇÃO DOS JOGOS=\n====================="); + if (rodadas.Count==0) + { + Console.WriteLine("Nenhuma partida Jogada"); + } + else + { + for (int i = 0; i < rodadas.Count; i++) + { + Console.WriteLine($"Rodada {i + 1}° -> {rodadas[i]} pontos | Tempo -> {(int)tempo_rodadas[i]} segundos"); + } + } + Console.WriteLine("\nPressione ENTER para voltar ao menu"); + Console.ReadLine(); + Console.Clear(); + break; + case 0: + break; + default: + Console.WriteLine("Opção inválida"); + break; + } + } catch (ArgumentException ex) { - case 1: - tempo_jogo = Time.MedirTempo(() => pontos = lb.Easy()); - rodadas.Add(pontos); - tempo_rodadas.Add(tempo_jogo); - break; - case 2: - pontos = lb.Inter(); - rodadas.Add(pontos); - break; - case 3: - pontos = lb.Dificult(); - rodadas.Add(pontos); - break; - case 4: - Console.WriteLine("=====================\n=PONTUAÇÃO DOS JOGOS=\n====================="); - for (int i = 0; i < rodadas.Count; i++) - { - Console.WriteLine($"Rodada {i + 1}° -> {rodadas[i]} pontos | Tempo -> {(int)tempo_rodadas[i]} segundos"); - } - Console.ReadLine(); - Console.WriteLine(); - break; - case 0: - break; - default: - Console.WriteLine("Opção inválida"); - break; + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"[ERRO]: {ex.Message}"); + Console.ResetColor(); + Console.WriteLine("Pressione qualquer tecla para voltar ao menu..."); + Console.ReadKey(); + Console.Clear(); } + }while(op != 0); } -} \ No newline at end of file +} From 1b15e254e0b987b838d8fa0eab082dfd7a0fe543 Mon Sep 17 00:00:00 2001 From: Lucas Correia <164992636+lucascorreia-tech@users.noreply.github.com> Date: Fri, 4 Sep 2026 16:42:11 -0300 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20Mudando=20o=20nome=20da=20inst=C3=A2?= =?UTF-8?q?ncia=20para=20Library?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LucasCorreia.MathGame/math-game/Teste.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LucasCorreia.MathGame/math-game/Teste.cs b/LucasCorreia.MathGame/math-game/Teste.cs index 7eb3573c..c2ca31c5 100644 --- a/LucasCorreia.MathGame/math-game/Teste.cs +++ b/LucasCorreia.MathGame/math-game/Teste.cs @@ -2,8 +2,8 @@ public class Teste { public void Test() { - Libary lb = new(); + Library lb = new(); lb.NovasPerguntas(7,2,"/"); } -} \ No newline at end of file +}