Exemple #1
0
        // Starts building a move according to chosen square
        private void squareClicked(SquareButton i_SquareButton)
        {
            string currentSquareLocationString;
            Square square        = m_Game.Board.GameBoard[i_SquareButton.RowIndex, i_SquareButton.ColIndex];
            Piece  pieceAtSquare = square.CurrentPiece;

            // First click- build the move's starting position
            if (m_CurrentMove.ToString() == string.Empty && pieceAtSquare != null)
            {
                if (pieceAtSquare.Side == m_Game.CurrentPlayer.Side)
                {
                    i_SquareButton.BackColor    = Color.Cyan;
                    currentSquareLocationString = MoveValidator.ConvertLocationToString(
                        i_SquareButton.RowIndex,
                        i_SquareButton.ColIndex);
                    m_CurrentMove.Append(currentSquareLocationString);
                    m_SelectedSquare = i_SquareButton;
                }
            }
            // Second click- add the move's ending position and execute it
            else if (m_CurrentMove.ToString() != string.Empty && pieceAtSquare == null)
            {
                m_SelectedSquare.BackColor  = Color.BurlyWood;
                currentSquareLocationString = MoveValidator.ConvertLocationToString(
                    i_SquareButton.RowIndex,
                    i_SquareButton.ColIndex);
                m_CurrentMove.Append(">");
                m_CurrentMove.Append(currentSquareLocationString);
                Move selectedMove = MoveValidator.ConvertStringToMove(m_CurrentMove.ToString());
                m_Game.ExecuteMove(selectedMove);
                m_CurrentMove.Clear();
            }
            // Second click on the same square- cancel the chosen square (reset the move)
            else if (pieceAtSquare != null && pieceAtSquare == m_Game.Board.GameBoard[m_SelectedSquare.RowIndex, m_SelectedSquare.ColIndex].CurrentPiece)
            {
                m_SelectedSquare.BackColor = Color.BurlyWood;
                m_SelectedSquare           = null;
                m_CurrentMove.Clear();
            }
        }