Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印我在键入“是”后使用的卡,以便再次玩_Python_Python 2.7 - Fatal编程技术网

Python 打印我在键入“是”后使用的卡,以便再次玩

Python 打印我在键入“是”后使用的卡,以便再次玩,python,python-2.7,Python,Python 2.7,我希望这场游戏开始时每一手牌都是前一手牌的剩余牌。取而代之的是,它从一副全新的牌开始。我如何修复它以继续? 我根据你的建议更新了代码,但是它没有显示我的展示卡,非常感谢 import random, sys suits = ('Clubs', 'Spades', 'Hearts', 'Diamonds') pip = ('Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King') pipValues

我希望这场游戏开始时每一手牌都是前一手牌的剩余牌。取而代之的是,它从一副全新的牌开始。我如何修复它以继续? 我根据你的建议更新了代码,但是它没有显示我的展示卡,非常感谢

import random, sys

suits = ('Clubs', 'Spades', 'Hearts', 'Diamonds')
pip = ('Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King')
pipValues = {'Ace':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'Jack':10, 'Queen':10, 'King':10}



class Card:


    def __init__(self, suit, pip):
        self.suit = suit
        self.pip = pip
        self.value = pipValues.get(pip)

    def __str__(self):
        thisCard = ""
        thisCard += str(self.pip)
        thisCard += str(self.suit)
        return (thisCard)

    def __repr__(self):
        thisCard = ""
        thisCard += str(self.pip)
        thisCard += str(self.suit)
        return (thisCard)




class Player:

    def __init__(self):
        self.hand = []
        self.handTotal = 0

    def __str__(self):
        printHand = ""
        for i in self.hand:
            printHand += str(i) + " "
        return (printHand)

    def __repr__(self):
        printHand = ""
        for i in self.hand:
            printHand += str(i) + " "
        return (printHand)


class Deck:



    def __init__(self):
        self.cardList = []
        #for each suit take every card
        for i in range(len(suits)):
            for j in range(len(pip)):
                self.cardList.append(Card(suits[i], pip[j]))



    def shuffle(self):
        random. shuffle (self.cardList)

    def dealOne(self, player):
        (player.hand).append(self.cardList[0])

        player.handTotal += self.cardList[0].value

        del self.cardList[0]
        print self.cardList
        return self.cardList


    def __str__(self):
        printString = ""
        for i in range(len(self.cardList)):
            if i % 13 == 0:
                printString += "\n \t"
                printString += str(self.cardList[i]) + " "
            else:
                printString += str(self.cardList[i]) + " "
        printString += "\n"


        return printString


def showHands(player, opponent):
    print ('Dealer shows ' + str(opponent.hand[0]) + ' faceup')
    print ('You show ' + str(player.hand[0]) +str(player.hand[0] ))


