Пример #1
0
        public void Two_different_types_of_ship_with_same_coordinates_are_equal()
        {
            Ship shipA = new Destroyer {
                Coordinates = new Location(10, 10)
            };
            Ship shipB = new BattleshipLogic.Battleship {
                Coordinates = new Location(10, 10)
            };

            var result = shipA.Equals(shipB);

            Assert.IsTrue(result);
        }
Пример #2
0
        public bool AddShip(ShipType type, int boardSize)
        {
            Ship s = null;

            switch (type)
            {
            case ShipType.BattleShip:
                s = new BattleshipLogic.Battleship();
                break;

            case ShipType.Destroyer:
                s = new Destroyer();
                break;
            }

            Random rnd           = new Random();
            var    maxCoordinate = boardSize - 1;

            var x = 0; var y = 0; var headingNorth = true;
            var canPlaceShip = false; var attemptedToPlace = 0;

            while (!canPlaceShip && attemptedToPlace < 20)
            {
                canPlaceShip = true;
                attemptedToPlace++;
                x            = rnd.Next(0, maxCoordinate);
                y            = rnd.Next(0, maxCoordinate);
                headingNorth = (rnd.Next(0, 2) == 1);

                if (headingNorth)
                {
                    if (y <= (maxCoordinate - s.Size))
                    {
                        for (int i = 0; i < s.Size; i++)
                        {
                            if (GetShipByLocation(x, y + i) != null)
                            {
                                canPlaceShip = false;
                            }
                        }
                    }
                    else
                    {
                        canPlaceShip = false;
                    }
                }
                else
                {
                    if (x <= (maxCoordinate - s.Size))
                    {
                        for (int i = 0; i < s.Size; i++)
                        {
                            if (GetShipByLocation(x + i, y) != null)
                            {
                                canPlaceShip = false;
                            }
                        }
                    }
                    else
                    {
                        canPlaceShip = false;
                    }
                }
            }
            if (canPlaceShip)
            {
                s.Coordinates = new Location(x, y, headingNorth);
                Ships.Add(s);
            }
            return(canPlaceShip);
        }