Exemple #1
0
    public Pawn UnpromotePawn(Board board, Move toCancel)
    {
        ChessboardBoxData birthCase = getBox(toCancel.destination);
        Pawn        pawn            = new Pawn();
        ChessPiece  promotedPiece   = toCancel.movingPiece;
        TeamManager playingTeam     = promotedPiece.team;

        SetPieceOnBox(pawn, promotedPiece.currentPosition, playingTeam);

        playingTeam.pieces.Remove(promotedPiece);
        switch (promotedPiece)
        {
        case Rook rook:
            playingTeam.rooks.Remove(rook);
            break;

        case Knight knight:
            playingTeam.knights.Remove(knight);
            break;

        case Bishop bishop:
            playingTeam.bishops.Remove(bishop);
            break;

        case Queen queen:
            playingTeam.queens.Remove(queen);
            break;

        default:
            throw new System.Exception("The unpromoted piece must be a rook, a knight, a bishop or a queen");
        }

        pawn.name = promotedPiece.name.Replace("_Promoted", "");
        return(pawn);
    }
Exemple #2
0
    public override void Move(Board board, int testedLine, int testedColumn)
    {
        Vector2           originPosition = currentPosition;
        ChessboardBoxData origin         = board.getBox(originPosition);
        ChessboardBoxData destination    = board.getBox(testedColumn, testedLine);
        ChessPiece        otherPiece     = destination.piece;
        bool isCastling = false;

        //manage castling case
        if (otherPiece != null && otherPiece.canCastling && otherPiece.team == team)
        {
            //piece swapping
            origin.piece = otherPiece;
            otherPiece.Moved(originPosition);
            isCastling = true;
        }
        //make the normal move
        else
        {
            origin.piece = null;
            if (otherPiece != null)
            {
                otherPiece.Captured();
            }
        }

        destination.piece = this;
        Move moveToSave = new Move(originPosition, new Vector2(testedColumn, testedLine), this, otherPiece, isCastling);

        ChessEngine.instance.game.Add(moveToSave);
        //Debug.Log("MOVE SAVED " + moveToSave.movingPiece.name + " " + moveToSave.destination);
        Moved(new Vector2(testedColumn, testedLine));
    }
Exemple #3
0
    //test if a movement is valid
    public override bool isValidMovement(Board board, int testedColumn, int testedLine, out bool pieceTaken, bool isSimulation)
    {
        pieceTaken = false;
        // the destination is out of the board
        if (testedLine < 0 || testedLine > 7 || testedColumn < 0 || testedColumn > 7)
        {
            return(false);
        }

        ChessboardBoxData testedBox  = board.boxesDatas[testedColumn][testedLine];
        ChessPiece        takenPiece = testedBox.piece;

        ChessPiece enPassantPiece = null;

        enPassantPiece = board.boxesDatas[testedColumn][testedLine - (int)team.teamForward.y].piece;

        bool tryCapture = testedColumn != (int)currentPosition.x;

        //pawn try a capture
        if (tryCapture)
        {
            if (takenPiece == null)
            {
                //Manage enPassant case
                bool availableEnPassant = enPassantPiece != null &&
                                          enPassantPiece.team != team &&
                                          enPassantPiece.enPassantAllowed;

                if (!availableEnPassant)
                {
                    return(false);
                }
                else
                {
                    availableEnPassantPosition = new Vector2(testedColumn, testedLine);
                    enPassantStoredPiece       = enPassantPiece;
                }
            }
            else
            {
                pieceTaken = true;
                //the box contains a friend piece movement is not valid
                if (takenPiece.team == team)
                {
                    return(false);
                }
            }
        }
        else
        {
            if (takenPiece != null)
            {
                pieceTaken = true;
                return(false);
            }
        }

        return(FinalValidation(board, testedColumn, testedLine, isSimulation));
    }
Exemple #4
0
 //revert some piece values after special move
 public virtual void RevertValuesIfNecessary(ChessboardBoxData box, bool liberated)
 {
     currentPosition = new Vector2(box.column, box.line);
     hasMoved        = ChessEngine.instance.hasMovedInPreviousMove(this);
     if (liberated)
     {
         Liberated();
     }
     else
     {
         ChessEngine.instance.reverted.Add(this);
     }
 }
Exemple #5
0
    public virtual void ForcedMove(Board board, Move move)
    {
        ChessboardBoxData originBox      = board.getBox(move.origin);
        ChessboardBoxData destinationBox = board.getBox(move.destination);

        if (move.capturedPiece != null)
        {
            move.capturedPiece.Captured();
        }
        originBox.piece      = null;
        destinationBox.piece = this;
        //update values again after forced move
        RedoValuesIfNecessary(destinationBox);
    }
