コード例 #1
0
        public void SupplyShip(Box startPosition, TypeShip ship, Orientation orientation)
        {
            string name;

            do
            {
                name = Guid.NewGuid().ToString();
            } while (!CheckAvailabilityName(name));
            int position = boxes.GetIndexBox(startPosition.Panel);

            if (orientation == Orientation.Vertical)
            {
                for (int i = 0, j = 0; i < (int)ship; i++, j += 10)
                {
                    if ((position + j) > 99)
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
                if (!CheckAvailabilityVertical(position, ship))
                {
                    throw new Exception("Рядом корабль");
                }


                for (int i = 0, j = 0; i < (int)ship; i++, j += 10)
                {
                    boxes[boxes.GetIndexBox(startPosition.Panel) + j].NameShip = name;
                    if (!boxes.Enemy)
                    {
                        boxes[boxes.GetIndexBox(startPosition.Panel) + j].Color     = Colors.Blue;
                        boxes[boxes.GetIndexBox(startPosition.Panel) + j].LastColor = Colors.Blue;
                    }
                    boxes[boxes.GetIndexBox(startPosition.Panel) + j].IsBusy = true;
                }
            }
            else
            {
                for (int i = 1; i < (int)ship; i++)
                {
                    if ((position + i) % 10 == 0)
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
                if (!CheckAvailabilityGorizontal(position, ship))
                {
                    throw new Exception("Рядом корабль");
                }
                for (int i = 0; i < (int)ship; i++)
                {
                    boxes[boxes.GetIndexBox(startPosition.Panel) + i].NameShip = name;
                    if (!boxes.Enemy)
                    {
                        boxes[boxes.GetIndexBox(startPosition.Panel) + i].Color     = Colors.Blue;
                        boxes[boxes.GetIndexBox(startPosition.Panel) + i].LastColor = Colors.Blue;
                    }
                    boxes[boxes.GetIndexBox(startPosition.Panel) + i].IsBusy = true;
                }
            }
        }