我有一张交易卡牌游戏(比如顶级王牌)左边有一副牌,右边有一张牌,我想在这些卡片列表之间传递物品,我可以做。但不是没有使用开关盒来选择新卡,如下面的代码,我需要传递卡对象并删除它们,所以最终一个列表将有所有30张卡和一个 将没有,当我左边的所有卡片简称为nextCard时,我该怎么做?我可以使用某种数组吗?请在你的答案中使用一些排序操作码或伪代码,因为我只是一个初学者。
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);
}
答案 0 :(得分:0)
我不确定为什么你不能使用内置的List methods。
因此,假设您在第一个列表中有一个卡片对象,并且您想将它从一个列表交换到另一个列表。您只需将其添加或插入第二个列表,然后将其从第一个列表中删除。
myListofCards2.Add(myListofCards[i]);
myListofCards.Remove(myListofCards[i]);
或者您可以使用RemoveAt(int index)
myListofCards.RemoveAt(i);
另一种选择是使用类似于一副牌的结构的queue。
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
}