private void RemoveEvent() { for (int i = 0; i < _max; i++) { for (int j = 0; j < _max; j++) { Panel panel = (Panel)this.tableLayoutPanel1.GetControlFromPosition(j, i); if (horizontalShip.IsFirst(i, j)) { panel.Click -= new EventHandler(horizontalShipFirstClick); } else if (horizontalShip.IsSecond(i, j)) { panel.Click -= new EventHandler(horizontalShipSecondClick); } else if (verticalShip.IsFirst(i, j)) { panel.Click -= new EventHandler(verticalShipFirstClick); } else if (verticalShip.IsSecond(i, j)) { panel.Click -= new EventHandler(verticalShipSecondClick); } else { panel.Click -= new EventHandler(panelClick); } } } }
private void InitPanels() { lblResult.Text = ""; _moveCount = 0; Array.Clear(panels, 0, 16); Random rnd = new Random(); int value = rnd.Next(0, _max * _max); int col = value % _max; int row = value / _max; horizontalShip = new Ship(); horizontalShip.X1 = row; horizontalShip.Y1 = col; horizontalShip.Uncovered1 = false; horizontalShip.X2 = row; if (col > 0) { horizontalShip.Y2 = col - 1; } else { horizontalShip.Y2 = col + 1; } horizontalShip.Uncovered2 = false; int value3 = horizontalShip.X2 * _max + horizontalShip.Y2; verticalShip = new Ship(); bool isVerticalNotOk = false; do { isVerticalNotOk = false; int value2 = rnd.Next(0, _max * _max); System.Diagnostics.Debug.WriteLine(value2); if (value2 == value || value2 == value3) { isVerticalNotOk = true; continue; } col = value2 % _max; row = value2 / _max; verticalShip.X1 = row; verticalShip.Y1 = col; verticalShip.Uncovered1 = false; if (row > 0) { verticalShip.X2 = row - 1; } else { verticalShip.X2 = row + 1; } verticalShip.Y2 = col; verticalShip.Uncovered2 = false; if (verticalShip.IsSecond(horizontalShip.X1, horizontalShip.Y1) || verticalShip.IsSecond(horizontalShip.X2, horizontalShip.Y2)) { isVerticalNotOk = true; } }while (isVerticalNotOk); for (int i = 0; i < _max; i++) { for (int j = 0; j < _max; j++) { Panel panel = (Panel)this.tableLayoutPanel1.GetControlFromPosition(j, i); if (horizontalShip.IsFirst(i, j)) { panel.Click += new EventHandler(horizontalShipFirstClick); } else if (horizontalShip.IsSecond(i, j)) { panel.Click += new EventHandler(horizontalShipSecondClick); } else if (verticalShip.IsFirst(i, j)) { panel.Click += new EventHandler(verticalShipFirstClick); } else if (verticalShip.IsSecond(i, j)) { panel.Click += new EventHandler(verticalShipSecondClick); } else { panel.Click += new EventHandler(panelClick); } panel.BackColor = Color.Gray; } } }