diff --git a/hw01/hw01/src/Engine/Command.cs b/hw01/hw01/src/Engine/Command.cs index 026056eff2aa0629b4a444bd8f410890cdb4ccb8..dc84a49c9b88b0d9c42b4a1fe16ed610ef0b6750 100644 --- a/hw01/hw01/src/Engine/Command.cs +++ b/hw01/hw01/src/Engine/Command.cs @@ -21,6 +21,7 @@ namespace Hw01 public Command(string input) { + input = input.Trim(); FullCommand = input; string[] args = input.Split(); try diff --git a/hw01/hw01/src/Engine/ConsoleHandler.cs b/hw01/hw01/src/Engine/ConsoleHandler.cs index 8a57308f6568051ca774bde26c7cf32f4f54c7ba..366d4a85e2aae9cb26304655a15b419231ac530e 100644 --- a/hw01/hw01/src/Engine/ConsoleHandler.cs +++ b/hw01/hw01/src/Engine/ConsoleHandler.cs @@ -13,11 +13,25 @@ namespace Hw01 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); diff --git a/hw01/hw01/src/Engine/Const.cs b/hw01/hw01/src/Engine/Const.cs index d3fa8145dd60055e1a3eadb9510f2af92bf533f6..3e07fa268430f165f3ba12277064b23a1f437783 100644 --- a/hw01/hw01/src/Engine/Const.cs +++ b/hw01/hw01/src/Engine/Const.cs @@ -12,12 +12,25 @@ 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 const string Rip = "You have lost the game :("; + public const string InspectNotStarted = "Game hasn't started yet"; + public const string Inspect = "The next diamond piece is guarded by these enemies:"; + public const string Info = "Diamond pieces: {0}\nYour adventurers:"; + public const string BadStateCommand = "{0} is not supported in this state"; + public const string BadCommand = "{0} is not suported command"; + public const string Round = "Round {0}: "; + public const string DamageDealt = " dealt {0} damage to "; + public const string CurrentHealth = " has currently {0} HP."; + public const string Defeated = " is defeated!"; + public const string CavePassed = "You have won the match! Adventurers gain {0} XP! You acquired the diamond piece!"; + public const string CaveFailed = "You have lost the match... Adventurers gain only {0} XP... You did not acquire the diamond piece..."; + public const string LevelUp = "Your adventurers leveled up!"; + public const string GamePassed = "You have succesfully passed the game, you fight {0} times!"; public static readonly Dictionary<HeroTypeEnum, List<HeroTypeEnum>> EficiencyAttack = new Dictionary<HeroTypeEnum, List<HeroTypeEnum>>() { @@ -28,6 +41,8 @@ namespace Hw01 public static List<string> HeroName = new List<string>() {"Rayna", "Dillian", "Oswyn", "Tamina", "Llana", "Laerdya", "Celethir", "Erunonidan", "Mylaela", "Aranadan", - "Thundruil", "Halrod", "Vallath", "Laeros", "Calothosk", "Deagh"}; + "Thundruil", "Halrod", "Vallath", "Laeros", "Calothosk", "Deagh", "Sakata", + "Calanon", "Mateo", "Maldor", "Wiebe", "Iiri", "Kerbasi", "Erubadhron", + "Haka", "Erynion", "Alagos", "Faelon", "Talfagoron", "Stodgy"}; } } diff --git a/hw01/hw01/src/Engine/NameGenerator.cs b/hw01/hw01/src/Engine/NameGenerator.cs new file mode 100644 index 0000000000000000000000000000000000000000..3660ca850e7e396ff0de6b8a7a8febbefc87943b --- /dev/null +++ b/hw01/hw01/src/Engine/NameGenerator.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hw01 +{ + class NameGenerator + { + private static Random random = new Random(); + private List<string> GeneratedNames = new List<string>(); + + public string GenerateName() + { + string name; + do + { + name = Const.HeroName[random.Next(Const.HeroName.Count)]; + } while (GeneratedNames.Contains(name)); + GeneratedNames.Add(name); + return name; + } + } +} diff --git a/hw01/hw01/src/Engine/Player.cs b/hw01/hw01/src/Engine/Player.cs new file mode 100644 index 0000000000000000000000000000000000000000..b420e15c87a84446cc05b44563c4c8c1ae1b78b8 --- /dev/null +++ b/hw01/hw01/src/Engine/Player.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hw01 +{ + class Player + { + public List<Hero> Heroes { get; set; } + public int DiamondPieces { get; set; } + public int Fight { get; set; } + + public Player() + { + Heroes = new List<Hero>(); + DiamondPieces = 0; + } + + public void LevelUp(int xp) + { + if (Heroes[0].Xp + xp >= Heroes[0].MaxXp) + { + ConsoleHandler.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 8e8bda102432973193cd92f157aff019bd336d2d..bbd3fa5736e9132661cd91f218392ea6df7c92db 100644 --- a/hw01/hw01/src/Engine/Utils.cs +++ b/hw01/hw01/src/Engine/Utils.cs @@ -11,17 +11,13 @@ namespace Hw01 static class Utils { private static Random random = new Random(); + public static NameGenerator NameGenerator = new NameGenerator(); - public static void GenerateStats(out int health, out int damage, out int speed) + public static void GenerateStats(double scale, out int health, out int damage, out int speed) { - health = 5 + random.Next(5); - damage = 2 + random.Next(4); - speed = 2 + random.Next(4); - } - - public static string GenerateName() - { - return Const.HeroName[random.Next(Const.HeroName.Count)]; + health = (int)((5 + random.Next(5)) * scale); + damage = (int)((4 + random.Next(4)) * scale); + speed = (int)((4 + random.Next(4)) * scale); } public static HeroTypeEnum GenerateHeroType() @@ -34,5 +30,118 @@ namespace Hw01 { return new Command(input); } + + public static int[] ReadReorderArgs() + { + 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) + { + return args; + } + else + { + ConsoleHandler.WriteLine(Const.BadInputParams, args.Length, 3); + } + } + } + + public static void PrintHeroes(List<Hero> heroes, bool withIndex, bool moreInfo) + { + for (int i = 0; i < heroes.Count; i++) + { + ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), heroes[i].HeroType.ToString(), true); + if (withIndex) + { + ConsoleHandler.Write("{0}. ", color, i + 1); + } + if (moreInfo) + { + ConsoleHandler.WriteLine(heroes[i].DetailedToString(), color); + } + else + { + ConsoleHandler.WriteLine(heroes[i].ToString(), color); + } + } + } + + public static void PrintHeroName(Hero hero, bool newLine) + { + ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), hero.HeroType.ToString(), true); + if (newLine) + { + ConsoleHandler.WriteLine(hero.Name, color); + } + else + { + ConsoleHandler.Write(hero.Name, color); + } + } + + public static void PrintInfo(Player player) + { + ConsoleHandler.WriteLine(Const.Info, player.DiamondPieces); + PrintHeroes(player.Heroes, false, true); + } + + public static void BadStateCommand(Command command) + { + ConsoleHandler.WriteLine(Const.BadStateCommand, command.FullCommand); + } + + public static void BadCommand(Command command) + { + ConsoleHandler.WriteLine(Const.BadCommand, command.FullCommand); + } + + public static void PrintRound(int round, Hero attacker, Hero protector) + { + ConsoleHandler.Write(Const.Round, round); + PrintHeroName(attacker, false); + ConsoleHandler.Write(" vs. "); + PrintHeroName(protector, true); + } + + public static void PrintDefeated(Hero hero) + { + PrintHeroName(hero, false); + ConsoleHandler.WriteLine(Const.Defeated); + } + + public static void PrintCurrentHealth(Hero hero) + { + PrintHeroName(hero, false); + ConsoleHandler.WriteLine(Const.CurrentHealth, hero.Health); + } + + public static void PrintDamageDealt(int damage, Hero attacker, Hero defender) + { + PrintHeroName(attacker, false); + ConsoleHandler.Write(Const.DamageDealt, damage); + PrintHeroName(defender, true); + } + + public static void CaveEnd(Player player, bool win) + { + if (win) + { + ConsoleHandler.WriteLine(Const.CavePassed, 150); + player.LevelUp(150); + player.DiamondPieces++; + } + else + { + ConsoleHandler.WriteLine(Const.CaveFailed, 100); + player.LevelUp(100); + } + } } } diff --git a/hw01/hw01/src/Engine/models/Entity.cs b/hw01/hw01/src/Engine/models/Entity.cs index 08634819d65f452d9caeddcd8dd0b8ba4dc25cdc..b0e1b86286586effa4ca37e92f46cfa5ab5596e6 100644 --- a/hw01/hw01/src/Engine/models/Entity.cs +++ b/hw01/hw01/src/Engine/models/Entity.cs @@ -9,41 +9,29 @@ namespace Hw01 abstract class Entity { - private string _name; - private int _health; - private int _maxHealth; - private int _damage; - private int _speed; - private int _level; - public string Name - { get { return _name; } } - public int Health - { - get { return _health; } - set { _health = value; } - } - public int MaxHealth - { - get { return _maxHealth; } - } - public int Damage - { get { return _damage; } } - public int Speed - { get { return _speed; } } - public int Level - { get { return _level; } } + public string Name { get; } + public int Health { get; set; } + public int MaxHealth { get; set; } + public int Damage { get; set; } + public int Speed { get; set; } + public int Level { get; set; } + public int MaxXp { get; } + public int Xp { get; set; } protected Entity(string name, int health, int damage, int speed, int level) { - _name = name; - _maxHealth = _health = health; - _damage = damage; - _speed = speed; - _level = level; + Name = name; + Health = health; + MaxHealth = health; + Damage = damage; + Speed = speed; + Level = level; + MaxXp = 100; + Xp = 0; } - public abstract void attack(Entity another); + public abstract void Attack(Entity another); public override abstract string ToString(); } diff --git a/hw01/hw01/src/Engine/models/Hero.cs b/hw01/hw01/src/Engine/models/Hero.cs index 96039ae8ab5f21106917b8a9e1ac1f331c45cf07..7f79ec9b159aa0c87e74a680098c5b9ca8498dc1 100644 --- a/hw01/hw01/src/Engine/models/Hero.cs +++ b/hw01/hw01/src/Engine/models/Hero.cs @@ -9,26 +9,24 @@ namespace Hw01 class Hero : Entity { - private HeroTypeEnum _heroType { get; } - public HeroTypeEnum HeroType - { get { return _heroType; } } + public HeroTypeEnum HeroType { get; } public Hero(string name, int health, int damage, - int speed, int level, HeroTypeEnum playerType) : base(name, health, damage, speed, level) + int speed, int level, HeroTypeEnum heroType) : base(name, health, damage, speed, level) { - _heroType = playerType; + HeroType = heroType; } - public override void attack(Entity another) + public override void Attack(Entity another) { - attackHero((Hero)another); + AttackHero((Hero)another); } - public void attackHero(Hero other) + public void AttackHero(Hero other) { if (Speed < other.Speed) { - other.attack(this); + other.Attack(this); if (Health <= 0) { return; @@ -36,19 +34,22 @@ namespace Hw01 } if (Const.EficiencyAttack[HeroType].Contains(other.HeroType)) { + Utils.PrintDamageDealt(Damage * 2, this, other); other.Health -= Damage * 2; } else { + Utils.PrintDamageDealt(Damage, this, other); other.Health -= Damage; } if (other.Health <= 0) { + Utils.PrintDefeated(other); return; } if (Speed >= other.Speed) { - other.attack(this); + other.Attack(this); } } @@ -56,5 +57,24 @@ namespace Hw01 { return Name + ": " + Damage + " Attack, " + Health + " HP, " + Speed + " Speed"; } + + public string DetailedToString() + { + return ToString() + ", level " + Level + ", " + Xp + "/" + MaxXp + " XP"; + } + + public void LevelUp(int xp) + { + Xp += xp; + Health = MaxHealth; + while (Xp > MaxXp) + { + Level++; + Xp -= MaxXp; + MaxHealth = Health = (int)(MaxHealth * 1.2); + Damage = (int)(Damage * 1.2); + Speed = (int)(Speed * 1.2); + } + } } } diff --git a/hw01/hw01/src/Game/Game.cs b/hw01/hw01/src/Game/Game.cs index c6666af4f286fd22fefc461442ad45f84f097fe9..c031f80aa52ede1ff2ec77e5a078321bfb895f33 100644 --- a/hw01/hw01/src/Game/Game.cs +++ b/hw01/hw01/src/Game/Game.cs @@ -25,29 +25,40 @@ namespace Hw01 while (!state.Exit) { - var input = Utils.ParseCommand(ConsoleHandler.Read()); - switch (input.Type) + var command = Utils.ParseCommand(ConsoleHandler.Read()); + switch (command.Type) { 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); - } + state.Pick(command); break; case CommandType.Reorder when state.AlreadyPicked: - state.Reorder(); - ConsoleHandler.WriteLine(Const.ReorderDone); + state.Reorder(command.Params); break; + case CommandType.Rip when state.AlreadyPicked: + state.Rip(); + break; + case CommandType.Inspect: + state.Inspect(); + break; + case CommandType.Info when state.AlreadyPicked: + state.Info(); + break; + case CommandType.Fight when state.AlreadyPicked: + state.Fight(); + break; + case CommandType.Fight: + case CommandType.Info: case CommandType.Reorder: case CommandType.Start: - ConsoleHandler.WriteLine("{0} is not supported in this state", input.FullCommand); + case CommandType.Rip: + Utils.BadStateCommand(command); break; default: - ConsoleHandler.WriteLine("{0} is not suported command", input.FullCommand); + Utils.BadCommand(command); + break; } } diff --git a/hw01/hw01/src/Game/State.cs b/hw01/hw01/src/Game/State.cs index cd15ca771d94e41a1d843403dafd3222b07d5818..80a5ed58667c198c5d204781523bc21c84651e87 100644 --- a/hw01/hw01/src/Game/State.cs +++ b/hw01/hw01/src/Game/State.cs @@ -9,10 +9,12 @@ namespace Hw01 class State { private List<Hero> _generatedHeroes = new List<Hero>(); - private List<Hero> _heroes = new List<Hero>(); + private List<List<Hero>> _caves = new List<List<Hero>>(); + public Player Player { get; set; } + public List<Hero> Heroes { get { return Player.Heroes; } set { Player.Heroes = value; } } 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 List<List<Hero>> Caves { get { return _caves; } } + public bool AlreadyPicked { get { return Player.Heroes.Count != 0; } } public bool Exit { get; set; } @@ -23,7 +25,25 @@ namespace Hw01 public void Init() { + Player = new Player(); GenerateHeroes(); + GenerateCaves(); + } + + public void GenerateCaves() + { + for (int caveIndex = 7; caveIndex > 0; caveIndex--) + { + List<Hero> cave = new List<Hero>(); + for (int i = 0; i < 3; i++) + { + int health, damage, speed; + Utils.GenerateStats(1 + (0.2 * caveIndex), out health, out damage, out speed); + string name = Utils.NameGenerator.GenerateName(); + cave.Add(new Hero(name, health, damage, speed, -1, Utils.GenerateHeroType())); + } + Caves.Add(cave); + } } private void GenerateHeroes() @@ -31,81 +51,63 @@ namespace Hw01 for (int i = 0; i < Const.NumberOfGeneratedHeroes; i++) { int health, damage, speed; - Utils.GenerateStats(out health, out damage, out speed); - GeneratedHeroes.Add(new Hero(Utils.GenerateName(), health, damage, speed, 0, Utils.GenerateHeroType())); + Utils.GenerateStats(1, out health, out damage, out speed); + string name = Utils.NameGenerator.GenerateName(); + + GeneratedHeroes.Add(new Hero(name, health, damage, speed, 0, Utils.GenerateHeroType())); } } public void PrintGeneratedHeroes(bool withIndexes, bool onlyName) { - PrintHeores(GeneratedHeroes, withIndexes, onlyName); + Utils.PrintHeroes(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()); - } - } + Utils.PrintHeroes(Heroes, withIndexes, onlyName); } - public bool Pick(Command command) + public void Pick(Command command) { - if (command.Params.Length < 1 || command.Params.Length > 3) + if (command.Params.Length != 3) { ConsoleHandler.WriteLine(Const.BadInputParams, command.Params.Length, 3); - return false; + return; } int[] args = Array.ConvertAll(command.Params, int.Parse).ToArray(); - if (args.Any(x => x >= GeneratedHeroes.Count || x < 1)) + if (args.Any(x => x > GeneratedHeroes.Count || x < 1)) { ConsoleHandler.WriteLine(Const.BadHeroIndex, args.First(x => x > GeneratedHeroes.Count || x < 1)); - return false; + return; } foreach (int i in args) { - Heroes.Add(GeneratedHeroes[i - 1]); + Player.Heroes.Add(GeneratedHeroes[i - 1]); } - return true; + ConsoleHandler.Write("You have chosen "); + Utils.PrintHeroName(Heroes[0], false); + ConsoleHandler.Write(", "); + Utils.PrintHeroName(Heroes[1], false); + ConsoleHandler.Write(" and "); + Utils.PrintHeroName(Heroes[2], false); + ConsoleHandler.WriteLine(""); + ConsoleHandler.WriteLine(Const.Commands); } - public bool Reorder() + public bool Reorder(string[] str_args) { - PrintPickedHeroes(true, true); int[] args; - while (true) + PrintPickedHeroes(true, true); + if (str_args.Length == 0) { - 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); - } + args = Utils.ReadReorderArgs(); + } + else + { + args = Array.ConvertAll(str_args, int.Parse).ToArray(); } + List<Hero> newHeroes = new List<Hero>(); foreach (int i in args) @@ -113,7 +115,61 @@ namespace Hw01 newHeroes.Add(Heroes[i - 1]); } Heroes = newHeroes; + ConsoleHandler.WriteLine(Const.ReorderDone); return true; } + + public void Rip() + { + ConsoleHandler.WriteLine(Const.Rip); + Exit = true; + } + + public void Inspect() + { + if (!AlreadyPicked) + { + ConsoleHandler.WriteLine(Const.InspectNotStarted); + return; + } + ConsoleHandler.WriteLine(Const.Inspect); + Utils.PrintHeroes(Caves.Last(), false, false); + } + + public void Info() + { + Utils.PrintInfo(Player); + } + + public bool Fight() + { + int round = 1; + Player.Fight++; + while (true) + { + if (Heroes.All(x => x.Health <= 0)) + { + Utils.CaveEnd(Player, false); + + return false; + } + if (Caves.Last().All(x => x.Health <= 0)) + { + Utils.CaveEnd(Player, true); + Caves.Remove(Caves.Last()); + if (Player.DiamondPieces == 7) + { + ConsoleHandler.WriteLine(Const.GamePassed, Player.Fight); + Exit = true; + } + return true; + } + Hero hero = Heroes.First(x => x.Health > 0); + Hero protector = Caves.Last().First(x => x.Health > 0); + Utils.PrintRound(round, hero, protector); + hero.AttackHero(protector); + round++; + } + } } }