我怎么能写卡片游戏,战争,清洁的功能?

时间:2013-03-13 19:48:46

标签: arrays unity3d unityscript

我一直致力于在Unity编写纸牌游戏战争,而且还有一些在战争功能中,我失去了2张牌。有没有更简单的方法,或者任何人都可以让我知道我做错了什么?

while(Flipped1[Flipped1.length-1].CardValue == Flipped2[Flipped2.length-1].CardValue)
{
    for(var i= 0; i < 3; i++)
    {
        //adds a card from the players hand to the flipped pile
        Flipped1.Add(playerOneCards[0]);
        Flipped2.Add(playerTwoCards[0]);

        //removes card from the array.
        playerOneCards.RemoveAt(0);
        playerTwoCards.RemoveAt(0);
    }

    p1War = Flipped1[Flipped1.length-1].img;
    p2War = Flipped2[Flipped2.length-1].img;

    Debug.Log(Flipped1[i].CardValue);
    Debug.Log(Flipped2[i].CardValue);

    Debug.Log(Flipped1[0].CardValue);
    Debug.Log(Flipped2[0].CardValue);

    if(Flipped1[i].CardValue < Flipped2[i].CardValue)
    {                                                           
        for(var j =0; j < Flipped1.length +2 || j < Flipped2.length+2; j++)
        {
            playerTwoCards.Add(Flipped1[0]);
            playerTwoCards.Add(Flipped2[0]);

            //removes card from the array.
            Flipped1.RemoveAt(0);
            Flipped2.RemoveAt(0);
        }
    }

    else
    {
        for(var q =0; q < Flipped1.length+2|| q < Flipped2.length+2; q++)
        {
            playerOneCards.Add(Flipped2[0]);
            playerOneCards.Add(Flipped1[0]);

            //removes card from the array.
            Flipped1.RemoveAt(0);
            Flipped2.RemoveAt(0);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题出在for循环中的逻辑中(在if / else中)。你实际上想要制作像while (Flipped1.length > 0)这样的东西,因为你要做的就是清空翻转的数组,而不是有效地将它与递增的var而不是常量进行比较。