BlackJack游戏主剧本麻烦(课程已经完成)

时间:2015-02-13 19:35:17

标签: python object object-oriented-analysis blackjack object-oriented-database

对于我的课程项目,我将制作一个功能正常的BlackJack游戏。这是游戏的失水版本(没有投注,加倍,分裂等等)。我们将使用不同的类来进行Deck,Hand和Card功能。我遇到的主要问题是如何将它们整合在一个有凝聚力的程序中,以便它可以运行。这是我的课程

class Card(object):

'''A simple playing card. A Card is characterized by two 
components:
rank: an integer value in the range 2-14, inclusive (Two-Ace)
suit: a character in 'cdhs' for clubs, diamonds, hearts, and
spades.'''

#------------------------------------------------------------

SUITS = 'cdhs'
SUIT_NAMES = ['Clubs', 'Diamonds', 'Hearts', 'Spades']

RANKS = range(2,15)
RANK_NAMES = ['Two', 'Three', 'Four', 'Five', 'Six',
              'Seven', 'Eight', 'Nine', 'Ten', 
              'Jack', 'Queen', 'King', 'Ace']
RANK_VALUES = [99, 99, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
#------------------------------------------------------------

def __init__(self, rank, suit):

    '''Constructor
    pre: rank in range(1,14) and suit in 'cdhs'
    post: self has the given rank and suit'''

    self.rank_num = rank
    self.suit_char = suit


#------------------------------------------------------------

def getSuit(self):

    '''Card suit
    post: Returns the suit of self as a single character'''

    return self.suit_char

#------------------------------------------------------------

def getRank(self):

    '''Card rank
    post: Returns the rank of self as an int'''

    return self.rank_num

#------------------------------------------------------------

def getCardValue(self):
    value = self.RANK_VALUES[self.rank_num]
    return value

#------------------------------------------------------------
def suitName(self):

    '''Card suit name
    post: Returns one of ('clubs', 'diamonds', 'hearts',
          'spades') corrresponding to self's suit.'''

    index = self.SUITS.index(self.suit_char)
    return self.SUIT_NAMES[index]        

#------------------------------------------------------------

def rankName(self):

    '''Card rank name
    post: Returns one of ('ace', 'two', 'three', ..., 'king')
          corresponding to self's rank.'''

    index = self.RANKS.index(self.rank_num)
    return self.RANK_NAMES[index]

#------------------------------------------------------------

def __str__(self):

    '''String representation
    post: Returns string representing self, e.g. 'Ace of Spades' '''

    return self.rankName() + ' of ' + self.suitName()

#------------------------------------------------------------
from random import randrange
from Card import Card
from Hand import Hand


class Deck(object):
def __init__(self):
    '''This creates the deck of cards, un-shuffled.'''
    cards = []
    for suit in Card.SUITS:
        for rank in Card.RANKS:
            cards.append(Card(rank,suit))
    self.cards = cards

def size(self):
    '''How many cards are left.'''
    return len(self.cards)

def deal(self):
    '''Deals a single card.
    Pre: self.size() > 0
    Post: Returns the next card in self, and removes it from self.'''
    return self.cards.pop()

def shuffle(self):
    '''This shuffles the deck so that the cards are in random order.'''
    n = self.size()
    cards = self.cards
    for i,card in enumerate(cards):
        pos = randrange(i,n)
        cards[i] = cards[pos]
        cards[pos] = card

def takeAHit(self, whatHand):
    aCard = self.deal()
    whatHand.addCard(aCard)
def __str__(self):
    if self.size() == 52:
        return 'The Deck is Full'
    elif self.size() > 0:
        return 'The Deck has been Used'
    else:
        return 'The Deck is Empty'


from Card import Card
class Hand(object):

"""A labeled collection of cards that can be sorted"""

#------------------------------------------------------------

def __init__(self, label=""):

    """Create an empty collection with the given label."""

    self.label = label
    self.cards = []

#------------------------------------------------------------

def add(self, card):

    """ Add card to the hand """

    self.cards.append(card)

#------------------------------------------------------------

def totalHand(self):
    totalHand = 0
    aceAmount = 0

    for c in self.cards:
        if c.getRank() == 14:
            aceAmount += 1
            totalHand +=c.getCardValue()
    while aceAmount > 0:
        if totalHand > 21:
            aceAmount -= 1
            totalHand -= 10
        else:
            break
        return totalHand

def __str__(self):
    if self.cards == []:
        return "".join([(self.label), "doesn't have any cards."])
    tempStringList = [ self. label, "'s Cards,  "]
    for c in self.cards:
        tempStringList.append(str(c))
        tempStringList.append(" , ")

        tempStringList.pop()
        tempStringList.append(" . ")

        return "".join(tempStringList)

现在我需要帮助的主要功能,我的教授只帮助了我一点点。

from Deck import Deck
from Card import Card
from Hand import Hand

def rules(playerTotal, dealerTotal):
if playerTotal > 21:
    print "You busted!"
    if dealerTotal == 21:
        print 'To make it worse, dealer has 21.'
elif dealerTotal > 21:
    print "The Dealer has busted. You win!"
elif playerTotal == 21:
    print " You got 21! So you win!"
    if dealerTotal == 21:
        print "The Dealer also got 21. Tough Break."
elif dealerTotal == 21:
    print "The Dealer got 21! Tough Break, you lose!"
else:
    if playerTotal > dealerTotal:
        print "You beat the Dealer! You got lucky punk."
    if playerTotal == dealerTotal:
        print "It is a push, no one wins!"
    else:
        print "Dealer wins! Better luck next time loser."

所以我知道我必须使用Hand的totalHand函数来确定playerTotal和dealerTotal的值是什么,但每次我尝试的东西都会出现这个错误。     Player1 = raw_input(“你叫什么名字?”)     经销商=“经销商”     twoCards =手(Player1)     print twoCards.totalHand()

打印出“无”,就是这样..

如果有人可以帮助我,我会很感激,我现在​​有点遗憾我的主要选择..

1 个答案:

答案 0 :(得分:0)

没有特别的顺序:

  • 你的洗牌有偏见。只需使用Python的random.shuffle(),它就是正确而快速的。

  • totalHand是错误的(永远不会在值上添加非a),并且太复杂了,因为你选择使用11作为aces的值,并且不报告总数是硬还是软(你需要适当的游戏)。将aces的值更改为1,并简化:

*

def totalHand(self):
    aceFound, isSoft = false, false
    total = 0

    for c in self.cards:
        total += c.getCardValue()
        if c.getCardValue() == 1:
            aceFound = true

    if aceFound and total < 12:
        total += 10
        isSoft = true

    return total, isSoft
  • 您无法将结果仅基于卡片总数。你必须按正确的顺序玩牌:

    1. 如果经销商很自然,游戏就结束了。自然推动的球员, 所有其他人都输了。
    2. 每个玩家轮流玩牌。自然是有偿的 移除,胸围失去并被移除。
    3. 经销商拉着他的手。如果他破产,剩下的所有球员都会获胜, 否则球员将根据总数输赢。