public List <ValidBoardMove> ExecuteFunctionOnCellsToLeft(Cell cell, Func <Cell, Cell, ValidBoardMove.MovePath, ValidBoardMove> func) { ValidBoardMove.MovePath path = ValidBoardMove.MovePath.Left; List <ValidBoardMove> moveList = new List <ValidBoardMove>(); for (var i = cell.ColumnNumber; i > 0; i--) { if (BoardProperties.ColumnNumbersMappedToLetters.TryGetValue(i, out char columnLetter)) { var sequentialCell = getCell(columnLetter.ToString() + cell.Row); if (Cell.IsNotNull(sequentialCell)) { if (sequentialCell.ColumnLetter + sequentialCell.Row.ToString() == cell.Coordinates) { continue; } var move = func(cell, sequentialCell, path); if (move == null) { break; } if (move.MoveProperties.IsValid || move.MoveProperties.IsAbsolutePinned) { moveList.Add(move); } if (move.MoveProperties.IsTerminatable) { break; } } } } return(moveList); }
private ValidBoardMove DetermineAllCellsOnPath(Cell fromCell, Cell toCell, ValidBoardMove.MovePath path) { var move = new ValidBoardMove(fromCell.Coordinates, toCell.Coordinates, path, isWhite); move.MoveProperties.IsValid = true; return(move); }
public List <ValidBoardMove> ExecuteFunctionOnCellsDown(Cell cell, Func <Cell, Cell, ValidBoardMove.MovePath, ValidBoardMove> func) { ValidBoardMove.MovePath path = ValidBoardMove.MovePath.Down; List <ValidBoardMove> moveList = new List <ValidBoardMove>(); for (var i = cell.Row; i > 0; i--) { var sequentialCell = getCell(cell.ColumnLetter.ToString() + i.ToString()); if (Cell.IsNotNull(sequentialCell)) { if (sequentialCell.ColumnLetter + sequentialCell.Row.ToString() == cell.Coordinates) { continue; } var move = func(cell, sequentialCell, path); if (move == null) { break; } if (move.MoveProperties.IsValid || move.MoveProperties.IsAbsolutePinned) { moveList.Add(move); } if (move.MoveProperties.IsTerminatable) { break; } } } return(moveList); }
public ValidBoardMove DetermineIfCellValid(Cell fromCell, Cell toCell, ValidBoardMove.MovePath path) { ValidNotationProperties validMoveProps = this.CellIsValid(fromCell, toCell); var move = new ValidBoardMove(fromCell.Coordinates, toCell.Coordinates, path, this.isWhite); if (validMoveProps.IsValid || validMoveProps.IsProtected) { this.PiecePressure.Add(move); } if (validMoveProps.IsPotentiallyPinned) { this.startingCell = fromCell; this.PinnedCell = toCell; DetermineIfAbsolutePinned(toCell, path); } move.MoveProperties = validMoveProps; return(move); }
private ValidBoardMove DetermineIfCellPinned(Cell pinnedCell, Cell potentialCell, ValidBoardMove.MovePath path) { var piece = Cell.GetPiece(potentialCell); if (piece == EnemyKing) { if (piece != null) { return(new ValidBoardMove(pinnedCell.Coordinates, potentialCell.Coordinates, path, piece.isWhite, true)); } return(null); } else { return(new ValidBoardMove(pinnedCell.Coordinates, potentialCell.Coordinates, path)); } }
private void DetermineIfAbsolutePinned(Cell pinnedCell, ValidBoardMove.MovePath path) { List <ValidBoardMove> test = new List <ValidBoardMove>(); switch (path) { case ValidBoardMove.MovePath.Down: test = ExecuteFunctionOnCellsDown(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.Up: test = ExecuteFunctionOnCellsUp(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.Right: test = ExecuteFunctionOnCellsToRight(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.Left: test = ExecuteFunctionOnCellsToLeft(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.DownLeft: test = ExecuteFunctionOnCellsDownLeft(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.DownRight: test = ExecuteFunctionOnCellsDownRight(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.UpLeft: test = ExecuteFunctionOnCellsUpLeft(pinnedCell, DetermineIfCellPinned); break; case ValidBoardMove.MovePath.UpRight: test = ExecuteFunctionOnCellsUpRight(pinnedCell, DetermineIfCellPinned); break; default: break; } List <ValidBoardMove> arePinned = test.Where(x => x.IsPinningMove == true && x.CoordinatesTo == EnemyKing.CurrentCoordinates).ToList(); if (arePinned.Count == 1) { var movesOnPath = FindPiecesBetween(arePinned[0]); var distinctCoords = new List <string>(); var filteredMoves = movesOnPath.Where(move => { var cell = this.getCell(move.CoordinatesTo); if (Cell.HasPiece(cell)) { var piece = Cell.GetPiece(cell); if (piece.isWhite != isWhite) { int index = distinctCoords.IndexOf(move.CoordinatesTo); if (index == -1) { distinctCoords.Add(move.CoordinatesTo); return(true); } } } return(false); }).ToList(); if (filteredMoves.Count == 1) { // we are absolutely pinned hgere var piece = Cell.GetPiece(this.PinnedCell); piece.PinnedMoves = test; if (piece.TypeOfPiece == PieceType.Rook) { var rook = (Rook)piece; rook.DetermineValidMoves(rook.CurrentCoordinates, null); } else if (piece.TypeOfPiece == PieceType.Bishop) { var bishop = (Bishop)piece; bishop.DetermineValidMoves(bishop.CurrentCoordinates, null); } else if (piece.TypeOfPiece == PieceType.Pawn) { var pawn = (Pawn)piece; pawn.DetermineValidMoves(pawn.CurrentCoordinates, null); } else if (piece.TypeOfPiece == PieceType.King) { throw new NotImplementedException(); } else if (piece.TypeOfPiece == PieceType.Queen) { var queen = (Queen)piece; queen.DetermineValidMoves(queen.CurrentCoordinates, null); } else if (piece.TypeOfPiece == PieceType.Knight) { var knight = (Knight)piece; knight.DetermineValidMoves(knight.CurrentCoordinates, null); } } } }