Ejemplo n.º 1
0
    public static bool GetKingMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos )
    {
        ChessPosition srcPos = selSquare.position;
        PlayerSide srcPlayerSide = selSquare.piece.playerSide;

        ChessPosition movePos = new ChessPosition(srcPos.pos);

        // all(radial) direction one move
        int nTempRank, nTempPile;

        for( int nMovePile=-1; nMovePile<=1; nMovePile++ ) {
            for( int nMoveRank=-1; nMoveRank<=1; nMoveRank++ ) {

                nTempRank = nMoveRank;
                nTempPile = nMovePile;

                movePos.SetPosition( srcPos );
                bool bValidMove = movePos.MovePosition( nTempRank, nTempPile );
                if( bValidMove ) {

                    ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
                    // normal move
                    if( trgSquare.IsBlank() ) {

                        sMove move = new sMove();
                        // normal move
                        move.moveType = MoveType.eNormal_Move;

                        move.srcSquare = selSquare;
                        move.trgSquare = trgSquare;

                        listRetBoardPos.Add( move );
                    }
                    // capture move
                    else if( trgSquare.IsEnemy( srcPlayerSide ) ) {

                        sMove move = new sMove();
                        // normal move
                        move.moveType = MoveType.eCapture_Move;

                        move.srcSquare = selSquare;
                        move.trgSquare = trgSquare;

                        listRetBoardPos.Add( move );
                    }
                }
            }
        }

        // castling move
        // king side castling
        movePos.SetPosition( srcPos );

        nTempRank = 2;
        nTempPile = 0;

        bool bValidCastlingMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidCastlingMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            if(	trgSquare.IsBlank() ) {

                // position check castling
                bool bCalstling = false;
                if( srcPlayerSide == PlayerSide.e_White ) {

                    if( board.currCastlingState.CastlingWKSide == CastlingState.eCastling_Enable_State ) {
                        bCalstling = true;
                    }
                }
                else {
                    if( board.currCastlingState.CastlingBKSide == CastlingState.eCastling_Enable_State ) {
                        bCalstling = true;
                    }
                }

                if( bCalstling ) {

                    // check look square blank
                    nTempRank = -1;
                    nTempPile = 0;

                    ChessPosition moveLookPos = new ChessPosition(movePos.pos);

                    bValidCastlingMove = moveLookPos.MovePosition( nTempRank, nTempPile );
                    if( bValidCastlingMove ) {

                        ChessBoardSquare lookTrgSquare = board.aBoardSquare[moveLookPos.nPile, moveLookPos.nRank];
                        if(	lookTrgSquare.IsBlank() ) {

                            sMove move = new sMove();

                            MoveType castlingSideType = srcPlayerSide ==
                                PlayerSide.e_White ? MoveType.eCastling_White_KingSide_Move : MoveType.eCastling_Black_KingSide_Move;
                            move.moveType = MoveType.eCastling_Move | castlingSideType;

                            move.srcSquare = selSquare;
                            move.trgSquare = trgSquare;

                            listRetBoardPos.Add( move );
                        }
                    }
                }
            }
        }

        // queen side castling
        movePos.SetPosition( srcPos );

        nTempRank = -2;
        nTempPile = 0;

        bValidCastlingMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidCastlingMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            if(	trgSquare.IsBlank() ) {

                // position check castling
                bool bCalstling = false;
                if( srcPlayerSide == PlayerSide.e_White ) {

                    if( board.currCastlingState.CastlingWQSide == CastlingState.eCastling_Enable_State ) {
                        bCalstling = true;
                    }
                }
                else {
                    if( board.currCastlingState.CastlingBQSide == CastlingState.eCastling_Enable_State ) {
                        bCalstling = true;
                    }
                }

                if( bCalstling ) {

                    // check look square blank
                    nTempRank = 1;
                    nTempPile = 0;

                    ChessPosition moveLookPos = new ChessPosition(movePos.pos);
                    bValidCastlingMove = moveLookPos.MovePosition( nTempRank, nTempPile );
                    if( bValidCastlingMove ) {

                        ChessBoardSquare lookTrgSquare = board.aBoardSquare[moveLookPos.nPile, moveLookPos.nRank];
                        if(	lookTrgSquare.IsBlank() ) {

                            sMove move = new sMove();

                            MoveType castlingSideType = srcPlayerSide == PlayerSide.e_White ?
                                MoveType.eCastling_White_QueenSide_Move : MoveType.eCastling_Black_QueenSide_Move;
                            move.moveType = MoveType.eCastling_Move | castlingSideType;

                            move.srcSquare = selSquare;
                            move.trgSquare = trgSquare;

                            listRetBoardPos.Add( move );
                        }
                    }
                }
            }
        }

        return true;
    }
