private static NeighborStatus GetNeighborStatus(Board board, Hex hex, Position pos) { Hex tr_hex = Neighborhood.GetNeighborHex(hex, pos); Piece tr_piece; if (board.TryGetPieceAtHex(tr_hex, out tr_piece)) { if (tr_piece.color == PieceColor.Black) { return(NeighborStatus.Black); } else if (tr_piece.color == PieceColor.White) { return(NeighborStatus.White); } else { throw new Exception(string.Format("Bad color status at {0},{1}", hex.column, hex.row)); } } else { return(NeighborStatus.Empty); } }
internal IList <Hex> BlockedNeighborHexes(Hex center) { List <Hex> blockedNeighbors = new List <Hex>(); for (int i = 1; i < 7; i++) { Hex neighborHex = Neighborhood.GetNeighborHex(center, (Position)i); if (_isBlocked[i]) { blockedNeighbors.Add(neighborHex); } } return(blockedNeighbors); }
internal IList <Hex> NonEmptyNeighborHexes(Hex center) { List <Hex> neighbors = new List <Hex>(); for (int i = 1; i < 7; i++) { Hex neighborHex = Neighborhood.GetNeighborHex(center, (Position)i); if (_neighborStatus[i] != NeighborStatus.Empty) { neighbors.Add(neighborHex); } } return(neighbors); }
/// <summary> /// Produces valid notation, but does not validate the move for the given board. /// </summary> /// <param name="move"></param> /// <param name="board"></param> /// <returns></returns> internal static string GetNotationForMove(Move move, Board board) { //TODO fix problem where beetle moves generate a "next to myself" reference piece string referencePieceNotation; string targetPieceNotation = GetNotationForPiece(move.pieceToMove); if (board.hivailableSpaces.Count == 1) { referencePieceNotation = "."; } else if (move.referencePiece != null) { referencePieceNotation = GetNotationForPiece(move.referencePiece); referencePieceNotation = string.Format(Neighborhood.neighborDirectionNotationTemplates[(int)move.targetPosition], referencePieceNotation); } else if (!move.hex.Equals(Board.invalidHex)) { Piece refPiece = null; if (board.TryGetPieceAtHex(move.hex, out refPiece)) { // assume this is a beetle moving on top of another piece referencePieceNotation = GetNotationForPiece(refPiece); } else { Hex refHex = Board.invalidHex; Position refPosition = Position.center; bool found = false; int i = 0; // zero is center. we dont care about center while (!found && i < Neighborhood.neighborDirections.Length) { i++; refHex = Neighborhood.neighborDirections[i] + move.hex; refPosition = Neighborhood.GetOpposite((Position)i); if (board.TryGetPieceAtHex(refHex, out refPiece)) { found = !refPiece.Equals(move.pieceToMove); } } // must be a first move if (!found) { referencePieceNotation = "."; } else { referencePieceNotation = GetNotationForPiece(refPiece); if (referencePieceNotation == targetPieceNotation) { if (refPiece is BeetleStack) { refPiece = BeetleStack.PopBeetleStack((BeetleStack)refPiece); } else { } referencePieceNotation = GetNotationForPiece(refPiece); } referencePieceNotation = string.Format(Neighborhood.neighborDirectionNotationTemplates[(int)refPosition], referencePieceNotation); } } } else { // must be a first move referencePieceNotation = "."; } return(targetPieceNotation + " " + referencePieceNotation); }
/// <summary> /// Make a move. Validates the move or fails. /// </summary> /// <param name="move"></param> public bool TryMakeMove(Move move) { if (_gameResult != GameResult.Incomplete) { _lastError = "This game is over"; return(false); } if (move.pieceToMove.color == PieceColor.White && !_whiteToPlay) { _lastError = "Cannot move a white piece on black's turn"; return(false); } if (move.pieceToMove.color == PieceColor.Black && _whiteToPlay) { _lastError = "Cannot move a black piece on white's turn"; return(false); } bool placement = true; Hex pieceToMoveHex = Board.invalidHex; Piece actualPiece = move.pieceToMove; if (!_unplayedPieces.Contains(move.pieceToMove)) { // the target piece is already played on the board placement = false; if (!TryGetHexOfPlayedPiece(move.pieceToMove, out pieceToMoveHex)) { _lastError = "Piece is played but Board can't find it"; return(false); } if (!TryGetPieceAtHex(pieceToMoveHex, out actualPiece)) { _lastError = "Piece is played but Board can't find it, again"; return(false); } } if (move.hex.Equals(invalidHex)) { Hex referencePieceHex; if (!TryGetHexOfPlayedPiece(move.referencePiece, out referencePieceHex)) { _lastError = "Cannot find reference piece on the board"; return(false); } move = Move.GetMove(move, Neighborhood.GetNeighborHex(referencePieceHex, move.targetPosition)); } if (!GetMoves().Contains(move)) { _lastError = "Not a valid move"; return(false); } ; if (placement) { Hivailability hivailability; if (!_hivailableHexes.TryGetValue(move.hex, out hivailability)) { _lastError = "Target hex is not hivailable"; return(false); } if (!hivailability.CanPlace(actualPiece.color)) { _lastError = "Target hivailable hex is not playable by your color"; return(false); } PlacePiece(actualPiece, move.hex); } else { // check that movement doesn't violate the one-hive rule if (_articulationPoints.Contains(actualPiece) && !(actualPiece is BeetleStack)) { _lastError = "Move violates one-hive rule"; return(false); } MovePiece(actualPiece, pieceToMoveHex, move.hex); } IncrementTurn(); return(true); }