diff --git a/MathGame/MathGame.slnx b/MathGame/MathGame.slnx new file mode 100644 index 00000000..c91187ad --- /dev/null +++ b/MathGame/MathGame.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame/MathGame/MathGame.csproj b/MathGame/MathGame/MathGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame/MathGame/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs new file mode 100644 index 00000000..9245f308 --- /dev/null +++ b/MathGame/MathGame/Program.cs @@ -0,0 +1,159 @@ +bool exitGame = false; +string? input; +List scores = new List(); +const int NumberOfQuestionsPerGame = 5; +const int ScorePerQuestion = 10; +const int MaximumScore = ScorePerQuestion * NumberOfQuestionsPerGame; + +do +{ + Console.WriteLine("Main menu"); + Console.WriteLine("Please select a game mode or type \"exit\" to close the app:"); + Console.WriteLine("1. Addition game"); + Console.WriteLine("2. Substraction game"); + Console.WriteLine("3. Multiplication game"); + Console.WriteLine("4. Division game"); + + bool validSelection; + do + { + input = Console.ReadLine(); + bool inputNotEmpty = input != null && input.Length > 0; + validSelection = true; + + if (inputNotEmpty) + { + string userSelection = input.ToLower(); + int score; + switch (userSelection) + { + case "exit": + exitGame = true; + break; + case "1": + Console.Clear(); + score = game(Operations.ADDITION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + case "2": + Console.Clear(); + score = game(Operations.SUBSTRACTION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + case "3": + Console.Clear(); + score = game(Operations.MULTIPLICATION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + case "4": + Console.Clear(); + score = game(Operations.DIVISION); + scores.Add(score); + Console.WriteLine("Press enter to continue..."); + Console.ReadLine(); + Console.Clear(); + break; + default: + Console.WriteLine("Invalid selection please try again."); + validSelection = false; + break; + } + } + } while (!validSelection); +} while (!exitGame); + +static List getFactors(int num) +{ + List factors = new List(); + + for (int i = 1; i <= num; i++) + { + if (num % i == 0) + { + factors.Add(i); + } + } + + return factors; +} + +static int game(Operations operation) +{ + int score = 0; + char operationSymbol = ((char)operation); + int minNumber = 1; + int maxNumber = 9; + + Random random = new Random(); + + for (int i = 0; i < NumberOfQuestionsPerGame; i++) + { + int number1 = random.Next(minNumber, maxNumber + 1); + int number2; + int expectedResult; + switch (operation) + { + case Operations.ADDITION: + number2 = random.Next(minNumber, maxNumber + 1); + expectedResult = number1 + number2; + break; + case Operations.SUBSTRACTION: + number2 = random.Next(minNumber, maxNumber + 1); + expectedResult = number1 - number2; + break; + case Operations.MULTIPLICATION: + number2 = random.Next(minNumber, maxNumber + 1); + expectedResult = number1 * number2; + break; + case Operations.DIVISION: + List number1Factors = getFactors(number1); + int number2PositionInFactors = random.Next(0, number1Factors.Count); + number2 = number1Factors[number2PositionInFactors]; + + expectedResult = number1 / number2; + break; + default: + number1 = 1; + number2 = 1; + expectedResult = 0; + break; + } + + Console.WriteLine($"Question {i + 1}/{NumberOfQuestionsPerGame}\nWhat is {number1} {operationSymbol} {number2} ?"); + + string? input = Console.ReadLine(); + int userAnswer; + + while (input == null || input.Length == 0 || !int.TryParse(input, out userAnswer)) + { + Console.WriteLine("Your answer must be an integer number."); + input = Console.ReadLine(); + } + + if (userAnswer == expectedResult) + { + score += ScorePerQuestion; + } + Console.Clear(); + } + + Console.WriteLine($"Your score was {score}/{MaximumScore}"); + + return score; +} +enum Operations +{ + ADDITION = '+', + SUBSTRACTION = '-', + MULTIPLICATION = '*', + DIVISION = '/' +} \ No newline at end of file