Enum for four cardinal directions used by the grid (Up, Right, Down, Left) and extension methods that provide a grid offset (Delta), the opposite direction (Opposite), and an IsOpposite helper.
namespace Coilgarden;
/// <summary>The four headings a snake can travel. Ordered clockwise from up.</summary>
public enum Direction
{
Up,
Right,
Down,
Left
}
public static class DirectionExtensions
{
/// <summary>The cell offset one step in this direction.</summary>
public static GridPos Delta( this Direction direction ) => direction switch
{
Direction.Up => new GridPos( 0, 1 ),
Direction.Right => new GridPos( 1, 0 ),
Direction.Down => new GridPos( 0, -1 ),
Direction.Left => new GridPos( -1, 0 ),
_ => GridPos.Zero
};
public static Direction Opposite( this Direction direction ) => direction switch
{
Direction.Up => Direction.Down,
Direction.Right => Direction.Left,
Direction.Down => Direction.Up,
Direction.Left => Direction.Right,
_ => direction
};
/// <summary>True when travelling <paramref name="other"/> would be a reversal.</summary>
public static bool IsOpposite( this Direction direction, Direction other ) =>
direction.Opposite() == other;
}