private void setBoardLayout() { int boardSize = m_SettingForm.BoardSize; int buttonSize = (this.ClientSize.Width - 30) / boardSize; int startingTopPoint = this.ClientSize.Height - ((buttonSize * boardSize) + 15); const int startingLeftPoint = 15; m_BoardButtons = new CheckersButton[m_SettingForm.BoardSize, m_SettingForm.BoardSize]; for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { CheckersButton button = new CheckersButton(i, j); button.Font = new Font("Microsoft Sans Serif", 11F, FontStyle.Bold, GraphicsUnit.Point, 0); button.Size = new Size(buttonSize, buttonSize); button.Top = startingTopPoint + (buttonSize * i); button.Left = startingLeftPoint + (buttonSize * j); if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) { button.BackColor = Color.WhiteSmoke; } else { button.BackColor = Color.Gray; button.Enabled = false; } this.Controls.Add(button); button.Click += new EventHandler(CheckersButton_Clicked); m_BoardButtons[i, j] = button; } } }
private void CheckersButton_Clicked(object sender, EventArgs e) { CheckersButton clickedButton = sender as CheckersButton; if (clickedButton != null) { if (preClickedbutton == null) { clickedButton.BackColor = Color.SkyBlue; preClickedbutton = clickedButton; } else if (preClickedbutton == clickedButton) { clickedButton.BackColor = Color.WhiteSmoke; preClickedbutton = null; } else { Move move = new Move(preClickedbutton.RowInBoard, preClickedbutton.ColInBoard, clickedButton.RowInBoard, clickedButton.ColInBoard); if (!m_Board.PlayTurn(move)) { MessageBox.Show("Illegal Move!"); } handleGameOverAndRematch(); preClickedbutton.BackColor = Color.WhiteSmoke; preClickedbutton = null; if (m_SettingForm.AgeinstComputer) { m_Board.PlayComputerTurn(); } handleGameOverAndRematch(); syncButtonsBoardWithLogicalBoard(); } } }