鱼C论坛

 找回密码
 立即注册
查看: 759|回复: 3

[技术交流] python 21点

[复制链接]
发表于 2023-12-11 09:54:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
怎么样可以做到最后比较两个玩家的点数?
import random
class Card:
    def __init__(self,r,s):
        self.rank=r
        self.suit=s
    def __str__(self):
        return(self.rank+self.suit)
class Player:
    def __init__(self, name):
        self.name = name
        self.hand = []
        self.bjhandvalue = 0
        
    def __str__(self):
        return (self.name+ " has the bjvalue "+self.bjhandvalue)

def main():
    instructions()
    deck=makeADeckOf52()

    for i in range(2):
        name = input("Enter the name of the player: ")
        player = Player(name)
        player.bjhandvalue=0
        player.bjhandvalue=turn(deck,player)
        displayHand(player)
        while player.bjhandvalue<21:
            answer=input("Do you want to request a card?[y/n]")
            if answer.lower=="y":
                card=dealAHand(deck,player)
                cardbjvalue=create_BJValuecard_LookUp(card)
                player.bjhandvalue=player.bjhandvalue+cardbjvalue
        if player.bjhandvalue>=21:
            numA=0
            for card in player.hand:
                if card.rank =="A":
                    numA=numA+1
            while player.bjhandvalue>=21 and numA>0:
                player.bjhandvalue=player.bjhandvalue-10
                numA=numA-1
            if player.bjvalue>=21:
                print(player.name+", you lose! Game over!")   
        elif  player.bjhandvalue<21:
            numA=0
            for card in player.hand:
                if card.rank =="A":
                    numA=numA+1
            while player.bjhandvalue<21 and numA>0:
                player.bjhandvalue=player.bjhandvalue+10
                numA=numA-1
            



def makeADeckOf52():
    #input: none
    #output: a list of 52 strings
    #process: pair the 4 suits with the 13 card rank values
    #modeling the 52 cards:
    #A-K of clubs,diamonds, hearts, spades
    cardSuits = ["&#9827;","&#9830;","&#9829;","&#9824;"]
    cardRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
    deck = []
    for s in cardSuits:
        for r in cardRanks:
            deck.append(Card(r,s))
    random.shuffle(deck)
    return(deck)

def turn(deck,player):
    player.bjhandvalue=0
    for i in range(2):
        card=dealAHand(deck, player)
        player.hand.append(card)
    player.bjhandvalue=update_hand_value(player)
    return player.bjhandvalue

def instructions():
    print("You two will play this Black Jack game in turn.")
    print("Each of you can get two cards randomly and you can choose request or hold a card.")
    print("The winner will be the person whose bjvalue is the closet to 21 and less than 21.")

def dealAHand(deck, player):
    card=deck.pop()
    return card

def create_BJValuecard_LookUp(cardStr):
    #assume card is in this format: A&#9827; , 10&#9829;
    #"&#9827;","&#9830;","&#9829;","&#9824;"

    rank = cardStr[:len(cardStr)-1]
    print(rank)
    specialValues = {"A":1, "J":10,"Q":10,"K":10}
    if rank in specialValues:
        bjValue = specialValues[rank]
    else:
        bjValue = int(rank)
    return(bjValue)

def displayHand(player):
    print(player.name+"' hand card is "+player.hand)
def update_hand_value(player):
    for card in player.hand:
        cardbjvalue=create_BJValuecard_LookUp(card)
        player.bjhandvalue=player.bjhandvalue+cardbjvalue
        return player.bjhandvalue
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-11 09:57:43 | 显示全部楼层
要比较两个玩家的点数,可以在游戏结束后,比较两个玩家的bjhandvalue属性大小即可。

首先,在Player类中添加一个方法用于获取玩家的点数:
def get_bjhandvalue(self):    return self.bjhandvalue
然后,在main函数中,创建两个Player对象,并在游戏结束后比较它们的点数:
def main():    instructions()    deck = makeADeckOf52()    players = []    for i in range(2):        name = input("Enter the name of the player: ")        player = Player(name)        player.bjhandvalue = 0        player.bjhandvalue = turn(deck, player)        displayHand(player)        while player.bjhandvalue == 21:            # 游戏逻辑            ...        players.append(player)    # 比较两个玩家的点数    if players[0].get_bjhandvalue() > players[1].get_bjhandvalue():        print(players[0].name + " wins!")    elif players[0].get_bjhandvalue() < players[1].get_bjhandvalue():        print(players[1].name + " wins!")    else:        print("It's a draw!")if __name__ == '__main__':    main()
这样就可以在游戏结束后比较两个玩家的点数了。记得在比较之前,确保两个玩家都已经完成了他们的回合,并计算出了最终的点数。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-11 10:36:56 | 显示全部楼层
FishC 发表于 2023-12-11 09:57
要比较两个玩家的点数,可以在游戏结束后,比较两个玩家的bjhandvalue属性大小即可。

首先,在Player类中 ...

import random
class Card:
    def __init__(self,r,s):
        self.rank=r
        self.suit=s
    def __str__(self):
        return(self.rank+self.suit)
class Player:
    def __init__(self, name):
        self.name = name
        self.hand = []
        self.bjhandvalue = 0
        
    def __str__(self):
        return (self.name+ " has the bjvalue "+self.bjhandvalue)

