1

I am only 10 years old an I am making a War (The Card Game) app. In the war app, I had to put arrays within arrays. How would I find an index of an item in this kind of array? Here's an example where I would get an error:

var card_array = [["2_of_clubs", "2_of_diamonds", "2_of_hearts", "2_of_spades"],
                  ["3_of_clubs", "3_of_diamonds", "3_of_hearts", "3_of_spades"],
                  ["4_of_clubs", "4_of_diamonds", "4_of_hearts", "4_of_spades"],
                  ["5_of_clubs", "5_of_diamonds", "5_of_hearts", "5_of_spades"],
                  ["6_of_clubs", "6_of_diamonds", "6_of_hearts", "6_of_spades"],
                  ["7_of_clubs", "7_of_diamonds", "7_of_hearts", "7_of_spades"],
                  ["8_of_clubs", "8_of_diamonds", "8_of_hearts", "8_of_spades"],
                  ["9_of_clubs", "9_of_diamonds", "9_of_hearts", "9_of_spades"],
                  ["10_of_clubs", "10_of_diamonds", "10_of_hearts", "10_of_spades"],
                  ["jack_of_clubs2", "jack_of_diamonds2", "jack_of_hearts2", "jack_of_spades2"],
                  ["queen_of_clubs2", "queen_of_diamonds2", "queen_of_hearts2", "queen_of_spades2"],
                  ["king_of_clubs2", "king_of_diamonds2", "king_of_hearts2", "king_of_spades2"],
                  ["ace_of_clubs", "ace_of_diamonds", "ace_of_hearts", "ace_of_spades"]]
var locationBottom = card_array.index(of: ["4_of_spades"])
var locationTop = card_array.index(of: ["king_of_diamonds2"])
print(locationTop)
print(locationBottom)                                            

3 Answers3

0

It looks like what you want is:

var locationBottom = card_array[0].index(of: "4_of_spades")
var locationTop = card_array[0].index(of: "king_of_diamonds2")

Instead, you're looking for an instance of an array containing solely one card in an array that contains an array of all 52 cards.

Tim Kokesh
  • 879
  • 7
  • 10
0

The first answer might be correct, but it's not as good as the following since calling an index of an array without checking if it exists will crash your program. :)

Below is an example where you first flatten the nested arrays down to one array, and the use the if let syntax to check for an optional value which is the index.

Then it prints the indexes to the console. I've also included a print statement of the flattened array for you to see what it looks like.

var card_array = [["2_of_clubs", "2_of_diamonds", "2_of_hearts", "2_of_spades"],
              ["3_of_clubs", "3_of_diamonds", "3_of_hearts", "3_of_spades"],
              ["4_of_clubs", "4_of_diamonds", "4_of_hearts", "4_of_spades"],
              ["5_of_clubs", "5_of_diamonds", "5_of_hearts", "5_of_spades"],
              ["6_of_clubs", "6_of_diamonds", "6_of_hearts", "6_of_spades"],
              ["7_of_clubs", "7_of_diamonds", "7_of_hearts", "7_of_spades"],
              ["8_of_clubs", "8_of_diamonds", "8_of_hearts", "8_of_spades"],
              ["9_of_clubs", "9_of_diamonds", "9_of_hearts", "9_of_spades"],
              ["10_of_clubs", "10_of_diamonds", "10_of_hearts", "10_of_spades"],
              ["jack_of_clubs2", "jack_of_diamonds2", "jack_of_hearts2", "jack_of_spades2"],
              ["queen_of_clubs2", "queen_of_diamonds2", "queen_of_hearts2", "queen_of_spades2"],
              ["king_of_clubs2", "king_of_diamonds2", "king_of_hearts2", "king_of_spades2"],
              ["ace_of_clubs", "ace_of_diamonds", "ace_of_hearts", "ace_of_spades"]]

let flatCardArray = card_array.flatMap({$0})
print(flatCardArray)

if let locationBottom = flatCardArray.index(of: "4_of_spades"), let locationTop = flatCardArray.index(of: "king_of_diamonds2") {
    print("index of locationTop is: ", locationTop)
    print("index of locationBottom is: ", locationBottom)
} else {
    print("oh no, couldn't find indexes!")
}

Or you could do

for (number, array) in card_array.enumerated() {
    if let locationBottom = array.index(of: "4_of_spades") {
        print("index of locationBottom is: ", locationBottom, ", in array number: ", number)
    }
    if let locationTop = array.index(of: "king_of_diamonds2") {
        print("index of locationTop is: ", locationTop, ", in array number: ", number)
    }
}
Frizzo
  • 432
  • 5
  • 10
0

You are using square array, so you have to find two indexes: one for a row, another for a column, here is a simple example:

let elementToFind = "king_of_diamonds2"
var columnIndex:Int?
var rowIndex:Int? = card_array.index(where: { (arrayInside) -> Bool in
    columnIndex = arrayInside.index(where: { (string) -> Bool in
        return string == elementToFind
    })
    return columnIndex != nil
})

print("Row: \(rowIndex!) Column: \(columnIndex!)")
Eugene Laminskiy
  • 594
  • 2
  • 11