如何迭代一个数组并将每个其他元素移动到Swift中的两个新数组?

时间:2022-02-01 07:32:37

I do realize similar questions have been asked before, I looked at them before seeking help. But they were either not in Swift or too complex for me to decipher. My question is different from How do I shuffle an array in Swift? in that I have already done some of the work, like shuffling. The title was actually different from the question asked, which was "How do I randomize or shuffle the elements within an array in Swift?" I am only interested in iterating the resulting array and I couldn't separate that part from the rest of the code in the answers given for a question that encompassed so much more. That said, there are some great suggestions on that page so I think people like me will benefit from having both pages available. Maybe someone can set up a reciprocal link on that page as I have done here.

我确实意识到之前有过类似的问题,我在寻求帮助之前先看了看。但他们或者不是在斯威夫特,也不是太复杂,我无法破译。我的问题不同于如何在Swift中对数组进行洗牌?因为我已经完成了一些工作,比如洗牌。标题实际上与提出的问题有所不同,即“如何在Swift中对数组中的元素进行随机化或随机化?”我只对迭代生成的数组感兴趣,我无法将该部分与其他代码分开,而这些代码的答案包含的内容更多。也就是说,在该页面上有一些很好的建议,所以我认为像我这样的人会从两个页面都可以获益。也许有人可以像我在这里所做的那样在该页面上设置互惠链接。

Admittedly, I am new to Swift and not a seasoned programmer in any language, but please don't assume I am coming here seeking help without trying to figure it out on my own. I am spending many hours learning the fundamentals of all C based languages and reading the Swift literature at developer.apple.com.

不可否认,我是Swift的新手,而不是任何语言的经验丰富的程序员,但请不要以为我来这里寻求帮助而不试图自己解决问题。我花了很多时间学习所有基于C语言的基础知识,并在developer.apple.com上阅读Swift文献。

So the question will be more obvious, I and attempting to build the card game War. Thus far I have accomplished constructing the (an array) deck of cards and randomized it (shuffled). I am stuck at looping through the resulting array of 52 objects and assigning (moving) them to the two players hands (two new arrays). I'm not sure how much of my code I should display in order to help me but if you need more, I'll gladly provide it. Please note that this is only an exercise, practice for me to learn how to write complex programs, and some code, like the function to randomize, is not mine, I found it right here at *. I'd almost prefer if you didn't just hand me the code that will work, I'm not likely going to learn as much that way, but if providing steps in plain English so I can figure out the syntax is too much trouble, so be it, provide an example, I'm sure I'll get plenty of chances to write/use the syntax later.

所以问题将更加明显,我并试图建立卡片游戏战争。到目前为止,我已经完成了(一个阵列)卡片组的构建并将其随机化(洗牌)。我被困在循环通过52个对象的结果数组并将它们分配(移动)到两个玩家手中(两个新阵列)。我不确定我应该显示多少代码以帮助我,但如果你需要更多代码,我很乐意提供它。请注意,这只是一个练习,练习让我学习如何编写复杂的程序,而一些代码,比如随机化的函数,不是我的,我在*找到它。我几乎更喜欢如果你不只是把我的代码交给我工作,我不太可能那么多学习,但如果用简单的英语提供步骤那么我可以弄清楚语法是太麻烦了,就这样,提供一个例子,我相信我会有很多机会在以后编写/使用语法。

One more note, I'm only working in a playground at the moment, when and if I can get all the code working, I'll move to the UI stuff.

还有一点需要注意,我现在只在游乐场工作,当我能够完成所有代码工作时,我会转向UI的东西。

Thanks in advance, Rick

提前谢谢,里克

/* Skipping past everything I did to get here, 
the array (shuffledDeck) has 52 shuffled cards (elements) in it. 
The array is NSMutableArray and contains strings like 
2Hearts, 5Spades, 14Clubs, etc. Each suit has 14 cards.*/

shuffledDeck

// create vars to hold shuffled hands
var playerOneHand = []
var playerTwoHand = []

/* Started a for loop to assign cards to each hand 
but don't know which method(s) is/are best to use 
to remove the first or last card and alternately 
append (move) it to the (hopefully) initialized 
variables playerOneHand and PlayerTwoHand. 
Optionally, since the cards are already shuffled, 
I could just split the deck using the range method, 
whichever is easier. I tried and failed at both ways.*/

var i = 0
for dealtCard in shuffledDeck {

}

2 个解决方案

#1


As mentioned by Martin R you can use the range method to assign the first half of the deck to the first player and the second to the second player as follow:

正如Martin R所提到的,您可以使用范围方法将牌组的前半部分分配给第一个玩家,将第二个分配给第二个玩家,如下所示:

