Skip to content

Commit

Permalink
Refactoring de Grid
Browse files Browse the repository at this point in the history
Ajout de methode "safe null"
  • Loading branch information
FaustVX committed Dec 12, 2013
1 parent 9627d5e commit c933935
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
56 changes: 48 additions & 8 deletions Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Cell<T> : Grid<T>.ICell
where T : Cell<T>
{
private readonly int _x, _y;
private readonly Grid<T> _list;
private T _up, _left, _down, _right;

/// <summary>
Expand All @@ -60,6 +61,7 @@ public Cell(int x, int y, Grid<T> list, bool torus)
{
_x = x;
_y = y;
_list = list;

if(torus)
{
Expand Down Expand Up @@ -116,6 +118,21 @@ public int Y
get { return _y; }
}

public Grid<T> Grid
{
get { return _list; }
}

public IEnumerable<T> Range(int radius)
{
return Grid.Range(this as T, radius);
}

public IEnumerable<T> Cricle(int radius)
{
return Grid.Circle(this as T, radius);
}

/// <summary>
/// liste toutes les cellules <typeparamref name="T"/> voisines en fonction du mode de sélection
/// </summary>
Expand Down Expand Up @@ -384,16 +401,19 @@ public struct DirectionalCell
{
private readonly T _cell;
private readonly Direction _direction;
private readonly int _value;

public DirectionalCell(T cell, Direction direction)
public DirectionalCell(T cell, Direction direction, int value)
{
_cell = cell;
_direction = direction;
_value = value;
}

public DirectionalCell(T cell, T nextCell)
public DirectionalCell(T cell, T nextCell, int value)
{
_cell = cell;
_value = value;
if (Equals(cell, default(T)))
{
_direction = Direction.Stay;
Expand Down Expand Up @@ -440,6 +460,25 @@ public Direction Direction
{
get { return _direction; }
}

public int Value
{
get { return _value; }
}

public float ToRad()
{
if (_direction == Direction.Stay)
return 0f;
return (float)((int)_direction * (Math.PI / 180));
}

public static float ToRad(Direction dir)
{
if (dir == Direction.Stay)
return 0f;
return (float)((int)dir * (Math.PI / 180));
}
}

public class NodeProxy<U>
Expand Down Expand Up @@ -477,7 +516,7 @@ public class Node<U>
public U Cell { get; private set; }
public int Value { get; private set; }

public Node(int x, int y)
public Node() //int x, int y)
{
Parent = null;
Cell = default(U);
Expand All @@ -497,9 +536,10 @@ public void Modify(Node<U> parent, U cell, int value)
private readonly T[,] _cells;
private readonly int _width, _height;
private readonly CreateCell _createCell;
private int _total;
private readonly int _total;

public delegate bool CanWalk<in TpathCell>(TpathCell first, TpathCell second, out int value);
public delegate bool CanWalk<in TPathCell>(TPathCell first, TPathCell second, out int value)
where TPathCell : T, IPathCell;

/// <summary>
/// Délégate de création de cellules
Expand Down Expand Up @@ -607,7 +647,7 @@ public IEnumerable<DirectionalCell> AStar<TPathCell>(TPathCell start, TPathCell
Node<TPathCell>[,] map = new Node<TPathCell>[Width, Height];
for (int x = 0; x < Width; ++x)
for (int y = 0; y < Height; ++y)
map[x, y] = new Node<TPathCell>(x, y);
map[x, y] = new Node<TPathCell>();

Node<TPathCell> startNode = map[start.X, start.Y];
startNode.Modify(null, start, 0);
Expand All @@ -617,10 +657,10 @@ public IEnumerable<DirectionalCell> AStar<TPathCell>(TPathCell start, TPathCell
IList<DirectionalCell> result = new List<DirectionalCell>();
var endNode = map[end.X, end.Y];
if (endNode.Parent != null)
result.Add(new DirectionalCell(endNode.Cell, Direction.Stay));
result.Add(new DirectionalCell(endNode.Cell, Direction.Stay, endNode.Value));
for (Node<TPathCell> n = endNode; n != null; n = n.Parent)
if (!Equals(n.Cell, default(TPathCell)) && n.Parent != null)
result.Add(new DirectionalCell(n.Parent.Cell, n.Cell));
result.Add(new DirectionalCell(n.Parent.Cell, n.Cell, n.Value));
if (result.Count == 0)
throw new Exception("Impossible de trouver un chemin");
return result.Reverse();
Expand Down
12 changes: 12 additions & 0 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ namespace CSharpHelper
{
public static class Helper
{
public static TResult IfNotNull<TSource, TResult>(this TSource source, Func<TSource, TResult> action)
{
return Equals(source, default(TSource)) ? default(TResult) : action(source);
}

public static void IfNotNull<TSource>(this TSource source, Action<TSource> action)
{
if (Equals(source, default(TSource)))
return;
action(source);
}

public static bool IsInArray<T>(this T[] array, int value)
{
return value >= 0 && value < array.Length;
Expand Down

0 comments on commit c933935

Please sign in to comment.