我正在尝试模拟扑克游戏,我需要将卡分发给“n”个玩家一次玩。我已经编写了一段简短的代码,用于查看现有的牌组并向其他玩家发放牌,但这是非常难以编码的。我想知道是否有一种方法可以在没有硬编码的情况下进行这种活动,使用“循环可以定义玩家的数量,代码可以查看修改后的套牌并为其他玩家提供新卡。这是我写的所以远:
Player_1 <- cards[sample(nrow(cards), 2), ]
Player_1
Remaining_Deck <- sqldf('SELECT * FROM cards EXCEPT SELECT * FROM Player_1') # subset from t1 not in t2
Player_2 <- cards[sample(nrow(Remaining_Deck), 2), ]
Player_2
Remaining_Deck2 <- sqldf('SELECT * FROM Remaining_Deck EXCEPT SELECT * FROM Player_2') # subset from t1 not in t2
Player_3 <- cards[sample(nrow(Remaining_Deck2), 2), ]
Player_3
Remaining_Deck3 <- sqldf('SELECT * FROM Remaining_Deck2 EXCEPT SELECT * FROM Player_3') # subset from t1 not in t2
Player_1
Player_2
Player_3
答案 0 :(得分:0)
为什么每次发牌都要重新洗牌?相反,洗牌一次,然后跟踪卡的索引来处理。你可以,如果你真的想要,删除卡,但这是不必要的。
首先,生成套牌(这更容易作为向量,除非您有其他信息附加到卡片上):
cards <-
as.character(outer(
c(2:10, "J", "Q", "K", "A")
, c("S", "H", "D", "C")
, paste
, sep = "_"
))
然后,为这只手洗牌:
deckThisHand <- sample(cards)
设置索引以处理顶部卡片:
currCardToDeal <- 1
然后,开始交易。注意,我包含了一个变量来存储要处理的数字,尽管这不是绝对必要的。如果您从hold-em更改为五张牌抽奖,那么就更容易了。
nToDeal <- 2
player1 <- deckThisHand[currCardToDeal:(currCardToDeal+nToDeal-1)]
currCardToDeal <- currCardToDeal+nToDeal-1
然后,你可以迭代玩家:
player2 <- deckThisHand[currCardToDeal:(currCardToDeal+nToDeal-1)]
currCardToDeal <- currCardToDeal+nToDeal-1
player3 <- deckThisHand[currCardToDeal:(currCardToDeal+nToDeal-1)]
currCardToDeal <- currCardToDeal+nToDeal-1