Compare commits
2 Commits
189bfc0bb8
...
08dc4700a3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08dc4700a3 | ||
|
|
449fa1b8e9 |
@ -8,21 +8,23 @@
|
||||
|
||||
/* 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 */; };
|
||||
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */; };
|
||||
24E748FC29993782009B5FE8 /* Constans.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E748FB29993781009B5FE8 /* Constans.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* 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 = "<group>"; };
|
||||
240EDC402998A3B900A46AC9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGameView.swift; sourceTree = "<group>"; };
|
||||
240EDC422998A3BA00A46AC9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
||||
245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = "<group>"; };
|
||||
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGame.swift; sourceTree = "<group>"; };
|
||||
24E748FB29993781009B5FE8 /* Constans.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constans.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@ -56,7 +58,8 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
|
||||
240EDC402998A3B900A46AC9 /* ContentView.swift */,
|
||||
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */,
|
||||
24E748FB29993781009B5FE8 /* Constans.swift */,
|
||||
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
|
||||
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
|
||||
240EDC422998A3BA00A46AC9 /* Assets.xcassets */,
|
||||
@ -144,7 +147,8 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
|
||||
240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */,
|
||||
240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */,
|
||||
24E748FC29993782009B5FE8 /* Constans.swift in Sources */,
|
||||
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
|
||||
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
|
||||
);
|
||||
|
||||
20
Memorize/Constans.swift
Normal file
20
Memorize/Constans.swift
Normal file
@ -0,0 +1,20 @@
|
||||
//
|
||||
// Constans.swift
|
||||
// Memorize
|
||||
//
|
||||
// Created by ching on 2023/2/12.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct const {
|
||||
enum DrawingConstants {
|
||||
static let cornerRadius: CGFloat = 20
|
||||
static let lineWidth: CGFloat = 3
|
||||
static let fontScale: CGFloat = 0.8
|
||||
static let gridWidth: CGFloat = 80
|
||||
static let gridAspectRatio: CGFloat = 2 / 3
|
||||
static let matchedCardOpacity: Double = 0
|
||||
}
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// Memorize
|
||||
//
|
||||
// Created by ching on 2023/2/12.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@ObservedObject var viewModel: EmojiMemoryGame
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) {
|
||||
ForEach(viewModel.cards) {
|
||||
card in CardView(card: card).aspectRatio(2 / 3, contentMode: .fit)
|
||||
.onTapGesture {
|
||||
viewModel.choose(card)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
}
|
||||
|
||||
struct CardView: View {
|
||||
let card: MemoryGame<String>.Card
|
||||
var body: some View {
|
||||
ZStack {
|
||||
let shape = RoundedRectangle(cornerRadius: 20.0)
|
||||
if card.isFaceUp {
|
||||
shape.fill().foregroundColor(.white)
|
||||
shape.strokeBorder(lineWidth: 3)
|
||||
Text(card.content).font(.largeTitle)
|
||||
} else if card.isMatched {
|
||||
shape.opacity(0)
|
||||
} else {
|
||||
shape.fill()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
let game = EmojiMemoryGame()
|
||||
ContentView(viewModel: game)
|
||||
.preferredColorScheme(.dark)
|
||||
ContentView(viewModel: game)
|
||||
.preferredColorScheme(.light)
|
||||
}
|
||||
}
|
||||
@ -8,22 +8,23 @@
|
||||
import SwiftUI
|
||||
|
||||
// viewmodel
|
||||
class EmojiMemoryGame: ObservableObject{
|
||||
class EmojiMemoryGame: ObservableObject {
|
||||
typealias Card = MemoryGame<String>.Card
|
||||
static let emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"]
|
||||
|
||||
static func createMemoryGame() -> MemoryGame<String> {
|
||||
MemoryGame<String>(numberOfPairsOfCards: 5) { pairIndex in EmojiMemoryGame.emojis[pairIndex] }
|
||||
MemoryGame<String>(numberOfPairsOfCards: emojis.count) { pairIndex in EmojiMemoryGame.emojis[pairIndex] }
|
||||
}
|
||||
|
||||
@Published private var model: MemoryGame<String> = createMemoryGame()
|
||||
@Published private var model = createMemoryGame()
|
||||
|
||||
var cards: [MemoryGame<String>.Card] {
|
||||
var cards: [Card] {
|
||||
return model.cards
|
||||
}
|
||||
|
||||
// MARK: - Intent(s)
|
||||
|
||||
func choose(_ card: MemoryGame<String>.Card) {
|
||||
func choose(_ card: Card) {
|
||||
model.choose(card)
|
||||
}
|
||||
}
|
||||
|
||||
62
Memorize/EmojiMemoryGameView.swift
Normal file
62
Memorize/EmojiMemoryGameView.swift
Normal file
@ -0,0 +1,62 @@
|
||||
//
|
||||
// EmojiMemoryGameView.swift
|
||||
// Memorize
|
||||
//
|
||||
// Created by ching on 2023/2/12.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct EmojiMemoryGameView: View {
|
||||
@ObservedObject var game: EmojiMemoryGame
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: const.DrawingConstants.gridWidth))]) {
|
||||
ForEach(game.cards) {
|
||||
card in CardView(card: card).aspectRatio(const.DrawingConstants.gridAspectRatio, contentMode: .fit)
|
||||
.onTapGesture {
|
||||
game.choose(card)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
}
|
||||
|
||||
struct CardView: View {
|
||||
let card: EmojiMemoryGame.Card
|
||||
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
ZStack {
|
||||
let shape = RoundedRectangle(cornerRadius: const.DrawingConstants.cornerRadius)
|
||||
if card.isFaceUp && !card.isMatched {
|
||||
shape.fill().foregroundColor(.white)
|
||||
shape.strokeBorder(lineWidth: const.DrawingConstants.lineWidth)
|
||||
Text(card.content).font(font(in: geometry.size))
|
||||
} else if card.isMatched {
|
||||
shape.opacity(const.DrawingConstants.matchedCardOpacity)
|
||||
} else {
|
||||
shape.fill()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func font(in size: CGSize) -> Font {
|
||||
Font.system(size: min(size.width, size.height) * const.DrawingConstants.fontScale)
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
let game = EmojiMemoryGame()
|
||||
EmojiMemoryGameView(game: game)
|
||||
.preferredColorScheme(.dark)
|
||||
EmojiMemoryGameView(game: game)
|
||||
.preferredColorScheme(.light)
|
||||
}
|
||||
}
|
||||
@ -12,7 +12,7 @@ struct MemorizeApp: App {
|
||||
let game = EmojiMemoryGame()
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView(viewModel: game)
|
||||
EmojiMemoryGameView(game: game)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,10 @@ import Foundation
|
||||
// model
|
||||
struct MemoryGame<CardContent> 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<CardContent> 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<CardContent> 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<CardContent> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user