Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix bug where Ant could violate One Hive rule

  • Loading branch information...
commit 98f61b6e3be4dc5c4abb129a89e3ded16d93caf0 1 parent 605c934
@chellmuth authored
View
50 lib/gamemodel.dart
@@ -19,7 +19,7 @@ class Player {
case '0':
case 'w':
case 'W': return Player.WHITE;
-
+
case '1':
case 'b':
case 'B': return Player.BLACK;
@@ -39,21 +39,21 @@ class Bug {
static const BEETLE = const Bug._internal('BEETLE');
static const SPIDER = const Bug._internal('SPIDER');
static const GRASSHOPPER = const Bug._internal('GRASSHOPPER');
-
+
static parse(String bug) {
switch(bug) {
case 'a':
case 'A': return Bug.ANT;
-
+
case 'q':
case 'Q': return Bug.QUEEN;
-
+
case 'b':
case 'B': return Bug.BEETLE;
-
+
case 's':
case 'S': return Bug.SPIDER;
-
+
case 'g':
case 'G': return Bug.GRASSHOPPER;
}
@@ -65,9 +65,9 @@ class Piece {
Player player;
Bug bug;
int bugCount;
-
+
Piece._internal(this.player, this.bug, this.bugCount);
-
+
factory Piece(player, bug, bugCount) {
switch (bug) {
case Bug.ANT:
@@ -82,10 +82,10 @@ class Piece {
return new Spider._internal(player, bugCount);
}
}
-
+
bool operator ==(other) {
if (other is !Piece) { return false; }
- return player == other.player && bug == other.bug && bugCount == other.bugCount;
+ return player == other.player && bug == other.bug && bugCount == other.bugCount;
}
int get hashCode {
@@ -95,6 +95,8 @@ class Piece {
List<Move> moves(GameState gamestate) {
return RangedSlideMoveFinder.findMoves(3, this, gamestate);
}
+
+ String toString() => "Piece: (${player}, ${bug}, ${bugCount})";
}
class Direction {
@@ -109,9 +111,9 @@ class Direction {
static const LEFT = const Direction._internal('LEFT');
static const UP_LEFT = const Direction._internal('UP_LEFT');
static const ABOVE = const Direction._internal('ABOVE');
-
+
static List<Direction> all() {
- return [
+ return [
Direction.UP_RIGHT,
Direction.RIGHT,
Direction.DOWN_RIGHT,
@@ -121,7 +123,7 @@ class Direction {
Direction.ABOVE
];
}
-
+
List<Coordinate> adjacentDirections() {
switch (this) {
case UP_RIGHT: return [ UP_LEFT, RIGHT ];
@@ -138,7 +140,7 @@ class Direction {
class Coordinate {
num row, col;
Coordinate(this.row, this.col);
-
+
Coordinate applyDirection(Direction direction) {
switch(direction) {
case Direction.UP_LEFT:
@@ -157,16 +159,16 @@ class Coordinate {
return new Coordinate(row, col);
}
}
-
+
bool isAdjacent(Coordinate other) {
for (var direction in Direction.all()) {
if (direction == Direction.ABOVE) { continue; }
-
+
if (applyDirection(direction) == other) { return true; }
}
return false;
}
-
+
Direction direction(Coordinate other) {
for (var direction in Direction.all()) {
if (applyDirection(direction) == other) {
@@ -188,7 +190,7 @@ class Coordinate {
String toString() {
return '(${row}, ${col})';
}
-
+
bool get _evenRow => row % 2 == 0;
}
@@ -196,26 +198,26 @@ class Tile {
num row, col;
Piece piece;
int height;
-
+
Tile(this.row, this.col, this.piece, { this.height: 0 });
-
+
bool operator ==(other) {
if (other is !Tile) { return false; }
- return row == other.row && col == other.col && piece == other.piece;
+ return row == other.row && col == other.col && piece == other.piece;
}
int get hashCode {
return row.hashCode ^ col.hashCode ^ piece.hashCode;
}
-
+
Coordinate get coordinate => new Coordinate(row, col);
}
class GameEvent {
Piece piece;
-
+
Piece relativePiece;
Direction direction;
-
+
GameEvent(this.piece, this.relativePiece, this.direction);
}
View
2  lib/move_finder.dart
@@ -132,7 +132,7 @@ class SlideMoveFinder {
) {
var originalGamestate = gamestate;
gamestate = gamestate.copy();
- gamestate.appendMove(new Move(gamestate.pieceAt(startingLocation), startingLocation, newLocation));
+ gamestate.appendMove(new Move(gamestate.pieceAt(startingLocation), startingLocation, location));
gamestate.stepBy(1);
if(_checkSlideRulesOnTransition([ location, newLocation ], gamestate)) {
newLocations.add(newLocation);
View
12 lib/rules.dart
@@ -7,17 +7,21 @@ class Move {
Piece piece;
Coordinate currentLocation;
Coordinate targetLocation;
-
+
Move(this.piece, this.currentLocation, this.targetLocation);
bool operator ==(other) {
if (other is !Move) { return false; }
- return piece == other.piece && currentLocation == other.currentLocation && targetLocation == other.targetLocation;
+ return piece == other.piece && currentLocation == other.currentLocation && targetLocation == other.targetLocation;
}
int get hashCode {
return piece.hashCode ^ currentLocation.hashCode ^ targetLocation.hashCode;
}
+
+ String toString() {
+ return "${this.piece} from ${currentLocation} to ${targetLocation}";
+ }
}
List<Move> validMoves(GameState gamestate) {
@@ -28,12 +32,12 @@ bool checkOneHiveRule(Move move, GameState gamestate) {
gamestate = gamestate.copy();
gamestate.appendMove(move);
gamestate.stepBy(1);
-
+
var tiles = gamestate.toList();
if (tiles.length == 1) { return true; }
var checkedPieces = new Set<Piece>();
-
+
void addNeighbors(Tile tile) {
checkedPieces.add(tile.piece);
for (Piece stackMate in gamestate.stackAt(tile.coordinate)) {
View
29 test/move_finder_test.dart
@@ -5,6 +5,7 @@ class TestMoveFinder {
group('Move Finder:', () {
group('Jump Finder:', _jumpFinder);
group('Ranged Slide Finder:', _rangedSlideFinder);
+ group('Slide Finder:', _slideFinder);
group('Climb Hive Finder:', _climbHiveFinder);
group('Dismount Hive Finder:', _dismountHiveFinder);
group('Atop Hive Finder:', _atopHiveFinder);
@@ -355,4 +356,32 @@ class TestMoveFinder {
expect(moves.map((move) => move.targetLocation).toList(), equals(moveCoordinates));
});
}
+
+ static void _slideFinder() {
+ test('respects one hive rule', () {
+ var _wA_ = new Piece(Player.BLACK, Bug.ANT, 1);
+ var gamestate = GameStateTestHelper.build([
+ [ ' ', ' ', 'bA' ],
+ [ '__', _wA_, 'bA' ]
+ ]);
+ gamestate.stepToEnd();
+ var piece = _wA_;
+ var moves = SlideMoveFinder.findMoves(piece, gamestate);
+ expect(moves.firstWhere((move) => move.targetLocation == new Coordinate(1, 0), orElse: () => null), isNull);
+ });
+
+ test('respects dangling piece one hive rule', () {
+ var _wA_ = new Piece(Player.BLACK, Bug.ANT, 1);
+ var gamestate = GameStateTestHelper.build([
+ [ ' ', ' ', 'bA', ' ' ],
+ [ 'wG', ' ', _wA_, ' ' ],
+ [ ' ', 'wG', ' ', 'wG' ],
+ [ ' ', 'wG', 'wG', ' ' ]
+ ]);
+ gamestate.stepToEnd();
+ var piece = _wA_;
+ var moves = SlideMoveFinder.findMoves(piece, gamestate);
+ expect(moves.isEmpty, isTrue);
+ });
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.