From 2f4f526e806ceaa7e013bcc1cd1b8afe939b30e3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Peter=20Kult=C3=A1n?= <xkultan@fi.muni.cz>
Date: Thu, 3 Mar 2022 22:51:47 +0100
Subject: [PATCH] Added start and reorder function

---
 hw01/hw01.sln                          |  2 +-
 hw01/hw01/src/Engine/Command.cs        | 37 +++++++++++
 hw01/hw01/src/Engine/ConsoleHandler.cs | 12 ++--
 hw01/hw01/src/Engine/Const.cs          | 16 +++--
 hw01/hw01/src/Engine/Enums.cs          | 18 ++++++
 hw01/hw01/src/Engine/IGame.cs          |  2 +-
 hw01/hw01/src/Engine/Utils.cs          | 15 +++--
 hw01/hw01/src/Engine/models/Entity.cs  |  2 +-
 hw01/hw01/src/Engine/models/Hero.cs    | 16 ++---
 hw01/hw01/src/Game/Game.cs             | 37 ++++++++---
 hw01/hw01/src/Game/Program.cs          |  2 +-
 hw01/hw01/src/Game/State.cs            | 90 ++++++++++++++++++++++++--
 12 files changed, 204 insertions(+), 45 deletions(-)
 create mode 100644 hw01/hw01/src/Engine/Command.cs
 create mode 100644 hw01/hw01/src/Engine/Enums.cs

diff --git a/hw01/hw01.sln b/hw01/hw01.sln
index 473b3e2..a6ab0a9 100644
--- a/hw01/hw01.sln
+++ b/hw01/hw01.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio Version 17
 VisualStudioVersion = 17.0.32126.317
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw01", "hw01\hw01.csproj", "{3BE575A4-9DBB-4412-A23B-B23746CF3607}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw01", "hw01\Hw01.csproj", "{3BE575A4-9DBB-4412-A23B-B23746CF3607}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/hw01/hw01/src/Engine/Command.cs b/hw01/hw01/src/Engine/Command.cs
new file mode 100644
index 0000000..026056e
--- /dev/null
+++ b/hw01/hw01/src/Engine/Command.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hw01
+{
+    class Command
+    {
+        public CommandType? Type { get; }
+        public string[] Params { get; }
+        public string FullCommand { get; }
+
+        public Command(CommandType type, string input)
+        {
+            FullCommand = input;
+            Type = type;
+            Params = input.Split().Skip(1).ToArray();
+        }
+
+        public Command(string input)
+        {
+            FullCommand = input;
+            string[] args = input.Split();
+            try
+            {
+                Type = (CommandType)Enum.Parse(typeof(CommandType), args[0], true);
+            }
+            catch
+            {
+                Type = null;
+            }
+            Params = args.Skip(1).ToArray();
+        }
+    }
+}
diff --git a/hw01/hw01/src/Engine/ConsoleHandler.cs b/hw01/hw01/src/Engine/ConsoleHandler.cs
index 92911a0..8a57308 100644
--- a/hw01/hw01/src/Engine/ConsoleHandler.cs
+++ b/hw01/hw01/src/Engine/ConsoleHandler.cs
@@ -4,23 +4,23 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
     static class ConsoleHandler
     {
-        public static void Write(string text)
+        public static void Write(string text, params object[] strings)
         {
-            Console.Write(text);
+            Console.Write(text, strings);
         }
 
-        public static void WriteLine(string text)
+        public static void WriteLine(string text, params object[] strings)
         {
-            Console.WriteLine(text);
+            Console.WriteLine(text, strings);
         }
 
         public static string Read()
         {
-            Write("Player:> ");
+            Write(Const.Prompt);
             return Console.ReadLine();
         }
     }
