Skip to content

Commit

Permalink
Partially implements AdjacentFeaturesMatchRule for top-bottom placeme…
Browse files Browse the repository at this point in the history
…nts.
  • Loading branch information
Lee Richardson committed Jan 24, 2018
1 parent 56da050 commit 9e69bfd
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 11 deletions.
37 changes: 37 additions & 0 deletions Sharpasonne.Models/Tile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;

namespace Sharpasonne.Models
Expand All @@ -12,5 +14,40 @@ internal Tile([NotNull] IEnumerable<IFeature> features)
{
this.Features = features.ToImmutableList();
}

public IFeature[] GetEdge(Orientation direction)
{
var segments = this.GetSegments(direction);
var features = segments.Select(segment =>
this.Features.First(feature =>
feature.Connections.Contains(segment)
)
);

return features.ToArray();
}

protected Segment[] GetSegments(Orientation direction)
{
switch (direction)
{
case Orientation.Top:
return new[]
{
Segment.TopLeft,
Segment.Top,
Segment.TopRight,
};
case Orientation.Bottom:
return new[]
{
Segment.BottomLeft,
Segment.Bottom,
Segment.BottomRight,
};
default:
throw new NotImplementedException();
}
}
}
}
90 changes: 90 additions & 0 deletions Sharpasonne.Tests/Rules/AdjacentFeaturesMatchRuleTest.cs
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);
}
}
}
2 changes: 1 addition & 1 deletion Sharpasonne/IRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sharpasonne
{
public interface IRule<T2> where T2: IGameAction
public interface IRule<T2> where T2 : IGameAction
{
bool Verify<T1>(IEngine engine, T1 gameAction)
where T1 : T2 ;
Expand Down
52 changes: 52 additions & 0 deletions Sharpasonne/Rules/AdjacentFeaturesMatchRule.cs
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);
}
}
}
10 changes: 0 additions & 10 deletions Sharpasonne/Rules/MatchingAdjecentFeatureRule.cs

This file was deleted.

0 comments on commit 9e69bfd

Please sign in to comment.