From 6d0ec2e0232c076d9c300607f6d6e42ef2a9bcea Mon Sep 17 00:00:00 2001 From: Lee Richardson Date: Thu, 8 Feb 2018 19:15:03 +0000 Subject: [PATCH] Implements checks for north, south, east and west. --- Sharpasonne.Models/Tile.cs | 24 ++++++++--- .../Rules/AdjacentFeaturesMatchRuleTest.cs | 40 ++++++++++++++++++- .../Rules/AdjacentFeaturesMatchRule.cs | 29 ++++++++++++-- 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/Sharpasonne.Models/Tile.cs b/Sharpasonne.Models/Tile.cs index 7a7b9b8..91c83c7 100644 --- a/Sharpasonne.Models/Tile.cs +++ b/Sharpasonne.Models/Tile.cs @@ -33,11 +33,11 @@ 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[] { @@ -45,6 +45,20 @@ protected Segment[] GetSegments(Orientation direction) 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(); } diff --git a/Sharpasonne.Tests/Rules/AdjacentFeaturesMatchRuleTest.cs b/Sharpasonne.Tests/Rules/AdjacentFeaturesMatchRuleTest.cs index f8a94d5..cb0a0e2 100644 --- a/Sharpasonne.Tests/Rules/AdjacentFeaturesMatchRuleTest.cs +++ b/Sharpasonne.Tests/Rules/AdjacentFeaturesMatchRuleTest.cs @@ -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 [] @@ -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[] @@ -86,5 +86,41 @@ public void Given_OneNeighbour_When_FeaturesDoNotMatch_Then_False() AssertFalse(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(engine, rightAction); + } } } \ No newline at end of file diff --git a/Sharpasonne/Rules/AdjacentFeaturesMatchRule.cs b/Sharpasonne/Rules/AdjacentFeaturesMatchRule.cs index 4a970b4..e80e929 100644 --- a/Sharpasonne/Rules/AdjacentFeaturesMatchRule.cs +++ b/Sharpasonne/Rules/AdjacentFeaturesMatchRule.cs @@ -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 { @@ -17,7 +16,7 @@ public bool Verify(IEngine engine, T1 action) .Where(o => o.Value.HasValue) .Select(o => ( o.Key, - o.Value.ValueOr(null as Placement) + o.Value.ValueOrFailure() )) .All(o => { @@ -26,10 +25,34 @@ public bool Verify(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(); }