public void OnSquareClicked(int row, int column) { int square = SquareHelper.MakeSquare(row, column); List <ushort> validMoves = this.position.GenerateValidMoves(); if (validMoves.Count == 0) { return; } if (this.previousSquareSelected == -1) { // Select a square if (this.position.Board[square] != Pieces.Empty) { if ((this.position.Board[square] & 0x8) == position.ToMove) { this.squares[row][column].Highlight = true; this.previousSquareSelected = square; // this.StatusText = "Select square to move to"; } else { // this.StatusText = String.Format("You must select a {0} piece", position.ToMove == Pieces.White ? "White" : "Red"); } } } else if (this.previousSquareSelected == square) { // Deselect the square this.squares[row][column].Highlight = false; this.previousSquareSelected = -1; // this.StatusText = "Select piece to move"; } else { // Find a move int from = this.previousSquareSelected; int to = square; ushort validMove = 0; foreach (ushort move in validMoves) { if (MoveHelper.From(move) == from && MoveHelper.To(move) == to) { validMove = move; } } if (validMove == 0) { // this.StatusText = "Invalid move!"; return; } this.squares[SquareHelper.GetRow(from)][SquareHelper.GetColumn(from)].Highlight = false; this.previousSquareSelected = -1; if (this.UserMove != null) { this.UserMove(validMove); } } }