diff --git a/hw01/hw01.sln b/hw01/hw01.sln index 473b3e2b8b145837e374fa4b25ee22253a9e3bac..a6ab0a9c898119dd266a9b31e2e18d9e12080765 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 0000000000000000000000000000000000000000..026056eff2aa0629b4a444bd8f410890cdb4ccb8 --- /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 92911a0e4205d6f73a35cf812f9c5ced6e97e1fe..8a57308f6568051ca774bde26c7cf32f4f54c7ba 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 f47b708d5942ccfe88716e02195fc4506e578e14..d3fa8145dd60055e1a3eadb9510f2af92bf533f6 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 0000000000000000000000000000000000000000..cef87a736530c69a80b6594a74cbb23f8ba358a0 --- /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 34b37ce1d0802b2916e238bf403b5f4be16500e0..940c39ef5a416b00eaa2f5826d2ef6dd3e74db9f 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 e025938883963fe24066d82a79ff536089f68916..8e8bda102432973193cd92f157aff019bd336d2d 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 1e4e546eb379efb798b9a5fc90d2c79f1b2fb8f8..08634819d65f452d9caeddcd8dd0b8ba4dc25cdc 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 838a3602c6e506ba1facefdd781d877ca3f44259..96039ae8ab5f21106917b8a9e1ac1f331c45cf07 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 180d4df5312c0c650d1a770657ccd9d40a9c8615..c6666af4f286fd22fefc461442ad45f84f097fe9 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 e557346e0b818bc9e6cc30ec92650c2e5fec5aa2..bbb70c1219c218b6ff294583b617dbf1024cafb6 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 c8b670029d34a506cfab7c7b6dfabb66f862f0f7..cd15ca771d94e41a1d843403dafd3222b07d5818 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; } } }