Ejemplo n.º 2
0
    public static bool GetPawnMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos )
    {
        //UnityEngine.Debug.LogError( "GetPawnMoveList - start" + " " + piece.position + " " + piece.playerSide );

        ChessPosition srcPos = selSquare.position;
        PlayerSide srcPlayerSide = selSquare.piece.playerSide;

        ChessPosition movePos = new ChessPosition(srcPos.pos);
        // pure move
        // pure move - one pile move
        int nTempRank, nTempPile;
        nTempRank = 0;
        nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1;

        bool bValidMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            // check already existing piece
            if( trgSquare.IsBlank() ) {

                // normal move
                sMove move = new sMove();
                move.moveType = MoveType.eNormal_Move | MoveType.ePawn_Move;
                // promote move
                if( srcPlayerSide == PlayerSide.e_White ) {
                    if( movePos.IsTopBoundary() )
                        move.moveType |= MoveType.ePromote_Move;
                }
                else {

                    if( movePos.IsBottomBoundary() )
                        move.moveType |= MoveType.ePromote_Move;
                }

                move.srcSquare = selSquare;
                move.trgSquare = trgSquare;

                listRetBoardPos.Add( move );
            }
        }

        // pure move - two pile move
        if( ( srcPos.nPile == 1 && srcPlayerSide == PlayerSide.e_White ) ||
            ( srcPos.nPile == 6 && srcPlayerSide == PlayerSide.e_Black ) ) {

            movePos.SetPosition( srcPos );

            nTempRank = 0;
            nTempPile = srcPlayerSide == PlayerSide.e_White ? 2 : -2;

            bValidMove = movePos.MovePosition( nTempRank, nTempPile );
            if( bValidMove ) {

                ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
                if(	trgSquare.IsBlank() ) {

                    sMove move = new sMove();
                    move.moveType = MoveType.eNormal_Move | MoveType.ePawn_Move | MoveType.ePawn_Two_Move ;

                    move.srcSquare = selSquare;
                    move.trgSquare = trgSquare;

                    // en passant target move check
                    move.enPassantTargetSquare.Rank = movePos.nRank;
                    move.enPassantTargetSquare.Pile = movePos.nPile;
                    move.enPassantTargetSquare.Available = true;

                    listRetBoardPos.Add( move );
                }
            }
        }

        // capture move
        // left diagonal capture
        // check left boundary
        movePos.SetPosition( srcPos );

        nTempRank = -1;
        nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1;

        bValidMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            if(	trgSquare.IsEnemy( srcPlayerSide ) ) {

                sMove move = new sMove();
                // capture move
                move.moveType = MoveType.eCapture_Move | MoveType.ePawn_Move;

                // promote move
                if( srcPlayerSide == PlayerSide.e_White ) {

                    if( movePos.IsTopBoundary() )
                        move.moveType |= MoveType.ePromote_Move;
                }
                else {

                    if( movePos.IsBottomBoundary() )
                        move.moveType |= MoveType.ePromote_Move;
                }

                move.srcSquare = selSquare;
                move.trgSquare = trgSquare;

                listRetBoardPos.Add( move );
            }
        }

        // right diagonal capture
        // check right boundary
        movePos.SetPosition( srcPos );

        nTempRank = 1;
        nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1;

        bValidMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            if(	trgSquare.IsEnemy( srcPlayerSide ) ) {

                sMove move = new sMove();
                // capture move
                move.moveType = MoveType.eCapture_Move | MoveType.ePawn_Move;

                // promote move
                if( srcPlayerSide == PlayerSide.e_White ) {

                    if( movePos.IsTopBoundary() )
                        move.moveType |= MoveType.ePromote_Move;
                }
                else {

                    if( movePos.IsBottomBoundary() )
                        move.moveType |= MoveType.ePromote_Move;
                }

                move.srcSquare = selSquare;
                move.trgSquare = trgSquare;

                listRetBoardPos.Add( move );
            }
        }

        // en-passant move
        // left en passant move check
        movePos.SetPosition( srcPos );

        nTempRank = -1;
        nTempPile = 0;

        bValidMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            if(	trgSquare.IsEnemy( srcPlayerSide ) &&
                trgSquare.piece.bEnPassantCapture ) {

                if( srcPlayerSide == PlayerSide.e_White )
                    bValidMove = movePos.MovePosition( 0, 1 );
                else
                    bValidMove = movePos.MovePosition( 0, -1 );

                if( bValidMove ) {

                    ChessBoardSquare finalTrgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
                    if(	finalTrgSquare.IsBlank() ) {

                        sMove move = new sMove();
                        // capture move
                        move.moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move;

                        move.srcSquare = selSquare;
                        move.trgSquare = finalTrgSquare;

                        listRetBoardPos.Add( move );
                    }
                }
            }
        }

        // right en passant move check
        movePos.SetPosition( srcPos );

        nTempRank = 1;
        nTempPile = 0;

        bValidMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            if(	trgSquare.IsEnemy( srcPlayerSide ) &&
                trgSquare.piece.bEnPassantCapture ) {

                if( srcPlayerSide == PlayerSide.e_White )
                    bValidMove = movePos.MovePosition( 0, 1 );
                else
                    bValidMove = movePos.MovePosition( 0, -1 );

                if( bValidMove ) {

                    ChessBoardSquare finalTrgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
                    if(	finalTrgSquare.IsBlank() ) {

                        sMove move = new sMove();
                        // capture move
                        move.moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move;

                        move.srcSquare = selSquare;
                        move.trgSquare = finalTrgSquare;

                        listRetBoardPos.Add( move );
                    }
                }
            }
        }

        return true;
    }
