【问题标题】:How to move stack list in other list c#?如何在另一个列表c#中移动堆栈列表?
【发布时间】:2017-12-28 09:09:06
【问题描述】:

我正在开发一款单人纸牌游戏。

我需要帮助将几张卡片从一堆移到另一堆。

我可以将一张牌移到另一堆,但我不知道如何更改它,因为我使用堆栈列表。

那么如何检查除第一个索引之外的另一个索引?

       // valid card at to pile
    public bool TryPushCardsOnPile(Card card, int index)
    {
        Stack<Card> pile = piles[index];
        if (pile.Count() == 0)
        {
            return (card.getRank() == CardRank.King);
        }
        Card topPile = piles[index].Peek();

        if (!topPile.IsVisible())
        {
            return false;
        }
        return (card.IsRed() != topPile.IsRed()) && (card.getRank() == topPile.getRank() - 1);
    }

在表单类中

        public void PileClicked(int index)
    {
        if (IsDeckDisplaySelected())
        {
            Card temp = game.deckDisplay.Last();
            if (game.TryPushCardsOnPile(temp, index))
            {
                game.piles[index].Push(temp);
                game.deckDisplay.Remove(temp);
                move++;
            }
            Unselect();
            SelectPile(index);
        }
        else if (IsPileSelected())
        {
            int oldPile = SelectedPile();
            if (index != oldPile)
            {
                Card temp = game.piles[oldPile].Peek();
                if (game.TryPushCardsOnPile (temp, index))
                {
                    game.piles[index].Push(temp);
                    game.piles[oldPile].Pop();
                    if (game.piles[oldPile].FirstOrDefault() != null)
                    {

                        game.piles[oldPile].FirstOrDefault().IsVisible();

                    }
                    move++;
                    Unselect();
                }
                else
                {
                   // game.piles[oldPile].Push(temp);
                   // game.AddToPile(temp, oldPile);
                    Unselect();
                    SelectPile(index);

                }
            }
            else Unselect();
        }
        else
        {
            SelectPile(index);
            game.piles[index].Peek().TurnCardUp();
        }
    }

谢谢你

【问题讨论】:

  • 请简化您的问题,并发布一个很好的minimal reproducible example,清楚地表明您正在尝试做什么。准确地解释代码现在做了什么,你想让它做什么,以及具体你有什么问题。目前尚不清楚为什么使用堆栈对您来说是一个问题,也不清楚为什么如果这是一个问题,您不只是使用不同的数据结构(例如某种类型的普通列表)可以添加/删除任意位置的元素。
  • 请阅读How to Ask 并使用tour。你需要描述一个(明确的)问题并提出一个明确的问题才能得到一个好的答案。 I need help 不是问题或问题

标签: c#


【解决方案1】:

虽然使用 List 甚至自定义链表可能是更好的选择,但将项目块从一个堆栈移动到另一个堆栈很容易:全部弹出、反转、推送到另一个堆栈:

 var items = sourceStack.Take(4).Reverse().ToList();
 items.ForEach(x => destinationStack.Push(x));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2018-09-03
    • 2021-04-05
    • 2020-03-04
    相关资源
    最近更新 更多