Example #1
0
        ///// ------------ Public methods ---------- END -------------///


        ///// ------------ Private supporting methods ---------- START -------------///
        private string IsValidPosition(IBattleship battleship, ShipModel shipRequested)
        {
            var pointRequested = Mapper.Map <ShipPoint>(shipRequested.StartPoint);
            var newShipPoints  = battleship.GetPointsOccupiedOnBoard(pointRequested, shipRequested.Orientation);

            //None of the Coordinate to fall outside of the board
            var invalidPosition = newShipPoints.Any(point => point.XCoordinate <1 || point.XCoordinate> GameConstant.MaxXOnBoard ||
                                                    point.YCoordinate <1 || point.YCoordinate> GameConstant.MaxYOnBoard);

            if (invalidPosition)
            {
                return("Selected ship position and orientation collectively makes ship fall out of board.");
            }

            //New ship should not overlap with any of the existing ship
            invalidPosition = ShipsOnBoard.Any() && ShipsOnBoard.Any(placedShip => placedShip.PointsOccupied.Any(p => newShipPoints.Any(np => np.XCoordinate == p.XCoordinate && np.YCoordinate == p.YCoordinate)));

            if (invalidPosition)
            {
                return("Selected ship position and orientation collectively makes ship collide with another ship on board.");
            }

            battleship.PointsOccupied = newShipPoints;

            return(string.Empty);
        }
Example #2
0
        public bool Attack(Coordinate attackCoord)
        {
            if (!ValidCoordinate(attackCoord))
            {
                throw new ArgumentException("Attacks must be within the bounds of the board");
            }

            if (Battleships[attackCoord.X, attackCoord.Y] != null)
            {
                IBattleship battleship = Battleships[attackCoord.X, attackCoord.Y];

                if (battleship.IsSunk())
                {
                    return(true);
                }


                battleship.Hit(attackCoord);
                if (battleship.IsSunk())
                {
                    RemainingBattleships--;
                }

                return(true);
            }

            return(false);
        }
Example #3
0
        public void MoveShip(string coordinates, IBattleship ship)
        {
            foreach (var tile in this.tileCollection)
            {
                tile.RemoveShip();
            }

            this.AddShip(coordinates, ship);
        }
Example #4
0
        private Dictionary<KLineType, List<IStockKLine>> GetFutureDatas(IBattleship battleship, DateTime beginTime, DateTime endTime)
        {
            var result = new Dictionary<KLineType, List<IStockKLine>>();

            foreach(var kLineType in battleship.Link.DataTypes)
            {
                var datas = battleship.GetKLines(_security, kLineType, beginTime, endTime).ToList();
                result.Add(kLineType, datas);
            }

            return result;
        }
Example #5
0
        private bool ValidBattleship(IBattleship battleship)
        {
            foreach (Coordinate battleshipCoord in battleship.Coordinates)
            {
                if (!ValidCoordinate(battleshipCoord) || Battleships[battleshipCoord.X, battleshipCoord.Y] != null)
                {
                    return(false);
                }
            }

            return(true);
        }
Example #6
0
        public void AddBattleship(IBattleship battleship)
        {
            if (!ValidBattleship(battleship))
            {
                throw new ArgumentException("Invalid battleship coordinates");
            }

            foreach (var coord in battleship.Coordinates)
            {
                Battleships[coord.X, coord.Y] = battleship;
            }

            RemainingBattleships++;
        }
