简体   繁体   中英

How do I iterate through one array and move every other element to two new arrays in Swift?

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.

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.

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 stackoverflow. 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.

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.

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 {

}
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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM