Python二十一点游戏

时间:2017-04-20 04:07:38

标签: python

我正在尝试制作一个小的二十一点游戏,并且在一个单独的文件中我称之为playing_cards.py,它包含“deck”,其中包含以下内容。我的问题是,我怎么能做到这一切,而不必有一堆if语句(仍然会导致问题)并仍然产生正确的数字。我还是初学者,所以任何帮助都会很精彩

deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
    'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
    'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
    'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']



import playing_cards


player_hand = []
card = playing_cards.deal_one_card()
player_hand.append(card)
card = playing_cards.deal_one_card()
player_hand.append(card)

dealer_hand = []
card = playing_cards.deal_one_card()
dealer_hand.append(card)
card = playing_cards.deal_one_card()
dealer_hand.append(card)
print(dealer_hand)

if player_hand[0][0] == "A":
    player_hand[0] = 11
    print(player_hand)
elif player_hand[1][0] == "A":
    player_hand[1] = 11
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "2":
    player_hand[0] = 2
    print(player_hand)
elif player_hand[1][0] == "2":
    player_hand[1] = 2
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "3":
    player_hand[0] = 3
    print(player_hand)
elif player_hand[1][0] == "3":
    player_hand[1] = 3
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "4":
    player_hand[0] = 4
    print(player_hand)
elif player_hand[1][0] == "4":
    player_hand[1] = 4
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "5":
    player_hand[0] = 5
    print(player_hand)
elif player_hand[1][0] == "5":
    player_hand[1] = 5
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "6":
    player_hand[0] = 6
    print(player_hand)
elif player_hand[1][0] == "6":
    player_hand[1] = 6
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "7":
    player_hand[0] = 7
    print(player_hand)
elif player_hand[1][0] == "7":
    player_hand[1] = 7
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "8":
    player_hand[0] = 8
    print(player_hand)
elif player_hand[1][0] == "8":
    player_hand[1] = 8
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "9":
    player_hand[0] = 9
    print(player_hand)
elif player_hand[1][0] == "9":
    player_hand[1] = 9
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "T":
    player_hand[0] = 10
    print(player_hand)
elif player_hand[1][0] == "T":
    player_hand[1] = 10
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "J":
    player_hand[0] = 10
    print(player_hand)
elif player_hand[1][0] == "J":
    player_hand[1] = 10
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "Q":
    player_hand[0] = 10
    print(player_hand)
elif player_hand[1][0] == "Q":
    player_hand[1] = 10
    print(player_hand)
else:
    print(player_hand)


if player_hand[0][0] == "K":
    player_hand[0] = 10
    print(player_hand)
elif player_hand[1][0] == "K":
    player_hand[1] = 10
    print(player_hand)
else:
    print(player_hand)

4 个答案:

答案 0 :(得分:0)

我没有教你字典(可能会有不同的课程),而是教你str.index()(< - link)。

您可以使用str.indexstr.find返回一个整数值,该整数值是较大字符串中子字符串开头的索引

通过将卡片排列在一个字符串中来替换所有各种if语句:

ranks = "..23456789TAJQK"

请注意,我已经仔细构建了这个字符串 ,其中包含我想要的字母。字符串的第一个字符有索引0:我在那里放了一个点,因为我不想要零。同样,字符串的第二个字符有索引1:不是我想要的东西。所以我把'' 2'在索引2,索引3处的3,等等。我把' T'在索引10处,' A'在索引11之后,我把所有其他东西都没有特别的顺序。

现在,让我们打印出一些测试数据:

print('2', ranks.index('2'))
print('T', ranks.index('T'))
print('A', ranks.index('A'))
print('Q', ranks.index('Q'))

由此,我明白了:

$ python test.py
2 2
T 10
A 11
Q 13

这意味着除了JQK部分外,我们还在进行中。我认为这已足够接近了:

rank = ranks.index(player_hand[0][0])

if rank > 11:    # J,Q,K
    rank = 10

player_hand[0] = rank

rank = ranks.index(player_hand[1][0])

if rank > 11:    # J,Q,K
    rank = 10

player_hand[1] = rank

答案 1 :(得分:0)

首先,我会将您对套牌的定义更改为以下内容,而不是更紧凑,但更容易检查没有错过任何卡片或套装(并且可能更容易修改):

suits = ['H', 'D', 'S', 'C']
cards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
deck = [card + suit for suit in suits for card in cards]

我并不完全清楚你接下来想要做什么,但似乎你使用deal_one_card()方法处理了一张你没有包含的卡片,然后尝试获取卡片的价值( 10为“10”或一张图片卡,数字卡的编号为1,ace为11。我认为最简单的方法可能是定义点词典:

points_dictionary = {'A': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'T': 10, 'J': 10, 'Q': 10, 'K': 10}

然后您可以查找特定卡片的点数,例如:

import random
a_card = random.choice(deck)
print(a_card)
print(points_dictionary[a_card[0]])

虽然显然你自己的deal_one_card()方法需要跟踪卡已经发出,因此不再包含在内。

答案 2 :(得分:0)

使用dictioanry,您可以在其中映射A,K,Q,J,T和数字的值。像这样:

CARD_VALUES = {
    "A" : 11,
    "K" : 10,
    "Q" : 10,
    "J" : 10,
    "T" : 10
}

你可以得到这样的映射,

card = int(CARD_VALUES.get(player_hand[0][0], player_hand[0][0]))

答案 3 :(得分:0)

我会用这样的东西:

suits = [ 'C', 'D', 'H', 'S' ]
cards = [ 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' ]
rank_values = { "A": ( 1, 11 ), "2": ( 2, ), "3": ( 3, ), "4": ( 4, ), "5": ( 5, ), "6": ( 6, ), "7": ( 7, ), "8": ( 8, ), "9": ( 9, ), "J": ( 10, ), "Q": 10, "K": 10 }

class Card( object ):
   def __init__( self, card_value ):
      self._card_value = card_value.upper()

   @property
   def suit( self ):
      return self._card_value[ 0 ]

   @property
   def rank( self ):
      return self._card_value[ 1 ]

   @property
   def values( self ):
      return sorted( rank_values[ self.rank ], reverse = True )

def get_deck():
   deck_values = [ card + suit for suit in suits for card in cards ]
   deck = [ Card( value ) for value in deck_values ]
   return deck

这使您可以调用get_deck()来检索卡片组,每张卡片都有values属性(以c.values访问,假设卡片为c),返回全部卡可以容纳的价值。这允许你也处理一个Ace是1分而不是11分的情况,因为它会破坏。