Example #7
0
        public void AddShip(string coordinates, IBattleship ship)
        {
            var tileIndex = this.GetTileIndex(coordinates);

            var colCount = this.numberOfHorizontalTiles - ship.Size.Width;
            var rowCount = this.numberOfVerticalTiles - ship.Size.Height;

            var x = (tileIndex.X).Clamp(colCount, 0);
            var y = (tileIndex.Y).Clamp(rowCount, 0);

            var shipTiles = from Tile tile in this.tileCollection
                            where tile.Location.X >= x && tile.Location.X < x + ship.Size.Width &&
                            tile.Location.Y >= y && tile.Location.Y < y + ship.Size.Height
                            select tile;

            foreach (var tile in shipTiles)
            {
                tile.AddShip(ship);
            }
        }
        public IList <IBoardCell> ListOfCellsAffected(IBattleship ship, IBoardCell startingCell, IBoard board)
        {
            switch (ship.Orientation)
            {
            case OrientationType.Vertical:
                int numberOfCells = startingCell.RowCoordinate + ship.Width;
                return(board.BoardCells.Where(x => x.RowCoordinate >= startingCell.RowCoordinate &&
                                              x.ColumnCoordinate >= startingCell.ColumnCoordinate &&
                                              x.RowCoordinate < numberOfCells &&
                                              x.ColumnCoordinate <= startingCell.ColumnCoordinate).ToList());

            case OrientationType.Horizontal:
                numberOfCells = startingCell.ColumnCoordinate + ship.Width;
                return(board.BoardCells.Where(x => x.RowCoordinate >= startingCell.RowCoordinate &&
                                              x.ColumnCoordinate >= startingCell.ColumnCoordinate &&
                                              x.RowCoordinate <= startingCell.RowCoordinate &&
                                              x.ColumnCoordinate < numberOfCells).ToList());
            }
            return(null);
        }
        /// Add a battleship to the board at a specified position.
        public bool AddBattleship(IBoard board, IBoardCell startCell, IBattleship ship)
        {
            if (board == null || startCell == null || ship == null)
            {
                return(false);
            }

            var startingCell = board.BoardCells.Where(x => x.RowCoordinate == startCell.RowCoordinate &&
                                                      x.ColumnCoordinate == startCell.ColumnCoordinate).FirstOrDefault();

            if (startingCell == null)
            {
                return(false);
            }

            if (_placeBattleship.CanShipBePlaced(ship, startCell, board))
            {
                _placeBattleship.ListOfCellsAffected(ship, startCell, board).ToList().ForEach(x => x.IsOccupied = true);
                return(true);
            }

            return(false);
        }
        public bool CanShipBePlaced(IBattleship ship, IBoardCell startingCell, IBoard board)
        {
            //If the ship width is less than one then an exception is thrown
            if (ship.Width < 1)
            {
                throw new ShipWidthCannotBeLessThanOneException("The ship width must be greater than 0");
            }

            if (ship.Orientation == OrientationType.Vertical &&
                (ship.Width + startingCell.RowCoordinate) > board.BoardCells.Max(x => x.RowCoordinate))
            {
                return(false);
            }

            if (ship.Orientation == OrientationType.Horizontal &&
                (ship.Width + startingCell.ColumnCoordinate) > board.BoardCells.Max(x => x.ColumnCoordinate))
            {
                return(false);
            }
            var listOfCellsAffected = ListOfCellsAffected(ship, startingCell, board);

            return(!listOfCellsAffected.Any(x => x.IsOccupied));
        }
Example #11
0
        public void Practice(IBattleship battleship, DateTime beginTime, DateTime endTime)
        {
            var morpheus = new Morpheus();

            // 创造出所有的候选人,并登录Matrix
            var candidate = morpheus.FindCandidate(_security).ToList();
            candidate.ForEach(p => p.Login(battleship));

            // 构造出未来的数据
            var futureDatas = GetFutureDatas(battleship, beginTime, endTime);

            //foreach(var dataItem in datas)
            //{
            //    if (!battleship.Link.ExistData(type, dataItem))
            //    {
            //        // 存储新来的数据
            //        battleship.Link.AddNewData(type, dataItem);
            //    }

            //    // todo 进度修改
            //    candidate.ForEach(p => p.OnKLineComing());
            //}
        }
Example #12
0
 public override float MovementSpeed(IBattleship _battleship)
     : base(Battleship)
Example #13
0
 /// <summary>
 /// 通过战舰联系到操作员登陆到Matrix
 /// </summary>
 /// <param name="battleship"></param>
 public void Login(IBattleship battleship)
 {
     _battleship = battleship;
     _battleship.UpgradeOperator(this);
     _logined = true;
 }
Example #14
0
 public void CreateBattleship(string coordinates, IBattleship ship)
 {
     ship.Owner = this;
     this.Board.AddShip(coordinates, ship);
 }
Example #15
0
 public void AddShip(IBattleship ship)
 {
     this.Ship = ship;
 }
Example #16
0
 public void Add(IBattleship battleship)
 {
     this.battleships.Add(battleship);
 }