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

Added start and reorder function

parent e18b774f
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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();
}
}
}
......@@ -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();
}
}
......
......@@ -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",
......
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
}
}
......@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hw01
namespace Hw01
{
interface IGame
{
......
......@@ -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);
}
}
}
......@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hw01
namespace Hw01
{
abstract class Entity
......
......@@ -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;
}
......
......@@ -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);
}
}
}
......
using System;
namespace hw01.src
namespace Hw01.src
{
class Program
{
......
......@@ -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;
}
}
}
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