Exemplo n.º 1
0
        public bool CheckLocation(BaseShip prototype, Location location)
        {
            int dj = 0;
            int di = 0;

            if (prototype.Orientation == Orientation.Horizontal)
            {
                dj = 1;
            }
            if (prototype.Orientation == Orientation.Vertical)
            {
                di = 1;
            }

            for (var i = 0; i < prototype.Size; i++)
            {
                if ((!GeneralFunction.PreventionIndexRange(location.I + i * di, location.J + i * dj)) || (completingCell[location.I + i * di, location.J + i * dj]))
                {
                    notvalidCellLocation[location.I, location.J] = true;
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        void CountingValidCell(BaseShip prototype)
        {
            GeneralFunction.FalseToMatrix(notvalidCellLocation);

            validCell = 0;
            for (var i = 0; i < Field.Size; i++)
            {
                for (var j = 0; j < Field.Size; j++)
                {
                    if (CheckLocation(prototype, new Location(i, j)))
                    {
                        validCell++;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static void MouseClicked(object sender, MouseEventArgs e)
        {
            SeaBattlePicture cell = (SeaBattlePicture)sender;

            if (!BeginArround)
            {
                if (cell.ShipIntoCell == null)
                {
                    return;
                }
                BeginArround = true;

                ship = cell.ShipIntoCell;
                ship.Destruction();
                shipsOnFild.RecalculationCompletingCell(ship);

                Location endLocation = new Location(
                    cell.CellLocation.I - (1 - (int)ship.Orientation),
                    cell.CellLocation.J - (int)ship.Orientation);

                DisplayFieldPlace(ship.Location, endLocation);

                protoShip          = new BaseShip(cell.CellLocation, ship.Orientation, ship.Size);
                checkValidLocation = shipsOnFild.CheckLocation(protoShip, protoShip.Location);
                DisplayPrototypeShip();
            }
            else
            {
                BeginArround = false;

                Ship newShips;

                checkValidLocation = shipsOnFild.CheckLocation(protoShip, protoShip.Location);

                if (checkValidLocation)
                {
                    newShips = new Ship(protoShip.Location, protoShip.Orientation,
                                        protoShip.Size, Form1.LeftField);
                }
                else
                {
                    newShips = new Ship(ship);

                    Location endedLocation = new Location(
                        cell.CellLocation.I + (1 - (int)protoShip.Orientation) * (protoShip.Size - 1),
                        cell.CellLocation.J + (int)protoShip.Orientation * (protoShip.Size - 1));

                    DisplayFieldPlace(cell.CellLocation, endedLocation);
                }

                shipsOnFild.AddShips(newShips);

                Location endLocation = new Location(
                    newShips.Location.I + (1 - (int)newShips.Orientation) * (newShips.Size - 1),
                    newShips.Location.J + (int)newShips.Orientation * (newShips.Size - 1)
                    );

                DisplayFieldPlace(newShips.Location, endLocation);

                ship = null;
            }
        }