public IEnumerator MoveDoble(GameObject nextTile, float a)
    {
        FichaTwoPlayers tile = nextTile.GetComponent <FichaTwoPlayers>();

        tile.inverse = !tile.inverse;

        if (a == 180)
        {
            a = 0;
        }
        if (a == 0)
        {
            a = 180;
        }

        if (classMatch.tileLeft == nextTile && colliderWall1 || classMatch.tileRight == nextTile && colliderWall1)
        {
            pos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1.9f);
        }

        if (classMatch.tileLeft == nextTile && colliderWall2 || classMatch.tileRight == nextTile && colliderWall2)
        {
            pos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1.9f);
        }

        var angl = nextTile.transform.rotation.eulerAngles;

        angl.z = a;
        angl.x = 90;
        nextTile.transform.rotation = Quaternion.Euler(angl);

        float normalizedTime = 0.0f;

        classMatch.putTile.Play();

        while (normalizedTime < 1)
        {
            tile.transform.position = Vector3.Lerp(tile.transform.position, pos, normalizedTime);
            normalizedTime         += Time.deltaTime / duration;
            yield return(null);
        }

        tile.transform.position = pos;

        if (classMatch.PlayerInTurn == 1)
        {
            classMatch.ChangeTilesToNormal();
        }

        yield return(new WaitForSeconds(1f));

        classMatch.noPlay = false;
        MatchDates.Instance.selectedFicha = null;
        classMatch.ValidateEnd();
        classMatch.ChangeTurn();
    }
    public void PlayBots()
    {
        GameObject tileOut = null;
        int        max     = 0;

        for (int i = 0; i < fichasPlayer2List.Count; i++)
        {
            FichaTwoPlayers ficha = fichasPlayer2List[i].GetComponent <FichaTwoPlayers>();

            if (!ficha.onTable)
            {
                if (ficha.pointDown == countLeft || ficha.pointDown == countRight || ficha.pointUp == countLeft || ficha.pointUp == countRight)
                {
                    if (ficha.countpoints >= max)
                    {
                        tileOut = fichasPlayer2List[i];
                        max     = ficha.countpoints;
                    }
                }
            }
        }

        if (tileOut == null)
        {
            msg.text = "El jugador " + PlayerInTurn + " se ha pasado";
            tocToc.Play();
            countPase++;
            ValidateEnd();
            Invoke(nameof(ChangeTurn), 2);
        }
        else
        {
            countTilesBot--;
            textCountTiles.text = countTilesBot + " fichas";
            tileOut.transform.SetParent(table.transform);

            if (tileOut.GetComponent <FichaTwoPlayers>().pointDown == countLeft || tileOut.GetComponent <FichaTwoPlayers>().pointUp == countLeft)
            {
                tileLeft.GetComponent <FichaTwoPlayers>().MovNexTile(tileOut);
                return;
            }

            if (tileOut.GetComponent <FichaTwoPlayers>().pointDown == countRight || tileOut.GetComponent <FichaTwoPlayers>().pointUp == countRight)
            {
                tileRight.GetComponent <FichaTwoPlayers>().MovNexTile(tileOut);
            }
        }
    }
    public void ThenChangeTurn()
    {
        if (PlayerInTurn == 1)
        {
            msg.text = "Coloque una ficha";
            panelBot.transform.GetComponent <Image>().sprite = imgPanel;
            textInTurn.SetActive(false);
            textNext.SetActive(true);

            int countTiletoPut = 0;
            for (int i = 0; i < fichasPlayerList.Count; i++)
            {
                FichaTwoPlayers ficha = fichasPlayerList[i].GetComponent <FichaTwoPlayers>();
                if (!ficha.onTable)
                {
                    if (ficha.pointDown == countLeft || ficha.pointDown == countRight || ficha.pointUp == countLeft || ficha.pointUp == countRight)
                    {
                        GameObject childTile = fichasPlayerList[i].transform.GetChild(0).gameObject;
                        childTile.GetComponent <SpriteRenderer>().sprite = fichasPlayerList[i].GetComponent <FichaTwoPlayers>().backsTile[1];
                        countTiletoPut++;
                    }
                    else
                    {
                        fichasPlayerList[i].GetComponent <SpriteRenderer>().sprite = ficha.backsTile[0];
                    }
                }
            }

            if (countTiletoPut == 0)
            {
                msg.text = "No tienes fichas para jugar";
                tocToc.Play();
                countPase++;
                ValidateEnd();
                Invoke(nameof(ChangeTurn), 2);
            }
        }
        else
        {
            msg.text = "Espere su turno";
            panelBot.transform.GetComponent <Image>().sprite = imgPanelInTurn;
            textInTurn.SetActive(true);
            textNext.SetActive(false);
            Invoke(nameof(PlayBots), 2);
        }
    }
    public void StartPlaybot()
    {
        GameObject tileOut = null;
        int        max     = 0;

        for (int i = 0; i < fichasPlayer2List.Count; i++)
        {
            FichaTwoPlayers ficha = fichasPlayer2List[i].GetComponent <FichaTwoPlayers>();

            if (ficha.countpoints >= max)
            {
                tileOut = fichasPlayer2List[i];
                max     = ficha.countpoints;
            }
        }

        StartCoroutine(tileOut.GetComponent <FichaTwoPlayers>().StartMov());
    }
    public IEnumerator Move(GameObject ficha, int a, float mov)
    {
        FichaTwoPlayers tile = ficha.GetComponent <FichaTwoPlayers>();

        if (a == 180)
        {
            tile.inverse = true;
        }

        tile.onTable         = true;
        classMatch.countPase = 0;

        ficha.transform.SetParent(classMatch.table.transform);
        ficha.transform.localScale = new Vector3(0.7f, 0.7f, 1);

        var angles = ficha.transform.rotation.eulerAngles;

        angles.y = a;
        angles.x = 90;
        ficha.transform.rotation = Quaternion.Euler(angles);

        pos = new Vector3(transform.position.x, transform.position.y, transform.position.z + mov);

        if (colliderWall1 && nocollider == false || colliderWall2 && nocollider == false)
        {
            tile.nocollider = true;
            tile.moved      = true;

            if (colliderWall1)
            {
                tile.colliderWall1 = true;
            }
            if (colliderWall2)
            {
                tile.colliderWall2 = true;
            }

            MovedTile(ficha);
            yield break;
        }

        if (moved)
        {
            if (colliderWall1)
            {
                tile.colliderWall1 = true;
            }
            if (colliderWall2)
            {
                tile.colliderWall2 = true;
            }

            tile.nocollider = true;

            if (tile.doble)
            {
                tile.movedDoble = true;
            }

            StartCoroutine(TurnTile(ficha, a));
            yield break;
        }

        if (movedDoble)
        {
            if (colliderWall1)
            {
                tile.colliderWall1 = true;
            }
            if (colliderWall2)
            {
                tile.colliderWall2 = true;
            }

            tile.nocollider = true;

            StartCoroutine(MoveDoble(ficha, a));;
            yield break;
        }

        float normalizedTime = 0.0f;

        classMatch.putTile.Play();

        while (normalizedTime < 1)
        {
            ficha.transform.position = Vector3.Lerp(ficha.transform.position, pos, normalizedTime);
            normalizedTime          += Time.deltaTime / duration;
            yield return(null);
        }

        ficha.transform.position = pos;

        if (classMatch.PlayerInTurn == 1)
        {
            classMatch.ChangeTilesToNormal();
        }

        yield return(new WaitForSeconds(1f));

        classMatch.noPlay = false;
        MatchDates.Instance.selectedFicha = null;
        classMatch.ValidateEnd();
        classMatch.ChangeTurn();
    }
    public void MovNexTile(GameObject nextTile)
    {
        FichaTwoPlayers tile = nextTile.GetComponent <FichaTwoPlayers>();

        if (transform.gameObject == classMatch.firstTile)
        {
            if (!closedUp && pointUp == tile.pointDown)
            {
                closedUp             = true;
                tile.closedDown      = true;
                classMatch.countLeft = tile.pointUp;
                classMatch.tileLeft  = nextTile;

                if (tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 90, 1.45f));
                }
                if (!tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 0, 1.9f));
                }
                if (!tile.doble && doble)
                {
                    StartCoroutine(Move(nextTile, 0, 1.45f));
                }

                return;
            }

            if (!closedUp && pointUp == tile.pointUp)
            {
                closedUp             = true;
                tile.closedUp        = true;
                classMatch.countLeft = tile.pointDown;
                classMatch.tileLeft  = nextTile;

                if (tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 90, 1.45f));
                }
                if (!tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 180, 1.9f));
                }
                if (!tile.doble && doble)
                {
                    StartCoroutine(Move(nextTile, 180, 1.45f));
                }
                return;
            }

            if (!closedDown && pointDown == tile.pointUp)
            {
                closedDown            = true;
                tile.closedUp         = true;
                classMatch.tileRight  = nextTile;
                classMatch.countRight = tile.pointDown;

                if (tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 90, -1.45f));
                }
                if (!tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 0, -1.9f));
                }
                if (!tile.doble && doble)
                {
                    StartCoroutine(Move(nextTile, 0, -1.45f));
                }
                return;
            }

            if (!closedDown && pointDown == tile.pointDown)
            {
                closedDown            = true;
                tile.closedDown       = true;
                classMatch.tileRight  = nextTile;
                classMatch.countRight = tile.pointUp;

                if (tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 90, -1.45f));
                }
                if (!tile.doble && !doble)
                {
                    StartCoroutine(Move(nextTile, 180, -1.9f));
                }
                if (!tile.doble && doble)
                {
                    StartCoroutine(Move(nextTile, 180, -1.45f));
                }
                return;
            }
        }
        else
        {
            if (!closedUp && !inverse && !doble && pointUp == tile.pointDown)
            {
                closedUp        = true;
                tile.closedDown = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointUp;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointUp;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, 1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 0, 1.9f));
                }

                return;
            }

            if (!closedDown && inverse && !doble && pointDown == tile.pointDown)
            {
                closedDown      = true;
                tile.closedDown = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointUp;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointUp;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, 1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 0, 1.9f));
                }
                return;
            }

            if (!closedDown && !inverse && !doble && pointDown == tile.pointUp)
            {
                closedDown    = true;
                tile.closedUp = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointDown;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointDown;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, -1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 0, -1.9f));
                }
                return;
            }

            if (!closedUp && inverse && !doble && pointUp == tile.pointUp)
            {
                closedUp      = true;
                tile.closedUp = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointDown;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointDown;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, -1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 0, -1.9f));
                }
                return;
            }

            if (!closedUp && !inverse && !doble && pointUp == tile.pointUp)
            {
                closedUp      = true;
                tile.closedUp = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointDown;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointDown;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, 1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 180, 1.9f));
                }
                return;
            }

            if (!closedDown && inverse && !doble && pointDown == tile.pointUp)
            {
                closedDown    = true;
                tile.closedUp = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointDown;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointDown;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, 1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 180, 1.9f));
                }
                return;
            }

            if (!closedDown && !inverse && !doble && pointDown == tile.pointDown)
            {
                closedDown      = true;
                tile.closedDown = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointUp;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointUp;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, -1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 180, -1.9f));
                }
                return;
            }

            if (!closedUp && inverse && !doble && pointUp == tile.pointDown)
            {
                closedUp        = true;
                tile.closedDown = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointUp;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointUp;
                }

                if (tile.doble)
                {
                    StartCoroutine(Move(nextTile, 90, -1.45f));
                }
                else
                {
                    StartCoroutine(Move(nextTile, 180, -1.9f));
                }
                return;
            }

            if (!closedUp && doble && pointUp == tile.pointDown)
            {
                closedUp        = true;
                tile.closedDown = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointUp;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointUp;
                }

                StartCoroutine(Move(nextTile, 0, 1.45f));
                return;
            }

            if (!closedDown && doble && pointDown == tile.pointUp)
            {
                closedDown    = true;
                tile.closedUp = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointDown;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointDown;
                }

                StartCoroutine(Move(nextTile, 0, -1.45f));
                return;
            }

            if (!closedUp && doble && pointUp == tile.pointUp)
            {
                closedUp      = true;
                tile.closedUp = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointDown;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointDown;
                }

                StartCoroutine(Move(nextTile, 180, 1.45f));
                return;
            }

            if (!closedDown && doble && pointDown == tile.pointDown)
            {
                closedDown      = true;
                tile.closedDown = true;

                if (classMatch.tileLeft == transform.gameObject)
                {
                    classMatch.countLeft = tile.pointUp;
                    classMatch.tileLeft  = nextTile;
                }
                else
                {
                    classMatch.tileRight  = nextTile;
                    classMatch.countRight = tile.pointUp;
                }

                StartCoroutine(Move(nextTile, 180, -1.45f));
            }
        }
    }
    private void OnMouseUpAsButton()
    {
        if (!onTable && classMatch.PlayerInTurn == 1 && !classMatch.noPlay && !classMatch.pause)
        {
            if (classMatch.startPlayer)
            {
                classMatch.noPlay      = true;
                classMatch.startPlayer = false;
                StartCoroutine(StartMov());
            }
            else
            {
                GameObject childTileRight = classMatch.tileRight.transform.GetChild(0).gameObject;
                childTileRight.GetComponent <SpriteRenderer>().sprite = classMatch.tileRight.GetComponent <FichaTwoPlayers>().backsTile[0];

                GameObject childTileLeft = classMatch.tileLeft.transform.GetChild(0).gameObject;
                childTileLeft.GetComponent <SpriteRenderer>().sprite = classMatch.tileLeft.GetComponent <FichaTwoPlayers>().backsTile[0];

                bool left  = false; // Verdadero cuando uno de mis extremos puede ir con la ficha de la izquierda
                bool right = false; // Verdadero cuando uno de mis extremos puede ir con la ficha de la derecha

                if (pointDown == classMatch.countLeft || pointUp == classMatch.countLeft)
                {
                    GameObject tileLeft = classMatch.tileLeft.transform.GetChild(0).gameObject;
                    tileLeft.GetComponent <SpriteRenderer>().sprite = classMatch.tileLeft.GetComponent <FichaTwoPlayers>().backsTile[1];
                    left = true;
                }

                if (pointDown == classMatch.countRight || pointUp == classMatch.countRight)
                {
                    GameObject tileRight = classMatch.tileRight.transform.GetChild(0).gameObject;
                    tileRight.GetComponent <SpriteRenderer>().sprite = classMatch.tileRight.GetComponent <FichaTwoPlayers>().backsTile[1];
                    right = true;
                }

                if (!right && left || right && !left) // La ficha que selecciono puede ponerse en la derecha o en la izquierda. Solo en un lado
                {
                    classMatch.noPlay = true;
                    classMatch.countTilesPlayer--;
                    classMatch.capicua = false;

                    GameObject childTileRight1 = classMatch.tileRight.transform.GetChild(0).gameObject;
                    childTileRight1.GetComponent <SpriteRenderer>().sprite = classMatch.tileRight.GetComponent <FichaTwoPlayers>().backsTile[0];

                    GameObject childTileLeft2 = classMatch.tileLeft.transform.GetChild(0).gameObject;
                    childTileLeft2.GetComponent <SpriteRenderer>().sprite = classMatch.tileLeft.GetComponent <FichaTwoPlayers>().backsTile[0];

                    MatchDates.Instance.selectedFicha = transform.gameObject;

                    if (left)
                    {
                        classMatch.tileLeft.GetComponent <FichaTwoPlayers>().MovNexTile(transform.gameObject);
                    }
                    else
                    {
                        classMatch.tileRight.GetComponent <FichaTwoPlayers>().MovNexTile(transform.gameObject);
                    }
                }

                // En la mesa los extremos son iguales pero las fichas son distontas. No se trata de la primera ficha
                if (classMatch.countLeft == classMatch.countRight && left && right && classMatch.tileLeft != classMatch.tileRight)
                {
                    classMatch.noPlay = true;
                    classMatch.countTilesPlayer--;
                    classMatch.capicua = false;

                    GameObject childTileRight1 = classMatch.tileRight.transform.GetChild(0).gameObject;
                    childTileRight1.GetComponent <SpriteRenderer>().sprite = classMatch.tileRight.GetComponent <FichaTwoPlayers>().backsTile[0];

                    GameObject childTileLeft2 = classMatch.tileLeft.transform.GetChild(0).gameObject;
                    childTileLeft2.GetComponent <SpriteRenderer>().sprite = classMatch.tileLeft.GetComponent <FichaTwoPlayers>().backsTile[0];

                    MatchDates.Instance.selectedFicha = transform.gameObject;

                    classMatch.tileRight.GetComponent <FichaTwoPlayers>().MovNexTile(transform.gameObject);
                }

                if (left || right)
                {
                    if (classMatch.tileLeft == classMatch.tileRight)
                    {
                        classMatch.noPlay = true;
                        classMatch.countTilesPlayer--;
                        classMatch.capicua = false;

                        GameObject childFirstTile = classMatch.firstTile.transform.GetChild(0).gameObject;
                        childFirstTile.GetComponent <SpriteRenderer>().sprite = classMatch.firstTile.GetComponent <FichaTwoPlayers>().backsTile[0];

                        MatchDates.Instance.selectedFicha = transform.gameObject;

                        classMatch.firstTile.GetComponent <FichaTwoPlayers>().MovNexTile(transform.gameObject);
                    }
                }

                if (MatchDates.Instance.selectedFicha == null && left && right)
                {
                    MatchDates.Instance.selectedFicha = transform.gameObject;
                }
            }
        }
        else if (onTable && classMatch.PlayerInTurn == 1 && MatchDates.Instance.selectedFicha != null)
        {
            if (transform.gameObject == classMatch.tileLeft || transform.gameObject == classMatch.tileRight)
            {
                classMatch.putTile.Play();
                classMatch.noPlay = true;

                GameObject childTileRight = classMatch.tileRight.transform.GetChild(0).gameObject;
                childTileRight.GetComponent <SpriteRenderer>().sprite = classMatch.tileRight.GetComponent <FichaTwoPlayers>().backsTile[0];

                GameObject childTileLeft = classMatch.tileLeft.transform.GetChild(0).gameObject;
                childTileLeft.GetComponent <SpriteRenderer>().sprite = classMatch.tileLeft.GetComponent <FichaTwoPlayers>().backsTile[0];

                FichaTwoPlayers selectedFicha = MatchDates.Instance.selectedFicha.GetComponent <FichaTwoPlayers>();

                classMatch.capicua = false;

                if (selectedFicha.pointDown == classMatch.countLeft && selectedFicha.pointUp == classMatch.countRight ||
                    selectedFicha.pointDown == classMatch.countRight && selectedFicha.pointUp == classMatch.countLeft)
                {
                    classMatch.capicua = true;
                }

                classMatch.countTilesPlayer--;
                MovNexTile(MatchDates.Instance.selectedFicha);
            }
        }
    }