【问题标题】:How could I write the function for the card game, War, cleaner?我如何为纸牌游戏 War、cleaner 编写函数?
【发布时间】:2013-03-01 23:07:04
【问题描述】:

我一直在写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);
        }
    }
}

【问题讨论】:

  • 您的for 循环遍历i,然后关闭。几行之后,您执行 if 语句:if(Flipped1[i].CardValue &lt; Flipped2[i].CardValue)。这不再在forloop 中。会不会是这个问题?

标签: arrays unity3d unityscript


【解决方案1】:

您的问题在于 for 循环中的逻辑(在 if/else 中)。您实际上想要制作类似 while (Flipped1.length &gt; 0) 的东西,因为您要做的是清空翻转数组,而不是有效地将其与递增的 var 减去常量进行比较。

【讨论】:

  • 在进行更改后,我最终得到的索引小于 0 或大于或等于列表计数
  • 索引?什么指数?您只是在翻转列表中循环,每次从底部拉出一个并将其添加到玩家卡列表中。就此而言,如果您不介意顺序略有不同,则可以使用内置的连接函数。另外,我很确定你想在 if 检查之前关闭你的第一个 while 循环。
  • 这就是我的想法,我最终发现我的循环运行时间比我需要的要长。
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2013-02-27
  • 2021-12-15
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多