/// <summary> /// True, if the given value and its neighbour value in the given direction are compatible. /// </summary> public static bool IsCompatibleWith(this BimaruValue value, Direction direction, BimaruValue neighbourValue) { BimaruValueConstraint constraintToNeighbour = value.GetConstraint(direction); BimaruValueConstraint constraintToThis = neighbourValue.GetConstraint(direction.GetOpposite()); return(constraintToNeighbour.DoesAllow(neighbourValue) && constraintToThis.DoesAllow(value)); }
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); }
public void FillUndeterminedFieldsColumn(int columnIndex, BimaruValueConstraint constraint) { BimaruValue valueToSet = constraint.GetRepresentativeValue(); if (valueToSet == BimaruValue.UNDETERMINED || numberOfUndeterminedFieldsPerColumn[columnIndex] == 0) { return; } foreach (GridPoint p in PointsOfColumn(columnIndex).Where(p => GetFieldValueNoCheck(p) == BimaruValue.UNDETERMINED)) { this[p] = valueToSet; } }
public void FieldValueChanged(IGame game, FieldValueChangedEventArgs <BimaruValue> e) { BimaruValue newValue = game.Grid[e.Point]; foreach (Direction direction in Directions.GetAllDirections()) { BimaruValueConstraint constraintInDirection = newValue.GetConstraint(direction); GridPoint pointInDirection = e.Point.GetNextPoint(direction); BimaruValue valueInDirection = game.Grid[pointInDirection]; // Skip set if constraint already satisfied if (!constraintInDirection.IsSatisfiedBy(valueInDirection)) { BimaruValue valueToSet = constraintInDirection.GetRepresentativeValue(); game.Grid[pointInDirection] = valueToSet; } } }
/// <summary> /// Constraint that a field value gives to the neighbour field in the given direction. /// </summary> public static BimaruValueConstraint GetConstraint(this BimaruValue value, Direction direction) { BimaruValueConstraint constraint = BimaruValueConstraint.NO; if (value.IsShip()) { if (direction.GetDirectionType() == DirectionType.DIAGONAL) { constraint = BimaruValueConstraint.WATER; } else if (value == direction.GetFirstShipValue()) { constraint = BimaruValueConstraint.SHIP; } else if (value != BimaruValue.SHIP_MIDDLE && value != BimaruValue.SHIP_UNDETERMINED) { constraint = BimaruValueConstraint.WATER; } } return(constraint); }
/// <summary> /// Whether the constraint allows the Bimaru value. Note that all /// constraints allow the value UNDETERMINED as the constraint /// can still be fullfilled in the future. /// </summary> public static bool DoesAllow(this BimaruValueConstraint constraint, BimaruValue value) { return(constraint.IsSatisfiedBy(value) || value == BimaruValue.UNDETERMINED); }
/// <summary> /// True, if the constraint is satisfied by the Bimaru value. /// </summary> public static bool IsSatisfiedBy(this BimaruValueConstraint constraint, BimaruValue value) { return(constraint == BimaruValueConstraint.NO || constraint == value.GetConstraint()); }
/// <summary> /// Get a representative value of the constraint. /// The value fullfills the constraint without assuming more. /// </summary> public static BimaruValue GetRepresentativeValue(this BimaruValueConstraint constraint) { return(representativeValue[constraint]); }