def playerTurn(deck, player, other):
    #First, check scores to see if either player has a blackjack:

    if player.handTotal == 21 or other.handTotal == 21:
        if other.handTotal == 21:
            print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
            print ("Dealer has " + str(other) + "for a total of 21")
            print ("Dealer has a Blackjack! Dealer wins!")
            print ("Thanks for playing. Come back again soon! ")
            message()
        else:
            print ("You hold " + str(player) + "for a total of 21")
            print ("You have a Blackjack! You win!")

            print ("Thanks for playing. Come back again soon! ")
            message()   

    hitOrStand = 0
    aces = False

    #IF TWO ACES
    if player.hand[0].pip == "A" and player.hand[1].pip == "A":
        player.hand[0].pipValue = 1

    if player.handTotal == 21:
            print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
            print ("Blackjack! You win!")
            print ("Thanks for playing. Come back soon!")
            print()
            message()
    while hitOrStand != 2:
        #check for aces
        for i in player.hand:
            if i.pip == "A" and i.value == 11:
                aces = True

        print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
        print()
        hitOrStand = input('Do you hit or stand? Enter "1" for hit and "2" for stand: ')
        while hitOrStand != 1 and hitOrStand != 2:
            try:
                hitOrStand = int(hitOrStand)
                break
            except ValueError:
                print ("Enter a valid integer \n")
            hitOrStand = input('Do you hit hit or stand? Enter "1" for hit and "2" for stand: ')
        print()

        if hitOrStand == 1:
            print('Card dealt:  ' + str(deck.cardList[0]))
            print()
            deck.dealOne(player)
            #check if an ace was drawn
            for i in player.hand:
                if i.pip == "A" and i.value == 11:
                    aces = True

        if player.handTotal == 21:
            print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
            print ("Blackjack!! You Win!")
            print()
            print ("Thanks for playing. Come back soon!")
            message()

        if player.handTotal > 21:
            #check for aces
            if aces:
                print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
                print ("Over 21. Value of ace changed to 1")
                #chanlge value of ace and hand
                player.handTotal = player.handTotal - 10
                for i in player.hand:
                    if i.pip == "A" and i.value == 11:
                        i.value = 1
                #check for other standard aces
                aces = False
                for i in player.hand:
                    if i.pip == "A" and i.value == 11:
                        aces = True
            else:
                print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
                print ("You Bust! Dealer Wins!")
                #exit, since you're a loser
                print ("Thanks for Playing! Come Back Soon!")
                print()
                raise SystemExit

        if hitOrStand == 2:
            print ('You stand at: ' + str(player.handTotal))
            print()

    print ("Now Dealer's Turn")
    print ()
def message():
        again = raw_input("Do you want to play again? (Y/N) : ")
        if(again == "Y" or again == "y"):
            main()
        else:
            print "\n\n-------Thank you for playing!--------\n\n"
            exit()

def opponentTurn(deck, player, other):
    if other.handTotal == 21:
        raise SystemExit
    aces = False
    hitOrStand = 0

    #IF TWO ACES
    if player.hand[0].pip == "A" and player.hand[1].pip == "A":
        player.hand[0].pipValue = 1

    while hitOrStand != 2:
        #check for aces
        for i in player.hand:
            if i.pip == "A" and i.value == 11:
                aces = True


        print ('Dealer holds ' + str(player) + 'for a total of ' + str(player.handTotal))
        print()
        #if blackjack
        if player.handTotal == 21:
            print ("Dealer has a BlackJack! Dealer Wins!")
            break
        if player.handTotal <21 and other.handTotal == 21:
            print ("Dealer's hand is " + str(player.handTotal) + ". You have a Blackjack! Congratulations! You win! ")
            break

        if player.handTotal < other.handTotal:
            hitOrStand = 1
        if player.handTotal >= other.handTotal:
            hitOrStand = 2

        if hitOrStand == 1:
            print("Dealer hits. " + 'Card dealt:  ' + str(deck.cardList[0]))
            deck.dealOne(player)
            #check if an ace was drawn
            for i in player.hand:
                if i.pip == "A" and i.value == 11:
                    aces = True

        if player.handTotal > 21:
            #check for aces
            if aces:
                print ('Dealer holds ' + str(player) + 'for a total of ' + str(player.handTotal))
                print ("Over 21. Value of ace changed to 1")
                #chanlge value of ace and hand
                player.handTotal = player.handTotal - 10
                for i in player.hand:
                    if i.pip == "A" and i.value == 11:
                        i.value = 1

                #check for other standard aces
                aces = False
                for i in player.hand:
                    if i.pip == "A" and i.value == 11:
                        aces = True
            else:
                print ('Dealer holds ' + str(player) + 'for a total of ' + str(player.handTotal))
                print ("Dealer Busts! You Win!")
                message()


        if hitOrStand == 2:
            print ("Dealer stands at " + str(player.handTotal))
            print ("Your score is " + str(other.handTotal))
            print ("Dealer Wins!")
            message()
