mirror of
https://github.com/farcasclaudiu/MartianRobots.git
synced 2026-06-28 21:01:08 +03:00
Add project files.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace MartianRobotsSolver
|
||||
{
|
||||
public class CommandProcessor
|
||||
{
|
||||
private Dictionary<string, IRobotCommand> processors = new Dictionary<string, IRobotCommand>();
|
||||
|
||||
public CommandProcessor()
|
||||
{
|
||||
Assembly.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(t => !t.IsAbstract && t.IsAssignableTo(typeof(IRobotCommand)))
|
||||
.Select(t =>
|
||||
{
|
||||
IRobotCommand? command = Activator.CreateInstance(t) as IRobotCommand;
|
||||
if(command != null)
|
||||
processors.Add(command.Command, command);
|
||||
return command;
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void Process(RobotInfo robotInfo, string cmd)
|
||||
{
|
||||
|
||||
if (processors.ContainsKey(cmd))
|
||||
{
|
||||
processors[cmd].Process(robotInfo);
|
||||
} else {
|
||||
throw new NotSupportedException($"cmd {cmd} is not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user