public GameObject GetPiece(int id)
 {
     //using the piece id return the gameobject piece
     foreach (GameObject p in pieces)
     {
         PieceProperties properties = p.GetComponent <PieceProperties>();
         if (properties.id == id)
         {
             return(p);
         }
     }
     return(null);
 }
    public GameObject FindPiece(int row, int column)
    {
        //Given the row and column on the chessboard, return the piece present at that location

        foreach (GameObject p in pieces)
        {
            PieceProperties properties = p.GetComponent <PieceProperties>();
            if (properties.row == row && properties.column == column)
            {
                return(p);
            }
        }
        return(null);
    }
    void Start()
    {
        properties = GetComponent <PieceProperties>();

        defaultMaterials = GetComponent <Renderer>().materials;

        // Add a BoxCollider if the interactible does not contain one.
        Collider collider = GetComponentInChildren <Collider>();

        if (collider == null)
        {
            gameObject.AddComponent <BoxCollider>();
        }

        //change the position of the piece on select i.e. lift in air
        iPosition     = this.transform.localPosition;
        ePosition     = iPosition + new Vector3(0, maxHeight, 0);
        journeyLength = Vector3.Distance(iPosition, ePosition);
    }
    // Check if given team's king is in check
    private static bool IsInCheck(int team)
    {
        // Find team's king piece and get its row and column
        GameObject king = null;

        foreach (GameObject piece in chessboardManager.pieces)
        {
            PieceProperties pProp = piece.GetComponent <PieceProperties>();
            if (pProp.team == team && pProp.type == PieceProperties.Type.King)
            {
                king = piece;
                break;
            }
        }

        int kRow = king.GetComponent <PieceProperties>().row;
        int kCol = king.GetComponent <PieceProperties>().column;

        // Check if any enemy pieces are pressuring king
        foreach (GameObject p in chessboardManager.pieces)
        {
            if (p.GetComponent <PieceProperties>().team != team)
            {
                List <Position> availableMoves = GetAvailableMoves(p);
                foreach (Position pos in availableMoves)
                {
                    if (pos.row == kRow && pos.col == kCol)
                    {
                        return(true);
                    }
                }
            }
        }

        // If not, return false
        return(false);
    }
    public static List <Position> GetAvailableMoves(GameObject piece)
    {
        // List to store all available moves
        List <Position> availableMoves = new List <Position>();

        // Get all the properties that we need
        PieceProperties pieceProp = piece.GetComponent <PieceProperties>();
        int             team      = pieceProp.team;
        int             row       = pieceProp.row;
        int             col       = pieceProp.column;

        PieceProperties.Type type = pieceProp.type;

        if (PieceProperties.Type.Pawn == type)
        {
            int dir  = 0;
            int dist = 1;
            if (team == 0)
            {
                dir = 1;
                if (row == 1)
                {
                    dist = 2;
                }
                ;
            }
            else if (team == 1)
            {
                dir = -1;
                if (row == 6)
                {
                    dist = 2;
                }
            }

            // Moving forwards
            GameObject otherPiece;
            for (int i = 1; i <= dist; i++)
            {
                // Stop right before any piece
                otherPiece = chessboardManager.FindPiece(row + i * dist, col);
                if (otherPiece != null)
                {
                    break;
                }

                availableMoves.Add(new Position(row + dir * i, col));
            }

            // Moving diagonal if there is an enemy piece
            otherPiece = chessboardManager.FindPiece(row + 1, col + 1);
            if (otherPiece != null && otherPiece.GetComponent <PieceProperties>().team != team)
            {
                availableMoves.Add(new Position(row + 1, col + 1));
            }
            otherPiece = chessboardManager.FindPiece(row + 1, col - 1);
            if (otherPiece != null && otherPiece.GetComponent <PieceProperties>().team != team)
            {
                availableMoves.Add(new Position(row + 1, col - 1));
            }
        }
        else if (PieceProperties.Type.Rook == type)
        {
            AddOrthogonal(availableMoves, row, col, team, 8);
        }
        else if (PieceProperties.Type.Bishop == type)
        {
            AddDiagonal(availableMoves, row, col, team, 8);
        }
        else if (PieceProperties.Type.Queen == type)
        {
            AddOrthogonal(availableMoves, row, col, team, 8);
            AddDiagonal(availableMoves, row, col, team, 8);
        }
        else if (PieceProperties.Type.King == type)
        {
            AddOrthogonal(availableMoves, row, col, team, 1);
            AddDiagonal(availableMoves, row, col, team, 1);
        }
        else if (PieceProperties.Type.Knight == type)
        {
            AddL(availableMoves, row, col, team);
        }

        // Glow all available moves
        FindAndGlowAll(availableMoves);

        return(availableMoves);
    }