Exemple #6
0
 public override void RedoValuesIfNecessary(ChessboardBoxData box)
 {
     base.RedoValuesIfNecessary(box);
     if (hasMoved)
     {
         Move penultimateMove = ChessEngine.instance.GetMyPenultimateMove(this);
         if (penultimateMove != null)
         {
             enPassantAllowed = Mathf.Abs(penultimateMove.destination.y - penultimateMove.origin.y) == 2;
         }
     }
     else
     {
         maxRange         = 2;
         enPassantAllowed = false;
     }
 }
Exemple #7
0
    public ChessPiece PromotePawn(Vector2 destination, PieceType type)
    {
        ChessboardBoxData birthCase = getBox((int)destination.x, (int)destination.y);
        Pawn        pawn            = (Pawn)birthCase.piece;
        TeamManager playingTeam     = pawn.team;

        ChessPiece newPiece = null;

        switch (type)
        {
        case PieceType.Bishop:
            Bishop bishop = new Bishop();
            playingTeam.bishops.Add(bishop);
            newPiece = bishop;
            break;

        case PieceType.Knight:
            Knight knight = new Knight();
            playingTeam.knights.Add(knight);
            newPiece = knight;
            break;

        case PieceType.Rook:
            Rook rook = new Rook();
            rook.canCastling = false;
            playingTeam.rooks.Add(rook);
            newPiece = rook;
            break;

        default:
            Queen queen = new Queen();
            playingTeam.queens.Add(queen);
            newPiece = queen;
            break;
        }

        newPiece.name     = pawn.name + "_promoted_" + type;
        newPiece.hasMoved = true;
        SetPieceOnBox(newPiece, pawn.currentPosition, playingTeam);

        playingTeam.pieces.Remove(pawn);
        playingTeam.pawns.Remove(pawn);

        return(newPiece);
    }
Exemple #8
0
    public virtual void Move(Board board, int testedLine, int testedColumn)
    {
        ChessboardBoxData originBox      = board.getBox(currentPosition);
        ChessboardBoxData destinationBox = board.getBox(testedColumn, testedLine);
        ChessPiece        otherPiece     = destinationBox.piece;

        if (otherPiece != null)
        {
            otherPiece.Captured();
        }

        originBox.piece      = null;
        destinationBox.piece = this;

        Move moveToSave = new Move(currentPosition, new Vector2(testedColumn, testedLine), this, otherPiece, false);

        ChessEngine.instance.game.Add(moveToSave);
        Moved(new Vector2(testedColumn, testedLine));
    }
Exemple #9
0
    //test if a movement is valid
    public override bool isValidMovement(Board board, int testedColumn, int testedLine, out bool pieceTaken, bool isSimulation)
    {
        pieceTaken = false;
        // the destination is out of the board
        if (testedLine < 0 || testedLine > 7 || testedColumn < 0 || testedColumn > 7)
        {
            return(false);
        }

        ChessboardBoxData testedBox  = board.boxesDatas[testedColumn][testedLine];
        ChessPiece        takenPiece = testedBox.piece;
        float             moveRange  = Vector2.Distance(currentPosition, new Vector2(testedColumn, testedLine));

        if (takenPiece != null)
        {
            pieceTaken = true;
            //Allow castling
            if (moveRange >= 3)
            {
                if (takenPiece.team != team)
                {
                    return(false);
                }
                else
                {
                    if (!canCastling || !takenPiece.canCastling)
                    {
                        return(false);
                    }
                }
            }
            //manage capture
            else
            {
                if (takenPiece.team == team)
                {
                    return(false);
                }
            }
        }

        return(FinalValidation(board, testedColumn, testedLine, isSimulation));
    }
Exemple #10
0
    public virtual void ForcedReverseMove(Board board, Move move)
    {
        ChessboardBoxData originBox      = board.getBox(move.destination);
        ChessboardBoxData destinationBox = board.getBox(move.origin);

        if (move.capturedPiece != null)
        {
            originBox       = board.getBox(move.capturedPiece.currentPosition);
            originBox.piece = move.capturedPiece;
            move.capturedPiece.RevertValuesIfNecessary(originBox, true);
        }
        else
        {
            originBox.piece = null;
        }

        destinationBox.piece = this;
        RevertValuesIfNecessary(destinationBox, false);
    }
Exemple #11
0
    Board ConstructBoard()
    {
        bool isWhite = true;
        List <List <ChessboardBoxData> > chessboardBoxes = new List <List <ChessboardBoxData> >();

        for (int caseColumn = 0; caseColumn < 8; caseColumn++)
        {
            List <ChessboardBoxData> currentColumn = new List <ChessboardBoxData>();
            for (int caseLine = 0; caseLine < 8; caseLine++)
            {
                ChessboardBoxData box = new ChessboardBoxData(isWhite ? ChessColor.White : ChessColor.Black, caseLine, caseColumn);
                currentColumn.Add(box);
                isWhite = !isWhite;
            }
            chessboardBoxes.Add(currentColumn);
            isWhite = !isWhite;
        }

        return(new Board(chessboardBoxes));
    }
