diff --git a/kedwards.MathGame/MathGame.csproj b/kedwards.MathGame/MathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/kedwards.MathGame/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/kedwards.MathGame/Program.cs b/kedwards.MathGame/Program.cs new file mode 100644 index 00000000..c89a3a54 --- /dev/null +++ b/kedwards.MathGame/Program.cs @@ -0,0 +1,184 @@ +// See https://aka.ms/new-console-template for more information +using System.Diagnostics; + +List pastScores = new List(); +bool playAgain = true; + +while(playAgain) +{ + Menu(); + string choice = UserChoices(); + if (choice == "5") + { + continue; + } + Questions(choice); + Console.WriteLine("Do you want to play again? (y/n)"); + string answer = Console.ReadLine().Trim().ToLower(); + + if (answer != "y") + { + playAgain = false; + Console.WriteLine("Thanks for playing!"); + } +} +void Menu() { + Console.WriteLine("------------------------------------"); + Console.WriteLine("Hello, Welcome to the Math Game!"); + Console.WriteLine("What is your name?"); + string userName = Console.ReadLine(); + Console.WriteLine($"Hello {userName}, let's start the game!"); + Console.WriteLine("Please choose your choices"); + +} + +string UserChoices() +{ + Console.WriteLine("Choose an operation:"); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Subtraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.WriteLine("5. ShowPastScores"); + + string choice = Console.ReadLine(); + + + if (choice.Trim() == "1") + { + Addition(); + } + else if (choice.Trim() == "2") + { + Subtraction(); + } + else if (choice.Trim() == "3") + { + Multiplication(); + } + else if (choice.Trim() == "4") + { + Division(); + } + + else if (choice.Trim() == "5") + { + ShowPastScore(); + } + else if (choice.Trim().ToLower() == "q") + { + Console.WriteLine("Goodbye"); + Environment.Exit(0); + } + else + { + Console.WriteLine(" Invalid Choice"); + } + return choice; +} +//creating Choices in switch break cases +// selection 1. - Addition so userInput1 + userInput2 +// selection 2. - Subtraction so userInput1 - userInput2 +// selection 3. - Multiplication so userInput1 * userInput2 +// selection 4. - Division so userInput1 / userInput2 + +void Questions(string question) +{ + Random random = new Random(); + int score = 0; + int timeLimitSeconds = 10; + for (int i = 0; i < 5; i ++) + { + int num1 = random.Next(1, 10); + int num2 = random.Next(1, 10); + int correctAnswer = 0; + switch (question) + { + case "1": + Console.WriteLine($"What is {num1} + {num2}? "); + correctAnswer = num1 + num2; + break; + case "2": + Console.WriteLine($"What is {num1} - {num2}? "); + correctAnswer = num1 - num2; + break; + case "3": + Console.WriteLine($"What is {num1} x {num2}? "); + correctAnswer = num1 * num2; + break; + case "4": + while (num2 == 0) + { + num2 = random.Next(1, 10); + } + correctAnswer = num1 / num2; + Console.WriteLine($"What is {num1} / {num2}? "); + break; + + case "q": + Console.WriteLine("Goodbye"); + Environment.Exit(1); + break; + default: + Console.WriteLine("Invalid choice"); + Environment.Exit(1); + break; + } + + //Timer Thread + Stopwatch stopwatch = Stopwatch.StartNew(); + string input = Console.ReadLine(); + stopwatch.Stop(); + if (stopwatch.Elapsed.TotalSeconds > timeLimitSeconds) + { + Console.WriteLine("Time's up!"); + Console.WriteLine($"The correct answer was {correctAnswer}"); + } + int userResult; + bool validInput = int.TryParse(input, out userResult); + if (validInput && userResult == correctAnswer) + { + Console.WriteLine("Your answer is correct"); + score++; + } + else + { + Console.WriteLine($"Your answer is incorrect! The answer is {correctAnswer}"); + } + + } + pastScores.Add(score); + Console.WriteLine($"Game Over! the final scoreis {score} / 5"); + ShowPastScore(); + +} +void Addition() +{ + Console.WriteLine(" Addition Selected"); +} +void Subtraction() +{ + Console.WriteLine(" Subtraction Selected"); +} +void Multiplication() +{ + Console.WriteLine(" Multiplication Selected"); +} +void Division() +{ + Console.WriteLine(" Division Selected"); +} + +void ShowPastScore() +{ + Console.WriteLine("Do you want to see your past scores? (y/n)"); + string input = Console.ReadLine(); + if (input == "y") + { + Console.WriteLine("\n Past Scores:"); + for (int i = 0; i < pastScores.Count; i++) + { + Console.WriteLine($"Game {i + 1}: {pastScores[i]} / 5"); + } + } +}