Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions week2/Memorize/Memorize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,32 @@
objects = {

/* Begin PBXBuildFile section */
A690699028F5413B0070676A /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A690698F28F5413B0070676A /* Extensions.swift */; };
A69F17AF288E87F600AF88CC /* MemorizeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17AE288E87F600AF88CC /* MemorizeApp.swift */; };
A69F17B1288E87F600AF88CC /* MemorizeGameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17B0288E87F600AF88CC /* MemorizeGameView.swift */; };
A69F17B3288E87F700AF88CC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A69F17B2288E87F700AF88CC /* Assets.xcassets */; };
A69F17B6288E87F700AF88CC /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A69F17B5288E87F700AF88CC /* Preview Assets.xcassets */; };
A69F17C5288F77E100AF88CC /* Theme.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17C4288F77E100AF88CC /* Theme.swift */; };
A69F17CB288F8BC400AF88CC /* CardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17CA288F8BC400AF88CC /* CardView.swift */; };
A69F17CF288F9B6400AF88CC /* CardListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17CE288F9B6400AF88CC /* CardListView.swift */; };
A69F17D72890CBC700AF88CC /* MemorizeGameDealer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17D62890CBC700AF88CC /* MemorizeGameDealer.swift */; };
A69F17E52898B11C00AF88CC /* ThemeNameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17E42898B11C00AF88CC /* ThemeNameView.swift */; };
A69F17E72898B26C00AF88CC /* NewGameButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17E62898B26C00AF88CC /* NewGameButton.swift */; };
A69F17E92898C1CD00AF88CC /* MemorizeGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17E82898C1CD00AF88CC /* MemorizeGame.swift */; };
A69F17F1289A2D7A00AF88CC /* ScoreView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17F0289A2D7A00AF88CC /* ScoreView.swift */; };
A69F17F7289B5A9300AF88CC /* SuccessView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A69F17F6289B5A9300AF88CC /* SuccessView.swift */; };
A6FD786128E40041009A2677 /* Cardify.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6FD786028E40041009A2677 /* Cardify.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
A690698F28F5413B0070676A /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
A69F17AB288E87F600AF88CC /* Memorize.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Memorize.app; sourceTree = BUILT_PRODUCTS_DIR; };
A69F17AE288E87F600AF88CC /* MemorizeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeApp.swift; sourceTree = "<group>"; };
A69F17B0288E87F600AF88CC /* MemorizeGameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeGameView.swift; sourceTree = "<group>"; };
A69F17B2288E87F700AF88CC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
A69F17B5288E87F700AF88CC /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
A69F17C4288F77E100AF88CC /* Theme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Theme.swift; sourceTree = "<group>"; };
A69F17CA288F8BC400AF88CC /* CardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardView.swift; sourceTree = "<group>"; };
A69F17CE288F9B6400AF88CC /* CardListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardListView.swift; sourceTree = "<group>"; };
A69F17D62890CBC700AF88CC /* MemorizeGameDealer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeGameDealer.swift; sourceTree = "<group>"; };
A69F17E42898B11C00AF88CC /* ThemeNameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeNameView.swift; sourceTree = "<group>"; };
A69F17E62898B26C00AF88CC /* NewGameButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewGameButton.swift; sourceTree = "<group>"; };
A69F17E82898C1CD00AF88CC /* MemorizeGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeGame.swift; sourceTree = "<group>"; };
A69F17F0289A2D7A00AF88CC /* ScoreView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScoreView.swift; sourceTree = "<group>"; };
A69F17F6289B5A9300AF88CC /* SuccessView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuccessView.swift; sourceTree = "<group>"; };
A6FD786028E40041009A2677 /* Cardify.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cardify.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -75,6 +71,7 @@
A69F17C3288F77AC00AF88CC /* Model */,
A69F17F8289FD3E600AF88CC /* View */,
A69F17F9289FD43A00AF88CC /* ViewModel */,
A690698F28F5413B0070676A /* Extensions.swift */,
);
path = Memorize;
sourceTree = "<group>";
Expand All @@ -101,11 +98,8 @@
children = (
A69F17B0288E87F600AF88CC /* MemorizeGameView.swift */,
A69F17CA288F8BC400AF88CC /* CardView.swift */,
A69F17CE288F9B6400AF88CC /* CardListView.swift */,
A69F17E42898B11C00AF88CC /* ThemeNameView.swift */,
A69F17E62898B26C00AF88CC /* NewGameButton.swift */,
A69F17F0289A2D7A00AF88CC /* ScoreView.swift */,
A69F17F6289B5A9300AF88CC /* SuccessView.swift */,
A6FD786028E40041009A2677 /* Cardify.swift */,
);
path = View;
sourceTree = "<group>";
Expand Down Expand Up @@ -189,14 +183,12 @@
buildActionMask = 2147483647;
files = (
A69F17E92898C1CD00AF88CC /* MemorizeGame.swift in Sources */,
A690699028F5413B0070676A /* Extensions.swift in Sources */,
A69F17C5288F77E100AF88CC /* Theme.swift in Sources */,
A69F17B1288E87F600AF88CC /* MemorizeGameView.swift in Sources */,
A69F17F7289B5A9300AF88CC /* SuccessView.swift in Sources */,
A69F17E72898B26C00AF88CC /* NewGameButton.swift in Sources */,
A69F17E52898B11C00AF88CC /* ThemeNameView.swift in Sources */,
A6FD786128E40041009A2677 /* Cardify.swift in Sources */,
A69F17AF288E87F600AF88CC /* MemorizeApp.swift in Sources */,
A69F17F1289A2D7A00AF88CC /* ScoreView.swift in Sources */,
A69F17CF288F9B6400AF88CC /* CardListView.swift in Sources */,
A69F17CB288F8BC400AF88CC /* CardView.swift in Sources */,
A69F17D72890CBC700AF88CC /* MemorizeGameDealer.swift in Sources */,
);
Expand Down
22 changes: 22 additions & 0 deletions week2/Memorize/Memorize/Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Extensions.swift
// Memorize
//
// Created by ohhyeongseok on 2022/10/11.
//

