예제 #1
0
        private int?GetShipPartLength(GridPoint startPoint, BimaruValue startValue, Direction direction)
        {
            int shipLength = 0;

            GridPoint   currentPoint = startPoint;
            BimaruValue currentValue = startValue;

            if (currentValue == direction.GetFirstShipValue())
            {
                shipLength++;
                currentPoint = currentPoint.GetNextPoint(direction);
                currentValue = this[currentPoint];
            }

            while (currentValue == BimaruValue.SHIP_MIDDLE)
            {
                shipLength++;
                currentPoint = currentPoint.GetNextPoint(direction);
                currentValue = this[currentPoint];
            }

            if (currentValue == direction.GetLastShipValue())
            {
                shipLength++;
                return(shipLength);
            }

            return(null);
        }
예제 #2
0
        private BimaruValue?GetShipUndeterminedFieldValue(IGame game, GridPoint pointShipUndetermined)
        {
            BimaruValue?newValue = null;

            Direction?shipDirection = FindNonDiagonalNeighbour(game, pointShipUndetermined, BimaruValueConstraint.SHIP);

            if (shipDirection.HasValue)
            {
                var oppositePoint      = pointShipUndetermined.GetNextPoint(shipDirection.Value.GetOpposite());
                var valueOppositePoint = game.Grid[oppositePoint];

                if (valueOppositePoint == BimaruValue.WATER)
                {
                    newValue = shipDirection.Value.GetFirstShipValue();
                }
                else if (valueOppositePoint.IsShip())
                {
                    newValue = BimaruValue.SHIP_MIDDLE;
                }
            }
            else if (AreAllNonDiagonalNeighboursWater(game, pointShipUndetermined))
            {
                newValue = BimaruValue.SHIP_SINGLE;
            }

            return(newValue);
        }
예제 #3
0
        private bool AreAllNonDiagonalNeighboursWater(IGame game, GridPoint center)
        {
            foreach (var direction in Directions.GetNonDiagonalDirections())
            {
                var pointInDirection = center.GetNextPoint(direction);
                if (game.Grid[pointInDirection] != BimaruValue.WATER)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #4
0
        private Direction?FindNonDiagonalNeighbour(IGame game, GridPoint center, BimaruValueConstraint constraint)
        {
            foreach (var direction in Directions.GetNonDiagonalDirections())
            {
                var pointInDirection = center.GetNextPoint(direction);
                if (constraint.IsSatisfiedBy(game.Grid[pointInDirection]))
                {
                    return(direction);
                }
            }

            return(null);
        }
예제 #5
0
        private void SetShipMiddleNeighbours(IGame game, GridPoint pointShipMiddle, DirectionType directionShipMiddle)
        {
            foreach (var direction in Directions.GetAllDirections())
            {
                var constraint = direction.GetDirectionType() == directionShipMiddle ?
                                 BimaruValueConstraint.SHIP :
                                 BimaruValueConstraint.WATER;

                // Skip set if constraint already satisfied
                var pointInDirection = pointShipMiddle.GetNextPoint(direction);
                if (!constraint.IsSatisfiedBy(game.Grid[pointInDirection]))
                {
                    BimaruValue valueToSet = constraint.GetRepresentativeValue();
                    game.Grid[pointInDirection] = valueToSet;
                }
            }
        }
예제 #6
0
        private void UpdateInvalidFieldBoundaries(GridPoint center)
        {
            BimaruValue centerValue = GetFieldValueNoCheck(center);

            foreach (Direction direction in Directions.GetAllDirections())
            {
                BimaruValue   neighbourValue = this[center.GetNextPoint(direction)];
                FieldBoundary boundary       = center.GetBoundary(direction);
                if (centerValue.IsCompatibleWith(direction, neighbourValue))
                {
                    invalidFieldBoundaries.Remove(boundary);
                }
                else
                {
                    invalidFieldBoundaries.Add(boundary);
                }
            }
        }