diff --git a/MathGame.Bashar-Amer/MathGame.Bashar-Amer.slnx b/MathGame.Bashar-Amer/MathGame.Bashar-Amer.slnx new file mode 100644 index 00000000..9c7fe748 --- /dev/null +++ b/MathGame.Bashar-Amer/MathGame.Bashar-Amer.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame.Bashar-Amer/MathGame.Bashar-Amer/MathGame.Bashar-Amer.csproj b/MathGame.Bashar-Amer/MathGame.Bashar-Amer/MathGame.Bashar-Amer.csproj new file mode 100644 index 00000000..97a374a3 --- /dev/null +++ b/MathGame.Bashar-Amer/MathGame.Bashar-Amer/MathGame.Bashar-Amer.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + MathGame.Bashar_Amer + enable + enable + + + diff --git a/MathGame.Bashar-Amer/MathGame.Bashar-Amer/MathGameLogic.cs b/MathGame.Bashar-Amer/MathGame.Bashar-Amer/MathGameLogic.cs new file mode 100644 index 00000000..57b6deb2 --- /dev/null +++ b/MathGame.Bashar-Amer/MathGame.Bashar-Amer/MathGameLogic.cs @@ -0,0 +1,50 @@ +namespace MathGame.Bashar_Amer; + +internal class MathGameLogic +{ + public List GameHistory { get; set; } = new(); + + public void ShowMenu() + { + Console.WriteLine("Please enter an option to select operation:"); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Subtraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.WriteLine("5. Random Mode"); + Console.WriteLine("6. Show History"); + Console.WriteLine("7. Change difficulty"); + Console.WriteLine("8. Exist"); + Console.WriteLine("--------------------"); + } + + public int CalculateMathOperation(int firstNumber, int secondNumber, char operation) + { + switch (operation) + { + case '+': + GameHistory.Add($"firstNumber + secondNumber = {firstNumber + secondNumber}"); + return firstNumber + secondNumber; + case '-': + GameHistory.Add($"firstNumber - secondNumber = {firstNumber - secondNumber}"); + return firstNumber - secondNumber; + case '*': + GameHistory.Add($"firstNumber * secondNumber = {firstNumber * secondNumber}"); + return firstNumber * secondNumber; + case '÷': + while (firstNumber < 0 || firstNumber > 100) + { + try { + Console.WriteLine("Please enter between 0 and 100."); + firstNumber = Convert.ToInt32(Console.ReadLine()); + } + catch (Exception) + { } + + } + GameHistory.Add($"firstNumber / secondNumber = {firstNumber / secondNumber}"); + return firstNumber / secondNumber; + } + return 0; + } +} diff --git a/MathGame.Bashar-Amer/MathGame.Bashar-Amer/Program.cs b/MathGame.Bashar-Amer/MathGame.Bashar-Amer/Program.cs new file mode 100644 index 00000000..3fc6ccf2 --- /dev/null +++ b/MathGame.Bashar-Amer/MathGame.Bashar-Amer/Program.cs @@ -0,0 +1,221 @@ +using System.Diagnostics; + +namespace MathGame.Bashar_Amer; + +internal class Program +{ + static async Task Main() + { + MathGameLogic mathGame = new(); + Random random = new(); + + int firstNumber; + int secondNumber; + int userMenuSelection; + int score = 0; + bool gameOver = false; + + DifficultyLevel difficultyLevel = DifficultyLevel.Easy; + + while (!gameOver) + { + userMenuSelection = GetUserMenuSelection(mathGame); + + firstNumber = random.Next(1, 101); + secondNumber = random.Next(1, 101); + + switch (userMenuSelection) + { + case 1: + score += await PerformOperation(mathGame,firstNumber,secondNumber,'+',score, difficultyLevel); + break; + case 2: + score += await PerformOperation(mathGame, firstNumber, secondNumber, '-', score, difficultyLevel); + break; + case 3: + score += await PerformOperation(mathGame, firstNumber, secondNumber, '*', score, difficultyLevel); + break; + case 4: + while (firstNumber % secondNumber != 0) + { + firstNumber = random.Next(1, 101); + secondNumber = random.Next(1, 101); + } + score += await PerformOperation(mathGame, firstNumber, secondNumber, '/', score, difficultyLevel); + break; + case 5: + int numberOfQuestions = 99; + Console.WriteLine("Please enter the number of questions you want to attempt."); + + while (!int.TryParse(Console.ReadLine(),out numberOfQuestions)) + Console.WriteLine("Please enter the number of questions you want to attempt as an integer number."); + + while (numberOfQuestions > 0) + { + int randomOperation = random.Next(1, 5); + + firstNumber = random.Next(1, 101); + secondNumber = random.Next(1, 101); + + switch (randomOperation) + { + case 1: + score += await PerformOperation(mathGame, firstNumber, secondNumber, '+', score, difficultyLevel); + break; + case 2: + score += await PerformOperation(mathGame, firstNumber, secondNumber, '-', score, difficultyLevel); + break; + case 3: + score += await PerformOperation(mathGame, firstNumber, secondNumber, '*', score, difficultyLevel); + break; + case 4: + while (firstNumber % secondNumber != 0) + { + firstNumber = random.Next(1, 101); + secondNumber = random.Next(1, 101); + } + score += await PerformOperation(mathGame, firstNumber, secondNumber, '÷', score, difficultyLevel); + break; + } + numberOfQuestions--; + } + break; + + case 6: + Console.WriteLine("Game History: \n"); + foreach (var operation in mathGame.GameHistory) + Console.WriteLine(operation); + + break; + + case 7: + difficultyLevel = ChangeDifficulty(); + Enum.IsDefined(typeof(DifficultyLevel), difficultyLevel); + Console.WriteLine($"Your new difficulty level: {difficultyLevel}"); + break; + + case 8: + gameOver = true; + Console.WriteLine($"Your final score is: {score}"); + break; + + } + + + } + } + + + static DifficultyLevel ChangeDifficulty() + { + int userSelection = 0; + Console.WriteLine("1 - Easy"); + Console.WriteLine("2 - Medium"); + Console.WriteLine("3 - Hard"); + + while (!int.TryParse(Console.ReadLine(), out userSelection) || userSelection < 1 || userSelection > 3) + { + Console.WriteLine("Please enter a valid option 1-3"); + } + + return userSelection switch + { + 1 => DifficultyLevel.Easy, + 2 => DifficultyLevel.Medium, + 3 => DifficultyLevel.Hard, + _ => DifficultyLevel.Easy + }; + } + + static void DisplayMathGameQuestion(int firstNumber, int secondNumber, char operation) + { + Console.WriteLine($"{firstNumber} {operation} {secondNumber} = ??"); + } + + static int GetUserMenuSelection(MathGameLogic mathGame) + { + int selection = -1; + mathGame.ShowMenu(); + + while (selection < 1 || selection > 8) + { + while (!int.TryParse(Console.ReadLine(), out selection)) + Console.WriteLine("Please enter a valid option 1-8"); + + if (!(selection >= 1 & selection <= 8)) + Console.WriteLine("Please enter a valid option 1-8"); + } + return selection; + } + + static async Task GetUserResponse(DifficultyLevel difficulty) + { + + int response = 0; + int timeout = (int)difficulty; + + Stopwatch stopwatch = new(); + stopwatch.Start(); + + Task getUserInputTask = Task.Run(() => Console.ReadLine()); + + try + { + string? result = await Task.WhenAny(getUserInputTask, Task.Delay(timeout * 1000)) + == getUserInputTask ? getUserInputTask.Result : null; + + stopwatch.Stop(); + + if (result != null && int.TryParse(result, out response)) + { + Console.WriteLine($"Time taken to answer: {stopwatch.Elapsed.ToString(@"mm\:ss\.fff")}"); + return response; + } + else + throw new OperationCanceledException(); + + } + catch (OperationCanceledException) + { + Console.WriteLine("Time is up!"); + return null; + } + } + + static int ValidateResult(int result, int? userResponse, int score) + { + if (result == userResponse) + { + Console.WriteLine("Your answer is correct! You earned 5 points"); + score += 5; + } + else + { + Console.WriteLine("Try again!"); + Console.WriteLine($"Correct answer is: {result}"); + } + return score; + } + + + static async Task PerformOperation(MathGameLogic mathGame, int firstNumber, int secondNumber, char operation, int score, DifficultyLevel difficulty) + { + int result; + int? userResponse; + + DisplayMathGameQuestion(firstNumber, secondNumber, operation); + result = mathGame.CalculateMathOperation(firstNumber, secondNumber, operation); + userResponse = await GetUserResponse(difficulty); + score = ValidateResult(result, userResponse, score); + return score; + } + +} + +enum DifficultyLevel +{ + Easy = 45, + Medium = 30, + Hard = 15 +} +