首页 > 解决方案 > 扑克游戏用不同的牌交换牌

问题描述

好的,在将牌发给两名玩家之后,我将进入下一步。

我需要该程序能够获取玩家想要摆脱的所需卡片并将它们交换为新的随机卡片。玩家将被询问想要交换多少张牌和哪些牌。代码应该类似于玩家为一张一次性卡片输入“1”,然后玩家可以选择移除哪张卡片。因此,该卡将从手牌或代码列表中移除并替换为 1 张新卡。这只发生一次,然后它应该打印两个玩家的手。

在我看到的每一个地方,它都以更复杂的方式完成,我知道这是简单的编码,但我真的很讨厌最简单的事情。

到目前为止我得到了什么:

def poker():

import random

(raw_input('Welcome to a classic game of Poker! You will recieve 5 cards. You will have the option to exchange 1 to 3 cards from your hand for new cards of the same amount you exchanged. IF you have an Ace in your beginning hand, you may exchange that Ace for up to four new cards (three other cards including the ace). ~Press Enter~')) 
(raw_input('S = Spades ,  H = Hearts ,  C = Clubs ,  D = Diamonds ~Press Enter~'))
deck = ['2S','2H','2C','2D','3S','3H','3C','3D','4S','4H','4C','4D','5S','5H','5C','5D','6S','6H','6C','6D','7S','7H','7C','7D','8S','8H','8C','8D','9S','9H','9C','9D','10S','10H','10C','10D','Jack(S)','Jack(H)','Jack(C)','Jack(D)','Queen(S)','Queen(H)','Queen(C)','Queen(D)','King(S)','King(H)','King(C)','King(D)', 'Ace(S)','Ace(H)','Ace(C)','Ace(D)']
new_cards = ''
player1 = []
player2 = []
random.shuffle(deck)

for i in range(5): player1.append(deck.pop(0)) and player2.append(deck.pop(0))

print player1

int(input('How many cards would you like to exchange? 1, 2, 3, or 4 IF you have an Ace.'))


#ignore this for now
int(input('Which card would you like to exchange? 1, 2, 3, 4, or 5? Note: The first card in your hand (or list in this case) is the number 1 spot. So if you want to exchange the first card, input 1. The same is for the other cards.')) 

最初交换的牌在交换后也无法从套牌列表中访问。所以就像... ['8D','2S','Queen(H),'8S','Jack(H)'] 如果我想移除 1 张牌,我选择移除 '2S', '2S'将不再在我手中,并将与牌组中的另一张牌交换出来。'2S'也不会以任何理由回到我的手中,因为它不能再次从列表中取出。所以输出应该是所有相同的卡,除了'2S'将丢失并且新卡将在它的位置。

标准是一次最多移除 3 张牌,但如果您的起始手牌中有 A,您也可以最多移除 4 张牌。但是你应该被拒绝,然后再一次问你如果你不提供一张A,你想摆脱多少张牌。

标签: python

解决方案


可行的方法如下:

n_cards_to_exchange = int(input('How many cards would you like to exchange? 1, 2, 3, or 4 IF you have an Ace.'))

for i in range(n_cards_to_exchange):
    print(player1)
    card_text = ', '.join([str(j) for j in range(1,5-i)]) + f', or {5-i}?' 
    card_id = int(input(f'Which card would you like to exchange? {card_text} Note: The first card in your hand (or list in this case) is the number 1 spot. So if you want to exchange the first card, input 1. The same is for the other cards.')) - 1
    deck.append(player1.pop(card_id))
random.shuffle(deck)
for i in range(n_cards_to_exchange):
    player1.append(deck.pop(0))

这个想法是玩家选择他想要丢弃的卡片数量,然后选择他想要多次丢弃的卡片。然后他从牌堆中抽回牌。如果您需要任何澄清,请随时询问。


推荐阅读