private void OnClick(object sender, EventArgs e) { BoardButton button = (sender as BoardButton); int rowIndex = button.RowIndex; int colIndex = button.ColIndex; button.Text = TicTacToeBoard.CurrentPlayer == ePlayer.Player ? "X" : "O"; button.Enabled = false; TicTacToeBoard.PlayTurn(ref rowIndex, ref colIndex); if (m_OpponentType == ePlayerType.Computer && !TicTacToeBoard.NewGame) { generateButtonClick(); } TicTacToeBoard.NewGame = false; }
public BoardForm() { m_GameSetting = new GameSettings(); m_GameSetting.ShowDialog(); r_GameBoardSize = m_GameSetting.Size; m_Player1Name = m_GameSetting.Player1Name; m_Player2Name = m_GameSetting.Player2Name; m_OpponentType = m_GameSetting.OpponentType; r_ButtonsMatrix = new BoardButton[r_GameBoardSize, r_GameBoardSize]; r_TicTacToeBoard = new TicTacToeBoard(r_GameBoardSize, m_OpponentType); TicTacToeBoard.Tie += onTie; TicTacToeBoard.Win += onWin; TicTacToeBoard.OnCellChanged += onCellChanged; int buttonHeight = 40; int buttonWidth = 40; int startHeight = this.Top; int startWidth = this.Left; int space = 10; this.m_Label1 = new System.Windows.Forms.Label(); this.m_Label2 = new System.Windows.Forms.Label(); this.m_LabelOpponentScore = new System.Windows.Forms.Label(); this.m_LabelPlayerScore = new System.Windows.Forms.Label(); InitializeComponent(); m_Label1.Text = m_Player1Name + ":"; m_Label2.Text = m_Player2Name + ":"; for (int i = 0; i < r_GameBoardSize; i++) { for (int j = 0; j < r_GameBoardSize; j++) { r_ButtonsMatrix[i, j] = new BoardButton(i, j); r_ButtonsMatrix[i, j].Top = startHeight + i * buttonHeight; r_ButtonsMatrix[i, j].Left = startWidth + j * buttonWidth; r_ButtonsMatrix[i, j].Height = buttonHeight; r_ButtonsMatrix[i, j].Width = buttonWidth; r_ButtonsMatrix[i, j].Text = ""; r_ButtonsMatrix[i, j].Click += OnClick; this.Controls.Add(r_ButtonsMatrix[i, j]); } } this.Controls.Add(m_Label1); this.m_Label1.Left = this.Left + 10; this.m_Label1.Top = r_ButtonsMatrix[r_GameBoardSize - 1, r_GameBoardSize - 1].Top + buttonHeight; this.m_Label1.AutoSize = true; this.Controls.Add(m_LabelPlayerScore); this.m_LabelPlayerScore.Left = m_Label1.Left + m_Label1.Width + 10; this.m_LabelPlayerScore.Top = m_Label1.Top; this.m_LabelPlayerScore.Text = "0"; this.m_LabelPlayerScore.AutoSize = true; this.Controls.Add(m_Label2); this.m_Label2.Left = m_LabelPlayerScore.Left + m_LabelPlayerScore.Width + 10; this.m_Label2.Top = m_Label1.Top; this.m_Label2.AutoSize = true; this.Controls.Add(m_LabelOpponentScore); this.m_LabelOpponentScore.Left = m_Label2.Left + m_Label2.Width + 10; this.m_LabelOpponentScore.Top = m_Label1.Top; this.m_LabelOpponentScore.Text = "0"; this.m_LabelOpponentScore.AutoSize = true; }