Ejemplo n.º 3
0
    // stright line move
    public static bool GetStraightMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos, MoveDirectionType moveDirection )
    {
        ChessPosition srcPos = selSquare.position;
        PlayerSide srcPlayerSide = selSquare.piece.playerSide;

        ChessPosition movePos = new ChessPosition(srcPos.pos);

        // all(radial) direction one move
        int nTempRank, nTempPile;

        int nIterCount;
        nIterCount = GetNumDirectionIterCount( movePos.nRank, movePos.nPile, moveDirection );
        //UnityEngine.Debug.LogError( "GetStraightMoveList() - nIterCount = " + nIterCount + " movePos.nRank, movePos.nPile " + movePos.nRank + " " + movePos.nPile );

        for( int nCurrIter=1; nCurrIter<=nIterCount; nCurrIter++ ) {

            nTempRank = 0;
            nTempPile = 0;

            GetNextDirectionRankPile( ref nTempRank, ref nTempPile, moveDirection, nCurrIter );
            //UnityEngine.Debug.LogError( "GetStraightMoveList() - nTempRank, nTempPile " + nTempRank + " " + nTempPile );

            movePos.SetPosition( srcPos );
            bool bValidMove = movePos.MovePosition( nTempRank, nTempPile );
            if( bValidMove ) {

                UnityEngine.Debug.LogError( "GetStraightMoveList() - bValidMove - nTempRank, nTempPile " + nTempRank + " " + nTempPile );

                ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
                // normal move
                if( trgSquare.IsBlank() ) {

                    sMove move = new sMove();

                    // normal move
                    move.moveType = MoveType.eNormal_Move;

                    move.srcSquare = selSquare;
                    move.trgSquare = trgSquare;

                    listRetBoardPos.Add( move );
                }
                // capture move
                else if( trgSquare.IsEnemy( srcPlayerSide ) ) {

                    sMove move = new sMove();

                    // normal move
                    move.moveType = MoveType.eCapture_Move;

                    move.srcSquare = selSquare;
                    move.trgSquare = trgSquare;

                    listRetBoardPos.Add( move );

                    return true;
                }
                // our piece
                else {

                    if( nCurrIter > 1 )
                        return true;
                    return false;
                }
            }
        }

        return false;
    }
Ejemplo n.º 4
0
    // leap move
    public static bool GetLeapMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos, MoveDirectionType moveDirection )
    {
        ChessPosition srcPos = selSquare.position;
        PlayerSide srcPlayerSide = selSquare.piece.playerSide;

        ChessPosition movePos = new ChessPosition(srcPos.pos);

        // all(radial) direction one move
        int nTempRank = 0, nTempPile = 0;

        GetNextDirectionRankPile( ref nTempRank, ref nTempPile, moveDirection, 0 );

        movePos.SetPosition( srcPos );
        bool bValidMove = movePos.MovePosition( nTempRank, nTempPile );
        if( bValidMove ) {

            ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank];
            // normal move
            if( trgSquare.IsBlank() ) {

                sMove move = new sMove();

                // normal move
                move.moveType = MoveType.eNormal_Move;

                move.srcSquare = selSquare;
                move.trgSquare = trgSquare;

                listRetBoardPos.Add( move );

                return true;
            }
            // capture move
            else if( trgSquare.IsEnemy( srcPlayerSide ) ) {

                sMove move = new sMove();

                // normal move
                move.moveType = MoveType.eCapture_Move;

                move.srcSquare = selSquare;
                move.trgSquare = trgSquare;

                listRetBoardPos.Add( move );

                return true;
            }
            // our piece
            else {

                return false;
            }
        }

        return false;
    }