示例#1
0
        public bool PlaceShip(Ship ship)
        {
            var decks = (int)ship.Type;
            var possibleCoordinates = new Coordinate[decks];
            var startingPoint = Coordinate.Copy(ship.StartingPoint);

            for (var i = 0; i < decks; i++)
            {
                //try to validate
                if (Field.ValidateCoordinate(startingPoint))
                {
                    //proximity check
                    if (Field.ProximityCheck(startingPoint)) return false;
                    possibleCoordinates[i] = Coordinate.Copy(startingPoint);

                    if (ship.Orientation == ShipOrientation.Vertical)
                        startingPoint.MoveDown();
                    else
                        startingPoint.MoveRight();
                }
                else
                    return false;
            }

            //if everything went fine, persist possibleCoordinates on map
            possibleCoordinates.ForEach(c => Field[c] = new Cell { State = CellState.ShipDeck, Coordinate = c, ShipId = ship.Id });

            //add ship to collection
            _ships.Add(ship);

            return true;
        }
示例#2
0
 public IEnumerable<Coordinate> GetSurroundedCoordinatesOfTheShip(Ship ship)
 {
     var result = new List<Coordinate>();
     var shipCoordinates = ship.GetCoordinates();
     foreach (var shipCoord in shipCoordinates)
     {
         var mutableCoordinate = Coordinate.Copy(shipCoord);
         if (Field.ValidateCoordinate(mutableCoordinate.MoveDown())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveLeft())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveUp())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveUp())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveRight())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveRight())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveDown())) result.Add(Coordinate.Copy(mutableCoordinate));
         if (Field.ValidateCoordinate(mutableCoordinate.MoveDown())) result.Add(Coordinate.Copy(mutableCoordinate));
     }
     return result.Where(c => !shipCoordinates.Contains(c)).Distinct().ToList();
 }