def main():
    instructions()
    deck=makeADeckOf52()
    players=[]

    for i in range(2):
        name = input("Enter the name of the player: ")
        player = Player(name)
        player.bjhandvalue=0
        player.bjhandvalue=turn(deck,player)
        displayHand(player)
        while player.bjhandvalue<21:
            answer=input("Do you want to request a card?[y/n]")
            if answer.lower=="y":
                card=dealAHand(deck,player)
                cardbjvalue=create_BJValuecard_LookUp(card)
                player.bjhandvalue=player.bjhandvalue+cardbjvalue
        if player.bjhandvalue>=21:
            numA=0
            for card in player.hand:
                if card.rank =="A":
                    numA=numA+1
            while player.bjhandvalue>=21 and numA>0:
                player.bjhandvalue=player.bjhandvalue-10
                numA=numA-1
            if player.bjvalue>=21:
                print(player.name+", you lose! Game over!")   
        elif  player.bjhandvalue<21:
            numA=0
            for card in player.hand:
                if card.rank =="A":
                    numA=numA+1
            while player.bjhandvalue<21 and numA>0:
                player.bjhandvalue=player.bjhandvalue+10
                numA=numA-1
        players.append(player)   

    if players[0].bjhandvalue>21 and players[1].bjhandvalue<21:
        print(players[1].name+", you wins!")
    elif players[1].bjhandvalue>21 and players[0].bjhandvalue<21:
        print(players[0].name+", you wins!")
    elif players[0].bjhandvalue>21 and players[1].bjhandvalue>21:
        print("Both of you lose.")
    elif players[0].bjhandvalue<21 and players[1].bjhandvalue<21:
        if (21-players[0].bjhandvalue)<(21-players[1].bjhandvalue):
            print(players[0].name+", you wins!")
        else:
            print(players[1].name+", you wins!")




def makeADeckOf52():
    #input: none
    #output: a list of 52 strings
    #process: pair the 4 suits with the 13 card rank values
    #modeling the 52 cards:
    #A-K of clubs,diamonds, hearts, spades
    cardSuits = ["&#9827;","&#9830;","&#9829;","&#9824;"]
    cardRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
    deck = []
    for s in cardSuits:
        for r in cardRanks:
            deck.append(Card(r,s))
    random.shuffle(deck)
    return(deck)

def turn(deck,player):
    player.bjhandvalue=0
    for i in range(2):
        card=dealAHand(deck, player)
        player.hand.append(card)
    player.bjhandvalue=update_hand_value(player)
    return player.bjhandvalue

def instructions():
    print("You two will play this Black Jack game in turn.")
    print("Each of you can get two cards randomly and you can choose request or hold a card.")
    print("The winner will be the person whose bjvalue is the closet to 21 and less than 21.")

def dealAHand(deck, player):
    card=deck.pop()
    return card

def create_BJValuecard_LookUp(cardStr):
    #assume card is in this format: A&#9827; , 10&#9829;
    #"&#9827;","&#9830;","&#9829;","&#9824;"

    rank = cardStr[:len(cardStr)-1]
    print(rank)
    specialValues = {"A":1, "J":10,"Q":10,"K":10}
    if rank in specialValues:
        bjValue = specialValues[rank]
    else:
        bjValue = int(rank)
    return(bjValue)

def displayHand(player):
    print(player.name+"' hand card is "+player.hand)
def update_hand_value(player):
    for card in player.hand:
        cardbjvalue=create_BJValuecard_LookUp(card)
        player.bjhandvalue=player.bjhandvalue+cardbjvalue
        return player.bjhandvalue
   
main()
你可以帮我检查一下有什么问题吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-11 10:41:16 | 显示全部楼层
pan11 发表于 2023-12-11 10:36
import random
class Card:
    def __init__(self, ...

看起来代码中缺少了一些函数的定义和实现,比如makeADeckOf52函数、turn函数、displayHand函数等。这些函数的具体实现我无法确定,所以无法直接运行代码。不过,根据你的代码逻辑,我可以尝试给出一种实现方式供你参考。

首先,需要添加一个函数用于比较两个玩家的点数:
def compare_players(player1, player2):    if player1.bjhandvalue > player2.bjhandvalue:        print(player1.name + " wins!")    elif player1.bjhandvalue < player2.bjhandvalue:        print(player2.name + " wins!")    else:        print("It's a draw!")
然后,在主函数main中,在游戏结束后调用compare_players函数比较两个玩家的点数:
def main():    instructions()    deck = makeADeckOf52()    players = []        for i in range(2):        name = input("Enter the name of the player: ")        player = Player(name)        player.bjhandvalue = 0        player.bjhandvalue = turn(deck, player)        displayHand(player)                while player.bjhandvalue == 21:            numA = 0            for card in player.hand:                if card.rank == "A":                    numA += 1            while player.bjhandvalue >= 21 and numA > 0:                player.bjhandvalue -= 10                numA -= 1            if player.bjhandvalue >= 21:                print(player.name + ", you lose! Game over!")            break  # 结束玩家回合                players.append(player)        if len(players) == 2:        compare_players(players[0], players[1])if __name__ == '__main__':    main()
这样,游戏结束后就会根据比较玩家的点数,输出胜负结果。请注意,为了保证代码的完整性,你还需要补充其他函数的定义和实现。希望对你有所帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-23 23:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表