diff --git a/TCSA.MathGame.sln b/TCSA.MathGame.sln new file mode 100644 index 00000000..07d18e2f --- /dev/null +++ b/TCSA.MathGame.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TCSA.MathGame", "TCSA.MathGame\TCSA.MathGame.csproj", "{5BFB832F-309D-487F-8705-CD057C6CE4C3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5BFB832F-309D-487F-8705-CD057C6CE4C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BFB832F-309D-487F-8705-CD057C6CE4C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BFB832F-309D-487F-8705-CD057C6CE4C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BFB832F-309D-487F-8705-CD057C6CE4C3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/TCSA.MathGame/GameEngine.cs b/TCSA.MathGame/GameEngine.cs new file mode 100644 index 00000000..e1ed9123 --- /dev/null +++ b/TCSA.MathGame/GameEngine.cs @@ -0,0 +1,64 @@ +using System.Diagnostics; +using TCSA.MathGame.Models; + +namespace TCSA.MathGame; + +internal class GameEngine +{ + internal void PlayGame(GameType gameType, Difficulty difficulty) + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + var score = 0; + + for (int i = 0; i < 5; i++) + { + Console.Clear(); + int correctAnswer = 0; + char operation = ' '; + (int firstNumber, int secondNumber) = Helpers.GetNumbers(gameType, difficulty); + + switch (gameType) + { + case GameType.Addition: + operation = '+'; + correctAnswer = firstNumber + secondNumber; + break; + case GameType.Subtraction: + operation = '-'; + correctAnswer = firstNumber - secondNumber; + break; + case GameType.Multiplication: + operation = '*'; + correctAnswer = firstNumber * secondNumber; + break; + case GameType.Division: + operation = '/'; + correctAnswer = firstNumber / secondNumber; + break; + } + + Console.WriteLine($"{firstNumber} {operation} {secondNumber}"); + var result = Console.ReadLine(); + result = Helpers.ValidateResult(result); + if (int.Parse(result) == correctAnswer) + { + Console.WriteLine($"Your answer was correct. Type any key for the next question."); + score++; + Console.ReadLine(); + } + else + { + Console.WriteLine($"Your answer was incorrect. Type any key for the next question."); + Console.ReadLine(); + } + } + + stopwatch.Stop(); + TimeSpan duration = stopwatch.Elapsed; + Console.WriteLine( + $"Game over. Your final score is {score}. Time: {duration.TotalSeconds:F2}. Press any key to go back to the main menu."); + Console.ReadLine(); + Helpers.AddToHistory(score, gameType, duration, difficulty); + } +} \ No newline at end of file diff --git a/TCSA.MathGame/Helpers.cs b/TCSA.MathGame/Helpers.cs new file mode 100644 index 00000000..54f1e1cf --- /dev/null +++ b/TCSA.MathGame/Helpers.cs @@ -0,0 +1,153 @@ +using TCSA.MathGame.Models; + +namespace TCSA.MathGame; + +internal class Helpers +{ + internal static List games = new List() + { + /*new Game { Date = DateTime.Now.AddDays(1), Type = GameType.Addition, Score = 5 }, + new Game { Date = DateTime.Now.AddDays(2), Type = GameType.Multiplication, Score = 4 }, + new Game { Date = DateTime.Now.AddDays(3), Type = GameType.Division, Score = 4 }, + new Game { Date = DateTime.Now.AddDays(4), Type = GameType.Subtraction, Score = 3 }, + new Game { Date = DateTime.Now.AddDays(5), Type = GameType.Addition, Score = 1 }, + new Game { Date = DateTime.Now.AddDays(6), Type = GameType.Multiplication, Score = 2 }, + new Game { Date = DateTime.Now.AddDays(7), Type = GameType.Division, Score = 3 }, + new Game { Date = DateTime.Now.AddDays(8), Type = GameType.Subtraction, Score = 4 }, + new Game { Date = DateTime.Now.AddDays(9), Type = GameType.Addition, Score = 4 }, + new Game { Date = DateTime.Now.AddDays(10), Type = GameType.Multiplication, Score = 1 }, + new Game { Date = DateTime.Now.AddDays(11), Type = GameType.Subtraction, Score = 0 }, + new Game { Date = DateTime.Now.AddDays(12), Type = GameType.Division, Score = 2 }, + new Game { Date = DateTime.Now.AddDays(13), Type = GameType.Subtraction, Score = 5 },*/ + }; + + internal static void PrintGames() + { + //var gamesToPrint = games.Where(x => x.Date > new DateTime(2022, 08, 09)).OrderByDescending(x => x.Score); + + Console.Clear(); + Console.WriteLine("Games History"); + Console.WriteLine("---------------------------"); + foreach (var game in games) + { + Console.WriteLine($"{game.Date} - {game.Type} - {game.Difficulty}: {game.Score}pts - {game.Duration.TotalSeconds:F2}"); + } + + Console.WriteLine("---------------------------\n"); + Console.WriteLine("Press any key to return to main menu"); + Console.ReadLine(); + } + + internal static void AddToHistory(int gameScore, GameType gameType, TimeSpan duration, Difficulty difficulty) + { + games.Add(new Game + { + Date = DateTime.Now, + Score = gameScore, + Type = gameType, + Difficulty = difficulty, + Duration = duration, + }); + } + + internal static string ValidateResult(string? result) + { + while (string.IsNullOrEmpty(result) || !Int32.TryParse(result, out _)) + { + Console.WriteLine("Your answer needs to be an integer. Try again."); + result = Console.ReadLine(); + } + + return result; + } + + internal static string? GetName() + { + Console.WriteLine("Please type your name"); + var name = Console.ReadLine(); + + while (string.IsNullOrEmpty(name)) + { + Console.WriteLine("Name can't be empty"); + name = Console.ReadLine(); + } + + return name; + } + + internal static (int min, int max) GetRange(GameType gameType, Difficulty difficulty) + { + switch (gameType) + { + case GameType.Addition: + switch (difficulty) + { + case Difficulty.Easy: + return (1, 11); + case Difficulty.Medium: + return (1, 51); + case Difficulty.Hard: + return (1, 101); + } + + break; + case GameType.Subtraction: + switch (difficulty) + { + case Difficulty.Easy: + return (1, 11); + case Difficulty.Medium: + return (1, 51); + case Difficulty.Hard: + return (1, 101); + } + + break; + case GameType.Multiplication: + switch (difficulty) + { + case Difficulty.Easy: + return (1, 6); + case Difficulty.Medium: + return (1, 11); + case Difficulty.Hard: + return (1, 21); + } + + break; + case GameType.Division: + switch (difficulty) + { + case Difficulty.Easy: + return (1, 11); + case Difficulty.Medium: + return (1, 31); + case Difficulty.Hard: + return (1, 51); + } + + break; + } + + throw new InvalidOperationException(); + } + + internal static (int firstNumber, int secondNumber) GetNumbers(GameType gameType, Difficulty difficulty) + { + (int min, int max) = GetRange(gameType, difficulty); + var random = new Random(); + int firstNumber = random.Next(min, max); + int secondNumber = random.Next(min, max); + + if (gameType == GameType.Division) + { + while (firstNumber % secondNumber != 0) + { + firstNumber = random.Next(min, max); + secondNumber = random.Next(min, max); + } + } + + return (firstNumber, secondNumber); + } +} \ No newline at end of file diff --git a/TCSA.MathGame/Menu.cs b/TCSA.MathGame/Menu.cs new file mode 100644 index 00000000..8417cda4 --- /dev/null +++ b/TCSA.MathGame/Menu.cs @@ -0,0 +1,103 @@ +using TCSA.MathGame.Models; + +namespace TCSA.MathGame; + +internal class Menu +{ + GameEngine gameEngine = new(); + + internal void ShowMenu(string? name, DateTime? date) + { + var random = new Random(); + Console.Clear(); + Console.WriteLine( + $"Hello {name.ToUpper()}. It's {date}. This is your math's game. That's great thay you're working on imroving yoiurself\n"); + Console.WriteLine("Press any key to show menu"); + Console.ReadLine(); + Console.WriteLine("\n"); + + bool isGameOn = true; + + do + { + Console.Clear(); + Console.WriteLine(@$" +What game would you like to play today? Choose from the options below: +V - View Previous Games +R - Random Game +A - Addition +S - Subtraction +M - Multiplication +D - Division +Q - Quit the program"); + Console.WriteLine("----------------------------------------------"); + + var gameSelected = Console.ReadLine(); + GameType? gameType = null; + //bool isGameSelected = false; + + switch (gameSelected.Trim().ToLower()) + { + case "v": + Helpers.PrintGames(); + break; + case "r": + gameType = (GameType)random.Next(0, 4); + break; + case "a": + gameType = GameType.Addition; + break; + case "s": + gameType = GameType.Subtraction; + break; + case "m": + gameType = GameType.Multiplication; + break; + case "d": + gameType = GameType.Division; + break; + case "q": + Console.WriteLine("Goodbye"); + isGameOn = false; + break; + default: + Console.WriteLine("Invalid Input"); + break; + } + + if (gameType.HasValue) + { + var difficulty = GetDifficulty(); + gameEngine.PlayGame(gameType.Value, difficulty); + } + } while (isGameOn); + } + + internal Difficulty GetDifficulty() + { + while (true) + { + Console.Clear(); + Console.WriteLine(@$" +Choose the difficulty: +1 - Easy +2 - Medium +3 - Hard"); + Console.WriteLine("----------------------------------------------"); + var chooseDifficulty = Console.ReadLine(); + + switch (chooseDifficulty) + { + case "1": + return Difficulty.Easy; + case "2": + return Difficulty.Medium; + case "3": + return Difficulty.Hard; + default: + Console.WriteLine("Invalid Input"); + break; + } + } + } +} \ No newline at end of file diff --git a/TCSA.MathGame/Models/Difficulty.cs b/TCSA.MathGame/Models/Difficulty.cs new file mode 100644 index 00000000..e179a195 --- /dev/null +++ b/TCSA.MathGame/Models/Difficulty.cs @@ -0,0 +1,8 @@ +namespace TCSA.MathGame.Models; + +internal enum Difficulty +{ + Easy, + Medium, + Hard +} \ No newline at end of file diff --git a/TCSA.MathGame/Models/Game.cs b/TCSA.MathGame/Models/Game.cs new file mode 100644 index 00000000..32db841c --- /dev/null +++ b/TCSA.MathGame/Models/Game.cs @@ -0,0 +1,18 @@ +namespace TCSA.MathGame.Models; + +internal class Game +{ + internal int Score { get; set; } + internal DateTime Date { get; set; } + internal GameType Type { get; set; } + internal Difficulty Difficulty { get; set; } + internal TimeSpan Duration { get; set; } +} + +internal enum GameType +{ + Addition, + Subtraction, + Multiplication, + Division, +} \ No newline at end of file diff --git a/TCSA.MathGame/Program.cs b/TCSA.MathGame/Program.cs new file mode 100644 index 00000000..5fe3e27f --- /dev/null +++ b/TCSA.MathGame/Program.cs @@ -0,0 +1,15 @@ +namespace TCSA.MathGame; + +class Program +{ + static void Main(string[] args) + { + var menu = new Menu(); + + var date = DateTime.UtcNow; + + string name = Helpers.GetName(); + + menu.ShowMenu(name, date); + } +} \ No newline at end of file diff --git a/TCSA.MathGame/TCSA.MathGame.csproj b/TCSA.MathGame/TCSA.MathGame.csproj new file mode 100644 index 00000000..6c1dc922 --- /dev/null +++ b/TCSA.MathGame/TCSA.MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + +