public bool CanMove(FigureMoving fm) { this.fm = fm; return(CanMoveFrom() && CanMoveTo() && CanFigureMove()); }
void EnPassantAttack(FigureMoving fm) { Square midSquare = new Square(enPassant); Figure pawn = moveColor == Color.white ? Figure.whitePawn : Figure.blackPawn; if (fm.to == midSquare && fm.figure == pawn)// en passant { InsertFigure(new Square(midSquare.x, fm.from.y), Figure.none); } }
void SetEnPassant(FigureMoving fm) { if (fm.figure == Figure.whitePawn || fm.figure == Figure.blackPawn) { if (fm.AbsDeltaY == 2)// jump is only possible if a pawn moves from Y:1 or Y:6 { int y = fm.to.y - fm.SignDeltaY; Square midSquare = new Square(fm.to.x, y); enPassant = midSquare.ToString(); return; } } enPassant = "-";// en passant only has power during one move }
void MoveCastleRook(FigureMoving fm) { if ((fm.figure == Figure.whiteKing || fm.figure == Figure.blackKing) && fm.AbsDeltaX == 2 && fm.AbsDeltaY == 0) { Figure rook = fm.figure == Figure.whiteKing ? Figure.whiteRook : Figure.blackRook; if (fm.SignDeltaX == 1)// kingside castle { InsertFigure(new Square(7, fm.from.y), Figure.none); InsertFigure(new Square(fm.to.x + (fm.SignDeltaX * -1), fm.to.y), rook); } else if (fm.SignDeltaX == -1)// queenside castle { InsertFigure(new Square(0, fm.from.y), Figure.none); InsertFigure(new Square(fm.to.x + (fm.SignDeltaX * -1), fm.to.y), rook); } } }
void UpdateCastleFlags(FigureMoving fm) { // we move our king or rook Figure king = moveColor == Color.white ? Figure.whiteKing : Figure.blackKing; Figure rook = moveColor == Color.white ? Figure.whiteRook : Figure.blackRook; if (fm.figure == king) { DisableQueensideCastle(moveColor); DisableKingsideCastle(moveColor); } else if (fm.figure == rook) { if (fm.from.x == 0) { DisableQueensideCastle(moveColor); } else if (fm.from.x == 7) { DisableKingsideCastle(moveColor); } } }
public Board Move(FigureMoving fm) { Board nextBoard = new Board(fen); nextBoard.InsertFigure(fm.from, Figure.none); nextBoard.InsertFigure(fm.to, fm.promotion == Figure.none ? fm.figure : fm.promotion); nextBoard.EnPassantAttack(fm); nextBoard.SetEnPassant(fm); nextBoard.MoveCastleRook(fm); nextBoard.UpdateCastleFlags(fm); if (moveColor == Color.black) { nextBoard.moveNumber++; } nextBoard.moveColor = moveColor.FlipColor(); // end of the move nextBoard.GenerateFen(); //apply the move to current board state return(nextBoard); }
public bool IsCheckAfterMove(FigureMoving fm) { Board afterBoard = Move(fm); return(afterBoard.CanAttackEnemyKing()); }