gpt4 book ai didi

快速缩短代码(功能类)

转载 作者:可可西里 更新时间:2023-11-01 02:01:38 25 4
gpt4 key购买 nike

我正在开发我的第一个自制应用程序(称为:“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 分别存在于前 4 个数组中在 playingCards 数组中。

所以来自数组 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.

我#m 不是很喜欢类(class)和结构,仍然需要在这个领域学习很多东西,但到目前为止,阅读书籍对我没有帮助。因此,如果有人可以帮助我使我的代码结构更清晰,那就太好了。

感谢您的帮助,抱歉英语不好,自行车道

最佳答案

我强烈建议你开发一个合适的数据模型层,而不是将东西存储在数组和子数组中。这是对其进行模式化的一种方法:

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 或 clown 或类似的东西。现在,我不是纸牌玩家,所以您必须原谅我对纸牌组构成的无知。以下是构建卡片组的方法:

// 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 中将这些牌分发给玩家并不重要。无论如何它们应该是随机的!)

关于快速缩短代码(功能类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com