From b19335ff4eb0a436cf19fe81a6db12a8fe9bf2d9 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 10 Nov 2026 15:40:14 +0200 Subject: [PATCH] I hope I did it right, forked the git, replaced the files with mine (totally ignored article that I should fork first...). Adding only relevant files for the project. --- .github/workflows/project-submission.yml | 18 -- MathGame.hakonRein/MathGame.hakonRein.slnx | 3 - .../MathGame.hakonRein/Program.cs | 128 -------------- MathGame/GameEngine.cs | 162 ++++++++++++++++++ MathGame/Helpers.cs | 82 +++++++++ .../MathGame.diademist.csproj | 20 +-- MathGame/Menu.cs | 61 +++++++ MathGame/Models/Game.cs | 22 +++ MathGame/Program.cs | 7 + 9 files changed, 344 insertions(+), 159 deletions(-) delete mode 100644 .github/workflows/project-submission.yml delete mode 100644 MathGame.hakonRein/MathGame.hakonRein.slnx delete mode 100644 MathGame.hakonRein/MathGame.hakonRein/Program.cs create mode 100644 MathGame/GameEngine.cs create mode 100644 MathGame/Helpers.cs rename MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj => MathGame/MathGame.diademist.csproj (96%) create mode 100644 MathGame/Menu.cs create mode 100644 MathGame/Models/Game.cs create mode 100644 MathGame/Program.cs diff --git a/.github/workflows/project-submission.yml b/.github/workflows/project-submission.yml deleted file mode 100644 index d01ee8b5..00000000 --- a/.github/workflows/project-submission.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Project Submission - -on: - pull_request_target: - branches: [ "main" ] - types: [opened, reopened] - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -permissions: - pull-requests: write - issues: write - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - call-submission-workflow: - uses: the-csharp-academy/base-repo/.github/workflows/project-submission.yml@main 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/GameEngine.cs b/MathGame/GameEngine.cs new file mode 100644 index 00000000..a3d50a2f --- /dev/null +++ b/MathGame/GameEngine.cs @@ -0,0 +1,162 @@ +using MathGame.Models; + +namespace MathGame +{ + internal class GameEngine + { + internal void AdditionGame(string message) + { + Console.WriteLine(message); + + var random = new Random(); + var score = 0; + + int firstNum; + int secondNum; + + for (int i = 0; i < 5; i++) + { + firstNum = random.Next(1, 9); + secondNum = random.Next(1, 9); + + Console.WriteLine($"{firstNum} + {secondNum}"); + var result = Console.ReadLine(); + + result = Helpers.ValidateResult(result); + + if (int.Parse(result) == firstNum + secondNum) + { + Console.WriteLine("Answer was correct! Type any key to continue."); + score++; + Console.ReadLine(); + } + else + { + Console.WriteLine("Answer is incorrect! Type any key to continue."); + Console.ReadLine(); + } + + if (i == 4) + { + Console.WriteLine($"Game over. Final score is {score}. Press any key to return to main menu."); + Console.ReadLine(); + } + } + + Helpers.AddToHistory(score, GameType.Addition); + } + + internal void SubstractionGame(string message) + { + Console.WriteLine(message); + + var random = new Random(); + var score = 0; + + int firstNum; + int secondNum; + + for (int i = 0; i < 5; i++) + { + firstNum = random.Next(1, 9); + secondNum = random.Next(1, 9); + + Console.WriteLine($"{firstNum} - {secondNum}"); + var result = Console.ReadLine(); + + result = Helpers.ValidateResult(result); + + if (int.Parse(result) == firstNum - secondNum) + { + Console.WriteLine("Answer was correct! Type any key to continue."); + score++; + Console.ReadLine(); + } + else + { + Console.WriteLine("Answer is incorrect! Type any key to continue."); + Console.ReadLine(); + } + + if (i == 4) + Console.WriteLine($"Game over. Final score is {score}"); + } + + Helpers.AddToHistory(score, GameType.Substraction); + } + + internal void MultiplicationGame(string message) + { + Console.WriteLine(message); + + var random = new Random(); + var score = 0; + + int firstNum; + int secondNum; + + for (int i = 0; i < 5; i++) + { + firstNum = random.Next(1, 9); + secondNum = random.Next(1, 9); + + Console.WriteLine($"{firstNum} * {secondNum}"); + var result = Console.ReadLine(); + + result = Helpers.ValidateResult(result); + + if (int.Parse(result) == firstNum * secondNum) + { + Console.WriteLine("Answer was correct! Type any key to continue."); + score++; + Console.ReadLine(); + } + else + { + Console.WriteLine("Answer is incorrect! Type any key to continue."); + Console.ReadLine(); + } + + if (i == 4) + Console.WriteLine($"Game over. Final score is {score}"); + } + + Helpers.AddToHistory(score, GameType.Multiplication); + } + + internal void DivisionGame(string message) + { + Console.WriteLine(message); + var score = 0; + + for (int i = 0; i < 5; i++) + { + var divisionNumbers = Helpers.GetDivisionNumbers(); + var firstNum = divisionNumbers[0]; + var secondNum = divisionNumbers[1]; + + Console.WriteLine($"{firstNum} / {secondNum}"); + var result = Console.ReadLine(); + + result = Helpers.ValidateResult(result); + + if (int.Parse(result) == firstNum / secondNum) + { + Console.WriteLine("Answer was correct! Type any key to continue."); + score++; + Console.ReadLine(); + } + else + { + Console.WriteLine("Answer is incorrect! Type any key to continue."); + Console.ReadLine(); + } + + if (i == 4) + Console.WriteLine($"Game over. Final score is {score}"); + } + + Helpers.AddToHistory(score, GameType.Division); + } + } +} diff --git a/MathGame/Helpers.cs b/MathGame/Helpers.cs new file mode 100644 index 00000000..b2fec3d7 --- /dev/null +++ b/MathGame/Helpers.cs @@ -0,0 +1,82 @@ +using MathGame.Models; + +namespace MathGame +{ + internal class Helpers + { + static List games = new(); + internal static int[] GetDivisionNumbers() + { + var random = new Random(); + var firstNum = random.Next(0, 99); + var secondNum = random.Next(0, 99); + + var result = new int[2]; + + while (firstNum % secondNum != 0) + { + firstNum = random.Next(0, 99); + secondNum = random.Next(0, 99); + } + + result[0] = firstNum; + result[1] = secondNum; + + Console.WriteLine(result); + + return result; + } + + internal static void QuitGame(string message) + { + Console.WriteLine(message); + Environment.Exit(1); + } + + internal static void AddToHistory(int score, GameType gameType) + { + games.Add(new Game + { + Date = DateTime.Now, + Score = score, + Type = gameType + }); + } + + internal static void PrintGames() + { + Console.WriteLine("Games history"); + Console.WriteLine("-------------"); + foreach (var game in games) + { + Console.WriteLine($"{game.Date} - {game.Type}: {game.Score}pts"); + } + Console.WriteLine("-------------"); + Console.WriteLine("Press any key to return to main menu"); + Console.ReadLine(); + } + + internal static string? ValidateResult(string result) + { + while (string.IsNullOrEmpty(result) || !Int32.TryParse(result, out _)) + { + Console.WriteLine("Answer needs to be an integer. Please try again."); + result = Console.ReadLine(); + } + return result; + } + + internal static string GetName() + { + Console.WriteLine("Type your name: "); + var name = Console.ReadLine(); + + while (string.IsNullOrEmpty(name)) + { + Console.WriteLine("Name cannot be empty"); + name = Console.ReadLine(); + } + return name; + } + } +} diff --git a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj b/MathGame/MathGame.diademist.csproj similarity index 96% rename from MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj rename to MathGame/MathGame.diademist.csproj index d20809e3..ed9781c2 100644 --- a/MathGame.hakonRein/MathGame.hakonRein/MathGame.hakonRein.csproj +++ b/MathGame/MathGame.diademist.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..3153a9af --- /dev/null +++ b/MathGame/Menu.cs @@ -0,0 +1,61 @@ +namespace MathGame +{ + internal class Menu + { + GameEngine gameEngine = new(); + internal void ShowMenu(string? name, DateTime date) + { + Console.Clear(); + Console.WriteLine("--------------------------------------\n"); + Console.WriteLine($"Hello {name}. It's {date.DayOfWeek}. This is your math's game. That's great that you're working on improving yourself\n"); + Console.ReadLine(); + Console.WriteLine("\n"); + + var isGameOn = true; + + do + { + Console.WriteLine($@"What game would you like to play today? Choose from the options below: + + A - Addition + S - Substraction + M - Multiplication + D - Division + V - View game history + Q - Quit the program"); + + Console.WriteLine("--------------------------------------"); + + var gameSelected = Console.ReadLine(); + + switch (gameSelected.Trim().ToLower()) + { + case "v": + Helpers.PrintGames(); + break; + case "a": + gameEngine.AdditionGame("Addition game selected"); + break; + case "d": + gameEngine.DivisionGame("Division game selected"); + break; + case "s": + gameEngine.SubstractionGame("Substraction game selected"); + break; + case "m": + gameEngine.MultiplicationGame("Multiplication game selected"); + break; + case "q": + Helpers.QuitGame("Bye-bye!"); + isGameOn = false; + Environment.Exit(1); + break; + default: + Console.WriteLine("Wrong input"); + Environment.Exit(1); + break; + } + } while (isGameOn); + } + } +} diff --git a/MathGame/Models/Game.cs b/MathGame/Models/Game.cs new file mode 100644 index 00000000..8a0d3ab4 --- /dev/null +++ b/MathGame/Models/Game.cs @@ -0,0 +1,22 @@ +namespace MathGame.Models; + +internal class Game +{ +// private int _score; +// public int Score +// { +// get { return _score; } +// set { _score = value; } +// } + public int Score { get; set; } + public DateTime Date { get; set; } + public GameType Type { get; set; } +} + +internal enum GameType +{ + Addition, + Substraction, + Multiplication, + Division +} \ No newline at end of file diff --git a/MathGame/Program.cs b/MathGame/Program.cs new file mode 100644 index 00000000..54a8a8c2 --- /dev/null +++ b/MathGame/Program.cs @@ -0,0 +1,7 @@ +using MathGame; + +string name = Helpers.GetName(); +var date = DateTime.UtcNow; +var menu = new Menu(); + +menu.ShowMenu(name, date); \ No newline at end of file