private void Click_MovePiece(object sender, MouseButtonEventArgs e) { Rectangle rect = (Rectangle)sender; int row = (int)rect.GetValue(Grid.RowProperty); int column = (int)rect.GetValue(Grid.ColumnProperty); Coord pos = GridPointToCoordinate(new Point(column, row)); PieceControl p = _pieces.Where(x => x.Piece.Position == pos && _lastClicked.Piece.Color != x.Piece.Color).FirstOrDefault(); if (p != null) // TODO This doesn't remove the PieceControl from the Chessboard { Pieces.Children.Remove(p); _pieces.Remove(p); } _lastClicked.Piece.Position = pos; _lastClicked.SetValue(Grid.RowProperty, row); _lastClicked.SetValue(Grid.ColumnProperty, column); _lastClicked = null; Overlay.Children.Clear(); }
private void Click_ShowPieceMoves(object sender, System.Windows.Input.MouseButtonEventArgs e) { _lastClicked = ((PieceControl)sender); Overlay.Children.Clear(); List <Coord> movePositions = _lastClicked.Piece.MovePositions(); List <Piece> blockingPieces = new List <Piece>(); foreach (Coord c in movePositions) { blockingPieces.AddRange(_pieces.Where(x => x.Piece.Position == c).Select(x => x.Piece)); } foreach (Piece pc in blockingPieces) { // Remove vertical blocked spaces if (pc.Position.Column == _lastClicked.Piece.Position.Column) { // remove all move positions above pc if (pc.Position.Row > _lastClicked.Piece.Position.Row) { movePositions.RemoveAll(x => x.Row > pc.Position.Row && x.Column == pc.Position.Column); } // remove all move positions bellow pc else if (pc.Position.Row < _lastClicked.Piece.Position.Row) { movePositions.RemoveAll(x => x.Row < pc.Position.Row && x.Column == pc.Position.Column); } } // Remove horizontal blocked spaces else if (pc.Position.Row == _lastClicked.Piece.Position.Row) { // Remove possitions right of pc if (pc.Position.Column > _lastClicked.Piece.Position.Column) { movePositions.RemoveAll(x => x.Row == pc.Position.Row && x.Column > pc.Position.Column); } // Remove positions left of pc else if (pc.Position.Column < _lastClicked.Piece.Position.Column) { movePositions.RemoveAll(x => x.Row == pc.Position.Row && x.Column < pc.Position.Column); } } // Remove all diagonal blocked spaces else if (Math.Abs(pc.Position.Row - _lastClicked.Piece.Position.Row) == Math.Abs(pc.Position.Column - _lastClicked.Piece.Position.Column)) { if (pc.Position.Row > _lastClicked.Piece.Position.Row) { // Top Right if (pc.Position.Column > _lastClicked.Piece.Position.Column) { movePositions.RemoveAll(x => x.Row > pc.Position.Row && x.Column > pc.Position.Column); } // Top Left else if (pc.Position.Column < _lastClicked.Piece.Position.Column) { movePositions.RemoveAll(x => x.Row > pc.Position.Row && x.Column < pc.Position.Column); } } else if (pc.Position.Row < _lastClicked.Piece.Position.Row) { // Bottom Right if (pc.Position.Column > _lastClicked.Piece.Position.Column) { movePositions.RemoveAll(x => x.Row <pc.Position.Row && x.Column> pc.Position.Column); } // Bottom Left else if (pc.Position.Column < _lastClicked.Piece.Position.Column) { movePositions.RemoveAll(x => x.Row < pc.Position.Row && x.Column < pc.Position.Column); } } } } foreach (Coord c in movePositions) { Rectangle rect = new Rectangle() { Fill = Brushes.Green, Opacity = 0.5 }; rect.MouseDown += Click_MovePiece; PieceControl pc = _pieces.Where(x => x.Piece.Position == c).FirstOrDefault(); if (pc != null) { if (pc.Piece.Color != _lastClicked.Piece.Color) { rect.Fill = Brushes.Red; } else { continue; } } if (_pieces.Any(x => x.Piece.Position == c && x.Piece.Color != _lastClicked.Piece.Color)) { rect.Fill = Brushes.Red; } Point pos = CoordinateToGridPoint(c); rect.SetValue(Grid.RowProperty, (int)pos.Y); rect.SetValue(Grid.ColumnProperty, (int)pos.X); Overlay.Children.Add(rect); } e.Handled = true; }