コード例 #1
0
ファイル: GameBoard.cs プロジェクト: remy22/BattleshipsGame
        /// <summary>
        /// Using a random staring point to try determing if a ship can be added on certain cells on the board
        /// </summary>
        /// <param name="randomStartPoint"></param>
        /// <param name="ship"></param>
        /// <returns></returns>
        private bool TryAddingShipToBoard(Cell randomStartPoint, Ship ship)
        {
            TempCells = null;
            List<Cell> tempCells = new List<Cell>();
            tempCells.Add(randomStartPoint);

            //try increasing
            List<Cell> cellsAfterDirection1 = null;
            var size = ship.Size;
            for (int i = 1; i < size; i++)
            {
                cellsAfterDirection1 = TryDirection1(tempCells);
            }

            var result = CheckIfPotentialDirectionFeasible(cellsAfterDirection1, size);

            if (result)
            {
                TempCells = cellsAfterDirection1;
            }

            List<Cell> cellsAfterDirection2 = null;
            if (result == false)
            {
                TempCells = null;
                TempCells = tempCells = new List<Cell>();
                tempCells.Add(randomStartPoint);

                for (int i = 1; i < size; i++)
                {
                    cellsAfterDirection2 = TryDirection2(tempCells);
                }

                result = CheckIfPotentialDirectionFeasible(cellsAfterDirection2, size);

                if (result)
                {
                    TempCells = cellsAfterDirection2;
                }

            }
            return result;
        }
コード例 #2
0
ファイル: GameBoard.cs プロジェクト: remy22/BattleshipsGame
        /// <summary>
        /// Method called once the TempCells list is validated that the ship can fit on the cells in the list
        /// </summary>
        private void AddShipToCellsOnBoard(Ship ship)
        {
            var list = TempCells;
            var fistCell = list.FirstOrDefault();

            foreach (var cell in list)
            {
                var displayName = cell.DisplayName;

                var cellFromBoard = (from c in Board.Cells
                                     where c.Key == displayName
                                     select c.Value).FirstOrDefault();

                if (cellFromBoard != null)
                {
                    if (fistCell != null)
                    {
                        cellFromBoard.ShipType = fistCell.ShipType;
                    }
                    cellFromBoard.HasShip = true;
                }
            }

            if (ShipSunkStatus == null)
            {
                ShipSunkStatus = new Dictionary<Guid, Tuple<Ship, List<Cell>>>();
            }

            ShipSunkStatus.Add(ship.ShipName, Tuple.Create(ship, TempCells));

            TempCells = null;
        }