diff --git a/Memorize.xcodeproj/project.pbxproj b/Memorize.xcodeproj/project.pbxproj index 8a8fb19..873e942 100644 --- a/Memorize.xcodeproj/project.pbxproj +++ b/Memorize.xcodeproj/project.pbxproj @@ -8,7 +8,7 @@ /* Begin PBXBuildFile section */ 240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */; }; - 240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* ContentView.swift */; }; + 240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */; }; 240EDC432998A3BA00A46AC9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC422998A3BA00A46AC9 /* Assets.xcassets */; }; 240EDC462998A3BA00A46AC9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */; }; 245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F22998EAD6000CE9DA /* MemoryGame.swift */; }; @@ -18,7 +18,7 @@ /* Begin PBXFileReference section */ 240EDC3B2998A3B900A46AC9 /* Memorize.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Memorize.app; sourceTree = BUILT_PRODUCTS_DIR; }; 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeApp.swift; sourceTree = ""; }; - 240EDC402998A3B900A46AC9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGameView.swift; sourceTree = ""; }; 240EDC422998A3BA00A46AC9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = ""; }; @@ -56,7 +56,7 @@ isa = PBXGroup; children = ( 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */, - 240EDC402998A3B900A46AC9 /* ContentView.swift */, + 240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */, 245099F22998EAD6000CE9DA /* MemoryGame.swift */, 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */, 240EDC422998A3BA00A46AC9 /* Assets.xcassets */, @@ -144,7 +144,7 @@ buildActionMask = 2147483647; files = ( 245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */, - 240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */, + 240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */, 240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */, 245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */, ); diff --git a/Memorize/EmojiMemoryGame.swift b/Memorize/EmojiMemoryGame.swift index f7283ea..47361c2 100644 --- a/Memorize/EmojiMemoryGame.swift +++ b/Memorize/EmojiMemoryGame.swift @@ -8,22 +8,23 @@ import SwiftUI // viewmodel -class EmojiMemoryGame: ObservableObject{ +class EmojiMemoryGame: ObservableObject { + typealias Card = MemoryGame.Card static let emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"] static func createMemoryGame() -> MemoryGame { - MemoryGame(numberOfPairsOfCards: 5) { pairIndex in EmojiMemoryGame.emojis[pairIndex] } + MemoryGame(numberOfPairsOfCards: emojis.count) { pairIndex in EmojiMemoryGame.emojis[pairIndex] } } - @Published private var model: MemoryGame = createMemoryGame() + @Published private var model = createMemoryGame() - var cards: [MemoryGame.Card] { + var cards: [Card] { return model.cards } // MARK: - Intent(s) - func choose(_ card: MemoryGame.Card) { + func choose(_ card: Card) { model.choose(card) } } diff --git a/Memorize/ContentView.swift b/Memorize/EmojiMemoryGameView.swift similarity index 76% rename from Memorize/ContentView.swift rename to Memorize/EmojiMemoryGameView.swift index 1937e60..937fc5d 100644 --- a/Memorize/ContentView.swift +++ b/Memorize/EmojiMemoryGameView.swift @@ -1,5 +1,5 @@ // -// ContentView.swift +// EmojiMemoryGameView.swift // Memorize // // Created by ching on 2023/2/12. @@ -7,15 +7,15 @@ import SwiftUI -struct ContentView: View { - @ObservedObject var viewModel: EmojiMemoryGame +struct EmojiMemoryGameView: View { + @ObservedObject var game: EmojiMemoryGame var body: some View { ScrollView { LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) { - ForEach(viewModel.cards) { + ForEach(game.cards) { card in CardView(card: card).aspectRatio(2 / 3, contentMode: .fit) .onTapGesture { - viewModel.choose(card) + game.choose(card) } } } @@ -26,11 +26,11 @@ struct ContentView: View { } struct CardView: View { - let card: MemoryGame.Card + let card: EmojiMemoryGame.Card var body: some View { ZStack { let shape = RoundedRectangle(cornerRadius: 20.0) - if card.isFaceUp { + if card.isFaceUp && !card.isMatched { shape.fill().foregroundColor(.white) shape.strokeBorder(lineWidth: 3) Text(card.content).font(.largeTitle) @@ -46,9 +46,9 @@ struct CardView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { let game = EmojiMemoryGame() - ContentView(viewModel: game) + EmojiMemoryGameView(game: game) .preferredColorScheme(.dark) - ContentView(viewModel: game) + EmojiMemoryGameView(game: game) .preferredColorScheme(.light) } } diff --git a/Memorize/MemorizeApp.swift b/Memorize/MemorizeApp.swift index 809b856..fc0e80e 100644 --- a/Memorize/MemorizeApp.swift +++ b/Memorize/MemorizeApp.swift @@ -12,7 +12,7 @@ struct MemorizeApp: App { let game = EmojiMemoryGame() var body: some Scene { WindowGroup { - ContentView(viewModel: game) + EmojiMemoryGameView(game: game) } } } diff --git a/Memorize/MemoryGame.swift b/Memorize/MemoryGame.swift index aa2aa68..5e7626b 100644 --- a/Memorize/MemoryGame.swift +++ b/Memorize/MemoryGame.swift @@ -10,7 +10,10 @@ import Foundation // model struct MemoryGame where CardContent: Equatable { private(set) var cards: [Card] - private var indexOfTheOneAndOnlyFaceUpCard: Int? + private var indexOfTheOneAndOnlyFaceUpCard: Int? { + get { cards.indices.filter { cards[$0].isFaceUp }.oneAndOnly } + set { cards.indices.forEach { cards[$0].isFaceUp = ($0 == newValue) } } + } mutating func choose(_ card: Card) { if let chosenIndex = cards.firstIndex(where: { $0.id == card.id }), !cards[chosenIndex].isFaceUp, !cards[chosenIndex].isMatched { @@ -19,16 +22,10 @@ struct MemoryGame where CardContent: Equatable { cards[chosenIndex].isMatched = true cards[potentialMatchIndex].isMatched = true } - indexOfTheOneAndOnlyFaceUpCard = nil + cards[chosenIndex].isFaceUp.toggle() } else { - for index in cards.indices { - if cards[index].isFaceUp { - cards[index].isFaceUp.toggle() - } - } indexOfTheOneAndOnlyFaceUpCard = chosenIndex } - cards[chosenIndex].isFaceUp.toggle() } } @@ -42,8 +39,8 @@ struct MemoryGame where CardContent: Equatable { } init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) { - cards = [Card]() - // + cards = [] + for pairIndex in 0 ..< numberOfPairsOfCards { let content: CardContent = createCardContent(pairIndex) cards.append(Card(content: content, id: pairIndex * 2)) @@ -52,9 +49,19 @@ struct MemoryGame where CardContent: Equatable { } struct Card: Identifiable { - var isFaceUp: Bool = false - var isMatched: Bool = false - var content: CardContent - var id: Int + var isFaceUp = false + var isMatched = false + let content: CardContent + let id: Int + } +} + +extension Array { + var oneAndOnly: Element? { + if count == 1 { + return first + } else { + return nil + } } }