如何使卡片在python中工作

2024-06-07 18:05:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我目前为止的代码:

import sys
import os
import random
Question():
    os.system('cls')
    SQ=input('Do you want to play blackjack y/n')
    if(SQ == y or SQ == Y):
        StartGame()
    if(SQ == n or SQ == N):
        sys.exit()
    if(SQ != n and SQ != N and SQ != y and SQ != Y):
        print('You did answer the question with a y or a n which correspond to yes and no accordingly')
        Question()
Question()

StartGame():
    slot1=False
    slot2=False
    slot3=False
    slot4=False
    slot5=False
    slot6=False
    slot7=False
    slot8=False
    slot9=False
    slot10=False
    slot11=False
    slot12=False
    slot13=False
    slot14=False
    slot15=False
    slot16=False
    slot17=False
    slot18=False
    slot19=False
    slot20=False
    slot21=False
    slot22=False
    slot22=False
    slot23=False
    Slot24=False
    slot25=False
    slot26=False
    slot27=False
    Slot28=False
    slot29=False
    slot30=False
    slot31=False
    slot32=False
    slot33=False
    slot34=False
    slot35=False
    slot36=False
    slot37=False
    slot38=False
    slot39=False
    slot40=False
    slot41=False
    slot42=False
    slot43=False
    slot44=False
    slot45=False
    slot46=False
    slot47=False
    slot48=False
    slot49=False
    slot50=False
    slot51=False
    slot52=False

    aceHEART = randrange(1, 52)
    aceHEART

我不明白正确的方法使插槽和随机数发生器一起进行随机洗牌。我如何才能使它不尝试在一个插槽中放置多张卡。我也不知道如何更有效地管理这些卡片。我正在用python制作一个21点游戏,但我不知道正确的方法。请尽你所能帮助我。在


Tags: orandto方法importfalseifos
3条回答

不知道你想做什么,但这里有一种方法来生成一副洗牌牌:

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suite = ['clubs', 'hearts', 'spades', 'diamonds']

deck = [r + ' ' + s for r in ranks for s in suite]

random.shuffle(deck)

或使用对象:

^{pr2}$

学会爱列表,用数字来代表卡片,而不是字符串。下面是一个简单的card类,应该可以很好地工作:

class Card(object):
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return "23456789TJQKA"[self.rank] + "cdhs"[self.suit]

然后创建牌组和手牌,它们只是卡片的列表:

^{pr2}$

例如,为什么要将列表用于手牌和牌组,是因为处理一张牌很简单:

hand.append(deck.pop())

为什么你想用数字而不是字符串来表示卡片等级,是为了使它们能够相加和比较。你可以在Mihai上面的代码中添加一个“value”成员,这会有所帮助。对于我的,你只需要稍微调整一下数字:

def bjTotal(hand):
    total = 0
    hasAce, isSoft = False, False

    for card in hand:
        if card.rank == 12:
            hasAce = True
            total += 1
        elif card.rank > 7:
            total + 10
        else:
            total += card.rank + 2

    if hasAce and total < 12:
        isSoft = True
        total += 10

    return total, isSoft

班级在表现牌和游戏方面做得很好

import random
class Card:
     def __init__(self,rank,suit):
        self.rank = rank
        self.suit = suit
     def rankName(self):
        return "A23456789TJQK"[self.rank]
     def suitName(self):
        return "HCDS"[self.suit]
     def __int__(self):
        if self.rank > 8: return 10
        if self.rank == 0:return 11
        return self.rank + 1
     def __eq__(self,other):
        try:
            return self.rank == other.rank
        except:
            return self.rank == other
     def __str__(self):
        return self.rankName() +self.suitName()
     def __repr__(self):
        return "<Card: %s>"%self

class Deck:
    def __init__(self,cards=None):
        if cards is None:
           cards = [Card(rank,suit) for rank in range(13) for suit in range(4)]
        self.cards = cards
        random.shuffle(self.cards)
    def draw(self):
        return self.cards.pop(0)
    def __add__(self,other):
        return Deck(self.cards+other.cards)


class Hand:
    def __init__(self):
        self.cards = []
    def __int__(self):
        total = sum(map(int,self.cards))
        aces = self.cards.count(0)
        while aces > 0 and total > 21:
            aces -= 1
            total -= 10
        return total
    def put(self,card):
        self.cards.append(card)
    def __str__(self):
        return ", ".join(map(str,self.cards))
    def __repr__(self):
        return "<Hand %s>"%self.cards

一旦你有了你的课程,你现在可以开始构建你的游戏

^{pr2}$

(无论如何都是这样)

相关问题 更多 >

    热门问题