private static bool IsTargetGridInRange(ChessPiece piece, ChessBoardGrid targetGrid, IList <ChessBoardGrid> boardGrids) { //a leopard is able to go anywhere if (piece.PieceType == PieceType.Leopard) { return(true); } var containerGrid = boardGrids.First(x => x.Pieces.Contains(piece)); if ((containerGrid.Pos.Y == targetGrid.Pos.Y && Math.Abs(containerGrid.Pos.X - targetGrid.Pos.X) == 1) || (containerGrid.Pos.X == targetGrid.Pos.X && Math.Abs(containerGrid.Pos.Y - targetGrid.Pos.Y) == 1)) { return(true); } return(false); }
internal static void TryMove(ChessBoardGrid targetGrid, ChessBoard chessBoard) { var selectedPiece = chessBoard.BoardGrids.SelectMany(x => x.Pieces).FirstOrDefault(y => y != null && y.Selected); if (selectedPiece == null || !IsTargetGridInRange(selectedPiece, targetGrid, chessBoard.BoardGrids)) { return; } var containerGrid = chessBoard.BoardGrids.First(x => x.Pieces.Contains(selectedPiece)); var indexOfPieceInGrid = containerGrid.Pieces.IndexOf(selectedPiece); if (targetGrid.TryMovePieceTo(selectedPiece)) { containerGrid.Pieces[indexOfPieceInGrid] = null; FinishCurrentTurn(chessBoard); } }