Pathwalker-Klasse (Blatt 7)

This commit is contained in:
2014-04-28 15:03:46 +02:00
parent a384310766
commit 7a8df2e147
5 changed files with 104 additions and 9 deletions

View File

@@ -71,6 +71,8 @@
<Compile Include="src\Time.cs" /> <Compile Include="src\Time.cs" />
<Compile Include="src\Buffer_Wafa.cs" /> <Compile Include="src\Buffer_Wafa.cs" />
<Compile Include="src\BufferManuel.cs" /> <Compile Include="src\BufferManuel.cs" />
<Compile Include="src\Coordinate.cs" />
<Compile Include="src\Pathwalker.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
</Project> </Project>

20
src/Coordinate.cs Normal file
View File

@@ -0,0 +1,20 @@
namespace WorldOfPeacecraft
{
public class Coordinate
{
public int X { get; set; }
public int Y { get; set; }
public Coordinate (int x, int y)
{
this.X = x;
this.Y = y;
}
public void setCoordinate(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}

View File

@@ -10,16 +10,14 @@ namespace WorldOfPeacecraft
public abstract class Entity : IPositionable public abstract class Entity : IPositionable
{ {
public int Id; public int Id;
public int PosX; public Coordinate Coord;
public int PosY;
public string Desc; public string Desc;
public bool Busy; public bool Busy;
public Entity (int id, int posX, int posY, string desc, bool busy) public Entity (int id, int posX, int posY, string desc, bool busy)
{ {
this.SetId (id); this.SetId (id);
this.SetPosX (posX); this.SetCoord(new Coordinate(posX, posY));
this.SetPosY (posY);
this.SetDesc (desc); this.SetDesc (desc);
this.SetBusy (busy); this.SetBusy (busy);
} }
@@ -36,22 +34,27 @@ namespace WorldOfPeacecraft
public void SetPosX (int x) public void SetPosX (int x)
{ {
this.PosX = x; this.Coord.X = x;
} }
public int getXPosition () public int getXPosition ()
{ {
return PosX; return Coord.X;
} }
public void SetPosY (int y) public void SetPosY (int y)
{ {
this.PosY = y; this.Coord.Y = y;
} }
public int getYPosition () public int getYPosition ()
{ {
return PosY; return Coord.Y;
}
public void SetCoord (Coordinate coord)
{
this.Coord = coord;
} }
public void SetDesc (string desc) public void SetDesc (string desc)

70
src/Pathwalker.cs Normal file
View 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)
{
}
}
}

View File

@@ -21,7 +21,7 @@ namespace WorldOfPeacecraft
public override string ToString() public override string ToString()
{ {
return "Player: " + Id + " " + PosX + " " + PosY + " " + Desc + " " + Busy + " " + Score; return "Player: " + Id + " " + Coord.X + " " + Coord.Y + " " + Desc + " " + Busy + " " + Score;
} }
} }
} }