Esempio n. 1
0
        public BattleshipsGame(Vector2 <int> size)
        {
            Field = new CellField(size);

            SetWindowSize();

            GenerateShips();
            PlaceShips();
            MapShips();
        }
Esempio n. 2
0
        public void PlaceShipRandomly(CellField field)
        {
            Random rnd = new Random();

            int startX    = rnd.Next(field.Size.X);
            int startY    = rnd.Next(field.Size.Y);
            int direction = rnd.Next(4);

            while (!FillShip(field, startX, startY, direction))
            {
                startX    = rnd.Next(field.Size.X);
                startY    = rnd.Next(field.Size.Y);
                direction = rnd.Next(4);
            }
        }
Esempio n. 3
0
        private bool FillShip(CellField field, int startX, int startY, int direction)
        {
            int startIndex = startX + (startY * field.Size.X);

            ShipParts.Clear();

            switch (direction)
            {
            default:
            case 0:
                if (startX + shipSize >= field.Size.X)
                {
                    return(false);
                }
                for (int i = 0; i < shipSize; i++)
                {
                    Cell cell = field.Cells[startIndex + i];
                    ShipParts.Add(cell);
                }

                break;

            case 1:
                if (startY + shipSize >= field.Size.Y)
                {
                    return(false);
                }
                for (int i = 0; i < shipSize; i++)
                {
                    Cell cell = field.Cells[startIndex + (i * field.Size.X)];
                    ShipParts.Add(cell);
                }

                break;

            case 2:
                if (startX - shipSize < 0)
                {
                    return(false);
                }
                for (int i = 0; i < shipSize; i++)
                {
                    Cell cell = field.Cells[startIndex - i];
                    ShipParts.Add(cell);
                }

                break;

            case 3:
                if (startY - shipSize < 0)
                {
                    return(false);
                }
                for (int i = 0; i < shipSize; i++)
                {
                    Cell cell = field.Cells[startIndex - (i * field.Size.X)];
                    ShipParts.Add(cell);
                }

                break;
            }
            return(true);
        }