diff --git a/hw01/hw01/src/Engine/Const.cs b/hw01/hw01/src/Engine/Const.cs
index f47b708..d3fa814 100644
--- a/hw01/hw01/src/Engine/Const.cs
+++ b/hw01/hw01/src/Engine/Const.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
     static class Const
     {
@@ -12,12 +12,18 @@ namespace hw01
 
         public const string StartText = "Welcome to The Quest for Carrefour! Please, choose three adventurers:";
         public const string Delimeter = "========================";
+        public const string PickedHeroes = "You have chosen {0}, {1} and {2}.";
+        public const string Commands = "Your commands are: inspect, fight, info, reorder, rip";
+        public const string Prompt = "Player:> ";
+        public const string BadInputParams = "Bad number of params: {0}, {1} are required";
+        public const string BadHeroIndex = "Hero with number {0} does not exist";
+        public const string ReorderDone = "The order of your adventurers has been updated.";
 
-        public static readonly Dictionary<HeroType, List<HeroType>> EficiencyAttack = new Dictionary<HeroType, List<HeroType>>()
+        public static readonly Dictionary<HeroTypeEnum, List<HeroTypeEnum>> EficiencyAttack = new Dictionary<HeroTypeEnum, List<HeroTypeEnum>>()
         {
-            {HeroType.red, new List<HeroType>() { HeroType.green} },
-            {HeroType.blue, new List<HeroType>() {HeroType.red} },
-            {HeroType.green, new List<HeroType>() {HeroType.blue} }
+            {HeroTypeEnum.red, new List<HeroTypeEnum>() { HeroTypeEnum.green} },
+            {HeroTypeEnum.blue, new List<HeroTypeEnum>() {HeroTypeEnum.red} },
+            {HeroTypeEnum.green, new List<HeroTypeEnum>() {HeroTypeEnum.blue} }
         };
 
         public static List<string> HeroName = new List<string>() {"Rayna", "Dillian", "Oswyn",
diff --git a/hw01/hw01/src/Engine/Enums.cs b/hw01/hw01/src/Engine/Enums.cs
new file mode 100644
index 0000000..cef87a7
--- /dev/null
+++ b/hw01/hw01/src/Engine/Enums.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hw01
+{
+    enum HeroTypeEnum
+    {
+        green, blue, red
+    }
+
+    enum CommandType
+    {
+        Start, Inspect, Fight, Info, Reorder, Rip, Exit=-1
+    }
+}
diff --git a/hw01/hw01/src/Engine/IGame.cs b/hw01/hw01/src/Engine/IGame.cs
index 34b37ce..940c39e 100644
--- a/hw01/hw01/src/Engine/IGame.cs
+++ b/hw01/hw01/src/Engine/IGame.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
     interface IGame
     {
diff --git a/hw01/hw01/src/Engine/Utils.cs b/hw01/hw01/src/Engine/Utils.cs
index e025938..8e8bda1 100644
--- a/hw01/hw01/src/Engine/Utils.cs
+++ b/hw01/hw01/src/Engine/Utils.cs
@@ -4,8 +4,10 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
+
+
     static class Utils
     {
         private static Random random = new Random();
@@ -22,10 +24,15 @@ namespace hw01
             return Const.HeroName[random.Next(Const.HeroName.Count)];
         }
 
-        public static HeroType GenerateHeroType()
+        public static HeroTypeEnum GenerateHeroType()
+        {
+            var enumList = Enum.GetValues(typeof(HeroTypeEnum));
+            return (HeroTypeEnum)enumList.GetValue(random.Next(enumList.Length));
+        }
+
+        public static Command ParseCommand(string input)
         {
-            var enumList = Enum.GetValues(typeof(HeroType));
-            return (HeroType)enumList.GetValue(random.Next(enumList.Length));
+            return new Command(input);
         }
     }
 }
diff --git a/hw01/hw01/src/Engine/models/Entity.cs b/hw01/hw01/src/Engine/models/Entity.cs
index 1e4e546..0863481 100644
--- a/hw01/hw01/src/Engine/models/Entity.cs
+++ b/hw01/hw01/src/Engine/models/Entity.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
 
     abstract class Entity
diff --git a/hw01/hw01/src/Engine/models/Hero.cs b/hw01/hw01/src/Engine/models/Hero.cs
index 838a360..96039ae 100644
--- a/hw01/hw01/src/Engine/models/Hero.cs
+++ b/hw01/hw01/src/Engine/models/Hero.cs
@@ -4,23 +4,17 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
-
-    enum HeroType
-    {
-        green, blue, red
-    }
-
     class Hero : Entity
     {
-      
-        private HeroType _heroType { get; }
-        public HeroType HeroType
+
+        private HeroTypeEnum _heroType { get; }
+        public HeroTypeEnum HeroType
         { get { return _heroType; } }
 
         public Hero(string name, int health, int damage,
-            int speed, int level, HeroType playerType) : base(name, health, damage, speed, level)
+            int speed, int level, HeroTypeEnum playerType) : base(name, health, damage, speed, level)
         {
             _heroType = playerType;
         }
diff --git a/hw01/hw01/src/Game/Game.cs b/hw01/hw01/src/Game/Game.cs
index 180d4df..c6666af 100644
--- a/hw01/hw01/src/Game/Game.cs
+++ b/hw01/hw01/src/Game/Game.cs
@@ -4,33 +4,52 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
     class Game : IGame
     {
 
-        State state;
+        private State state;
 
         public void init()
         {
             ConsoleHandler.WriteLine(Const.StartText);
             ConsoleHandler.WriteLine(Const.Delimeter);
             state = new State();
-            state.PrintHeroes();
+            state.PrintGeneratedHeroes(true, false);
             ConsoleHandler.WriteLine(Const.Delimeter);
         }
 
         public void run()
         {
-            // select heroes
-            while (true)
+
+            while (!state.Exit)
             {
-                var input = ConsoleHandler.Read();
-                if (input == "exit")
+                var input = Utils.ParseCommand(ConsoleHandler.Read());
+                switch (input.Type)
                 {
-                    break;
+                    case CommandType.Exit:
+                        state.Exit = true;
+                        break;
+                    case CommandType.Start when !state.AlreadyPicked:
+                        if (state.Pick(input))
+                        {
+                            ConsoleHandler.WriteLine(Const.PickedHeroes, state.Heroes[0].Name, state.Heroes[1].Name, state.Heroes[2].Name);
+                            ConsoleHandler.WriteLine(Const.Commands);
+                        }
+                        break;
+                    case CommandType.Reorder when state.AlreadyPicked:
+                        state.Reorder();
+                        ConsoleHandler.WriteLine(Const.ReorderDone);
+                        break;
+                    case CommandType.Reorder:
+                    case CommandType.Start:
+                        ConsoleHandler.WriteLine("{0} is not supported in this state", input.FullCommand);
+                        break;
+                    default:
+                        ConsoleHandler.WriteLine("{0} is not suported command", input.FullCommand);
+                        break;
                 }
-                ConsoleHandler.WriteLine(input);
             }
         }
     }
diff --git a/hw01/hw01/src/Game/Program.cs b/hw01/hw01/src/Game/Program.cs
index e557346..bbb70c1 100644
--- a/hw01/hw01/src/Game/Program.cs
+++ b/hw01/hw01/src/Game/Program.cs
@@ -1,6 +1,6 @@
 using System;
 
-namespace hw01.src
+namespace Hw01.src
 {
     class Program
     {
diff --git a/hw01/hw01/src/Game/State.cs b/hw01/hw01/src/Game/State.cs
index c8b6700..cd15ca7 100644
--- a/hw01/hw01/src/Game/State.cs
+++ b/hw01/hw01/src/Game/State.cs
@@ -4,11 +4,17 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace hw01
+namespace Hw01
 {
     class State
     {
-        List<Hero> Heroes = new List<Hero>();
+        private List<Hero> _generatedHeroes = new List<Hero>();
+        private List<Hero> _heroes = new List<Hero>();
+        public List<Hero> GeneratedHeroes { get { return _generatedHeroes; } }
+        public List<Hero> Heroes { get { return _heroes; } set { _heroes = value; } }
+        public bool AlreadyPicked { get { return Heroes.Count != 0; } }
+        public bool Exit { get; set; }
+
 
         public State()
         {
@@ -26,16 +32,88 @@ namespace hw01
             {
                 int health, damage, speed;
                 Utils.GenerateStats(out health, out damage, out speed);
-                Heroes.Add(new Hero(Utils.GenerateName(), health, damage, speed, 0, Utils.GenerateHeroType()));
+                GeneratedHeroes.Add(new Hero(Utils.GenerateName(), health, damage, speed, 0, Utils.GenerateHeroType()));
+            }
+        }
+
+        public void PrintGeneratedHeroes(bool withIndexes, bool onlyName)
+        {
+            PrintHeores(GeneratedHeroes, withIndexes, onlyName);
+        }
+
+        public void PrintPickedHeroes(bool withIndexes, bool onlyName)
+        {
+            PrintHeores(Heroes, withIndexes, onlyName);
+        }
+
+        private void PrintHeores(List<Hero> heroes, bool withIndex, bool onlyName)
+        {
+            for (int i = 0; i < heroes.Count; i++)
+            {
+                if (withIndex)
+                {
+                    ConsoleHandler.Write("{0}. ", i + 1);
+                }
+                if (onlyName)
+                {
+                    ConsoleHandler.WriteLine(heroes[i].Name);
+                }
+                else
+                {
+                    ConsoleHandler.WriteLine(heroes[i].ToString());
+                }
             }
         }
 
-        public void PrintHeroes()
+        public bool Pick(Command command)
         {
-            for (int i = 0;i < Heroes.Count;i++)
+            if (command.Params.Length < 1 || command.Params.Length > 3)
+            {
+                ConsoleHandler.WriteLine(Const.BadInputParams, command.Params.Length, 3);
+                return false;
+            }
+            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));
+                return false;
+            }
+            foreach (int i in args)
+            {
+                Heroes.Add(GeneratedHeroes[i - 1]);
+            }
+            return true;
+        }
+
+        public bool Reorder()
+        {
+            PrintPickedHeroes(true, true);
+            int[] args;
+            while (true)
+            {
+                string command = ConsoleHandler.Read();
+                args = Array.ConvertAll(command.Split(), int.Parse);
+                if (args.Any(x => x < 1 || x > 3))
+                {
+                    Console.WriteLine(Const.BadHeroIndex, args.First(x => x > 3 || x < 1));
+                }
+                else if (args.Length < 4 && args.Length > 0)
+                {
+                    break;
+                }
+                else
+                {
+                    ConsoleHandler.WriteLine(Const.BadInputParams, args.Length, 3);
+                }
+            }
+
+            List<Hero> newHeroes = new List<Hero>();
+            foreach (int i in args)
             {
-                ConsoleHandler.WriteLine((i + 1) + ". " +  Heroes[i].ToString());
+                newHeroes.Add(Heroes[i - 1]);
             }
+            Heroes = newHeroes;
+            return true;
         }
     }
 }
-- 
GitLab