diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade.slnx b/MathGame.royeeet/MathGame-dotnet-upgrade.slnx new file mode 100644 index 00000000..2d17fb2b --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade/GameEngine.cs b/MathGame.royeeet/MathGame-dotnet-upgrade/GameEngine.cs new file mode 100644 index 00000000..010a1463 --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade/GameEngine.cs @@ -0,0 +1,48 @@ +using MathGame; +using MathGame_dotnet_upgrade.Models; + +namespace MathGame_dotnet_upgrade +{ + internal class GameEngine + { + public void PlayGame(GameType gameType, string symbol, Func operation, GameDifficulty difficulty) + { + int score = 0; + Helpers helpers = new Helpers(); + var loop = Helpers.InitialiseGame(); + + do + { + GameType currentGameType = gameType; + string currentSymbol = symbol; + Func currentOperation = operation; + + if (gameType == GameType.Random) + { + currentGameType = Helpers.Randomise(); + currentSymbol = currentGameType switch + { + GameType.Addition => "+", + GameType.Subtraction => "-", + GameType.Multiplication => "x", + GameType.Division => "/", + }; + currentOperation = currentSymbol switch + { + "+" => (a, b) => a + b, + "-" => (a, b) => a - b, + "x" => (a, b) => a * b, + "/" => (a, b) => a / b, + _ => (a, b) => a + b + }; + } + var (firstValue, secondValue, userAnswer) = currentGameType == GameType.Division ? Helpers.CheckThatNumbersAreDivisable(difficulty) : Helpers.GetNumbersAndQuestion(currentSymbol, difficulty); + + int expected = currentOperation(firstValue, secondValue); + + helpers.GameLogic(userAnswer, expected, ref score, gameType, difficulty); + + } while (!loop); + } + } +} diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade/Helpers.cs b/MathGame.royeeet/MathGame-dotnet-upgrade/Helpers.cs new file mode 100644 index 00000000..cb67c66b --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade/Helpers.cs @@ -0,0 +1,251 @@ +using MathGame_dotnet_upgrade; +using MathGame_dotnet_upgrade.Models; +using System.Diagnostics; + +namespace MathGame +{ + internal class Helpers + { + public static string player = GetName(); + public static List games = new List(); + public static Stopwatch timer = new Stopwatch(); + + public static void GameHistory() + { + Console.Clear(); + Console.WriteLine("game history"); + + if (games == null || games.Count == 0) + { + Console.WriteLine("nothing to show"); + } + else + { + foreach (var game in games) + { + Console.WriteLine($"Player Name: {player} || Game mode: {game.Type} || Game difficulty: {game.Difficulty} || Score: {game.Score} || Time: {game.Time:mm\\:ss}"); + } + } + + Console.WriteLine("press any key to exit"); + Console.ReadLine(); + Console.Clear(); + + Menu menu = new Menu(); + menu.GameMenu(player); + } + + internal static string GetName() + { + Console.WriteLine("enter your name: "); + var player = Console.ReadLine(); + while (string.IsNullOrEmpty(player)) + { + Console.WriteLine("i said state your name"); + player = Console.ReadLine(); + } + return player; + } + + internal static void AddToHistory(int score, GameType gameChoice, TimeSpan timeTaken, GameDifficulty gameDifficulty) + { + if (Game.GameChoice == "r") + { + gameChoice = GameType.Random; + } + + var game = new Game + { + Type = gameChoice, + Score = score, + Time = timeTaken, + Difficulty = gameDifficulty + }; + games.Add(game); + } + + internal static int Validation(string input) + { + int result; + while (string.IsNullOrEmpty(input) || !Int32.TryParse(input, out result)) + { + Console.WriteLine("answer needs to be an integer, try again"); + input = Console.ReadLine(); + } + return result; + } + + internal static string ValidationYesOrNo(string prompt) + { + Console.WriteLine(prompt); + string yesOrNo = Console.ReadLine(); + while (yesOrNo != "y" && yesOrNo != "n") + { + Console.WriteLine("decide yes or no"); + yesOrNo = Console.ReadLine(); + } + return yesOrNo; + } + + public static (int, int, int) GetNumbersAndQuestion(string operation, GameDifficulty difficulty) + { + Random rand = new Random(); + int firstValue = 0; + int secondValue = 0; + + switch (difficulty) + { + case GameDifficulty.Easy: + firstValue = rand.Next(0, 10); + secondValue = rand.Next(0, 10); + break; + case GameDifficulty.Medium: + firstValue = rand.Next(10, 50); + secondValue = rand.Next(10, 50); + break; + case GameDifficulty.Hard: + firstValue = rand.Next(10, 100); + secondValue = rand.Next(10, 100); + break; + } + + Console.Clear(); + Console.WriteLine("solve the following: "); + Console.WriteLine($"{firstValue} {operation} {secondValue}"); + + Console.Write("your answer: "); + timer.Start(); + string input = Console.ReadLine(); + + int userAnswer = Validation(input); + return (firstValue, secondValue, userAnswer); + } + + internal static bool InitialiseGame() + { + Console.Clear(); + timer.Reset(); + bool loop = false; + return loop; + } + + internal static (int, int, int) CheckThatNumbersAreDivisable(GameDifficulty difficulty) + { + Random rand = new Random(); + int dividend = 0; + int divisor = 0; + + do + { + switch (difficulty) + { + case GameDifficulty.Easy: + dividend = rand.Next(1, 100); + divisor = rand.Next(1, 100); + break; + case GameDifficulty.Medium: + dividend = rand.Next(1, 100); + divisor = rand.Next(1, 200); + break; + case GameDifficulty.Hard: + dividend = rand.Next(1, 100); + divisor = rand.Next(1, 300); + break; + } + } + while (dividend % divisor != 0); + + Console.WriteLine("solve the following: "); + Console.WriteLine($"{dividend} / {divisor}"); + timer.Start(); + string input = Console.ReadLine(); + int userAnswer = Validation(input); + + return (dividend, divisor, userAnswer); + } + + public static GameType Randomise() + { + Random rand = new Random(); + var randomGameMode = (GameType)rand.Next(4); + return randomGameMode; + } + + internal void GameLogic(int userAnswer, int expected, ref int score, GameType gameChoice, GameDifficulty gameDifficulty) + { + GameEngine engine = new GameEngine(); + Menu menu = new Menu(); + + if (userAnswer == expected) + { + Console.Clear(); + Console.WriteLine("correct"); + score++; + } + + else + { + timer.Stop(); + TimeSpan timeTaken = timer.Elapsed; + string elapsedTime = "time taken (mm:ss): " + timeTaken.ToString(@"mm\:ss"); + Console.WriteLine(elapsedTime); + + AddToHistory(score, gameChoice, timeTaken, gameDifficulty); + Console.WriteLine($"incorrect. the correct answer was {expected}. your score is {score}."); + string restartGame = ValidationYesOrNo("try again? y/n"); + + if (restartGame == "y") + { + Console.Clear(); + timer.Reset(); + switch (gameChoice) + { + case GameType.Addition: + gameDifficulty = ChooseDifficulty(); + engine.PlayGame(GameType.Addition, "+", (a, b) => a + b, gameDifficulty); + break; + case GameType.Subtraction: + gameDifficulty = ChooseDifficulty(); + engine.PlayGame(GameType.Subtraction, "-", (a, b) => a - b, gameDifficulty); + break; + case GameType.Multiplication: + gameDifficulty = ChooseDifficulty(); + engine.PlayGame(GameType.Multiplication, "x", (a, b) => a * b, gameDifficulty); + break; + case GameType.Division: + gameDifficulty = ChooseDifficulty(); + engine.PlayGame(GameType.Division, "/", (a, b) => a / b, gameDifficulty); + break; + case GameType.Random: + gameDifficulty = ChooseDifficulty(); + engine.PlayGame(GameType.Random, "", null, gameDifficulty); + break; + } + } + else + { + Console.Clear(); + menu.GameMenu(player); + } + } + } + + public static GameDifficulty ChooseDifficulty() + { + Console.Clear(); + Console.WriteLine("choose a difficulty: "); + Console.WriteLine("1 - easy"); + Console.WriteLine("2 - medium"); + Console.WriteLine("3 - hard"); + string input = Console.ReadLine(); + int choice = Validation(input); + return choice switch + { + 1 => GameDifficulty.Easy, + 2 => GameDifficulty.Medium, + 3 => GameDifficulty.Hard, + _ => throw new ArgumentOutOfRangeException("invalid choice") + }; + } + } +} diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade/MathGame-dotnet-upgrade.csproj b/MathGame.royeeet/MathGame-dotnet-upgrade/MathGame-dotnet-upgrade.csproj new file mode 100644 index 00000000..6becffce --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade/MathGame-dotnet-upgrade.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + MathGame_dotnet_upgrade + enable + enable + + + diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade/Menu.cs b/MathGame.royeeet/MathGame-dotnet-upgrade/Menu.cs new file mode 100644 index 00000000..3da5f2a4 --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade/Menu.cs @@ -0,0 +1,73 @@ +using MathGame_dotnet_upgrade; +using MathGame_dotnet_upgrade.Models; + +namespace MathGame +{ + internal class Menu + { + GameEngine gameMenu = new(); + Game game = new(); + + public void GameMenu(string player) + { + Console.WriteLine("choose an operation to play in: "); + Console.WriteLine("a - addition"); + Console.WriteLine("s - subtraction"); + Console.WriteLine("m - multiplication"); + Console.WriteLine("d - division"); + Console.WriteLine("r - random"); + Console.WriteLine("v - view game history"); + Console.WriteLine("q - quit"); + Game.GameChoice = Console.ReadLine(); + var whichOperator = ""; + + switch (Game.GameChoice.Trim().ToLower()) + { + case "a": + whichOperator = "+"; + var gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Addition, whichOperator, (a, b) => a + b, gameDifficulty); + break; + + case "s": + whichOperator = "-"; + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Subtraction, whichOperator, (a, b) => a - b, gameDifficulty); + break; + + case "m": + whichOperator = "x"; + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Multiplication, whichOperator, (a, b) => a * b, gameDifficulty); + break; + + case "d": + whichOperator = "/"; + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Division, whichOperator, (a, b) => a / b, gameDifficulty); + break; + + case "r": + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Random, "", null, gameDifficulty); + break; + + case "v": + Helpers.GameHistory(); + break; + + case "q": + Console.WriteLine("thanks for playing!"); + Console.Read(); + Environment.Exit(0); + break; + + default: + Console.Clear(); + Console.WriteLine("select one of the options"); + GameMenu(player); + break; + } + } + } +} diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade/Models/Game.cs b/MathGame.royeeet/MathGame-dotnet-upgrade/Models/Game.cs new file mode 100644 index 00000000..34259c74 --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade/Models/Game.cs @@ -0,0 +1,26 @@ +namespace MathGame_dotnet_upgrade.Models; + +public class Game +{ + public int Score { get; set; } + public GameType Type { get; set; } + public TimeSpan Time { get; set; } + public static string GameChoice { get; set; } + public GameDifficulty Difficulty { get; set; } +} + +public enum GameType +{ + Addition, + Subtraction, + Multiplication, + Division, + Random +} + +public enum GameDifficulty +{ + Easy, + Medium, + Hard +} \ No newline at end of file diff --git a/MathGame.royeeet/MathGame-dotnet-upgrade/Program.cs b/MathGame.royeeet/MathGame-dotnet-upgrade/Program.cs new file mode 100644 index 00000000..b7a52984 --- /dev/null +++ b/MathGame.royeeet/MathGame-dotnet-upgrade/Program.cs @@ -0,0 +1,24 @@ +using MathGame; + +/* math game requirements: + * 1. open with a menu prompting the user for their name, followed by choosing one of the 4 operators - done + * + * 2. for each operator, two random numbers should be generated, prompting the user to solve displayed equations - done + * + * 3. they should loop until the user inputs the wrong answer, with exceptions handled (putting a letter instead of a number) - done + * + * 4. the divisions must result in integers only, with dividends being 0-100 - done + * + * 5. any games played should be stored in some kind of list, which the user can view, no need for db tho - done + * + * 6. implement levels of difficulty - done + * + * 7. add a timer for games - done + * + * 8. use one method for all games - done + * + * 9. create a 'random game' option - done + */ + +Menu menu = new Menu(); +menu.GameMenu(Helpers.player); \ No newline at end of file