private void Button_Click(object sender, EventArgs e) { CheckersButton clickedButton = (CheckersButton)sender; if (clickedButton.BackColor == Color.White && !string.IsNullOrEmpty(clickedButton.Text) && !IsButtonClicked) { IsButtonClicked = true; clickedButton.BackColor = Color.LightBlue; GameLogic.PlayerTurn.NextMove = new Move(clickedButton.Coordinates); EnableEmptyButtons(); clickedButton.Enabled = true; } else if (clickedButton.BackColor == Color.White && string.IsNullOrEmpty(clickedButton.Text) && IsButtonClicked) { Move playerNextMove = GameLogic.PlayerTurn.NextMove; Square previousButtonClickedCoordinates = GameLogic.PlayerTurn.NextMove.From; CheckersButton previousButtonClicked = GameBoardButtonMatrix[previousButtonClickedCoordinates.Row, previousButtonClickedCoordinates.Column]; previousButtonClicked.BackColor = Color.White; IsButtonClicked = false; GameLogic.PlayerTurn.NextMove.To = clickedButton.Coordinates; if (GameLogic.PlayerTurn.MoveInputPossible(GameLogic.GameBoard, ref playerNextMove)) { PlayMove(playerNextMove); } else { string message = string.Format(@"Your move is illegal! Please try again"); GameLogic.PlayerTurn.NextMove = null; MessageBox.Show(message, "Checkers", MessageBoxButtons.OK, MessageBoxIcon.Error); EnableCurrentPlayerButtons(); } } else if (clickedButton.BackColor == Color.LightBlue) { clickedButton.BackColor = Color.White; IsButtonClicked = false; GameLogic.PlayerTurn.NextMove = null; EnableCurrentPlayerButtons(); } }
public void CreateAndDisplayBoard() { int boardSize = GameLogic.GameBoard.BoardSize; int start_i = 40; int start_j = 10; for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { CheckersButton button = new CheckersButton(i, j); button.Top = start_i + ((i * k_ButtonHeight) + k_Distance); button.Left = start_j + ((j * k_ButtonWidth) + k_Distance); button.Width = k_ButtonWidth; button.Height = k_ButtonHeight; button.Enabled = false; button.TabStop = false; button.Click += new EventHandler(Button_Click); if ((i % 2 == 0 && j % 2 != 0) || (i % 2 != 0 && j % 2 == 0)) { button.BackColor = Color.White; } else { button.BackColor = Color.Gray; } GameBoardButtonMatrix[i, j] = button; } } for (int i = 0; i < (boardSize - 2) / 2; i++) { for (int j = 0; j < boardSize; j++) { if (i % 2 == 0) { GameBoardButtonMatrix[i, j + 1].Text = "O"; j += 1; } else { GameBoardButtonMatrix[i, j].Text = "O"; j += 1; } } } for (int i = boardSize - 1; i > (((boardSize - 2) / 2) + 1); i--) { for (int j = 0; j < boardSize; j++) { if (i % 2 == 0) { GameBoardButtonMatrix[i, j + 1].Text = "X"; j += 1; } else { GameBoardButtonMatrix[i, j].Text = "X"; j += 1; } } } DisplayBoard(); }