2021-09-27 get set

2021-09-27  本文已影响0人  脑子
import Foundation

struct MemoryGame<CardContent> where CardContent: Equatable {
    private(set) var cards: Array<Card>
    
    private var indexOfTheOneAndOnlyFaceUpCard: Int? {
        get {
            var faceUpCardsIndices = [Int]()
            //把朝上卡的index做一个数组,如果有朝上的把index加入数组
            for index in cards.indices {
                if cards[index].isFaceUp {
                    faceUpCardsIndices.append(index)
                }
            }
            //如果数组有一个数
            if faceUpCardsIndices.count == 1 {
                //返回数组中第一个数的index
                return faceUpCardsIndices.first
            } else {
                return nil
            }
        }
        set {
            for index in cards.indices {
                //如果唯一朝上卡index 不在数组里,所有卡都朝下
                if index != indexOfTheOneAndOnlyFaceUpCard {
                    cards[index].isFaceUp = false
                } else {
                    //如果在,都朝上,其实只有一张朝上
                    cards[index].isFaceUp = true
                }
               
            }
        }
    }
    
    mutating func choose(_ card: Card) {
        if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}),
           !cards[chosenIndex].isMatched,
           !cards[chosenIndex].isFaceUp
           {
            if let potentialIndex = indexOfTheOneAndOnlyFaceUpCard {
                if cards[chosenIndex].content == cards[potentialIndex].content {
                    cards[chosenIndex].isMatched = true
                    cards[potentialIndex].isMatched = true
                }
                //匹配上之后唯一朝上的卡就没有了
                //indexOfTheOneAndOnlyFaceUpCard = nil
                //匹配后把选中卡朝上
                cards[chosenIndex].isFaceUp = true
            } else {
//            } else {
//                //没匹配上,所有卡都朝下
//                for index in cards.indices {
//                    cards[index].isFaceUp = false
//                }
                //唯一朝上的卡就是选中卡
                indexOfTheOneAndOnlyFaceUpCard = chosenIndex
            }

        }
    }
    
    init(numberOfPairOfCards: Int, createCardContent: (Int) ->CardContent) {
        cards = []
        for pairIndex in 0..<numberOfPairOfCards {
            let content = createCardContent(pairIndex)
            cards.append(Card(content: content, id: pairIndex*2))
            cards.append(Card(content: content, id: pairIndex*2+1))
        }
    }
    
    struct Card:Identifiable {
        var isFaceUp = false
        var isMatched = false
        var content: CardContent
        var id: Int
    }
}

import Foundation

struct MemoryGame<CardContent> where CardContent: Equatable {
    private(set) var cards: Array<Card>
    
    //计算变量
    private var indexOfTheOneAndOnlyFaceUpCard: Int? {
        get {
            var faceUpCardsIndices = [Int]()
            for index in cards.indices {
                if cards[index].isFaceUp {
                    faceUpCardsIndices.append(index)
                }
            }
            if faceUpCardsIndices.count == 1 {
                return faceUpCardsIndices.first
            } else {
                return nil
            }
        }
        set {
            for index in cards.indices {
                if index != indexOfTheOneAndOnlyFaceUpCard {
                    cards[index].isFaceUp = false
                } else {
                    cards[index].isFaceUp = true
                }
                
            }
        }
    }
    mutating func choose(_ card: Card) {
        if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}),
           !cards[chosenIndex].isMatched,
           !cards[chosenIndex].isFaceUp
           {
            if let potentialIndex = indexOfTheOneAndOnlyFaceUpCard {
                if cards[chosenIndex].content == cards[potentialIndex].content {
                    cards[chosenIndex].isMatched = true
                    cards[potentialIndex].isMatched = true
                }
                //匹配后把选中卡朝上
                cards[chosenIndex].isFaceUp = true
                
            } else {
                //没有匹配上把所有卡都朝下,制作朝上的卡
//                for index in cards.indices {
//                    cards[index].isFaceUp = false
//                }
                indexOfTheOneAndOnlyFaceUpCard = chosenIndex
            }
           // cards[chosenIndex].isFaceUp.toggle()
        }
    }
    
    init(numberOfPairOfCards: Int, createCardContent: (Int) ->CardContent) {
        cards = []
        for pairIndex in 0..<numberOfPairOfCards {
            let content = createCardContent(pairIndex)
            cards.append(Card(content: content, id: pairIndex*2))
            cards.append(Card(content: content, id: pairIndex*2+1))
        }
    }
    
    struct Card:Identifiable {
        var isFaceUp = false
        var isMatched = false
        var content: CardContent
        var id: Int
    }
}
import Foundation

struct MemoryGame<CardContent> where CardContent: Equatable {
    private(set) var cards: Array<Card>
    
    //计算变量
    private var indexOfTheOneAndOnlyFaceUpCard: Int? {
        //找到唯一朝上的卡
        get {
            var faceUpCardsIndices = [Int]()
            for index in cards.indices {
                if cards[index].isFaceUp {
                    faceUpCardsIndices.append(index)
                }
            }
            if faceUpCardsIndices.count == 1 {
                return faceUpCardsIndices.first
            } else {
                return nil
            }
        }
        //设置唯一朝上的卡
        set {
            for index in cards.indices {
                if index != newValue {
                    cards[index].isFaceUp = false
                } else {
                    cards[index].isFaceUp = true
                }
                
            }
        }
    }
    mutating func choose(_ card: Card) {
        if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}),
           !cards[chosenIndex].isMatched,
           !cards[chosenIndex].isFaceUp
           {
            if let potentialIndex = indexOfTheOneAndOnlyFaceUpCard {
                if cards[chosenIndex].content == cards[potentialIndex].content {
                    cards[chosenIndex].isMatched = true
                    cards[potentialIndex].isMatched = true
                }
                //如果没有朝上的卡,直接把选中的卡isFaceUp,永远有朝上的卡
                cards[chosenIndex].isFaceUp = true
                
            } else {
                indexOfTheOneAndOnlyFaceUpCard = chosenIndex
            }

        }
    }
    
    init(numberOfPairOfCards: Int, createCardContent: (Int) ->CardContent) {
        cards = []
        for pairIndex in 0..<numberOfPairOfCards {
            let content = createCardContent(pairIndex)
            cards.append(Card(content: content, id: pairIndex*2))
            cards.append(Card(content: content, id: pairIndex*2+1))
        }
    }
    
    struct Card:Identifiable {
        var isFaceUp = false
        var isMatched = false
        var content: CardContent
        var id: Int
    }
}
上一篇下一篇

猜你喜欢

热点阅读