Exemple #12
0
    public override void ForcedMove(Board board, Move move)
    {
        ChessboardBoxData originBox      = board.getBox(move.origin);
        ChessboardBoxData destinationBox = board.getBox(move.destination);

        if (move.capturedPiece != null)
        {
            move.capturedPiece.Captured();
        }
        else if (move.castlingPiece != null)
        {
            originBox.piece = move.castlingPiece;
            move.castlingPiece.RedoValuesIfNecessary(originBox);
        }
        else
        {
            originBox.piece = null;
        }
        destinationBox.piece = this;
        RedoValuesIfNecessary(destinationBox);
    }
Exemple #13
0
    public void SetPieceOnBox(ChessPiece piece, int lineDestination, int columnDestination, TeamManager team)
    {
        //get the chessboard box
        ChessboardBoxData target = ChessEngine.instance.board.getBox(columnDestination, lineDestination);

        //assign a piece to the targeted chessboard box
        target.piece = piece;
        //assign the piece team
        piece.team            = team;
        piece.currentPosition = new Vector2(columnDestination, lineDestination);
        piece.SetMovementLimit();
        //add the piece in team piece's list
        team.pieces.Add(piece);
        switch (piece)
        {
        case Rook rook:
            team.rooks.Add(rook);
            break;

        case Bishop bishop:
            team.bishops.Add(bishop);
            break;

        case Knight knight:
            team.knights.Add(knight);
            break;

        case Queen queen:
            team.queens.Add(queen);
            break;

        case Pawn pawn:
            team.pawns.Add(pawn);
            break;

        default:
            break;
        }
    }
Exemple #14
0
    public override void ForcedReverseMove(Board board, Move move)
    {
        ChessboardBoxData originBox      = board.getBox(move.destination);
        ChessboardBoxData destinationBox = board.getBox(move.origin);

        if (move.capturedPiece != null)
        {
            originBox.piece = move.capturedPiece;
            move.capturedPiece.RevertValuesIfNecessary(originBox, true);
        }
        else if (move.castlingPiece != null)
        {
            originBox.piece = move.castlingPiece;
            move.castlingPiece.RevertValuesIfNecessary(originBox, false);
        }
        else
        {
            originBox.piece = null;
        }
        destinationBox.piece = this;
        RevertValuesIfNecessary(destinationBox, false);
        //Debug.Log("REVERTED");
    }
Exemple #15
0
    //test if a movement is valid
    public virtual bool isValidMovement(Board board, int testedColumn, int testedLine, out bool pieceTaken, bool isSimulation)
    {
        pieceTaken = false;
        // the destination is out of the board
        if (testedLine < 0 || testedLine > 7 || testedColumn < 0 || testedColumn > 7)
        {
            return(false);
        }

        ChessboardBoxData testedBox  = board.boxesDatas[testedColumn][testedLine];
        ChessPiece        takenPiece = testedBox.piece;

        //the box contains a friend piece movement is not valid
        if (takenPiece != null)
        {
            pieceTaken = true;
            if (takenPiece.team == team)
            {
                return(false);
            }
        }

        return(FinalValidation(board, testedColumn, testedLine, isSimulation));
    }
Exemple #16
0
    //test if a movement is valid
    public override bool isValidMovement(Board board, int testedColumn, int testedLine, out bool pieceTaken, bool isSimulation)
    {
        pieceTaken = false;
        // the destination is out of the board
        if (testedLine < 0 || testedLine > 7 || testedColumn < 0 || testedColumn > 7)
        {
            return(false);
        }

        ChessboardBoxData testedBox  = board.boxesDatas[testedColumn][testedLine];
        ChessPiece        takenPiece = testedBox.piece;

        //Allow castling
        if (takenPiece != null)
        {
            pieceTaken = true;
            if (takenPiece.team == team && (!canCastling || !takenPiece.canCastling))
            {
                return(false);
            }
        }

        return(FinalValidation(board, testedColumn, testedLine, isSimulation));
    }
Exemple #17
0
 //update values again after forced move
 public virtual void RedoValuesIfNecessary(ChessboardBoxData box)
 {
     currentPosition = new Vector2(box.column, box.line);
     hasMoved        = ChessEngine.instance.hasMovedInPreviousMove(this);
     ChessEngine.instance.movedDuringThisTurn.Add(this);
 }
Exemple #18
0
 public override void RevertValuesIfNecessary(ChessboardBoxData box, bool liberated)
 {
     base.RevertValuesIfNecessary(box, liberated);
     canCastling = !hasMoved;
 }
Exemple #19
0
 public override void RedoValuesIfNecessary(ChessboardBoxData box)
 {
     base.RedoValuesIfNecessary(box);
     canCastling = !hasMoved;
 }