Assets/Scripts/Pieces/KingPiece.cs
/// <summary>
/// kings can move 1 unit in each direction
/// the main restrictions are:
/// The positions of the pieces (must be inside the board)
/// can not go into a own piece
/// </summary>
/// <returns>bidemensional boolean array with the possible moves</returns>
kings can move 1 unit in each direction
the main restrictions are:
The positions of the pieces (must be inside the board)
can not go into a own piece
kings can move 1 unit in each direction the main restrictions are : The positions of the pieces ( must be inside the board ) can not go into a own piece
public override bool[,] PossibleMove(BoardManager instance)
    {
        bool[,] possibleMovesMap = new bool[8, 8];
        ChessPiece enemyPiece;
        if (CurrentX - 1 >= 0)
        {
            if (CurrentZ - 1 >= 0)
            {
                enemyPiece = instance.chessPieces[CurrentX - 1, CurrentZ - 1];
                if (enemyPiece == null)
                {
                    possibleMovesMap[CurrentX - 1, CurrentZ - 1] = true;
                }
                else
                {
                    if (isWhite != enemyPiece.isWhite)
                    {
                        possibleMovesMap[CurrentX - 1, CurrentZ - 1] = true;
                    }
                }
            }
            if (CurrentZ + 1 <= 7)
            {
                enemyPiece = instance.chessPieces[CurrentX - 1, CurrentZ + 1];
                if (enemyPiece == null)
                {
                    possibleMovesMap[CurrentX - 1, CurrentZ + 1] = true;
                }
                else
                {
                    if (isWhite != enemyPiece.isWhite)
                    {
                        possibleMovesMap[CurrentX - 1, CurrentZ + 1] = true;
                    }
                }
            }
            enemyPiece = instance.chessPieces[CurrentX - 1, CurrentZ];
            if (enemyPiece == null)
            {
                possibleMovesMap[CurrentX - 1, CurrentZ] = true;
            }
            else
            {
                if (isWhite != enemyPiece.isWhite)
                {
                    possibleMovesMap[CurrentX - 1, CurrentZ] = true;
                }
            }
        }
        if (CurrentX + 1 <= 7)
        {
            if (CurrentZ - 1 >= 0)
            {
                enemyPiece = instance.chessPieces[CurrentX + 1, CurrentZ - 1];
                if (enemyPiece == null)
                {
                    possibleMovesMap[CurrentX + 1, CurrentZ - 1] = true;
                }
                else
                {
                    if (isWhite != enemyPiece.isWhite)
                    {
                        possibleMovesMap[CurrentX + 1, CurrentZ - 1] = true;
                    }
                }
            }
            if (CurrentZ + 1 <= 7)
            {
                enemyPiece = instance.chessPieces[CurrentX + 1, CurrentZ + 1];
                if (enemyPiece == null)
                {
                    possibleMovesMap[CurrentX + 1, CurrentZ + 1] = true;
                }
                else
                {
                    if (isWhite != enemyPiece.isWhite)
                    {
                        possibleMovesMap[CurrentX + 1, CurrentZ + 1] = true;
                    }
                }
            }
            enemyPiece = instance.chessPieces[CurrentX + 1, CurrentZ];
            if (enemyPiece == null)
            {
                possibleMovesMap[CurrentX + 1, CurrentZ] = true;
            }
            else
            {
                if (isWhite != enemyPiece.isWhite)
                {
                    possibleMovesMap[CurrentX + 1, CurrentZ] = true;
                }
            }
        }
        if (CurrentZ + 1 <= 7)
        {
            enemyPiece = instance.chessPieces[CurrentX, CurrentZ + 1];
            if (enemyPiece == null)
            {
                possibleMovesMap[CurrentX, CurrentZ + 1] = true;
            }
            else
            {
                if (isWhite != enemyPiece.isWhite)
                {
                    possibleMovesMap[CurrentX, CurrentZ + 1] = true;
                }
            }
        }
        if (CurrentZ -1 > 0)
        {
            enemyPiece = instance.chessPieces[CurrentX, CurrentZ - 1];
            if (enemyPiece == null)
            {
                possibleMovesMap[CurrentX, CurrentZ - 1] = true;
            }
            else
            {
                if (isWhite != enemyPiece.isWhite)
                {
                    possibleMovesMap[CurrentX, CurrentZ - 1] = true;
                }
            }
        }
        if (isWhite)
        {
            if (!instance.didWhiteKingEverMoved && !instance.didWhiteRook0EverMoved && instance.chessPieces[CurrentX - 1, CurrentZ] == null && instance.chessPieces[CurrentX - 2, CurrentZ] == null && instance.chessPieces[CurrentX - 3, CurrentZ] == null)
            {
                possibleMovesMap[CurrentX - 3, CurrentZ] = true;
            }
            if (!instance.didWhiteKingEverMoved && !instance.didWhiteRook1EverMoved && instance.chessPieces[CurrentX + 1, CurrentZ] == null && instance.chessPieces[CurrentX + 2, CurrentZ] == null)
            {
                possibleMovesMap[CurrentX + 2, CurrentZ] = true;
            }
        }
        else
        {
            if (!instance.didBlackKingEverMoved && !instance.didBlackRook0EverMoved && instance.chessPieces[CurrentX - 1, CurrentZ] == null && instance.chessPieces[CurrentX - 2, CurrentZ] == null && instance.chessPieces[CurrentX - 3, CurrentZ] == null)
            {
                possibleMovesMap[CurrentX - 3, CurrentZ] = true;
            }
            if (!instance.didBlackKingEverMoved && !instance.didBlackRook1EverMoved && instance.chessPieces[CurrentX + 1, CurrentZ] == null && instance.chessPieces[CurrentX + 2, CurrentZ] == null)
            {
                possibleMovesMap[CurrentX + 2, CurrentZ] = true;
            }
        }
         check problem
        return possibleMovesMap;
    }