let cards:[String] = ["2♦️","3♦️","4♦️","5♦️","6♦️","7♦️","8♦️","9♦️","T♦️","J♦️","Q♦️","K♦️","A♦️","2♠️","3♠️","4♠️","5♠️","6♠️","7♠️","8♠️","9♠️","T♠️","J♠️","Q♠️","K♠️","A♠️","2♥️","3♥️","4♥️","5♥️","6♥️","7♥️","8♥️","9♥️","T♥️","J♥️","Q♥️","K♥️","A♥️","2♣️","3♣️","4♣️","5♣️","6♣️","7♣️","8♣️","9♣️","T♣️","J♣️","Q♣️","K♣️","A♣️"]

extension Array {
    var shuffled:[T] {
       var elements = self
        for index in 0..<elements.count - 1 {
            swap(&elements[index], &elements[Int(arc4random_uniform(UInt32(elements.count - 1 - index))) + index])
        }
        return elements
    }
}


let cardsShuffled = cards.shuffled

let playerOneHand = cardsShuffled[0...25]
let playerTwoHand = cardsShuffled[26...51]

Note: The shuffle extension was created using this answer as reference

注意:使用此答案作为参考创建了shuffle扩展名

#2


var shuffledDeck:[String] = ["2Hearts", "5Spades", "14Clubs", "etc"]
//shuffledDeck will of course be your shuffled deck
var playerOneHand:[String] = []
var playerTwoHand:[String] = []

for (index, cardString) in enumerate(shuffledDeck) {
    if index % 2 == 0 {
        playerOneHand.append(cardString)
    }else{
        playerTwoHand.append(cardString)
    }
}

I’m looping through every item in the shuffledDeck, but with that I use the index of the array to get a number. I use this number to see if that number devided by 2 is equal to 0 (the number is even) or not (uneven) if a number is even, I get the item that is in the array at the given index and add that item to the hand of player one. If the index is uneven I add the item to the second player’s hand. This means the first item goed to player one’s hand, the second item goes to the hand of the second player. the third Item goes back to the first player and so on.

我循环遍历shuffledDeck中的每个项目,但是我使用数组的索引来获取数字。我使用这个数字来看看如果数字是2等于0(数字是偶数)还是没有(不均匀)如果数字是偶数,我得到给定索引处的数组中的项目并添加该项目一个玩家的手。如果索引不均匀,我将项目添加到第二个玩家的手中。这意味着第一个项目转到玩家的手上,第二个项目转到第二个玩家的手上。第三个项目回到第一个玩家,依此类推。

#1


As mentioned by Martin R you can use the range method to assign the first half of the deck to the first player and the second to the second player as follow:

正如Martin R所提到的,您可以使用范围方法将牌组的前半部分分配给第一个玩家,将第二个分配给第二个玩家,如下所示:

let cards:[String] = ["2♦️","3♦️","4♦️","5♦️","6♦️","7♦️","8♦️","9♦️","T♦️","J♦️","Q♦️","K♦️","A♦️","2♠️","3♠️","4♠️","5♠️","6♠️","7♠️","8♠️","9♠️","T♠️","J♠️","Q♠️","K♠️","A♠️","2♥️","3♥️","4♥️","5♥️","6♥️","7♥️","8♥️","9♥️","T♥️","J♥️","Q♥️","K♥️","A♥️","2♣️","3♣️","4♣️","5♣️","6♣️","7♣️","8♣️","9♣️","T♣️","J♣️","Q♣️","K♣️","A♣️"]

extension Array {
    var shuffled:[T] {
       var elements = self
        for index in 0..<elements.count - 1 {
            swap(&elements[index], &elements[Int(arc4random_uniform(UInt32(elements.count - 1 - index))) + index])
        }
        return elements
    }
}


let cardsShuffled = cards.shuffled

let playerOneHand = cardsShuffled[0...25]
let playerTwoHand = cardsShuffled[26...51]

Note: The shuffle extension was created using this answer as reference

注意:使用此答案作为参考创建了shuffle扩展名

#2


var shuffledDeck:[String] = ["2Hearts", "5Spades", "14Clubs", "etc"]
//shuffledDeck will of course be your shuffled deck
var playerOneHand:[String] = []
var playerTwoHand:[String] = []

for (index, cardString) in enumerate(shuffledDeck) {
    if index % 2 == 0 {
        playerOneHand.append(cardString)
    }else{
        playerTwoHand.append(cardString)
    }
}

I’m looping through every item in the shuffledDeck, but with that I use the index of the array to get a number. I use this number to see if that number devided by 2 is equal to 0 (the number is even) or not (uneven) if a number is even, I get the item that is in the array at the given index and add that item to the hand of player one. If the index is uneven I add the item to the second player’s hand. This means the first item goed to player one’s hand, the second item goes to the hand of the second player. the third Item goes back to the first player and so on.

我循环遍历shuffledDeck中的每个项目,但是我使用数组的索引来获取数字。我使用这个数字来看看如果数字是2等于0(数字是偶数)还是没有(不均匀)如果数字是偶数,我得到给定索引处的数组中的项目并添加该项目一个玩家的手。如果索引不均匀,我将项目添加到第二个玩家的手中。这意味着第一个项目转到玩家的手上,第二个项目转到第二个玩家的手上。第三个项目回到第一个玩家,依此类推。