From 5415763e0833efb012ae154d8a7d107b11a9770b Mon Sep 17 00:00:00 2001 From: sange Date: Mon, 29 Jun 2026 21:00:53 +0530 Subject: [PATCH 1/2] Project Finished ! Add math quiz game with menu, scoring, and history Initialized solution and project for MathGame. Implemented console-based math quiz using Spectre.Console. Added menu for quiz, score history, and exit options. Tracks user scores and quiz completion time. --- Sangeerths.MathGame/Sangeerths.MathGame.slnx | 3 + .../Sangeerths.MathGame/Program.cs | 76 +++++++++++++++++++ .../Sangeerths.MathGame.csproj | 14 ++++ 3 files changed, 93 insertions(+) create mode 100644 Sangeerths.MathGame/Sangeerths.MathGame.slnx create mode 100644 Sangeerths.MathGame/Sangeerths.MathGame/Program.cs create mode 100644 Sangeerths.MathGame/Sangeerths.MathGame/Sangeerths.MathGame.csproj diff --git a/Sangeerths.MathGame/Sangeerths.MathGame.slnx b/Sangeerths.MathGame/Sangeerths.MathGame.slnx new file mode 100644 index 00000000..a583d445 --- /dev/null +++ b/Sangeerths.MathGame/Sangeerths.MathGame.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs b/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs new file mode 100644 index 00000000..603c1cad --- /dev/null +++ b/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs @@ -0,0 +1,76 @@ +using System.Diagnostics; +using Spectre.Console; + +namespace MathGame +{ + class Program + { + static void Main(string[] args) + { + List history= new List(); + + while (true) + { + var choice = AnsiConsole.Prompt(new SelectionPrompt().Title("* * MENU * * ").AddChoices("Quiz me ..", "Previous Scores", "Exit")); + int points = 0; + switch (choice) + { + + case "Quiz me ..": + var timer = new Stopwatch(); + timer.Start(); + Console.WriteLine(" what is 5 + 5"); + int result1 = Convert.ToInt32(Console.ReadLine()); + points = MathOperation("add", result1, points); + + Console.WriteLine("What is 10 - 2"); + int result2 = Convert.ToInt32(Console.ReadLine()); + points = MathOperation("sub", result2, points); + + Console.WriteLine("what is 9 * 5"); + int result3 = Convert.ToInt32(Console.ReadLine()); + points = MathOperation("mul", result3, points); + Console.WriteLine("What is 10 / 10"); + int result4 = Convert.ToInt32(Console.ReadLine()); + points = MathOperation("div", result4, points); + timer.Stop(); + TimeSpan timeTaken = timer.Elapsed; + Console.WriteLine($"Time taken: {timeTaken}"); + history.Add( points); + break; + case "Previous Scores": + for (int i = 0; i < history.Count; i++) + { + Console.WriteLine($"Attempt {i+1} : points {history[i]} "); + } + break; + case "Exit": + Environment.Exit(0); + break; + default: + Console.WriteLine("Invalid choice"); + break; + + } + + + } + } + + static int MathOperation(string operation, int result, int points) + { + if (operation == "add" && result == 10 || operation == "sub" && result == 8 || operation == "div" && result == 1 || operation == "mul" && result == 45) + { + Console.WriteLine("Correct answer"); + points += 10; + + } + else + Console.WriteLine("Wrong answer"); + + return points; + + } + + } +} diff --git a/Sangeerths.MathGame/Sangeerths.MathGame/Sangeerths.MathGame.csproj b/Sangeerths.MathGame/Sangeerths.MathGame/Sangeerths.MathGame.csproj new file mode 100644 index 00000000..85c10f3f --- /dev/null +++ b/Sangeerths.MathGame/Sangeerths.MathGame/Sangeerths.MathGame.csproj @@ -0,0 +1,14 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + From c008b721511f1b410982ca448c1f24bdca91cb34 Mon Sep 17 00:00:00 2001 From: sange Date: Tue, 30 Jun 2026 22:29:59 +0530 Subject: [PATCH 2/2] feat: add random quizzes, input validation, and detailed game history --- .../Sangeerths.MathGame/Program.cs | 147 ++++++++++++++---- 1 file changed, 118 insertions(+), 29 deletions(-) diff --git a/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs b/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs index 603c1cad..0c69beb3 100644 --- a/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs +++ b/Sangeerths.MathGame/Sangeerths.MathGame/Program.cs @@ -7,8 +7,7 @@ class Program { static void Main(string[] args) { - List history= new List(); - + List history = new List(); while (true) { var choice = AnsiConsole.Prompt(new SelectionPrompt().Title("* * MENU * * ").AddChoices("Quiz me ..", "Previous Scores", "Exit")); @@ -19,29 +18,36 @@ static void Main(string[] args) case "Quiz me ..": var timer = new Stopwatch(); timer.Start(); - Console.WriteLine(" what is 5 + 5"); - int result1 = Convert.ToInt32(Console.ReadLine()); - points = MathOperation("add", result1, points); - - Console.WriteLine("What is 10 - 2"); - int result2 = Convert.ToInt32(Console.ReadLine()); - points = MathOperation("sub", result2, points); - - Console.WriteLine("what is 9 * 5"); - int result3 = Convert.ToInt32(Console.ReadLine()); - points = MathOperation("mul", result3, points); - Console.WriteLine("What is 10 / 10"); - int result4 = Convert.ToInt32(Console.ReadLine()); - points = MathOperation("div", result4, points); + var op = AnsiConsole.Prompt(new SelectionPrompt().Title("Choose an operation:").AddChoices("+", "-", "*", "/")); + points = MathOperation(op); timer.Stop(); - TimeSpan timeTaken = timer.Elapsed; - Console.WriteLine($"Time taken: {timeTaken}"); - history.Add( points); + + history.Add(new GameHistory + { + Date = DateTime.Now, + GameType = op, + Score = points, + TimeTaken = timer.Elapsed + }); + break; case "Previous Scores": - for (int i = 0; i < history.Count; i++) + if (history.Count == 0) + { + Console.WriteLine("No games played yet."); + } + else { - Console.WriteLine($"Attempt {i+1} : points {history[i]} "); + Console.WriteLine("\nGame History\n"); + + foreach (var game in history) + { + Console.WriteLine( + $"Date : {game.Date:dd-MM-yyyy HH:mm:ss}\n" + + $"Game : {game.GameType}\n" + + $"Score: {game.Score}\n" + + $"Time : {game.TimeTaken.TotalSeconds:F2} seconds\n"); + } } break; case "Exit": @@ -57,20 +63,103 @@ static void Main(string[] args) } } - static int MathOperation(string operation, int result, int points) + static int MathOperation(string op) { - if (operation == "add" && result == 10 || operation == "sub" && result == 8 || operation == "div" && result == 1 || operation == "mul" && result == 45) + Random random = new Random(); + int points = 0; + int count = 0; + while (count < 5) { - Console.WriteLine("Correct answer"); - points += 10; - + int a = random.Next(1, 100); + int b = random.Next(1, 100); + int answer = 0; + switch (op) + { + + case "+": + + Console.WriteLine($"What is {a} + {b} "); + if (!int.TryParse(Console.ReadLine(), out answer)) + { + Console.WriteLine("Invalid input."); + } + else if (answer == a + b) + { + Console.WriteLine("Correct!"); + points += 10; + } + else + { + Console.WriteLine($"Wrong! The correct answer is {a + b}"); + } + break; + case "-": + Console.WriteLine($"What is {a} - {b}"); + if (!int.TryParse(Console.ReadLine(), out answer)) + { + Console.WriteLine("Invalid input."); + } + else if (answer == a - b) + { + Console.WriteLine("Correct!"); + points += 10; + } + else + { + Console.WriteLine($"Wrong! The correct answer is {a + b}"); + } + break; + case "*": + Console.WriteLine($"What is {a} * {b}"); + if (!int.TryParse(Console.ReadLine(), out answer)) + { + Console.WriteLine("Invalid input."); + } + else if (answer == a * b) + { + Console.WriteLine("Correct!"); + points += 10; + } + else + { + Console.WriteLine($"Wrong! The correct answer is {a + b}"); + } + break; + case "/": + Console.WriteLine($"What is {a} / {b}"); + if (!int.TryParse(Console.ReadLine(), out answer)) + { + Console.WriteLine("Invalid input."); + } + else if (answer == a / b) + { + Console.WriteLine("Correct!"); + points += 10; + } + else + { + Console.WriteLine($"Wrong! The correct answer is {a + b}"); + } + break; + + default: + Console.WriteLine("Invalid operation"); + break; + } + count++; } - else - Console.WriteLine("Wrong answer"); return points; } - + + class GameHistory + { + public DateTime Date { get; set; } + public string GameType { get; set; } + public int Score { get; set; } + public TimeSpan TimeTaken { get; set; } + } + } }