Here I will upload solutions for assignments of cs193p 2020
We can shuffle cards in our init of our Model MemoryGame.
init(numberOfPairsOfCards: Int, cardContentFactory: (Int) -> CardContent) {
cards = Array<Card>()
for pairIndex in 0..<numberOfPairsOfCards {
let content: CardContent = cardContentFactory(pairIndex)
cards.append(Card(content: content, id: pairIndex*2))
cards.append(Card(content: content, id: pairIndex*2+1))
}
cards.shuffle() //Here we can shuffle the cards
}
In swift we have a built-in method called shuffle(), and he is shuffling elements.
Changing ratio surely is a UI thing, so we need to change something in our View. Luckily, we have a built-in method called aspectRatio(), so we need to use it
HStack {
return ForEach(viewModel.cards){ card in
CardView(card: card).onTapGesture {
self.viewModel.choose(card: card)
}
.aspectRatio(2/3, contentMode: .fit) // Here the aspectRatio() Usage
}
}
We are returning numberOfPairsOfCards in the EmojiMemoryGame in the function called createMemoryGame(), so all what we need is generate random number from 2 to 5 and set it as numberOfPairsOfCards.
return MemoryGame<String>(numberOfPairsOfCards: .random(in: 2...5)) { pairIndex in emojis[pairIndex]}
//Take note at .random(in: 2...5) here we're generating numberOfPairsOfCards
Changing fonts is UI thing,so we need to change something in our View. I decided to solve this problem in one line, and used ternary operator for that.
struct ContentView: View {
var viewModel: EmojiMemoryGame
var body: some View {
HStack {
return ForEach(viewModel.cards){ card in
CardView(card: card).onTapGesture {
self.viewModel.choose(card: card)
}
.aspectRatio(2/3, contentMode: .fit)
}
}
.font(viewModel.cards.count == 5 ? .largeTitle : .body) // Ternary operator here.
.padding()
.foregroundColor(Color.orange)
}
}
I think that one of the ways to solve this is to change the emojis array in the EmojiMemoryGame. My solution is deleting some emojis.
while emojis.count > 5 {
emojis.remove(at: emojis.firstIndex(of: emojis.randomElement()!)!)
}