76 lines
1.6 KiB
C#
76 lines
1.6 KiB
C#
using System;
|
|
using System.Runtime.Serialization;
|
|
using System.Collections.Generic;
|
|
|
|
namespace WorldOfPeacecraft
|
|
{
|
|
class Pathwalker
|
|
{
|
|
private LinkedList<Coordinate> Coords;
|
|
|
|
public void Stop()
|
|
{
|
|
Coords = null;
|
|
}
|
|
|
|
public bool HasMoreSteps()
|
|
{
|
|
return Coords != null && Coords.Count >= 2;
|
|
}
|
|
|
|
public string NextStep ()
|
|
{
|
|
if (Coords == null) {
|
|
throw new PathwalkerException ("There are no coordinates set");
|
|
}
|
|
if (Coords.Count < 2) {
|
|
throw new PathwalkerException ("There are no more steps to take");
|
|
}
|
|
Coordinate src = Coords.First.Value;
|
|
Coords.RemoveFirst ();
|
|
Coordinate dst = Coords.First.Value;
|
|
int distance = Math.Abs (src.X - dst.X) + Math.Abs (src.Y - dst.Y);
|
|
if (distance > 1) {
|
|
throw new PathwalkerException ("The distance between (" + src.X + "|" + src.Y + ") and (" + dst.X + "|" + dst.Y + ") is greater than 1");
|
|
}
|
|
string command;
|
|
if (src.X > dst.X) {
|
|
command = "mv:lft";
|
|
} else if (src.X < dst.X) {
|
|
command = "mv:rgt";
|
|
} else if (src.Y > dst.Y) {
|
|
command = "mv:up";
|
|
} else if (src.Y < dst.Y) {
|
|
command = "mv:dwn";
|
|
} else {
|
|
command = NextStep ();
|
|
}
|
|
return command;
|
|
}
|
|
|
|
public void SetCoords(LinkedList<Coordinate> coords)
|
|
{
|
|
this.Coords = coords;
|
|
}
|
|
}
|
|
|
|
public class PathwalkerException : Exception
|
|
{
|
|
public PathwalkerException () : base()
|
|
{
|
|
}
|
|
|
|
public PathwalkerException (string message) : base(message)
|
|
{
|
|
}
|
|
|
|
public PathwalkerException (SerializationInfo info, StreamingContext context) : base(info, context)
|
|
{
|
|
}
|
|
|
|
public PathwalkerException (string message, Exception innerException) : base(message, innerException)
|
|
{
|
|
}
|
|
}
|
|
}
|