diff --git a/MathGame.hakonRein/MathGame.hakonRein.slnx b/MathGame.hakonRein/MathGame.hakonRein.slnx deleted file mode 100644 index c77eeba5..00000000 --- a/MathGame.hakonRein/MathGame.hakonRein.slnx +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/MathGame.hakonRein/MathGame.hakonRein/Program.cs b/MathGame.hakonRein/MathGame.hakonRein/Program.cs deleted file mode 100644 index d2ab993f..00000000 --- a/MathGame.hakonRein/MathGame.hakonRein/Program.cs +++ /dev/null @@ -1,128 +0,0 @@ -/* - * A simple math game that allows the user to select an operation and perform calculations. - */ - -int menuChoice = 0; -int totalPoints = 0; -List history = new List(); - -string difficulty = "easy"; -const int EASY_RANGE = 10; -const int DIFFICULT_RANGE = 100; - -// Used to store operations for the random operator option -string[] operations = { "+", "-", "*", "/" }; - -Random numberGenerator = new Random(); - -do -{ - DisplayMenu(); - while (!int.TryParse(Console.ReadLine(), out menuChoice) || menuChoice < 1 || menuChoice > 8) - { - Console.Write("Invalid input. Please enter a number between 1 and 8: "); - } - - int num1 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); - int num2 = numberGenerator.Next(0, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); - int correctAnswer; - string operation = ""; - - switch (menuChoice) - { - case 1: - operation = "+"; - break; - case 2: - operation = "-"; - break; - case 3: - operation = "*"; - break; - case 4: - operation = "/"; - break; - case 5: - // Handle random operator - operation = operations[numberGenerator.Next(0, operations.Length)]; - break; - case 6: - Console.WriteLine("\nHistory of calculations:"); - foreach (var record in history) - { - Console.WriteLine(record); - } - break; - case 7: - difficulty = difficulty == "easy" ? "difficult" : "easy"; - Console.WriteLine($"Changed difficulty to {difficulty}"); - break; - case 8: - Console.WriteLine("Exiting the game. Thank you for playing!"); - break; - } - - // Ensure that division problems are valid (no division by zero and integer results) - if (operation == "/") - { - num2 = numberGenerator.Next(1, difficulty == "easy" ? EASY_RANGE : DIFFICULT_RANGE); - num1 = num2 * numberGenerator.Next(0, 10); - } - - // Calculate the correct answer and check this against the user's guess - if (menuChoice >= 1 && menuChoice <= 5) - { - correctAnswer = Calculate(num1, num2, operation); - Console.Write($"\n{num1} {operation} {num2} = "); - int userAnswer; - while (!int.TryParse(Console.ReadLine(), out userAnswer)) - { - Console.Write("Invalid input. Please enter a valid integer: "); - } - if (userAnswer == correctAnswer) - { - Console.WriteLine("Correct!"); - totalPoints++; - history.Add($"{num1} {operation} {num2} = {correctAnswer} (Correct)"); - } - else - { - Console.WriteLine($"Incorrect. The correct answer is {correctAnswer}."); - history.Add($"{num1} {operation} {num2} = {userAnswer} (Incorrect - Correct answer: {correctAnswer})"); - } - Console.WriteLine($"Total points: {totalPoints}"); - } - -} while (menuChoice != 8); - - -int Calculate(int num1, int num2, string operation) -{ - switch (operation) - { - case "+": - return num1 + num2; - case "-": - return num1 - num2; - case "*": - return num1 * num2; - case "/": - return num1 / num2; - default: - throw new ArgumentException("Invalid operator"); - } -} - -void DisplayMenu() -{ - Console.WriteLine("\nSelect operation:"); - Console.WriteLine("1. Addition"); - Console.WriteLine("2. Subtraction"); - Console.WriteLine("3. Multiplication"); - Console.WriteLine("4. Division"); - Console.WriteLine("5. Random operator"); - Console.WriteLine("6. Show history"); - Console.WriteLine("7. Change difficulty to " + (difficulty == "easy" ? "difficult" : "easy")); - Console.WriteLine("8. Exit"); - Console.Write("Your choice (1-8): "); -} \ No newline at end of file diff --git a/MathGame/DifficultyLevel.cs b/MathGame/DifficultyLevel.cs new file mode 100644 index 00000000..8c222407 --- /dev/null +++ b/MathGame/DifficultyLevel.cs @@ -0,0 +1,8 @@ +namespace MathGame; + +public enum DifficultyLevel +{ + Easy = 1, + Intermediate = 2, + Advanced = 3 +} diff --git a/MathGame/Game.cs b/MathGame/Game.cs new file mode 100644 index 00000000..846b8e04 --- /dev/null +++ b/MathGame/Game.cs @@ -0,0 +1,113 @@ +using System.Diagnostics; + +namespace MathGame; + +public static class Game +{ + private static List<(int, TimeSpan)> games = []; + private readonly static string[] mathOperators = ["+", "-", "*", "/"]; + private static readonly Random random = new(); + private static DifficultyLevel difficultyLevel = DifficultyLevel.Easy; + + public static void PlayRound() + { + int points = 0; + + difficultyLevel = Menu.ChooseOperationDifficulty(); + + Stopwatch sw = Stopwatch.StartNew(); + + for (int i = 0; i < 5; i++) + { + Operation operation = Menu.ChooseOperation(); + SolveEquation(ref points, operation); + } + + sw.Stop(); + + Console.WriteLine($"\nPoints for this round: {points}"); + Console.WriteLine($"Time spent: {sw.Elapsed:hh\\:mm\\:ss\\.fff}"); + + games.Add((points, sw.Elapsed)); + } + + public static void ShowHistory() + { + if (games.Count == 0) + Console.WriteLine("No games yet"); + + for (int i = 0; i < games.Count; i++) + { + Console.WriteLine($"Points gained in {i + 1} game : {games[i].Item1}"); + Console.WriteLine($"Time spent on {i + 1} game : {games[i].Item2:hh\\:mm\\:ss\\.fff}"); + } + Console.WriteLine(); + } + + private static (int, int) GenerateNumbers(Operation operation) + { + int num1 = 0, num2 = 0; + switch (difficultyLevel) + { + case DifficultyLevel.Easy: + num1 = random.Next(0, 10); + num2 = random.Next(0, 10); + break; + case DifficultyLevel.Intermediate: + num1 = random.Next(0, 100); + num2 = random.Next(0, 100); + break; + case DifficultyLevel.Advanced: + num1 = random.Next(0, 1000); + num2 = random.Next(0, 1000); + break; + } + + + if (operation == Operation.Division && (num2 == 0 || num1 % num2 != 0 || num1 > num2)) + return GenerateNumbers(operation); + + return (num1, num2); + } + + private static void SolveEquation(ref int points, Operation operation) + { + (int num1, int num2) = GenerateNumbers(operation); + + Console.Write($"{num1} {mathOperators[(int)operation - 1]} {num2} = "); + + int result = GetAnswer(); + + switch (operation) + { + case Operation.Addition: + if (result == num1 + num2) + points++; + break; + case Operation.Substraction: + if (result == num1 - num2) + points++; + break; + case Operation.Multiplication: + if (result == num1 * num2) + points++; + break; + case Operation.Division: + if (result == num1 / num2) + points++; + break; + } + } + + private static int GetAnswer() + { + bool userInput = int.TryParse(Console.ReadLine(), out int result); + + if (!userInput) + { + Console.Write("Incorrect format. Try again: "); + return GetAnswer(); + } + return result; + } +} \ No newline at end of file diff --git a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj b/MathGame/MathGame.csproj similarity index 96% rename from MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj rename to MathGame/MathGame.csproj index d20809e3..ed9781c2 100644 --- a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj +++ b/MathGame/MathGame.csproj @@ -1,10 +1,10 @@ - - - - Exe - net10.0 - enable - enable - - - + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame/Menu.cs b/MathGame/Menu.cs new file mode 100644 index 00000000..219db96b --- /dev/null +++ b/MathGame/Menu.cs @@ -0,0 +1,65 @@ +namespace MathGame; + +public static class Menu +{ + public static string StartMenu() + { + string? input; + + Console.WriteLine("Play"); + Console.WriteLine("History"); + Console.WriteLine("Exit"); + + Console.Write("Enter your choise => "); + + input = Console.ReadLine(); + if (input is null) + { + Console.WriteLine("Your input is incorrect"); + return StartMenu(); + } + + return input; + } + public static Operation ChooseOperation() + { + string? input; + + Console.WriteLine(); + Console.WriteLine("Choose an operation: "); + Console.WriteLine("1. Addition"); + Console.WriteLine("2. Substraction"); + Console.WriteLine("3. Multiplication"); + Console.WriteLine("4. Division"); + Console.Write("Enter your choise => "); + + input = Console.ReadLine(); + if (input == string.Empty || !Enum.TryParse(input, out Operation operation) || !Enum.IsDefined(operation)) + { + Console.WriteLine("Your input is incorrect"); + return ChooseOperation(); + } + + return operation; + } + public static DifficultyLevel ChooseOperationDifficulty() + { + string? input; + + Console.Clear(); + Console.WriteLine("Choose an operation difficulty level: "); + Console.WriteLine("1. Easy"); + Console.WriteLine("2. Intermediate"); + Console.WriteLine("3. Advanced"); + Console.Write("Enter your choise => "); + + input = Console.ReadLine(); + if (input == string.Empty || !Enum.TryParse(input, out DifficultyLevel difficultyLevel) || !Enum.IsDefined(difficultyLevel)) + { + Console.WriteLine("Your input is incorrect"); + return ChooseOperationDifficulty(); + } + + return difficultyLevel; + } +} \ No newline at end of file diff --git a/MathGame/Operation.cs b/MathGame/Operation.cs new file mode 100644 index 00000000..f4d57692 --- /dev/null +++ b/MathGame/Operation.cs @@ -0,0 +1,9 @@ +namespace MathGame; + +public enum Operation +{ + Addition = 1, + Substraction = 2, + Multiplication = 3, + Division = 4 +} diff --git a/MathGame/Program.cs b/MathGame/Program.cs new file mode 100644 index 00000000..47b2cebc --- /dev/null +++ b/MathGame/Program.cs @@ -0,0 +1,28 @@ +using MathGame; + +Console.WriteLine("Welcome to math game!"); + +string input; +do +{ + input = Menu.StartMenu().Trim().ToLower(); + + switch (input) + { + case "play": + Console.Clear(); + Game.PlayRound(); + break; + case "history": + Console.Clear(); + Game.ShowHistory(); + break; + case "exit": + Console.WriteLine("Thanks for the game!"); + break; + default: + Console.WriteLine("Your input is incorrect"); + break; + } + +} while (input != "exit");