Pathwalker-Klasse (Blatt 7)
This commit is contained in:
70
src/Pathwalker.cs
Normal file
70
src/Pathwalker.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WorldOfPeacecraft
|
||||
{
|
||||
class Pathwalker
|
||||
{
|
||||
private LinkedList<Coordinate> Coords;
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Coords = null;
|
||||
}
|
||||
|
||||
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 grater than 1");
|
||||
}
|
||||
string command;
|
||||
if (src.X > dst.X) {
|
||||
command = "mv:dwn";
|
||||
} else if (src.X < dst.X) {
|
||||
command = "mv:up";
|
||||
} else if (src.Y > dst.Y) {
|
||||
command = "mv:lft";
|
||||
} else if (src.Y < dst.Y) {
|
||||
command = "mv:rgt";
|
||||
} 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user