Skip to content
Snippets Groups Projects
Commit c1741a9e authored by Peter Kultán's avatar Peter Kultán :slight_smile:
Browse files

Hw01 first version complete

parent 2f4f526e
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@ namespace Hw01
public Command(string input)
{
input = input.Trim();
FullCommand = input;
string[] args = input.Split();
try
......
......@@ -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);
......
......@@ -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"};
}
}
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;
}
}
}
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));
}
}
}
......@@ -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);
}
}
}
}
......@@ -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();
}
......
......@@ -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);
}
}
}
}
......@@ -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;
}
}
......
......@@ -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++;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment