private void GetValidSlides(PieceName target, Position currentPosition, HashSet <Position> visitedPositions, int currentRange, int?maxRange, MoveSet validMoves) { if (!maxRange.HasValue || currentRange < maxRange.Value) { for (int slideDirection = 0; slideDirection < EnumUtils.NumDirections; slideDirection++) { Position slidePosition = currentPosition.NeighborAt(slideDirection); if (!visitedPositions.Contains(slidePosition) && !HasPieceAt(slidePosition)) { // Slide position is open int right = EnumUtils.RightOf(slideDirection); int left = EnumUtils.LeftOf(slideDirection); if (HasPieceAt(currentPosition.NeighborAt(right)) != HasPieceAt(currentPosition.NeighborAt(left))) { // Can slide into slide position Move move = new Move(target, slidePosition); if (validMoves.Add(move)) { // Sliding from this position has not been tested yet visitedPositions.Add(move.Position); GetValidSlides(target, slidePosition, visitedPositions, currentRange + 1, maxRange, validMoves); } } } } } }
private MoveSet GetValidBeetleMovements(Piece targetPiece) { MoveSet validMoves = new MoveSet(); // Look in all directions for (int direction = 0; direction < EnumUtils.NumDirections; direction++) { Position newPosition = targetPiece.Position.NeighborAt(direction); Piece topNeighbor = GetPieceOnTopInternal(newPosition); // Get positions to left and right or direction we're heading int leftOfTarget = EnumUtils.LeftOf(direction); int rightOfTarget = EnumUtils.RightOf(direction); Position leftNeighborPosition = targetPiece.Position.NeighborAt(leftOfTarget); Position rightNeighborPosition = targetPiece.Position.NeighborAt(rightOfTarget); Piece topLeftNeighbor = GetPieceOnTopInternal(leftNeighborPosition); Piece topRightNeighbor = GetPieceOnTopInternal(rightNeighborPosition); // At least one neighbor is present uint currentHeight = targetPiece.Position.Stack + 1; uint destinationHeight = null != topNeighbor ? topNeighbor.Position.Stack + 1 : 0; uint topLeftNeighborHeight = null != topLeftNeighbor ? topLeftNeighbor.Position.Stack + 1 : 0; uint topRightNeighborHeight = null != topRightNeighbor ? topRightNeighbor.Position.Stack + 1 : 0; // "Take-off" beetle currentHeight--; if (!(currentHeight == 0 && destinationHeight == 0 && topLeftNeighborHeight == 0 && topRightNeighborHeight == 0)) { // Logic from http://boardgamegeek.com/wiki/page/Hive_FAQ#toc9 if (!(destinationHeight < topLeftNeighborHeight && destinationHeight < topRightNeighborHeight && currentHeight < topLeftNeighborHeight && currentHeight < topRightNeighborHeight)) { Position targetPosition = (newPosition.Stack == destinationHeight) ? newPosition : topNeighbor.Position.GetAbove(); Move targetMove = new Move(targetPiece.PieceName, targetPosition); validMoves.Add(targetMove); } } } return(validMoves); }