From 5e564ef67bad210017e316dd67c511ce5b7d7652 Mon Sep 17 00:00:00 2001 From: cdparkes Date: Wed, 30 Sep 2026 00:33:54 +0200 Subject: [PATCH 1/3] Uploading my previous Solution to submit it for review --- mathGame.cparkes1/Program.cs | 103 +++++++++++++++++++++ mathGame.cparkes1/mathGame.cparkes1.csproj | 10 ++ mathGame.cparkes1/mathGame.cparkes1.slnx | 3 + 3 files changed, 116 insertions(+) create mode 100644 mathGame.cparkes1/Program.cs create mode 100644 mathGame.cparkes1/mathGame.cparkes1.csproj create mode 100644 mathGame.cparkes1/mathGame.cparkes1.slnx diff --git a/mathGame.cparkes1/Program.cs b/mathGame.cparkes1/Program.cs new file mode 100644 index 00000000..9f10e249 --- /dev/null +++ b/mathGame.cparkes1/Program.cs @@ -0,0 +1,103 @@ +using System.Data; +using System.Numerics; + +Random random = new Random(); +List scoreList = new List(); + +string userSelection = ""; + +while (true) +{ + int scoreTracker = 0; + int turnCounter = 0; + + Console.Clear(); + Console.WriteLine(""" + What Mathematical operator do you want? + + Answer 5 Addition Questions + - Answer 5 Subtraction Questions + * Answer 5 Multiplication Questions + / Answer 5 Division Questions + Scores Show the previous Scores + Exit Exit the Program + """); + + userSelection = Console.ReadLine() ?? ""; + + if (userSelection.ToLower() == "exit") + { + break; + } + else if (userSelection.ToLower() == "scores") + { + Console.Clear(); + if (scoreList.Count == 0) + { + Console.WriteLine("No scores yet."); + Console.WriteLine("Press enter to return to main menu"); + Console.ReadLine(); + } + + Console.WriteLine("Previous Scores:"); + foreach (var s in scoreList) + Console.WriteLine(s); + Console.WriteLine("Press enter to return to main menu"); + Console.ReadLine(); + continue; + } + else if (userSelection != "+" && userSelection != "-" && userSelection != "*" && userSelection != "/") + { + Console.Clear(); + Console.WriteLine("Invalid Option. Please try again."); + Console.WriteLine("Press enter to return to main menu"); + Console.ReadLine(); + } + + Console.Clear(); + do + { + int firstNumber = random.Next(101); + int secondNumber = random.Next(101); + int expectedSolution = 0; + + if (userSelection.Equals("/") && firstNumber == 0 || userSelection.Equals("/") && secondNumber == 0 || userSelection.Equals("/") && firstNumber % secondNumber != 0) + { + firstNumber = random.Next(101); + secondNumber = random.Next(101); + continue; + } + + Console.Write($"{firstNumber} {userSelection} {secondNumber} = "); + string? input = Console.ReadLine(); + if (!int.TryParse(input, out int userSolution)) + { + Console.WriteLine("\nInvalid input. Please enter an integer."); + continue; + } + + switch (userSelection) + { + case "+": expectedSolution = firstNumber + secondNumber; break; + case "-": expectedSolution = firstNumber - secondNumber; break; + case "*": expectedSolution = firstNumber * secondNumber; break; + case "/": expectedSolution = firstNumber / secondNumber; break; + } + ; + + if (userSolution == expectedSolution) + { + Console.WriteLine("\n Correct\n"); + scoreTracker++; + } + else + { + Console.WriteLine("\nIncorrect\n"); + } + + turnCounter++; + } while (turnCounter < 5); + Console.WriteLine($"Congrats you got {scoreTracker} points"); + scoreList.Add(scoreTracker); + Console.WriteLine("Press enter to return to main menu"); + Console.ReadLine(); +} \ No newline at end of file diff --git a/mathGame.cparkes1/mathGame.cparkes1.csproj b/mathGame.cparkes1/mathGame.cparkes1.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/mathGame.cparkes1/mathGame.cparkes1.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/mathGame.cparkes1/mathGame.cparkes1.slnx b/mathGame.cparkes1/mathGame.cparkes1.slnx new file mode 100644 index 00000000..22a45c0b --- /dev/null +++ b/mathGame.cparkes1/mathGame.cparkes1.slnx @@ -0,0 +1,3 @@ + + + From 0ac96e65409dd92c269b305ec9456a02747fae5b Mon Sep 17 00:00:00 2001 From: cdparkes Date: Wed, 30 Sep 2026 09:25:33 +0200 Subject: [PATCH 2/3] Add timing and store score with elapsed time Import System.Diagnostics and add Stopwatch timing around the question loop. Capture start timestamp before the do/while loop and compute elapsed time after, formatted as mm:ss.fff and shown in console output. Replace scoreTracker int with score and update increments/usages. Change scoreList from List to List and store combined "score + formatted elapsed" entries. Update console messages to display points alongside elapsed time. --- mathGame.cparkes1/Program.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/mathGame.cparkes1/Program.cs b/mathGame.cparkes1/Program.cs index 9f10e249..d694d336 100644 --- a/mathGame.cparkes1/Program.cs +++ b/mathGame.cparkes1/Program.cs @@ -1,14 +1,15 @@ using System.Data; using System.Numerics; +using System.Diagnostics; Random random = new Random(); -List scoreList = new List(); +List scoreList = new List(); string userSelection = ""; while (true) { - int scoreTracker = 0; + int score = 0; int turnCounter = 0; Console.Clear(); @@ -54,6 +55,7 @@ Exit Exit the Program } Console.Clear(); + long startTime = Stopwatch.GetTimestamp(); do { int firstNumber = random.Next(101); @@ -87,7 +89,7 @@ Exit Exit the Program if (userSolution == expectedSolution) { Console.WriteLine("\n Correct\n"); - scoreTracker++; + score++; } else { @@ -96,8 +98,11 @@ Exit Exit the Program turnCounter++; } while (turnCounter < 5); - Console.WriteLine($"Congrats you got {scoreTracker} points"); - scoreList.Add(scoreTracker); + + TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime); + string formattedElapsedTime = elapsedTime.ToString(@"mm\:ss\.fff"); + Console.WriteLine($"Congrats you got {score} points in {formattedElapsedTime}"); + scoreList.Add($"{score} {formattedElapsedTime}"); Console.WriteLine("Press enter to return to main menu"); Console.ReadLine(); } \ No newline at end of file From cdf990ab0258f2b8f2d3e5e3f50ad3b7e62fc795 Mon Sep 17 00:00:00 2001 From: cdparkes Date: Wed, 30 Sep 2026 09:58:49 +0200 Subject: [PATCH 3/3] Use GameResult model for score history Replace the old string-based score list with a typed GameResult model and update program logic to store, sort, and display GameResult instances. - Add GameResult class (Score:int, Time:TimeSpan) with ctor and ToString() that formats time as mm:ss.fff. - Add using System.Collections.Generic and System.Linq to Program.cs. - Replace List scoreList with List scoreHistory. - Build sortedLeaderboard with LINQ: OrderByDescending(r => r.Score).ThenBy(r => r.Time) and print entries via GameResult.ToString(). - On game end compute elapsedTime (TimeSpan) and add new GameResult(score, elapsedTime) instead of a preformatted string. --- mathGame.cparkes1/GameResult.cs | 16 ++++++++++++++++ mathGame.cparkes1/Program.cs | 18 +++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 mathGame.cparkes1/GameResult.cs diff --git a/mathGame.cparkes1/GameResult.cs b/mathGame.cparkes1/GameResult.cs new file mode 100644 index 00000000..5987020c --- /dev/null +++ b/mathGame.cparkes1/GameResult.cs @@ -0,0 +1,16 @@ +public class GameResult +{ + public int Score { get; set; } + public TimeSpan Time { get; set; } + + public GameResult(int score, TimeSpan time) + { + Score = score; + Time = time; + } + + public override string ToString() + { + return $"Score: {Score} | Time: {Time:mm\\:ss\\.fff}"; + } +} \ No newline at end of file diff --git a/mathGame.cparkes1/Program.cs b/mathGame.cparkes1/Program.cs index d694d336..6da80b18 100644 --- a/mathGame.cparkes1/Program.cs +++ b/mathGame.cparkes1/Program.cs @@ -1,9 +1,11 @@ using System.Data; using System.Numerics; using System.Diagnostics; +using System.Collections.Generic; +using System.Linq; Random random = new Random(); -List scoreList = new List(); +List scoreHistory = new(); string userSelection = ""; @@ -32,15 +34,20 @@ Exit Exit the Program else if (userSelection.ToLower() == "scores") { Console.Clear(); - if (scoreList.Count == 0) + if (scoreHistory.Count == 0) { Console.WriteLine("No scores yet."); Console.WriteLine("Press enter to return to main menu"); Console.ReadLine(); } + List sortedLeaderboard = scoreHistory + .OrderByDescending(r => r.Score) + .ThenBy(r => r.Time) + .ToList(); + Console.WriteLine("Previous Scores:"); - foreach (var s in scoreList) + foreach (var s in sortedLeaderboard) Console.WriteLine(s); Console.WriteLine("Press enter to return to main menu"); Console.ReadLine(); @@ -102,7 +109,8 @@ Exit Exit the Program TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime); string formattedElapsedTime = elapsedTime.ToString(@"mm\:ss\.fff"); Console.WriteLine($"Congrats you got {score} points in {formattedElapsedTime}"); - scoreList.Add($"{score} {formattedElapsedTime}"); + scoreHistory.Add(new GameResult(score, elapsedTime)); Console.WriteLine("Press enter to return to main menu"); Console.ReadLine(); -} \ No newline at end of file +} +