在列表之间添加和删除对象

本文关键字:删除 对象 添加 列表 之间 | 更新日期: 2023-09-27 18:27:46

我有一个交易牌游戏(就像王牌一样),左边有一副牌,右边有一副,我想在这些牌列表之间传递对象,我可以这样做。但如果不使用开关盒来选择新牌,我需要传递卡对象并将其移除,这样最终一个列表将包含所有30张牌和一张将没有,当我左边的所有卡片都被简单地称为nextCard时,我该如何做到这一点?我可以使用某种数组吗?请在你的答案中使用一些os代码或伪代码,因为我只是一个初学者。

TL:DR;需要在列表之间移动卡片对象,将它们添加到一个列表,从另一个列表中删除。现在我正在使用switch语句将对象数据传递到列表中。我会回答你的任何问题。

List<Cards> myListofCards;
List<Cards> myListofCards2;
//First card will be displayed on the left on the screen
myListofCards = new List<Cards>();
nextCard = new Cards();
nextCard.cardName = "Character";
nextCard.strength = 45;
myListofCards.Add(nextCard);
listOfCards.ItemsSource = myListofCards;
//Second card will be displayed on the right on the screen
myListofCards2 = new List<Cards>();
nextCard2 = new Cards();
nextCard2.strength2 = "Character2";
nextCard2.age2 = 42;
myListofCards2.Add(nextCard);
listOfCards2.ItemsSource = myListofCards2;
//When the value of the card on the left of the screen is compared 
//to the one on the right, it is higher, so the card on the right
//should move to the deck on the left side. Just to test this out
//I used the following switch case statement. It works but, when I
//eventually have about 30 cards and they are being added and removed
//from the 2 different decks, the case statements obviously will not 
//work anymore, so what I want is to be able to move the different cards
//between lists without the switch case. 

int index = rand.Next(random.Count);
var i= random[index];
random.RemoveAt(index);
switch (i)
{
    case 1:
    myListofCards = new List<Cards>();
    nextCard = new Cards();
    nextCard.cardName = "Character";
    nextCard.strength = 45;
    myListofCards.Add(nextCard);
    listOfCards.ItemsSource = myListofCards;
    break;
    case 2:
    myListofCards = new List<Cards>();
    nextCard = new Cards();
    nextCard.cardName = "Char";
    nextCard.strength = 55;
    myListofCards.Add(nextCard);
    listOfCards.ItemsSource = myListofCards;
    break;
}
int index = rand.Next(random.Count);
var j= random[index];
random.RemoveAt(index);
switch (j)
{
    case 1:
    myListofCards2 = new List<Cards>();
    nextCard2 = new Cards();
    nextCard2.cardName = "Character2";
    nextCard2.strength = 45;
    myListofCards2.Add(nextCard);
        listOfCards2.ItemsSource = myListofCards;
    break;
    case 2:
    myListofCards2 = new List<Cards>();
    nextCard2 = new Cards();
    nextCard2.cardName = "Char2";
    nextCard2.strength = 60;
    myListofCards2.Add(nextCard);
    listOfCards2.ItemsSource = myListofCards;
    break;
}
public void cardSwapandScore()
    {
        winback.Visibility = Visibility.Visible;
        win.Visibility = Visibility.Visible;
        if (nextCard.power > nextCardComp.powerComp)
        {
            win.Text = "You win";
            /////////////////////////////////////////////////
            myListofCards.Add(nextCardComp);
            myListofCards2.Remove(nextCardComp);
            /////////////////////////////////////////////////
        }
        else
        win.Text = "You Lose";
        win.Visibility = Visibility.Visible;
        ////////////////////////////////
        myListofCards2.Add(nextCard);
        myListofCards.Remove(nextCard);

    }

在列表之间添加和删除对象

我不知道为什么不能使用内置的List方法。

所以假设你在第一个列表中有一个卡片对象,并且你想把它从一个列表交换到另一个列表。您只需将它添加或插入到第二个列表中,然后从第一个列表中删除它。

myListofCards2.Add(myListofCards[i]);
myListofCards.Remove(myListofCards[i]);

或者你可以使用RemoveAt(int index)

myListofCards.RemoveAt(i);

另一种选择是使用类似于一副牌结构的队列。

 var cardLeft = queueLeft.Dequeue(); // Removes next element and returns it
 var cardRight = queueRight.Dequeue();

if(cardLeft > cardRight)
  {
    queueLeft.Enqueue(cardLeft);
    queueLeft.Enqueue(cardRight );
  }
 else if(cardRight > cardLeft)
 {
    queueRight.Enqueue(cardLeft);
    queueRight.Enqueue(cardRight );
 }
 else
 {
   ''Do something for a tie
  }