#who won
def main():
    cardDeck = Deck()
    print ('Initial Deck: ')
    print(cardDeck)

    cardDeck.shuffle()
    print ('Shuffled Deck: ')
    print(cardDeck)

    keep_playing = True
    while keep_playing:

        player = Player()
        opponent = Player()

        #give each player 2 cards, alternating
        cardDeck.dealOne(player)
        cardDeck.dealOne(opponent)
        cardDeck.dealOne(player)
        cardDeck.dealOne(opponent)

        print ('Deck after giving 2 cards each')
        print (cardDeck)

        #show 1 faceup card for each player
        showHands(player,opponent)

        #start playing
        playerTurn(cardDeck,player, opponent)
        opponentTurn(cardDeck, opponent, player)

        again = raw_input("Do you want to play again? (Y/N) : ")
        keep_playing = again in "Yy"

    # Reach here after dropping out of the while loop
    print "\n\n-------Thank you for playing!--------\n\n"
main()
import random,sys
套装=(‘梅花’、‘黑桃’、‘红桃’、‘钻石’)
pip=(‘A’、‘2’、‘3’、‘4’、‘5’、‘6’、‘7’、‘8’、‘9’、‘10’、‘杰克’、‘女王’、‘国王’)
pipValues={'Ace':11,'2':2,'3':3,'4':4,'5':5,'6':6,'7,'8':8,'9':9,'10':10,'Jack':10,'Queen':10,'King':10}
班级卡:
定义初始(自我、套装、pip):
西服
self.pip=pip
self.value=pipValues.get(pip)
定义(自我):
此卡=“”
此卡+=str(self.pip)
此卡+=str(自助套装)
退货(此卡)
定义报告(自我):
此卡=“”
此卡+=str(self.pip)
此卡+=str(自助套装)
退货(此卡)
职业球员:
定义初始化(自):
self.hand=[]
self.handTotal=0
定义(自我):
printHand=“”
对于我自己来说:
printHand+=str(i)+“
退货(印刷品)
定义报告(自我):
printHand=“”
对于我自己来说:
printHand+=str(i)+“
退货(印刷品)
舱面:
定义初始化(自):
self.cardList=[]
#每件衣服都要带上每一张卡片
对于范围内的i(len(套装)):
对于范围内的j(len(pip)):
self.cardList.append(Card(适合[i],pip[j]))
def随机播放(自):
随机的洗牌(self.cardList)
def dealOne(自我、玩家):
(player.hand).append(self.cardList[0])
player.handTotal+=self.cardList[0]。值
del self.cardList[0]
打印自助卡列表
返回自助卡列表
定义(自我):
printString=“”
对于范围内的i(len(self.cardList)):
如果i%13==0:
printString+=“\n\t”
printString+=str(self.cardList[i])+“”
其他:
printString+=str(self.cardList[i])+“”
printString+=“\n”
返回打印字符串
def展示(玩家、对手):
打印(‘庄家显示’+str(对手手[0])+‘脸朝上’)
打印('youshow'+str(player.hand[0])+str(player.hand[0]))
def玩家翻转(牌组、玩家、其他):
#首先,检查分数,看两位玩家是否有21点:
如果player.handTotal==21或other.handTotal==21:
如果other.handTotal==21:
打印('您持有'+str(player)+'总共'+str(player.handTotal))
打印(“经销商有“+str(其他)+”共21条”)
打印(“庄家有21点!庄家赢!”)
打印(“感谢您的播放。请稍后再来!”)
消息()
其他:
打印(“您持有”+str(玩家)+“总共21”)
打印(“你有21点!你赢了!”)
打印(“感谢您的播放。请稍后再来!”)
消息()
hitOrStand=0
A=错误
#如果两个A
如果player.hand[0]。pip==“A”和player.hand[1]。pip==“A”:
player.hand[0]。pipValue=1
如果player.handTotal==21:
打印('您持有'+str(player)+'总共'+str(player.handTotal))
打印(“21点!你赢了!”)
打印(“感谢您的播放。请尽快回来!”)
打印()
消息()
当击球手站立时!=2:
#检查A
对于player.hand中的i:
如果i.pip==“A”和i.value==11:
A=正确
打印('您持有'+str(player)+'总共'+str(player.handTotal))
打印()
hitOrStand=input('你是击中还是站立?输入“1”表示击中,输入“2”表示站立:')
当击球手站立时!=1和hitOrStand!=2:
尝试:
hitOrStand=int(hitOrStand)
打破
除值错误外:
打印(“输入有效的整数\n”)
hitOrStand=input('你是击中还是站立?输入“1”表示击中,输入“2”表示站立:')
打印()
如果hitOrStand==1:
打印('已发卡片:'+str(卡片组.卡片列表[0]))
打印()
牌组。发牌(玩家)
#检查是否抽到A
对于player.hand中的i:
如果i.pip==“A”和i.value==11:
A=正确
如果player.handTotal==21:
打印('您持有'+str(player)+'总共'+str(player.handTotal))
打印(“21点!!你赢了!”)
打印()
打印(“感谢您的播放。请尽快回来!”)
消息()
如果player.handTotal>21:
#检查A
如果ACE:
打印('您持有'+str(player)+'总共'+str(player.handTotal))
打印(“超过21。ace值更改为1”)
#ace和hand的信道值
player.handTotal=player.handTotal-10
对于player.hand中的i:
如果i.pip==“A”和i.value==11:
i、 值=1
#检查其他标准ACE
A=错误
对于player.hand中的i:
如果i.pip==“A”和i.value==11:
A=正确
其他:
打印('您持有'+str(player)+'总共'+str(player.handTotal))
打印(“你破产了!庄家赢了!”)
#退出,因为你是个失败者
打印(“谢谢玩!快回来!”)
打印()
升起系统出口
如果hitOrStand==2:
打印('您站在:'+str(player.handtottal))
打印()
打印(“现在轮到经销商了”)
打印()
def消息():
再次=未加工的
def main():
    cardDeck = Deck()
    print ('Initial Deck: ')
    print(cardDeck)

    cardDeck.shuffle()
    print ('Shuffled Deck: ')
    print(cardDeck)

    keep_playing = True
    while keep_playing:

        player = Player()
        opponent = Player()

        #give each player 2 cards, alternating
        cardDeck.dealOne(player)
        cardDeck.dealOne(opponent)
        cardDeck.dealOne(player)
        cardDeck.dealOne(opponent)

        print ('Deck after giving 2 cards each')
        print (cardDeck)

        #show 1 faceup card for each player
        showHands(player,opponent)

        #start playing
        playerTurn(cardDeck,player, opponent)
        opponentTurn(cardDeck, opponent, player)

        again = raw_input("Do you want to play again? (Y/N) : ")
        keep_playing = again in "Yy"

    # Reach here after dropping out of the while loop
    print "\n\n-------Thank you for playing!--------\n\n"
aces = False
#IF TWO ACES
if player.hand[0].pip == "A" and player.hand[1].pip == "A":
    player.hand[0].pipValue = 1
#check for aces
    for i in player.hand:
        if i.pip == "A" and i.value == 11:
            aces = True
#check if an ace was drawn
        for i in player.hand:
            if i.pip == "A" and i.value == 11:
                aces = True
#check for aces
if aces:
    print ('You hold ' + str(player) + 'for a total of ' + str(player.handTotal))
    print ("Over 21. Value of ace changed to 1")
    #chanlge value of ace and hand
    player.handTotal = player.handTotal - 10
    for i in player.hand:
        if i.pip == "A" and i.value == 11:
            i.value = 1
    #check for other standard aces
    aces = False
    for i in player.hand:
        if i.pip == "A" and i.value == 11:
            aces = True
@property  
def handTotal(self):
  while sum(card.value for card in self.hand) > 21 and \
        any(card.pip == 'Ace' and card.value == 11 for card in self.hand):
    for card in self.hand:
      if card.pip == 'Ace' and card.value == 11:
        card.value = 1
        break
  return sum(card.value for card in self.hand)
def message():
    global playing
    again = input("Do you want to play again? (Y/N) : ")
    if again.lower() == "n":
        print("\n\n-------Thank you for playing!--------\n\n")
        playing = False
    return True # Tells `turn()` the game is over