Skip to content

Commit

Permalink
Implements checks for north, south, east and west.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Richardson committed Feb 8, 2018
1 parent 9e69bfd commit 6d0ec2e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
24 changes: 19 additions & 5 deletions Sharpasonne.Models/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,32 @@ protected Segment[] GetSegments(Orientation direction)
{
case Orientation.Top:
return new[]
{
Segment.TopLeft,
Segment.Top,
Segment.TopRight,
};
{
Segment.TopLeft,
Segment.Top,
Segment.TopRight,
};
case Orientation.Bottom:
return new[]
{
Segment.BottomLeft,
Segment.Bottom,
Segment.BottomRight,
};
case Orientation.Left:
return new[]
{
Segment.LeftTop,
Segment.Left,
Segment.LeftBottom,
};
case Orientation.Right:
return new[]
{
Segment.RightTop,
Segment.Right,
Segment.RightBottom,
};
default:
throw new NotImplementedException();
}
Expand Down
40 changes: 38 additions & 2 deletions Sharpasonne.Tests/Rules/AdjacentFeaturesMatchRuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Given_Empty_Then_True()
}

[Fact]
public void Given_OneNeighbour_When_FeaturesMatch_Then_True()
public void Given_OneSouthernNeighbour_When_FeaturesMatch_Then_True()
{
var aboveTile = new TileBuilder()
.CreateTile(new []
Expand Down Expand Up @@ -52,7 +52,7 @@ public void Given_OneNeighbour_When_FeaturesMatch_Then_True()
}

[Fact]
public void Given_OneNeighbour_When_FeaturesDoNotMatch_Then_False()
public void Given_OneSouthernNeighbour_When_FeaturesDoNotMatch_Then_False()
{
var aboveTile = new TileBuilder()
.CreateTile(new IFeature[]
Expand Down Expand Up @@ -86,5 +86,41 @@ public void Given_OneNeighbour_When_FeaturesDoNotMatch_Then_False()

AssertFalse<AdjacentFeaturesMatchRule>(engine, belowAction);
}

[Fact]
public void Given_OneEasternNeighbour_When_FeaturesDoNotMatch_Then_False()
{
var leftTile = new TileBuilder()
.CreateTile(new IFeature[]
{
new City(
ImmutableHashSet.Create(Segment.RightBottom),
hasShield: true
),
new Field(ImmutableHashSet.Create(
Segment.Right,
Segment.RightTop
)),
})
.ValueOrFailure();

var rightTile = new TileBuilder()
.CreateTile(new[]
{
new Field(ImmutableHashSet.Create(
Segment.LeftTop,
Segment.Left,
Segment.LeftBottom
)),
})
.ValueOrFailure();

var leftAction = MakePlaceTile(0, 0, leftTile);
var rightAction = MakePlaceTile(1, 0, rightTile);
var board = MakeBoard(leftAction);
var engine = MockEngine(board);

AssertFalse<AdjacentFeaturesMatchRule>(engine, rightAction);
}
}
}
29 changes: 26 additions & 3 deletions Sharpasonne/Rules/AdjacentFeaturesMatchRule.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections;
using System.Linq;
using Sharpasonne.GameActions;
using Sharpasonne.Models;
using System.Collections.Generic;
using Optional.Unsafe;

namespace Sharpasonne.Rules
{
Expand All @@ -17,7 +16,7 @@ public bool Verify<T1>(IEngine engine, T1 action)
.Where(o => o.Value.HasValue)
.Select(o => (
o.Key,
o.Value.ValueOr(null as Placement)
o.Value.ValueOrFailure()
))
.All(o =>
{
Expand All @@ -26,10 +25,34 @@ public bool Verify<T1>(IEngine engine, T1 action)
switch (direction)
{
case Orientation.Top:
{
return this.EdgesMatch(
placement.Tile.GetEdge(Orientation.Bottom),
action.Placement.Tile.GetEdge(Orientation.Top)
);
}
case Orientation.Bottom:
{
return this.EdgesMatch(
placement.Tile.GetEdge(Orientation.Top),
action.Placement.Tile.GetEdge(Orientation.Bottom)
);
}
case Orientation.Left:
{
return this.EdgesMatch(
placement.Tile.GetEdge(Orientation.Right),
action.Placement.Tile.GetEdge(Orientation.Left)
);
}
case Orientation.Right:
{
return this.EdgesMatch(
placement.Tile.GetEdge(Orientation.Left),
action.Placement.Tile.GetEdge(Orientation.Right)
);
}
default:
throw new NotImplementedException();
}
Expand Down

0 comments on commit 6d0ec2e

Please sign in to comment.