public bool CheckLocation(PrototypeShip 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); }
void CountingValidCell(PrototypeShip 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++; } } } }
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 PrototypeShip(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; } }