From 445c2e7d5877cbd0d999003b37948f9795668f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Kult=C3=A1n?= <xkultan@fi.muni.cz> Date: Sun, 6 Mar 2022 12:48:53 +0100 Subject: [PATCH] remade ConsoleHandler from static to singleton pattern --- .gitignore | 1 + hw01/hw01.sln | 6 ++++ hw01/hw01/src/ConsoleProvider.cs | 41 ++++++++++++++++++++++++ hw01/hw01/src/Engine/ConsoleHandler.cs | 30 +----------------- hw01/hw01/src/Engine/Player.cs | 2 +- hw01/hw01/src/Engine/Utils.cs | 44 +++++++++++++++----------- hw01/hw01/src/Game/Game.cs | 8 ++--- hw01/hw01/src/Game/State.cs | 29 ++++++++++------- hw01/hw01/src/IConsoleProvider.cs | 21 ++++++++++++ 9 files changed, 118 insertions(+), 64 deletions(-) create mode 100644 hw01/hw01/src/ConsoleProvider.cs create mode 100644 hw01/hw01/src/IConsoleProvider.cs diff --git a/.gitignore b/.gitignore index 8a30d25..20f3889 100644 --- a/.gitignore +++ b/.gitignore @@ -396,3 +396,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/hw01/UnitTest_Hw01 diff --git a/hw01/hw01.sln b/hw01/hw01.sln index a6ab0a9..76861ab 100644 --- a/hw01/hw01.sln +++ b/hw01/hw01.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.32126.317 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw01", "hw01\Hw01.csproj", "{3BE575A4-9DBB-4412-A23B-B23746CF3607}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest_Hw01", "UnitTest_Hw01\UnitTest_Hw01.csproj", "{7A6CB679-A3CE-470D-926A-EADC2165C456}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {3BE575A4-9DBB-4412-A23B-B23746CF3607}.Debug|Any CPU.Build.0 = Debug|Any CPU {3BE575A4-9DBB-4412-A23B-B23746CF3607}.Release|Any CPU.ActiveCfg = Release|Any CPU {3BE575A4-9DBB-4412-A23B-B23746CF3607}.Release|Any CPU.Build.0 = Release|Any CPU + {7A6CB679-A3CE-470D-926A-EADC2165C456}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A6CB679-A3CE-470D-926A-EADC2165C456}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7A6CB679-A3CE-470D-926A-EADC2165C456}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A6CB679-A3CE-470D-926A-EADC2165C456}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/hw01/hw01/src/ConsoleProvider.cs b/hw01/hw01/src/ConsoleProvider.cs new file mode 100644 index 0000000..2d05830 --- /dev/null +++ b/hw01/hw01/src/ConsoleProvider.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hw01 +{ + class ConsoleProvider : IConsoleProvider + { + public void Write(string text, params object[] strings) + { + Console.Write(text, strings); + } + + public void Write(string text, ConsoleColor color, params object[] strings) + { + Console.ForegroundColor = color; + Write(text, strings); + Console.ForegroundColor = ConsoleColor.White; + } + + public void WriteLine(string text, params object[] strings) + { + Console.WriteLine(text, strings); + } + + public void WriteLine(string text, ConsoleColor color, params object[] strings) + { + Console.ForegroundColor = color; + WriteLine(text, strings); + Console.ForegroundColor = ConsoleColor.White; + } + + public string Read() + { + Write(Const.Prompt); + return Console.ReadLine(); + } + } +} diff --git a/hw01/hw01/src/Engine/ConsoleHandler.cs b/hw01/hw01/src/Engine/ConsoleHandler.cs index 366d4a8..ecba2dc 100644 --- a/hw01/hw01/src/Engine/ConsoleHandler.cs +++ b/hw01/hw01/src/Engine/ConsoleHandler.cs @@ -8,34 +8,6 @@ namespace Hw01 { static class ConsoleHandler { - public static void Write(string text, params object[] strings) - { - Console.Write(text, strings); - } - - public static void Write(string text, ConsoleColor color, params object[] strings) - { - Console.ForegroundColor = color; - Write(text, strings); - Console.ForegroundColor = ConsoleColor.White; - } - - public static void WriteLine(string text, params object[] strings) - { - Console.WriteLine(text, strings); - } - - public static void WriteLine(string text, ConsoleColor color, params object[] strings) - { - Console.ForegroundColor = color; - WriteLine(text, strings); - Console.ForegroundColor = ConsoleColor.White; - } - - public static string Read() - { - Write(Const.Prompt); - return Console.ReadLine(); - } + public static IConsoleProvider ConsoleProvider = new ConsoleProvider(); } } diff --git a/hw01/hw01/src/Engine/Player.cs b/hw01/hw01/src/Engine/Player.cs index b420e15..a5042db 100644 --- a/hw01/hw01/src/Engine/Player.cs +++ b/hw01/hw01/src/Engine/Player.cs @@ -22,7 +22,7 @@ namespace Hw01 { if (Heroes[0].Xp + xp >= Heroes[0].MaxXp) { - ConsoleHandler.WriteLine(Const.LevelUp); + ConsoleHandler.ConsoleProvider.WriteLine(Const.LevelUp); } Heroes.ForEach(hero => hero.LevelUp(xp)); } diff --git a/hw01/hw01/src/Engine/Utils.cs b/hw01/hw01/src/Engine/Utils.cs index bbd3fa5..8b14661 100644 --- a/hw01/hw01/src/Engine/Utils.cs +++ b/hw01/hw01/src/Engine/Utils.cs @@ -36,8 +36,16 @@ namespace Hw01 int[] args; while (true) { - string command = ConsoleHandler.Read(); - args = Array.ConvertAll(command.Split(), int.Parse); + string command = ConsoleHandler.ConsoleProvider.Read(); + try + { + args = Array.ConvertAll(command.Split(), int.Parse); + } + catch (Exception ex) + { + ConsoleHandler.ConsoleProvider.WriteLine("Bad input for reorder"); + continue; + } if (args.Any(x => x < 1 || x > 3)) { Console.WriteLine(Const.BadHeroIndex, args.First(x => x > 3 || x < 1)); @@ -48,7 +56,7 @@ namespace Hw01 } else { - ConsoleHandler.WriteLine(Const.BadInputParams, args.Length, 3); + ConsoleHandler.ConsoleProvider.WriteLine(Const.BadInputParams, args.Length, 3); } } } @@ -60,15 +68,15 @@ namespace Hw01 ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), heroes[i].HeroType.ToString(), true); if (withIndex) { - ConsoleHandler.Write("{0}. ", color, i + 1); + ConsoleHandler.ConsoleProvider.Write("{0}. ", color, i + 1); } if (moreInfo) { - ConsoleHandler.WriteLine(heroes[i].DetailedToString(), color); + ConsoleHandler.ConsoleProvider.WriteLine(heroes[i].DetailedToString(), color); } else { - ConsoleHandler.WriteLine(heroes[i].ToString(), color); + ConsoleHandler.ConsoleProvider.WriteLine(heroes[i].ToString(), color); } } } @@ -78,54 +86,54 @@ namespace Hw01 ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), hero.HeroType.ToString(), true); if (newLine) { - ConsoleHandler.WriteLine(hero.Name, color); + ConsoleHandler.ConsoleProvider.WriteLine(hero.Name, color); } else { - ConsoleHandler.Write(hero.Name, color); + ConsoleHandler.ConsoleProvider.Write(hero.Name, color); } } public static void PrintInfo(Player player) { - ConsoleHandler.WriteLine(Const.Info, player.DiamondPieces); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Info, player.DiamondPieces); PrintHeroes(player.Heroes, false, true); } public static void BadStateCommand(Command command) { - ConsoleHandler.WriteLine(Const.BadStateCommand, command.FullCommand); + ConsoleHandler.ConsoleProvider.WriteLine(Const.BadStateCommand, command.FullCommand); } public static void BadCommand(Command command) { - ConsoleHandler.WriteLine(Const.BadCommand, command.FullCommand); + ConsoleHandler.ConsoleProvider.WriteLine(Const.BadCommand, command.FullCommand); } public static void PrintRound(int round, Hero attacker, Hero protector) { - ConsoleHandler.Write(Const.Round, round); + ConsoleHandler.ConsoleProvider.Write(Const.Round, round); PrintHeroName(attacker, false); - ConsoleHandler.Write(" vs. "); + ConsoleHandler.ConsoleProvider.Write(" vs. "); PrintHeroName(protector, true); } public static void PrintDefeated(Hero hero) { PrintHeroName(hero, false); - ConsoleHandler.WriteLine(Const.Defeated); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Defeated); } public static void PrintCurrentHealth(Hero hero) { PrintHeroName(hero, false); - ConsoleHandler.WriteLine(Const.CurrentHealth, hero.Health); + ConsoleHandler.ConsoleProvider.WriteLine(Const.CurrentHealth, hero.Health); } public static void PrintDamageDealt(int damage, Hero attacker, Hero defender) { PrintHeroName(attacker, false); - ConsoleHandler.Write(Const.DamageDealt, damage); + ConsoleHandler.ConsoleProvider.Write(Const.DamageDealt, damage); PrintHeroName(defender, true); } @@ -133,13 +141,13 @@ namespace Hw01 { if (win) { - ConsoleHandler.WriteLine(Const.CavePassed, 150); + ConsoleHandler.ConsoleProvider.WriteLine(Const.CavePassed, 150); player.LevelUp(150); player.DiamondPieces++; } else { - ConsoleHandler.WriteLine(Const.CaveFailed, 100); + ConsoleHandler.ConsoleProvider.WriteLine(Const.CaveFailed, 100); player.LevelUp(100); } } diff --git a/hw01/hw01/src/Game/Game.cs b/hw01/hw01/src/Game/Game.cs index c031f80..16fc29a 100644 --- a/hw01/hw01/src/Game/Game.cs +++ b/hw01/hw01/src/Game/Game.cs @@ -13,11 +13,11 @@ namespace Hw01 public void init() { - ConsoleHandler.WriteLine(Const.StartText); - ConsoleHandler.WriteLine(Const.Delimeter); + ConsoleHandler.ConsoleProvider.WriteLine(Const.StartText); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Delimeter); state = new State(); state.PrintGeneratedHeroes(true, false); - ConsoleHandler.WriteLine(Const.Delimeter); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Delimeter); } public void run() @@ -25,7 +25,7 @@ namespace Hw01 while (!state.Exit) { - var command = Utils.ParseCommand(ConsoleHandler.Read()); + var command = Utils.ParseCommand(ConsoleHandler.ConsoleProvider.Read()); switch (command.Type) { case CommandType.Exit: diff --git a/hw01/hw01/src/Game/State.cs b/hw01/hw01/src/Game/State.cs index 80a5ed5..1a7d39f 100644 --- a/hw01/hw01/src/Game/State.cs +++ b/hw01/hw01/src/Game/State.cs @@ -72,27 +72,27 @@ namespace Hw01 { if (command.Params.Length != 3) { - ConsoleHandler.WriteLine(Const.BadInputParams, command.Params.Length, 3); + ConsoleHandler.ConsoleProvider.WriteLine(Const.BadInputParams, command.Params.Length, 3); return; } int[] args = Array.ConvertAll(command.Params, int.Parse).ToArray(); if (args.Any(x => x > GeneratedHeroes.Count || x < 1)) { - ConsoleHandler.WriteLine(Const.BadHeroIndex, args.First(x => x > GeneratedHeroes.Count || x < 1)); + ConsoleHandler.ConsoleProvider.WriteLine(Const.BadHeroIndex, args.First(x => x > GeneratedHeroes.Count || x < 1)); return; } foreach (int i in args) { Player.Heroes.Add(GeneratedHeroes[i - 1]); } - ConsoleHandler.Write("You have chosen "); + ConsoleHandler.ConsoleProvider.Write("You have chosen "); Utils.PrintHeroName(Heroes[0], false); - ConsoleHandler.Write(", "); + ConsoleHandler.ConsoleProvider.Write(", "); Utils.PrintHeroName(Heroes[1], false); - ConsoleHandler.Write(" and "); + ConsoleHandler.ConsoleProvider.Write(" and "); Utils.PrintHeroName(Heroes[2], false); - ConsoleHandler.WriteLine(""); - ConsoleHandler.WriteLine(Const.Commands); + ConsoleHandler.ConsoleProvider.WriteLine(""); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Commands); } public bool Reorder(string[] str_args) @@ -103,6 +103,11 @@ namespace Hw01 { args = Utils.ReadReorderArgs(); } + else if (str_args.Any(x => x[0] < '0' || x[0] > '3')) + { + ConsoleHandler.ConsoleProvider.WriteLine(Const.BadHeroIndex, str_args.First(x => x[0] < '0' || x[0] > '3')); + args = Utils.ReadReorderArgs(); + } else { args = Array.ConvertAll(str_args, int.Parse).ToArray(); @@ -115,13 +120,13 @@ namespace Hw01 newHeroes.Add(Heroes[i - 1]); } Heroes = newHeroes; - ConsoleHandler.WriteLine(Const.ReorderDone); + ConsoleHandler.ConsoleProvider.WriteLine(Const.ReorderDone); return true; } public void Rip() { - ConsoleHandler.WriteLine(Const.Rip); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Rip); Exit = true; } @@ -129,10 +134,10 @@ namespace Hw01 { if (!AlreadyPicked) { - ConsoleHandler.WriteLine(Const.InspectNotStarted); + ConsoleHandler.ConsoleProvider.WriteLine(Const.InspectNotStarted); return; } - ConsoleHandler.WriteLine(Const.Inspect); + ConsoleHandler.ConsoleProvider.WriteLine(Const.Inspect); Utils.PrintHeroes(Caves.Last(), false, false); } @@ -159,7 +164,7 @@ namespace Hw01 Caves.Remove(Caves.Last()); if (Player.DiamondPieces == 7) { - ConsoleHandler.WriteLine(Const.GamePassed, Player.Fight); + ConsoleHandler.ConsoleProvider.WriteLine(Const.GamePassed, Player.Fight); Exit = true; } return true; diff --git a/hw01/hw01/src/IConsoleProvider.cs b/hw01/hw01/src/IConsoleProvider.cs new file mode 100644 index 0000000..dcbb810 --- /dev/null +++ b/hw01/hw01/src/IConsoleProvider.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hw01 +{ + interface IConsoleProvider + { + public void Write(string text, params object[] strings); + + public void Write(string text, ConsoleColor color, params object[] strings); + + public void WriteLine(string text, params object[] strings); + + public void WriteLine(string text, ConsoleColor color, params object[] strings); + + public string Read(); + } +} -- GitLab