Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add hive dismount to beetle move finder

  • Loading branch information...
commit e1b33464d91f60e8dd29624f907784679de64b99 1 parent 2dc98ac
@chellmuth authored
View
9 lib/gamestate.dart
@@ -103,6 +103,15 @@ class GameState {
return tile.coordinate;
}
+ int getHeight(Piece piece) {
+ var location = locate(piece);
+ List<Piece> stack = stackAt(location);
+ for (var i = 0; i < stack.length; i++) {
+ if (stack[i] == piece) { return i + 1; }
+ }
+ throw new Exception('Unplayed piece');
+ }
+
bool isLocationEmpty(Coordinate location) {
return pieceAt(location) == null;
}
View
28 lib/move_finder.dart
@@ -27,13 +27,31 @@ bool _checkSlideRulesOnTransition(List<Coordinate> transition, GameState gamesta
return true;
}
+class DismountHiveMoveFinder {
+ static List<Move> findMoves(Piece piece, GameState gamestate) {
+ List<Move> moves = [];
+ List<Tile> tiles = gamestate.toList();
+ var tile = tiles.firstWhere((tile) => tile.piece == piece);
+ if (tile.height == 1) { return []; }
+
+ for (Direction direction in Direction.all()) {
+ Coordinate adjacentLocation = tile.coordinate.applyDirection(direction);
+ if (gamestate.isLocationEmpty(adjacentLocation)) {
+ Move move = new Move(piece, tile.coordinate, adjacentLocation);
+ moves.add(move);
+ }
+ }
+ return moves;
+ }
+}
+
class ClimbHiveMoveFinder {
static List<Move> findMoves(Piece piece, GameState gamestate) {
List<Move> moves = [];
List<Tile> tiles = gamestate.toList();
var tile = tiles.firstWhere((tile) => tile.piece == piece);
if (tile.height > 1) { return []; }
-
+
for (Tile neighbor in gamestate.neighbors(tile.coordinate)) {
Move move = new Move(piece, tile.coordinate, neighbor.coordinate);
if (checkOneHiveRule(move, gamestate)) {
@@ -63,7 +81,7 @@ class JumpMoveFinder {
moves.add(new Move(piece, tile.coordinate, targetCoordinate));
}
}
-
+
return moves.where((move) => checkOneHiveRule(move, gamestate)).toList();
}
}
@@ -125,7 +143,7 @@ class RangedSlideMoveFinder {
}
return transitionLists;
}
-
+
static List<List<Coordinate>> _buildTransitionLists(int range, Piece piece, GameState gamestate) {
gamestate = gamestate.copy();
@@ -159,9 +177,9 @@ class RangedSlideMoveFinder {
static List<Move> findMoves(int range, Piece piece, GameState gamestate) {
var startingLocation = gamestate.locate(piece);
- List<List<Coordinate>> transitionLists = _buildTransitionLists(range, piece, gamestate);
+ List<List<Coordinate>> transitionLists = _buildTransitionLists(range, piece, gamestate);
Set<Coordinate> moveLocations = _buildMoveLocationsFromTransitionLists(transitionLists, gamestate);
- return moveLocations.map((location) => new Move(piece, startingLocation, location)).toList();
+ return moveLocations.map((location) => new Move(piece, startingLocation, location)).toList();
}
}
View
11 lib/piece_impl.dart
@@ -12,8 +12,15 @@ class Beetle extends Piece {
Beetle._internal(player, bugCount) : super._internal(player, Bug.BEETLE, bugCount);
List<Move> moves(GameState gamestate) {
- var moves = RangedSlideMoveFinder.findMoves(1, this, gamestate);
- moves.addAll(ClimbHiveMoveFinder.findMoves(this, gamestate));
+ var moves = [];
+
+ int height = gamestate.getHeight(this);
+ if (height == 1) {
+ moves.addAll(RangedSlideMoveFinder.findMoves(1, this, gamestate));
+ moves.addAll(ClimbHiveMoveFinder.findMoves(this, gamestate));
+ } else {
+ moves.addAll(DismountHiveMoveFinder.findMoves(this, gamestate));
+ }
return moves;
}
}
View
42 test/move_finder_test.dart
@@ -6,6 +6,7 @@ class TestMoveFinder {
group('Jump Finder:', _jumpFinder);
group('Ranged Slide Finder:', _rangedSlideFinder);
group('Climb Hive Finder:', _climbHiveFinder);
+ group('Dismount Hive Finder:', _dismountHiveFinder);
});
}
@@ -216,7 +217,7 @@ class TestMoveFinder {
expect(moves.map((move) => move.targetLocation).toList(), equals(moveCoordinates));
});
}
-
+
static void _climbHiveFinder() {
test('climb onto neighboring pieces', () {
var _wB_ = new Piece(Player.WHITE, Bug.BEETLE, 1);
@@ -247,10 +248,47 @@ class TestMoveFinder {
Piece piece = _wB_;
gamestate.appendMove(new Move(piece, new Coordinate(1, 1), new Coordinate(0, 1)));
gamestate.stepToEnd();
-
+
var moves = ClimbHiveMoveFinder.findMoves(piece, gamestate);
expect(moves.isEmpty, isTrue);
});
+ }
+
+ static void _dismountHiveFinder() {
+ test('empty if not on hive', () {
+ var _wB_ = new Piece(Player.WHITE, Bug.BEETLE, 1);
+ var gamestate = GameStateTestHelper.build([
+ [ ' ', 'wG' ],
+ [ ' ', _wB_ ]
+ ]);
+ gamestate.stepToEnd();
+
+ Piece piece = _wB_;
+
+ var moves = DismountHiveMoveFinder.findMoves(piece, gamestate);
+ expect(moves.isEmpty, isTrue);
+ });
+
+ test('returns empty adjacent moves', () {
+ var _wB_ = new Piece(Player.WHITE, Bug.BEETLE, 1);
+ var gamestate = GameStateTestHelper.build([
+ [ ' ', 'wG', '__' ],
+ [ 'wG', 'wG', '__' ],
+ [ ' ', _wB_, 'wG' ]
+ ]);
+ gamestate.stepToEnd();
+
+ Piece piece = _wB_;
+ gamestate.appendMove(new Move(piece, new Coordinate(2, 1), new Coordinate(1, 1)));
+ gamestate.stepToEnd();
+ var moves = DismountHiveMoveFinder.findMoves(piece, gamestate);
+ var moveCoordinates = [
+ new Coordinate(0, 2),
+ new Coordinate(1, 2),
+ new Coordinate(2, 1),
+ ];
+ expect(moves.map((move) => move.targetLocation).toList(), equals(moveCoordinates));
+ });
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.