我正在制作纸牌游戏,我首先将所有卡片放入一组编号为1-52的元组中。但是通过使用随机数生成器来挑选卡片,很难从卡片中取出卡片,因为我无法从随机数生成器中专门删除该数字。任何帮助表示赞赏。
func createDeck(){
deck.append((name: "ace1", number: 1))
deck.append((name: "ace2", number: 2))
deck.append((name: "ace3", number: 3))
deck.append((name: "ace4", number: 4))
deck.append((name: "two1", number: 5))
deck.append((name: "two2", number: 6))
deck.append((name: "two3", number: 7))
deck.append((name: "two4", number: 8))
deck.append((name: "three1", number: 9))
deck.append((name: "three2", number: 10))
deck.append((name: "three3", number: 11))
deck.append((name: "three4", number: 12))
deck.append((name: "four1", number: 13))
deck.append((name: "four2", number: 14))
deck.append((name: "four3", number: 15))
deck.append((name: "four4", number: 16))
deck.append((name: "five1", number: 17))
deck.append((name: "five2", number: 18))
deck.append((name: "five3", number: 19))
deck.append((name: "five4", number: 20))
deck.append((name: "six1", number: 21))
deck.append((name: "six2", number: 22))
deck.append((name: "six3", number: 23))
deck.append((name: "six4", number: 24))
deck.append((name: "seven1", number: 25))
deck.append((name: "seven2", number: 26))
deck.append((name: "seven3", number: 27))
deck.append((name: "seven4", number: 28))
deck.append((name: "eight1", number: 29))
deck.append((name: "eight2", number: 30))
deck.append((name: "eight3", number: 31))
deck.append((name: "eight4", number: 32))
deck.append((name: "nine1", number: 33))
deck.append((name: "nine2", number: 34))
deck.append((name: "nine3", number: 35))
deck.append((name: "nine4", number: 36))
deck.append((name: "ten1", number: 37))
deck.append((name: "ten2", number: 38))
deck.append((name: "ten3", number: 39))
deck.append((name: "ten4", number: 40))
deck.append((name: "jack1", number: 41))
deck.append((name: "jack2", number: 42))
deck.append((name: "jack3", number: 43))
deck.append((name: "jack4", number: 44))
deck.append((name: "queen1", number: 45))
deck.append((name: "queen2", number: 46))
deck.append((name: "queen3", number: 47))
deck.append((name: "queen4", number: 48))
deck.append((name: "king1", number: 49))
deck.append((name: "king2", number: 50))
deck.append((name: "king3", number: 51))
deck.append((name: "king4", number: 52))
}
func randomNumber(){
random = arc4random_uniform(52) + 0;
print(random)
}
func deal(){
var aces = Set<Int>(arrayLiteral:1, 2, 3,4)
var twos = Set<Int>(arrayLiteral:5, 6, 7,8)
var threes = Set<Int>(arrayLiteral:9, 10, 11,12)
var fours = Set<Int>(arrayLiteral:13, 14, 15,16)
var fives = Set<Int>(arrayLiteral:17, 18,19,20)
var sixes = Set<Int>(arrayLiteral:21, 22, 23,24)
var sevens = Set<Int>(arrayLiteral:25, 26, 27,28)
var eights = Set<Int>(arrayLiteral:29, 30, 31,32)
var nines = Set<Int>(arrayLiteral:33, 34, 35,36)
var tens = Set<Int>(arrayLiteral:37, 38, 39,40)
var jacks = Set<Int>(arrayLiteral:41, 42, 43,44)
var queens = Set<Int>(arrayLiteral:45, 46, 47,48)
var kings = Set<Int>(arrayLiteral:49, 50, 51,52)
if aces.contains(Int(random)){
playern1 = Int(random)
aces.removeFirst()
}
if twos.contains(Int(random)){
twos.removeFirst()
...
答案 0 :(得分:0)
维护一系列仍然可用且尚未挑选的卡片。出于讨论的目的,我将其称为“交易数组”(这必须单独维护到卡座阵列!它可以作为卡组阵列的可变副本开始。)。< / p>
当您随机挑选/交易卡片时,将其从交易数组中删除。
您的随机数生成器应该选择一个介于零和交易数组中剩余项目数之间的数字 - 不在0到52之间。
(假设交易数组不为空 - 在从交易数组中随机选择一张卡之前检查一下。)
从交易阵列中选择一张卡时,请使用随机数作为阵列的索引(而不是您创建的卡号,这对于从中挑选卡片没有任何实际用途交易数组随机)。
(请注意,我在这里没有包含任何示例代码,因为我不清楚你的代码的用途是什么。如果我试图在这里发布代码,我可能会错误地解释你的代码因此发布错误的代码。)
答案 1 :(得分:0)
您可能需要重新设计卡片的定义方式:
enum Suit: String {
case heart, diamond, club, spade
}
enum Rank: Int {
case _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6, _7 = 7, _8 = 8, _9 = 9, _10 = 10
case jack = 11, queen = 12, king = 13
}
struct Card {
let suit: Suit
let rank: Rank
}
(或替代方案可以是enum Card {case card(suit: Suit, rank: Rank)}
)
然后定义你的套牌:
var deck = [Card(suit: .heart, rank: ._1),
Card(suit: .heart, rank: ._2),
Card(suit: .heart, rank: ._3),
Card(suit: .heart, rank: ._4),
Card(suit: .heart, rank: ._5),
Card(suit: .heart, rank: ._6),
// ...
]
然后从牌组中挑选一张牌:
func pickRandomCardFromDeck() -> Card {
return deck.remove(at: Int(arc4random_uniform(UInt32(deck.count))))
}
[编辑评论]
要检查一对:
extension Card {
func isPair(with card: Card) -> Bool {
return rank == card.rank
}
}