当前位置: 首页>>代码示例>>Python>>正文


Python Deck.dealCard方法代码示例

本文整理汇总了Python中Deck.dealCard方法的典型用法代码示例。如果您正苦于以下问题:Python Deck.dealCard方法的具体用法?Python Deck.dealCard怎么用?Python Deck.dealCard使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Deck的用法示例。


在下文中一共展示了Deck.dealCard方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import Deck [as 别名]
# 或者: from Deck import dealCard [as 别名]
def main():
    '''Main program for the blackjack game'''

    #Creates the GUI and the deck object
    setupGUI()
    deck = Deck()

    #List of the card objects
    listUser = []
    listDealer = []

    #List the the card values
    userTotal = []
    dealerTotal = []

    counter = 0

    pt = win.getMouse()

    while not quitt.clicked(pt):

        if start.clicked(pt):

            #Shuffles the deck
            deck.shuffle()

            #Gets the users first 2 cards and draws them to the window
            uCard1 = deck.dealCard()
            uCard1.draw(win,userCard1.getAnchor())

            uCard2 = deck.dealCard()
            uCard2.draw(win,userCard2.getAnchor())

            #Adds the 2 cards to the users list of cards
            listUser.append(uCard1)
            listUser.append(uCard2)

            #Gets the dealers 2 cards and draws one of them to the window
            dCard1 = deck.dealCard()
            
            dCard2 = deck.dealCard()
            dCard2.draw(win,dealerCard2.getAnchor())

            #Adds the 2 cards to the dealers list of cards
            listDealer.append(dCard1)
            listDealer.append(dCard2)

            #Gets the values of the users cards and appends them to the list
            num = addTotal(uCard1)
            userTotal.append(num)

            num = addTotal(uCard2)
            userTotal.append(num)

            message.setText("Press Hit Me or Stand")

            start.deactivate()
            stand.activate()
            hitMe.activate()

            #Checks the users total to see if they have 21
            userTotal = winLose(userTotal)

        if hitMe.clicked(pt):

            #Deals a card to the user and adds it to the list
            listUser.append(deck.dealCard())

            #Draws the new card to the window
            x = userCard2.getAnchor().getX()
            y = userCard2.getAnchor().getY()
            listUser[counter+2].draw(win,Point(x+20*(counter+1),y))

            #Adds the value of the card to the list and checks to see if the
            #user has got 21 or over 21
            num = addTotal(listUser[counter+2])
            userTotal.append(num)

            userTotal = winLose(userTotal)
            
            counter = counter + 1

        if stand.clicked(pt):
            
            stand.deactivate()
            hitMe.deactivate()
            playAgain.activate()

            #Reveals the dealers cards
            dCard2.undraw()
            dCard1.draw(win,dealerCard1.getAnchor())
            dCard2.draw(win,dealerCard2.getAnchor())

            #Gets the value of the dealers cards and adds them to the list
            num2 = addTotal(dCard1)
            dealerTotal.append(num2)

            num2 = addTotal(dCard2)
            dealerTotal.append(num2)
            
#.........这里部分代码省略.........
开发者ID:Frank-K,项目名称:Blackjack_Game,代码行数:103,代码来源:Blackjack.py

示例2: __init__

# 需要导入模块: import Deck [as 别名]
# 或者: from Deck import dealCard [as 别名]
class Blackjack: #create new class

    def __init__(self,dHand=[],pHand=[]):
        self.dHand=dHand
        self.pHand=pHand
        self.deck=Deck() #create new deck
        self.deck.shuffle() #shuffle deck

    def makeCard(self,card,xpos,ypos): #additonal method that pairs the card dealt with the picture of that card
        card=card
        card=card.split()
        rank=card[0]
        suit=card[2]

        if suit=="Spades":
            suit="s"
        elif suit=="Hearts":
            suit="h"
        elif suit=="Clubs":
            suit="c"
        else:
            suit="d"

        if rank=="Jack":
            rank="11"
        if rank=="Queen":
            rank="12"
        if rank=="King":
            rank=13
        if rank=="Ace":
            rank=1
        rank=str(rank)

        self.cardPic=Image(Point(xpos,ypos),suit+rank+".gif")
        return self.cardPic #returns the picture of the card in the correct spot 
        

    def initDeal(self,gwin,xposD,yposD,xposP,yposP):
        #Start with the player's hand first
        card1p=self.deck.dealCard()
        self.pHand.append(card1p)
        card1p=self.makeCard(card1p,xposP,yposP)
        card1p.draw(gwin)
        
        card2p=self.deck.dealCard()
        self.pHand.append(card2p)
        card2p=self.makeCard(card2p,xposP+100,yposP)
        card2p.draw(gwin)

        #Now the dealer's hand
        card1d=self.deck.dealCard()
        self.dHand.append(card1d)
        card1d=self.makeCard(card1d,xposD+100,yposD)
        card1d.draw(gwin)

        card2d=self.deck.dealCard()
        self.dHand.append(card2d)
        self.hiddenCard=self.makeCard(card2d,xposD,yposD)
        #one card needs to be face down, but counted
        card2d=Image(Point(xposD,yposD),"b2fv.gif")
        card2d.draw(gwin)
        


    def hit(self,gwin,xpos,ypos,hand):
        newCard=self.deck.dealCard() #deal a new card
        hand.append(newCard) #add it to the list
        newCard=self.makeCard(newCard,xpos,ypos)
        newCard.draw(gwin)

    
        
    def evaluateHand(self,hand):
        handTotal=0 #accumulator to know which person got higher total
        j=0
        for i in hand:#go through the hand list one by one
            c=hand[j]
            j=j+1
            c=c.split()
            #give appropriate cards the correct values
            if c[0]=="Jack" or c[0]=="Queen" or c[0]=="King":
                c[0]=10
                value=c[0]
            elif c[0]=="Ace" and handTotal+11<21:
                c[0]=11
                value=c[0]
            elif c[0]=="Ace" and handTotal+11>21:
                c[0]=1
                value=c[0]
            else:
                value=eval(c[0])
            handTotal=handTotal+value #accumulate values
            self.handTotal=handTotal
        return self.handTotal #return hand total

    def dealerPlays(self,gwin,xpos,ypos):
        self.evaluateHand(self.dHand) #call this method to have a total returned to you
        while self.handTotal<17: #while the total is less than 17, keep hitting
            self.hit(gwin,xpos,ypos,self.dHand)
            xpos=xpos+100
#.........这里部分代码省略.........
开发者ID:janester,项目名称:blackjack,代码行数:103,代码来源:Backjack.py


注:本文中的Deck.dealCard方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。