diff --git a/Memorize.xcodeproj/project.pbxproj b/Memorize.xcodeproj/project.pbxproj index ee6f11c..8a8fb19 100644 --- a/Memorize.xcodeproj/project.pbxproj +++ b/Memorize.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* ContentView.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 */; }; + 245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -19,6 +21,8 @@ 240EDC402998A3B900A46AC9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.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 = ""; }; + 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGame.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -53,6 +57,8 @@ children = ( 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */, 240EDC402998A3B900A46AC9 /* ContentView.swift */, + 245099F22998EAD6000CE9DA /* MemoryGame.swift */, + 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */, 240EDC422998A3BA00A46AC9 /* Assets.xcassets */, 240EDC442998A3BA00A46AC9 /* Preview Content */, ); @@ -137,8 +143,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */, 240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */, 240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */, + 245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Memorize/ContentView.swift b/Memorize/ContentView.swift index 1313308..1203249 100644 --- a/Memorize/ContentView.swift +++ b/Memorize/ContentView.swift @@ -14,42 +14,17 @@ struct ContentView: View { VStack { ScrollView { LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) { - ForEach(emojis[0.. 1{ - emojiCount -= 1 - } - }, label: { - Image(systemName: "minus.circle") - }) - } - var add: some View { - Button(action: { - if emojiCount < emojis.count { - emojiCount += 1 - } - }, label: { - Image(systemName: "plus.circle") - }) - } } struct CardView: View { @@ -78,17 +53,6 @@ struct CardView: View { } } - - - - - - - - - - - struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() diff --git a/Memorize/EmojiMemoryGame.swift b/Memorize/EmojiMemoryGame.swift new file mode 100644 index 0000000..3132c2a --- /dev/null +++ b/Memorize/EmojiMemoryGame.swift @@ -0,0 +1,23 @@ +// +// EmojiMemoryGame.swift +// Memorize +// +// Created by ching on 2023/2/12. +// + +import SwiftUI + +// viewmodel +class EmojiMemoryGame { + static let emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"] + + static func createMemoryGame() -> MemoryGame { + MemoryGame(numberOfPairsOfCards: 5) { pairIndex in EmojiMemoryGame.emojis[pairIndex] } + } + + private var model: MemoryGame = createMemoryGame() + + var cards: [MemoryGame.Card] { + return model.cards + } +} diff --git a/Memorize/MemoryGame.swift b/Memorize/MemoryGame.swift new file mode 100644 index 0000000..ec65cdf --- /dev/null +++ b/Memorize/MemoryGame.swift @@ -0,0 +1,31 @@ +// +// MemoryGame.swift +// Memorize +// +// Created by ching on 2023/2/12. +// + +import Foundation + +// model +struct MemoryGame { + private(set) var cards: [Card] + + func choose(_ card: Card) {} + + init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) { + cards = [Card]() + // + for pairIndex in 0 ..< numberOfPairsOfCards { + let content: CardContent = createCardContent(pairIndex) + cards.append(Card(content: content)) + cards.append(Card(content: content)) + } + } + + struct Card { + var isFaceUp: Bool = false + var isMatched: Bool = false + var content: CardContent + } +}