Ejemplo n.º 1
0
        public bool PlaceShipOnBoard(ShipPosition pos)
        {
            if (WrongPosition(pos))
            {
                return(false);
            }

            bool        ret  = true;
            List <Cell> ship = new List <Cell>();

            if (pos.Align == ShipPosition.AlignmentType.Horizontal)
            {
                for (int i = 0; i < pos.Length && ret; i++)
                {
                    if (pos.X + i >= 10 ||            // Out of Bound
                        Board[pos.X + i, pos.Y] == 1) // Overlap
                    {
                        ret = false;
                    }
                    else
                    {
                        ship.Add(new Cell {
                            X = pos.X + i, Y = pos.Y
                        });
                        Board[pos.X + i, pos.Y] = 1;
                    }
                }
            }
            else
            {
                for (int i = 0; i < pos.Length && ret; i++)
                {
                    if (pos.Y + i >= 10 ||            // Out of Bound
                        Board[pos.X, pos.Y + i] == 1) // Overlap
                    {
                        ret = false;
                    }
                    else
                    {
                        ship.Add(new Cell {
                            X = pos.X, Y = pos.Y + i
                        });
                        Board[pos.X, pos.Y + i] = 1;
                    }
                }
            }
            if (ret)
            {
                Ships.Add(new Ship {
                    _deck = ship
                });
            }
            return(ret);
        }
Ejemplo n.º 2
0
 private bool WrongPosition(ShipPosition pos) => pos == null || pos.X < 0 || pos.Y < 0 || pos.Y > 9 || pos.X > 9 || pos.Length > 10 || pos?.Length < 1;