swift缩短代码(函数类)

时间:2017-08-22 23:51:32

标签: swift

我正在研究我的第一个自制应用程序(称为“Busdriver”) 并有一些问题。

致我的人:我从2016年11月开始使用xCode和swift,并制作了我的第一个Cardplay应用程序。该应用程序工作正常,但我确信有很多可能性来简化我的代码。

首先: 有没有办法删除另一个数组中的整个数组? 例如:

let cardValues = [["D2",2,"black],["D3",3,"black]["D4",4,"red]]
// i want to get this result:
// [["D2",2,"black],["D3",3,"black]]

另一个问题是,有没有办法总结以下几行代码:

var playingCards = [["♦2", 2, "red"], ["♦3", 3, "red"], ["♦4", 4, "red"], ["♦5", 5, "red"], ["♦6", 6, "red"], ["♦7", 7, "red"], ["♦8", 8, "red"], ["♦9", 9, "red"], ["♦10", 10, "red"], ["♦B", 11, "red"], ["♦D", 12, "red"], ["♦K", 13, "red"], ["♦A", 20, "red"], ["♥2", 2, "red"], ["♥3", 3, "red"], ["♥4", 4, "red"], ["♥5", 5, "red"], ["♥6", 6, "red"], ["♥7", 7, "red"], ["♥8", 8, "red"], ["♥9", 9, "red"], ["♥10", 10, "red"], ["♥B", 11, "red"], ["♥D", 12, "red"], ["♥K", 13, "red"], ["♥A", 20, "red"], ["♣2", 2, "black"], ["♣3", 3, "black"], ["♣4", 4, "black"], ["♣5", 5, "black"]]

var cardDeckPlayer1 = []
var cardDeckPlayer2 = []
var cardDeckPlayer3 = []
var cardDeckPlayer4 = []
var cardDeckPlayer5 = []
var cardDeckPlayer6 = []

let numberOfPlayers = 4 // this value varies from 2 to 6

if numberOfPlayers == 2
{
    cardDeckPlayer1.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    cardDeckPlayer2.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(SplayingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(SpielKarten[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)
}
if numberOfPlayers == 3
{
    cardDeckPlayer1.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    cardDeckPlayer2.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    cardDeckPlayer3.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer3.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer3.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer3.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)
}
if numberOfPlayers == 4
{
    cardDeckPlayer1.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer1.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    cardDeckPlayer2.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer2.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    cardDeckPlayer3.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer3.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer3.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer3.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    cardDeckPlayer4.insert(playingCards[0], at: 0)
    playingCards.remove(at: 0)
    cardDeckPlayer4.insert(playingCards[0], at: 1)
    playingCards.remove(at: 0)
    cardDeckPlayer4.insert(playingCards[0], at: 2)
    playingCards.remove(at: 0)
    cardDeckPlayer4.insert(playingCards[0], at: 3)
    playingCards.remove(at: 0)

    // etc. until if numberOfPlayers == 6...

所以我想要一个简化上面代码的函数,类或其他东西,取决于numberOfPlayers值,将创建2或3或4或5或6个carddecks,并且这个cardDecks分别存在前4个数组在playCards数组中。

所以来自数组playingCards = [[1,2,3],[4,5,6],[7,8,9],[10,11,12], [13,14,15],[16,17,18],[19,20,21],[21,22,23], [24,25,26],[27,28,29],[30,31,32],[33,34,35],[36,37,38],[39,40,41],[42,43,44],[45,46,47]]

结果应该是这样的:

numberOfPlayers = 3

cardDeckPlayer1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
cardDeckPlayer2 = [[13,14,15],[16,17,18],[19,20,21],[21,22,23]]
cardDeckPlayer3 = [[24,25,26],[27,28,29],[30,31,32],[33,34,35]]
// etc. 

我不是真正进入课程和结构,并且仍然需要在这个领域学到很多东西,但到目前为止,阅读书籍并没有帮助我。因此,如果有人可以帮助我更清楚地构建我的代码,那就太棒了。

感谢您的帮助,对不起英语不好, cyclopath

1 个答案:

答案 0 :(得分:1)

我强烈建议您开发一个合适的数据模型层,而不是将内容存储在数组和子数组中。这是模式化的一种方式:

struct Card: CustomStringConvertible {
    var rank: Rank
    var suit: Suit

    var color: Color {
        switch self.suit {
        case .spade, .club:    return .black
        case .heart, .diamond: return .red
        }
    }

    var description: String {
        return "\(suit.rawValue)\(rank.description) \(color.rawValue)"
    }

    enum Rank: Int, CustomStringConvertible {
        case two = 2, three, four, five, six, seven, eight, nine, ten
        case jack, queen, king, ace

        var description: String {
            switch self {
            case .two, .three, .four, .five, .six, .seven, .eight, .nine, .ten:
                return "\(self.rawValue)"
            case .jack:
                return "J"
            case .queen:
                return "Q"
            case .king:
                return "K"
            case .ace:
                return "A"
            }
        }
    }

    enum Suit: String {
        case spade = "♠︎"
        case heart = "♥"
        case club = "♣"
        case diamond = "♦︎"
    }

    enum Color: String {
        case red = "red"
        case black = "black"
    }
}

struct Player {
    var deck = [Card]()

    static func byDistributing(cards: [Card], toNumberOfPlayers n: Int) -> [Player] {
        guard 2 <= n && n <= 6 else {
            fatalError("numberOfPlayer must be between 2 and 6")
        }
        guard cards.count % n == 0 else {
            fatalError("cards.count must be divisible by numberOfPlayers") // so every one got an equal-length deck
        }

        let numberOfCardsPerPlayer = cards.count / n
        let players = stride(from: 0, to: cards.count, by: numberOfCardsPerPlayer).map { i -> Player in
            let deck = Array(cards[i..<(i + numberOfCardsPerPlayer)])
            return Player(deck: deck)
        }

        return players
    }
}

它使用了很多enum,所以你不能拥有卡1或小丑或类似的东西。现在,我不是一名纸牌选手,所以你必须原谅我对纸牌组成的无知。以下是你如何构建卡片组:

// A standard 52-card deck in America
var playingCards = [Card]()
for suit in [Card.Suit.diamond, .heart, .club, .spade] {
    for value in 2...14 {
        let rank = Card.Rank(rawValue: value)!
        playingCards.append(Card(rank: rank, suit: suit))
    }
}

// Now distribute it among 4 players
let players = Player.byDistributing(cards: playingCards, toNumberOfPlayers: 4)
print(players[0].deck)   // all diamonds
print(players[1].deck)   // all hearts
print(players[2].deck)   // all clubs
print(players[3].deck)   // all spades

(你的下一个问题可能就是如何改变一副扑克牌,所以我们如何将这些牌分发给byDistributing中的玩家并不重要。无论如何,它们应该是随机的!)< / p>