forked from overengineering/sharpasonne
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partially implements AdjacentFeaturesMatchRule for top-bottom placeme…
…nts.
- Loading branch information
Lee Richardson
committed
Jan 24, 2018
1 parent
56da050
commit 9e69bfd
Showing
5 changed files
with
180 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Sharpasonne.GameActions; | ||
using Sharpasonne.Models; | ||
using Sharpasonne.Models.Features; | ||
using Sharpasonne.Rules; | ||
using Xunit; | ||
using System.Collections.Immutable; | ||
using Optional.Unsafe; | ||
|
||
namespace Sharpasonne.Tests.Rules | ||
{ | ||
public class AdjacentFeaturesMatchRuleTest : UnitTest<PlaceTileGameAction> | ||
{ | ||
[Fact] | ||
public void Given_Empty_Then_True() | ||
{ | ||
AssertTrue<AdjacentFeaturesMatchRule>(new Engine(), MakePlaceTile(0, 0)); | ||
} | ||
|
||
[Fact] | ||
public void Given_OneNeighbour_When_FeaturesMatch_Then_True() | ||
{ | ||
var aboveTile = new TileBuilder() | ||
.CreateTile(new [] | ||
{ | ||
new Field(ImmutableHashSet.Create( | ||
Segment.BottomLeft, | ||
Segment.Bottom, | ||
Segment.BottomRight | ||
)), | ||
}) | ||
.ValueOrFailure(); | ||
|
||
var belowTile = new TileBuilder() | ||
.CreateTile(new [] | ||
{ | ||
new Field(ImmutableHashSet.Create( | ||
Segment.TopLeft, | ||
Segment.Top, | ||
Segment.TopRight | ||
)), | ||
}) | ||
.ValueOrFailure(); | ||
|
||
var aboveAction = MakePlaceTile(0, 1, aboveTile); | ||
var belowAction = MakePlaceTile(0, 0, belowTile); | ||
var board = MakeBoard(aboveAction); | ||
var engine = MockEngine(board); | ||
|
||
AssertTrue<AdjacentFeaturesMatchRule>(engine, belowAction); | ||
} | ||
|
||
[Fact] | ||
public void Given_OneNeighbour_When_FeaturesDoNotMatch_Then_False() | ||
{ | ||
var aboveTile = new TileBuilder() | ||
.CreateTile(new IFeature[] | ||
{ | ||
new City( | ||
ImmutableHashSet.Create(Segment.BottomLeft), | ||
hasShield: true | ||
), | ||
new Field(ImmutableHashSet.Create( | ||
Segment.Bottom, | ||
Segment.BottomRight | ||
)), | ||
}) | ||
.ValueOrFailure(); | ||
|
||
var belowTile = new TileBuilder() | ||
.CreateTile(new[] | ||
{ | ||
new Field(ImmutableHashSet.Create( | ||
Segment.TopLeft, | ||
Segment.Top, | ||
Segment.TopRight | ||
)), | ||
}) | ||
.ValueOrFailure(); | ||
|
||
var aboveAction = MakePlaceTile(0, 1, aboveTile); | ||
var belowAction = MakePlaceTile(0, 0, belowTile); | ||
var board = MakeBoard(aboveAction); | ||
var engine = MockEngine(board); | ||
|
||
AssertFalse<AdjacentFeaturesMatchRule>(engine, belowAction); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
using Sharpasonne.GameActions; | ||
using Sharpasonne.Models; | ||
using System.Collections.Generic; | ||
|
||
namespace Sharpasonne.Rules | ||
{ | ||
public class AdjacentFeaturesMatchRule : IRule<PlaceTileGameAction> | ||
{ | ||
public bool Verify<T1>(IEngine engine, T1 action) | ||
where T1 : PlaceTileGameAction | ||
{ | ||
var adjacent = engine.Board.GetAdjecentTiles(action.Point); | ||
var allMatch = adjacent | ||
.Where(o => o.Value.HasValue) | ||
.Select(o => ( | ||
o.Key, | ||
o.Value.ValueOr(null as Placement) | ||
)) | ||
.All(o => | ||
{ | ||
(var direction, var placement) = o; | ||
switch (direction) | ||
{ | ||
case Orientation.Top: | ||
return this.EdgesMatch( | ||
placement.Tile.GetEdge(Orientation.Bottom), | ||
action.Placement.Tile.GetEdge(Orientation.Top) | ||
); | ||
default: | ||
throw new NotImplementedException(); | ||
} | ||
}); | ||
|
||
return allMatch; | ||
} | ||
|
||
protected bool EdgesMatch( | ||
IFeature[] from, | ||
IFeature[] to | ||
) | ||
{ | ||
var fromTypes = from.Select(feature => feature.GetType()); | ||
var toTypes = to.Select(feature => feature.GetType()); | ||
|
||
return fromTypes.SequenceEqual(toTypes); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.