I am trying to lessen the number of lines in a function that I wrote. In order to achieve this I need to select a property of a struct based on some condition and pass that property as a parameter in order to compare that property among other elements in an array that are of the same struct type. Here is my gigantic code with repetition of same thing for different properties.
I have tried the following the code. Although it works as a charm but I feel the same work has been done a number of times.
func checkContentsForMatching(forIndex: Int) {
var cards = [SetCard]()
for index in game.indicesOfChosenCards.indices {
cards.append(cardTitles[testCards[game.indicesofChosenCards[index]]]!)
}
if (cards[0].color == cards[1].color) && (cards[1].color == cards[2].color) && (cards[0].color == cards[2].color) {
game.playingCards[game.indicesOfChosenCards[0]].color = true
game.playingCards[game.indicesOfChosenCards[1]].color = true
game.playingCards[game.indicesOfChosenCards[2]].color = true
}
else if cards[0].color == cards[1].color {
game.playingCards[game.indicesOfChosenCards[0]].color = true
game.playingCards[game.indicesOfChosenCards[1]].color = true
}
else if cards[1].color == cards[2].color {
game.playingCards[game.indicesOfChosenCards[1]].color = true
game.playingCards[game.indicesOfChosenCards[2]].color = true
}
else if cards[0].color == cards[2].color {
game.playingCards[game.indicesOfChosenCards[0]].color = true
game.playingCards[game.indicesOfChosenCards[2]].color = true
}
if (cards[0].shape == cards[1].shape) && (cards[1].shape == cards[2].shape) && (cards[0].shape == cards[2].shape) {
game.playingCards[game.indicesOfChosenCards[0]].shape = true
game.playingCards[game.indicesOfChosenCards[1]].shape = true
game.playingCards[game.indicesOfChosenCards[2]].shape = true
}
else if cards[0].shape == cards[1].shape {
game.playingCards[game.indicesOfChosenCards[0]].shape = true
game.playingCards[game.indicesOfChosenCards[1]].shape = true
}
else if cards[1].shape == cards[2].shape {
game.playingCards[game.indicesOfChosenCards[1]].shape = true
game.playingCards[game.indicesOfChosenCards[2]].shape = true
}
else if cards[0].shape == cards[2].shape {
game.playingCards[game.indicesOfChosenCards[0]].shape = true
game.playingCards[game.indicesOfChosenCards[2]].shape = true
}