Exemple #1
0
        private Cell GetRandomCell(Cell damaged, IBattlefield battlefield)
        {
            var cells = battlefield.GetNeighbours(damaged).Where(c => !c.State.HasFlag(CellState.WasFired) &&
                                                                 (c.Coordinates.X.Equals(damaged.Coordinates.X) ||
                                                                  c.Coordinates.Y.Equals(damaged.Coordinates.Y))).ToList();

            return(RandomHelper.GetHelper().GetRandomCell(cells));
        }
        private void EncircleShip(ICollection <Cell> range)
        {
            List <Cell> neighs = new();

            foreach (var cell in range)
            {
                neighs.AddRange(_battlefield.GetNeighbours(cell).Where(c => c.State is CellState.Empty));
            }

            neighs = neighs.Distinct().ToList();

            foreach (var cell in neighs)
            {
                cell.State = CellState.NearShip;
            }
        }
Exemple #3
0
        private IList <Cell> GetSuitableCells(Ship ship, IBattlefield battlefield)
        {
            List <Cell> cells = new();

            foreach (var cell in ship.Cells)
            {
                cells.AddRange(battlefield.GetNeighbours(cell).Where(c => !c.State.HasFlag(CellState.WasFired)));
            }

            cells = cells.Distinct().ToList();

            var a = ship.Cells.FirstOrDefault(c => c.State.HasFlag(CellState.WasFired));
            var b = ship.Cells.LastOrDefault(c => c.State.HasFlag(CellState.WasFired));

            cells = a.Coordinates.X == b.Coordinates.X
                ? cells.Where(c => c.Coordinates.X == a.Coordinates.X).ToList()
                : cells.Where(c => c.Coordinates.Y == a.Coordinates.Y).ToList();

            return(cells);
        }