private void button_Cliked(object sender, EventArgs e) { ButtonLocat currentButton = sender as ButtonLocat; if (currentButton != null) { if (currentButton.BackColor != Color.CornflowerBlue) { currentButton.BackColor = Color.CornflowerBlue; if (m_Source == null) { m_Source = currentButton.LocatOfButton; } } else { currentButton.BackColor = Color.Empty; m_Source = null; } if (m_Source != null && !m_Source.Value.Equals(currentButton.LocatOfButton)) { m_Dest = currentButton.LocatOfButton; OnBoardUiMove(m_Source.Value, m_Dest.Value); cleanAfterMove(); } } }
private void initializeButtonBoard() { int lengthAndWidthOfButton = m_FormOfBoard.ClientSize.Width / r_SizeOfBoard; Locat locateForButtons = new Locat(); int spaceForLabels = m_LabelPlayer1.Top + m_LabelPlayer1.Height; m_MatOfButton = new ButtonLocat[r_SizeOfBoard, r_SizeOfBoard]; for (int i = 0; i < r_SizeOfBoard; i++) { for (int j = 0; j < r_SizeOfBoard; j++) { if (i < ((r_SizeOfBoard / 2) - 1) && (i + j) % 2 != 0) { locateForButtons.X = (byte)j; locateForButtons.Y = (byte)i; ButtonLocat buttonOfPlayr1 = new ButtonLocat(locateForButtons); buttonOfPlayr1.Text = k_Player1Sign; buttonOfPlayr1.Width = lengthAndWidthOfButton; buttonOfPlayr1.Height = lengthAndWidthOfButton; buttonOfPlayr1.Left = j * lengthAndWidthOfButton; buttonOfPlayr1.Top = (i * lengthAndWidthOfButton) + spaceForLabels; buttonOfPlayr1.Click += button_Cliked; m_FormOfBoard.Controls.Add(buttonOfPlayr1); m_MatOfButton[i, j] = buttonOfPlayr1; } else if (i >= ((r_SizeOfBoard / 2) + 1) && (i + j) % 2 != 0) { locateForButtons.X = (byte)j; locateForButtons.Y = (byte)i; ButtonLocat buttonOfPlayr2 = new ButtonLocat(locateForButtons); buttonOfPlayr2.Text = k_Player2Sign; buttonOfPlayr2.Width = lengthAndWidthOfButton; buttonOfPlayr2.Height = lengthAndWidthOfButton; buttonOfPlayr2.Left = j * lengthAndWidthOfButton; buttonOfPlayr2.Top = (i * lengthAndWidthOfButton) + spaceForLabels; buttonOfPlayr2.Click += button_Cliked; m_FormOfBoard.Controls.Add(buttonOfPlayr2); m_MatOfButton[i, j] = buttonOfPlayr2; } else { locateForButtons.X = (byte)j; locateForButtons.Y = (byte)i; ButtonLocat buttonOfEmptyPlace = new ButtonLocat(locateForButtons); buttonOfEmptyPlace.Text = k_EmptyPlace; buttonOfEmptyPlace.Width = lengthAndWidthOfButton; buttonOfEmptyPlace.Height = lengthAndWidthOfButton; buttonOfEmptyPlace.Left = j * lengthAndWidthOfButton; buttonOfEmptyPlace.Top = (i * lengthAndWidthOfButton) + spaceForLabels; if ((i + j) % 2 == 0) { buttonOfEmptyPlace.BackColor = Color.Gray; buttonOfEmptyPlace.Enabled = false; } else { buttonOfEmptyPlace.Click += button_Cliked; } m_FormOfBoard.Controls.Add(buttonOfEmptyPlace); m_MatOfButton[i, j] = buttonOfEmptyPlace; } } } }