示例#1
0
        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);
            }
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
文件: Board.cs 项目: vfridell/HiveLib
        /// <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);
        }