public override List <Point> GetValidMoves() { ChessPiece[,] board = ChessBoard.GetBoard; List <Point> validMoves = new List <Point>(); for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { if (this.Color == Color.Black) { if (j == Coordinates.Y && i > Coordinates.X) { if (i == Coordinates.X + 1 || i == Coordinates.X + 2 && hasStepped is false) { validMoves.Add(new Point(i, j)); } } } else { if ((j == Coordinates.Y && i < Coordinates.X)) { if (i == Coordinates.X - 1 || i == Coordinates.X - 2 && hasStepped is false) { validMoves.Add(new Point(i, j)); } } } } } validMoves = RemoveInvalidMoves(validMoves); List <Point> situationalSteps; if (this.Color == Color.Black) { situationalSteps = new List <Point>() { new Point(Coordinates.X + 1, Coordinates.Y - 1), new Point(Coordinates.X + 1, Coordinates.Y + 1) }; } else { situationalSteps = new List <Point>() { new Point(Coordinates.X - 1, Coordinates.Y - 1), new Point(Coordinates.X - 1, Coordinates.Y + 1) }; } foreach (Point situationalStep in situationalSteps) { if (situationalStep.X >= 0 && situationalStep.X < board.GetLength(1) && situationalStep.Y >= 0 && situationalStep.Y < board.GetLength(0)) { ChessPiece chessPiece = board[(int)situationalStep.X, (int)situationalStep.Y]; if (chessPiece == null || chessPiece.GetColor == this.Color) { situationalSteps = situationalSteps.Where(step => step != situationalStep).ToList(); } } else { situationalSteps = situationalSteps.Where(step => step != situationalStep).ToList(); } } validMoves = validMoves.Concat(situationalSteps).ToList(); return(validMoves); }
private void MoveChessPiece(object sender, MouseButtonEventArgs e) { Border border = (Border)sender; Border highlighted = (Border)mainWindow.FindName("highlighted"); if (highlighted != null) { int originX = Grid.GetRow(highlighted); int originY = Grid.GetColumn(highlighted); ChessPiece currentChessPiece = GetBoard[originX, originY]; if (border.Child == null || currentChessPiece.GetColor == currentTurnColor) { bool kingIsChecked = KingIsChecked(currentTurnColor); int destinationX = Grid.GetRow(border); int destinationY = Grid.GetColumn(border); if (currentChessPiece.MoveResolvesCheck(currentTurnColor, new Point(destinationX, destinationY))) { List <Point> validMoves = null; if (!KingIsChecked(currentTurnColor)) { validMoves = currentChessPiece.GetValidMoves(); } else { validMoves = currentChessPiece.GetValidMovesIfKingIsChecked(); } if (validMoves.Where(point => point.X == destinationX && point.Y == destinationY).Any()) { if (currentChessPiece.GetType().Name == "Pawn") { ((Pawn)currentChessPiece).SetHasStepped = true; } currentChessPiece.SetCoordinates = new Point(destinationX, destinationY); GetBoard[destinationX, destinationY] = GetBoard[originX, originY]; GetBoard[originX, originY] = null; currentTurnColor = currentTurnColor == Color.White ? Color.Black : Color.White; UpdateBoard(); mainWindow.UnregisterName(highlighted.Name); if (IsCheckMate(currentTurnColor)) { MessageBox.Show(currentTurnColor.ToString() + " has lost"); } } else { ColorTile(highlighted); List <Point> highlightedValidMoves = null; if (!KingIsChecked(currentTurnColor)) { highlightedValidMoves = currentChessPiece.GetValidMoves(); } else { highlightedValidMoves = currentChessPiece.GetValidMovesIfKingIsChecked(); } UnshowValidMoves(highlightedValidMoves); mainWindow.UnregisterName(highlighted.Name); } } else { ColorTile(highlighted); List <Point> highlightedValidMoves = null; if (!KingIsChecked(currentTurnColor)) { highlightedValidMoves = currentChessPiece.GetValidMoves(); } else { highlightedValidMoves = currentChessPiece.GetValidMovesIfKingIsChecked(); } UnshowValidMoves(highlightedValidMoves); mainWindow.UnregisterName(highlighted.Name); } } } }