public override bool [ , ] PossibleMove ( BoardManager instance ) { bool [ , ] possibleMovesMap = new bool [ 8 , 8 ] ; ChessPiece enemyPiece ; if ( CurrentX - 1 >= 0 ) { if ( CurrentZ - 1 >= 0 ) { enemyPiece = instance . chessPieces [ CurrentX - 1 , CurrentZ - 1 ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX - 1 , CurrentZ - 1 ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX - 1 , CurrentZ - 1 ] = true ; } } } if ( CurrentZ + 1 <= 7 ) { enemyPiece = instance . chessPieces [ CurrentX - 1 , CurrentZ + 1 ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX - 1 , CurrentZ + 1 ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX - 1 , CurrentZ + 1 ] = true ; } } } enemyPiece = instance . chessPieces [ CurrentX - 1 , CurrentZ ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX - 1 , CurrentZ ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX - 1 , CurrentZ ] = true ; } } } if ( CurrentX + 1 <= 7 ) { if ( CurrentZ - 1 >= 0 ) { enemyPiece = instance . chessPieces [ CurrentX + 1 , CurrentZ - 1 ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX + 1 , CurrentZ - 1 ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX + 1 , CurrentZ - 1 ] = true ; } } } if ( CurrentZ + 1 <= 7 ) { enemyPiece = instance . chessPieces [ CurrentX + 1 , CurrentZ + 1 ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX + 1 , CurrentZ + 1 ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX + 1 , CurrentZ + 1 ] = true ; } } } enemyPiece = instance . chessPieces [ CurrentX + 1 , CurrentZ ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX + 1 , CurrentZ ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX + 1 , CurrentZ ] = true ; } } } if ( CurrentZ + 1 <= 7 ) { enemyPiece = instance . chessPieces [ CurrentX , CurrentZ + 1 ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX , CurrentZ + 1 ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX , CurrentZ + 1 ] = true ; } } } if ( CurrentZ - 1 > 0 ) { enemyPiece = instance . chessPieces [ CurrentX , CurrentZ - 1 ] ; if ( enemyPiece == null ) { possibleMovesMap [ CurrentX , CurrentZ - 1 ] = true ; } else { if ( isWhite != enemyPiece . isWhite ) { possibleMovesMap [ CurrentX , CurrentZ - 1 ] = true ; } } } if ( isWhite ) { if ( ! instance . didWhiteKingEverMoved && ! instance . didWhiteRook0EverMoved && instance . chessPieces [ CurrentX - 1 , CurrentZ ] == null && instance . chessPieces [ CurrentX - 2 , CurrentZ ] == null && instance . chessPieces [ CurrentX - 3 , CurrentZ ] == null ) { possibleMovesMap [ CurrentX - 3 , CurrentZ ] = true ; } if ( ! instance . didWhiteKingEverMoved && ! instance . didWhiteRook1EverMoved && instance . chessPieces [ CurrentX + 1 , CurrentZ ] == null && instance . chessPieces [ CurrentX + 2 , CurrentZ ] == null ) { possibleMovesMap [ CurrentX + 2 , CurrentZ ] = true ; } } else { if ( ! instance . didBlackKingEverMoved && ! instance . didBlackRook0EverMoved && instance . chessPieces [ CurrentX - 1 , CurrentZ ] == null && instance . chessPieces [ CurrentX - 2 , CurrentZ ] == null && instance . chessPieces [ CurrentX - 3 , CurrentZ ] == null ) { possibleMovesMap [ CurrentX - 3 , CurrentZ ] = true ; } if ( ! instance . didBlackKingEverMoved && ! instance . didBlackRook1EverMoved && instance . chessPieces [ CurrentX + 1 , CurrentZ ] == null && instance . chessPieces [ CurrentX + 2 , CurrentZ ] == null ) { possibleMovesMap [ CurrentX + 2 , CurrentZ ] = true ; } } return possibleMovesMap ; }
kings can move 1 unit in each direction
the main restrictions are:
The positions of the pieces (must be inside the board)
can not go into a own piece
kings can move 1 unit in each direction the main restrictions are : The positions of the pieces ( must be inside the board ) can not go into a own piece
//TODO //to be optimazed later //left side tiles //to the left //right side tiles //to the right //up //down //castling //TODO check problem
bidemensional boolean array with the possible moves
bidemensional boolean array with the possible moves
instance
BoardManager