public void ApplyMovement(Piece piece, Square oldSquare, Square newSquare) { if (newSquare.Piece != null) return; PieceMovement movement = this.GetValidMovement(piece, oldSquare, newSquare); if (movement == null) return; oldSquare.Piece = null; newSquare.Piece = piece; DrawingManager.Instance.DrawableObjects.Add(oldSquare); DrawingManager.Instance.DrawableObjects.Add(newSquare); piece.Selected = false; if (movement.JumpPosition != null) { BoardPosition killPosition = oldSquare.BoardPosition + movement.JumpPosition; Square killSquare = _draughts.Board[killPosition]; killSquare.Piece = null; DrawingManager.Instance.DrawableObjects.Add(killSquare); } if (piece is CommonPiece && ((piece.BoardPosition.I == 7 && piece.Color == PieceColor.Black) || (piece.BoardPosition.I == 0 && piece.Color == PieceColor.White))) { newSquare.Piece = new QueenPiece(piece.Color, piece.BoardPosition); if (this.PieceUpgraded != null) this.PieceUpgraded(this, new PieceEventArgs(newSquare.Piece)); _draughts.ChangePlayer(); } else this.ChangePlayerIfNeeded(piece, movement); this.CheckPlayerVictory(); }
private PieceMovement GetValidMovement(Piece piece, Square oldSquare, Square newSquare) { if (newSquare.Piece != null) return null; List<PieceMovement> pieceValidMovements = this.GetValidMovements(piece); PieceMovement newMovement = pieceValidMovements.FirstOrDefault(m => (oldSquare.BoardPosition + m.BoardMovement).Equals(newSquare.BoardPosition)); if (newMovement == null) return null; if (newMovement.JumpPosition == null) { List<PieceMovement> allPiecesValidMovements = this.GetPlayerValidMovements(_draughts.CurrentPlayer); return allPiecesValidMovements.FirstOrDefault(m => m.JumpPosition != null) == null ? newMovement : null; } else if (!(piece is QueenPiece)) { if (this.CheckIfPlayerHasQueenJumpMovements()) return null; } return newMovement; }
public bool ValidateNewPosition(Piece piece, Square oldSquare, Square newSquare) { PieceMovement movement = this.GetValidMovement(piece, oldSquare, newSquare); //newSquare.Invalid = movement == null; if (movement == null) return false; return true; }