Skip to content

Commit

Permalink
Implements UniqueTileInstanceRule and adds a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Richardson committed Jan 24, 2018
1 parent c4c9f8b commit 06a860d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
26 changes: 24 additions & 2 deletions Sharpasonne.Tests/Rules/UniqueTileInstanceRuleTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Sharpasonne.GameActions;
using System.Collections.Generic;
using System.Linq;
using Sharpasonne.GameActions;
using Sharpasonne.Models;
using Sharpasonne.Rules;
using Xunit;

Expand All @@ -9,7 +12,26 @@ public class UniqueTileInstanceRuleTests : UnitTest<PlaceTileGameAction>
[Fact]
public void When_BoardIsEmpty_Then_True()
{
AssertTrue<UniqueTileInstanceRule>(new Engine(), MakePlaceTile(0, 0));
this.AssertTrue<UniqueTileInstanceRule>(new Engine(), this.MakePlaceTile(0, 0));
}

[Fact]
public void Given_BoardHasInstanceofTile_When_PlaceSameInstanceAgain_Then_False()
{
var action = MakePlaceTile(0, 0);
var board = new[] { action }.ToDictionary(
a => a.Point,
a => a.Placement
);

var engine = this.MockEngine(new Board(board));

var secondPlaceTileGameAction = new PlaceTileGameAction(
new Point(0, 0),
new Placement(action.Placement.Tile, Orientation.Top)
);

this.AssertFalse<UniqueTileInstanceRule>(engine, secondPlaceTileGameAction);
}
}
}
9 changes: 8 additions & 1 deletion Sharpasonne/Rules/UniqueTileInstanceRule.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Sharpasonne.GameActions;

namespace Sharpasonne.Rules
Expand All @@ -6,7 +7,13 @@ public class UniqueTileInstanceRule : IRule<PlaceTileGameAction>
{
public bool Verify<T1>(IEngine engine, T1 gameAction) where T1 : PlaceTileGameAction
{
return true;
var allTiles = engine.Board
.ToImmutableDictionary()
.Values
.Select(placement => placement.Tile);

return !allTiles
.Contains(gameAction.Placement.Tile);
}
}
}

0 comments on commit 06a860d

Please sign in to comment.