我一直在尝试为 IRC 制作一个扑克游戏机器人,但我似乎无法处理这些牌。
我知道这个算法效率很低,但这是我使用我目前的 Python 技能所能想到的最好的算法。欢迎任何改进!
玩家是一个字典,其中键是玩家的昵称,值是他们拥有的金额。
当代码运行时,如果只有 1 名玩家,它应该给出 5 张牌。但如果有 2 名玩家,则每人会生成 4 到 6 张牌。我还没有测试更多的玩家。
一些预先初始化的变量:
numberOfPlayers = 0 #The numerical value of the amount of players in the game
players = {} #The nickname of each player and the amount of money they have
bets = {} #How much each player has bet
decks = 1 #The number of decks in play
suits = ["C","D","H","S"] #All the possible suits (Clubs, Diamonds, Hearts, Spades)
ranks = ["2","3","4","5","4","6","7","8","9","J","Q","K","A"] #All the possible ranks (Excluding jokers)
cardsGiven = {} #The cards that have been dealt from the deck, and the amount of times they have been given. If one deck is in play, the max is 1, if two decks are in play, the max is 2 and so on...
hands = {} #Each players cards
编码:
def deal(channel, msgnick):
try:
s.send("PRIVMSG " + channel + " :Dealing...\n")
for k, v in players.iteritems():
for c in range(0, 5):
suit = random.randrange(1, 4)
rank = random.randrange(0,12)
suit = suits[suit]
rank = ranks[rank]
card = rank + suit
print(card)
if card in cardsGiven:
if cardsGiven[card] < decks:
if c == 5:
hands[k] = hands[k] + card
cardsGiven[card] += 1
else:
hands[k] = hands[k] + card + ", "
cardsGiven[card] += 1
else:
c -= 1
else:
if c == 5:
hands[k] = hands[k] + card
cardsGiven[card] = 1
else:
hands[k] = hands[k] + card + ", "
cardsGiven[card] = 1
s.send("NOTICE " + k + " :Your hand: " + hands[k] + "\n")
except Exception:
excHandler(s, channel)
如果需要任何示例或进一步解释,请询问:)