import Foundation

extension Array {

var oneAndOnly: Element? {
count == 1 ? first : nil
}
}

extension Array where Element: Identifiable {

func index(matching element: Element) -> Index? {
firstIndex(where: { $0.id == element.id })
}
}
16 changes: 13 additions & 3 deletions week2/Memorize/Memorize/Model/MemorizeGame.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import Foundation

struct MemorizeGame<CardContent: Equatable> {

// MARK: Property(ies)

private(set) var cards: [Card] = []
private(set) var score = 0
private var currentOpenedCards: [Card] = []
var isFinished: Bool {
cards.filter({ $0.isMatched }).count / 2 == cards.count / 2
}

// MARK: Initializer(s)

init(numberOfPairsOfCards: Int, createContent: (Int) -> CardContent) {
cards = makeCards(numberOfPairsOfCards: numberOfPairsOfCards, createContent: createContent)
}

// MARK: Method(s)

func makeCards(numberOfPairsOfCards: Int, createContent: (Int) -> CardContent) -> [Card] {
var cards: [Card] = []
for pairIndex in 0 ..< numberOfPairsOfCards {
Expand Down Expand Up @@ -61,8 +68,7 @@ struct MemorizeGame<CardContent: Equatable> {
cards[chosenIndex].isMatched = true
}
}
}
else {
} else {
currentOpenedCards.forEach { opendCard in
if opendCard.isFaceUpAtLeastOnce == true {
score -= 1
Expand All @@ -74,8 +80,12 @@ struct MemorizeGame<CardContent: Equatable> {
}
}

mutating func shuffle() {
cards.shuffle()
}

struct Card: Identifiable {
var isFaceUp = false
var isFaceUp = true
var isMatched = false
var isFaceUpAtLeastOnce = false
let content: CardContent
Expand Down
15 changes: 0 additions & 15 deletions week2/Memorize/Memorize/View/CardListView.swift

This file was deleted.

35 changes: 15 additions & 20 deletions week2/Memorize/Memorize/View/CardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,23 @@ struct CardView: View {
let card: MemorizeGame<String>.Card

var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 20)
if card.isFaceUp {
shape.fill().foregroundColor(.white)
shape.strokeBorder(lineWidth: 5)
Text(card.content).font(.largeTitle)
} else if card.isMatched {
shape.fill(.white)
} else {
shape.fill().foregroundColor(memorizeGameDealer.themeColor)
}
}
.onTapGesture {
if !card.isMatched {
memorizeGameDealer.choose(card: card)
}
}
Text(card.content)
.font(.largeTitle)
.rotationEffect(Angle(degrees: card.isMatched ? 360 : 0))
.animation(matchedRotateAnimation, value: card.isMatched)
.cardify(isFaceUp: card.isFaceUp, isMatched: card.isMatched)
}

private var matchedRotateAnimation: Animation {
Animation
.linear(duration: AnimationConstants.matchedRotateDuration)
.repeatCount(AnimationConstants.matchedRotateCount, autoreverses: true)
}
}

struct CardView_Previews: PreviewProvider {
static var previews: some View {
CardView(card: MemorizeGame<String>.Card(content: "aa", id: 1))
extension CardView {
private enum AnimationConstants {
static let matchedRotateDuration = 5.0
static let matchedRotateCount = 5
}
}
48 changes: 48 additions & 0 deletions week2/Memorize/Memorize/View/Cardify.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Cardify.swift
// Memorize
//
// Created by ohhyeongseok on 2022/09/28.

import SwiftUI

struct Cardify: ViewModifier, Animatable {
@EnvironmentObject var memorizeGameDealer: MemorizeGameDealer
var isMatched: Bool
var rotation: Double
var animatableData: Double {
get { rotation }
set { rotation = newValue }
}

init(isFaceUp: Bool, isMatched: Bool) {
rotation = isFaceUp ? 0 : 180
self.isMatched = isMatched
}

func body(content: Content) -> some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 20)
if rotation < 90 {
shape.fill().foregroundColor(.white)
shape.strokeBorder(lineWidth: 5)
} else if isMatched{
shape.fill(.white)
} else {
shape.fill().foregroundColor(memorizeGameDealer.themeColor)
}
content.opacity(rotation < 90 ? 1 : 0)
}
.rotation3DEffect(Angle(degrees: rotation), axis: (0, 1, 0))

}
}

extension View {

func cardify(isFaceUp: Bool, isMatched: Bool) -> some View {
self.modifier(Cardify(isFaceUp: isFaceUp, isMatched: isMatched))
}

}

Loading