猜牌游戏和帮助实施

我是个新手,我的前几十个小时都是在网上上课,做作业项目,当然还有一些文档阅读。

我几乎完成了我的一个课程。我的最后一个项目是创建简单的卡片游戏,我已经完成了(代码如下)。我想简单地解释一下我的游戏是什么。它是一个简单的 “赌博 “猜牌游戏,玩家人数不限。玩家只需简单地猜测下一张牌会比上一张牌低或高,然后根据他们的猜测正确与否来决定输赢。

就我的测试而言,我的代码是正常的(我发现了一个至今无法解决的bug–当所有玩家在一局中输掉时,他们并没有像他们应该的那样被踢出游戏)。

总之,我的主要问题是我不知道如何在我的代码中实现这一点:在游戏过程中的任何时候,有人应该能够键入”–help “被带到一个屏幕,在那里他们可以阅读游戏规则和如何游戏的说明。当他们读完之后,他们应该能够键入”–resume “来回到游戏,并从他们离开的地方开始。Python简单的纸牌游戏 –)

我上网查了一下,只查到一个有用的东西,就是 help() 函数。我试着实现它,但我不确定它是否是正确的选择,如何做到这一点 –help和 –resume flag (是否叫flag? 我不是100%确定所有Python术语)

我的代码是

from random import shuffle, randrange


def cardDeck():  # create deck of the cards

    cardDeck = []
    for value in range(4):  # four sets of cards
        for i in range(2, 11):  # for number values
            if value == 0:
                cardDeck.append(str(i) + '♠')
            if value == 1:
                cardDeck.append(str(i) + '♣')
            if value == 2:
                cardDeck.append(str(i) + '♦')
            if value == 3:
                cardDeck.append(str(i) + '♥')
    figures = ['J', 'Q', 'K', 'A']
    for figure in figures:  # for four set of figures
        cardDeck.append(str(figure) + '♠')
        cardDeck.append(str(figure) + '♣')
        cardDeck.append(str(figure) + '♦')
        cardDeck.append(str(figure) + '♥')
    shuffle(cardDeck)
    return cardDeck


class Player: # define player class
    def __init__(self, nickname='Player', bankroll=100, value=0):
        self.nick = nickname
        self.bankroll = int(bankroll)
        self.value = value
        self.BetKind = ''
        self.amount = 0

    def __str__(self):
        return self.nick + ' plays'

    def win(self):

        self.bankroll += 2 * int(self.amount)

    def MassBet(self):

        for i in range(1000):
            self.amount = int(input('how much do you want to bet? '))
            if self.amount <= self.bankroll:
                break
            else:
                print('You can bet only your current bank!')
        for i in range(1000):
            self.BetKind = input('higher/lower [h/l] ')
            if self.BetKind == 'h' or self.BetKind == 'l':
                break
            else:
                print("Please enter only 'h' or 'l' ")
        self.bankroll -= int(self.amount)

    def GetValue(self, card):
        self.value = Values[card[0:-1]]


Deck = cardDeck()
Values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
          '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14} # define value of each card

PlayerList = []
NPlayers = int(input('How many players are going to play? ')) # Here you can define players
for i in range(NPlayers):
    print('Inicialization, player ', i+1)
    nickname = input('What is your nickname? ')
    bankroll = input('What is your bank? ')
    player = Player(nickname, bankroll)
    PlayerList.append(player)


oldCardsValues = []
oldCardsValues.append(Deck[-1]) # first card that everybody see
Deck.pop()
round = 0
while True:
    print('You bet againts: ', oldCardsValues[-1])
    for player in PlayerList:       # define kind of the bet for each player
        print(player.nick+', ', end = '')
        player.MassBet()
    DrawCard = Deck.pop()
    if NPlayers == 1:
        print('You draw: ', DrawCard)
    else:
        print('All players bet! Draw is: ', DrawCard)
    for player in PlayerList: # define if player won or lost
        player.GetValue(DrawCard)
        if player.BetKind == 'l':
            if Values[oldCardsValues[-1][0:-1]] > player.value:
                player.win()
                print(player.nick,'won! New bankroll: ', player.bankroll)
            elif Values[oldCardsValues[-1][0:-1]] < player.value:
                print(player.nick, 'lost! New bankroll: ', player.bankroll)
            else:
                print('Same card', player.nick, 'lost')
        elif player.BetKind == 'h':
            if Values[oldCardsValues[-1][0:-1]] < player.value:
                player.win()
                print(player.nick,'won! New bankroll: ', player.bankroll)
            elif Values[oldCardsValues[-1][0:-1]] > player.value:
                print(player.nick, 'lost! New bankroll: ', player.bankroll)
            else:
                print('Same card', player.nick, 'lost')
        print(player.bankroll)
        if player.bankroll <= 0: # if player run out of cash he is out
            print(player.nick, 'I am sorry, you run out of your cash. You are out.')
            PlayerList.remove(player)
    if len(PlayerList) == 0: # if there are no remaining players game is over
        print('No more players left. Game Over!')
        break

    round += 1
    MixCoeficcient = randrange(25, 35)
    if len(Deck) < MixCoeficcient: # create new deck in random interval of the remaining cards
        Deck = cardDeck()

    oldCardsValues.append(DrawCard)
    print('-'*40)

请你告诉我如何在我的游戏中实现这个 –帮助和 –简历?在哪里找,甚至告诉我应该找什么?

另外,如果你愿意给我一些关于代码的反馈(哪些可以用更简单的方式写,或者在我未来的编码载体中我应该避免什么,或者其他什么),我会非常感激。

解决方案:

为了能够在游戏中的任何地方得到”–help “或”–resume “的提示,你需要在每一个你通常会使用Python常规的地方使用一个特殊的输入函数。input() 功能。类似这样。

def specialInput(prompt):
    action = input(prompt)
    if action == '--help':
        print("You need help")
        # Go to help screen
        showHelp()
        return specialInput(prompt)
    elif action == '--resume':
        print("You want to resume")
        # Resume the game
        # resume()
        return specialInput(prompt)
    else:
        return action

这个函数通过输入一个给定的提示音来获取输入信息,并检查它是否等同于 --help--resume. 如果是这样,它将显示帮助或者继续执行程序。不一定(🤣)要有一个 resume() 不过,功能。你可以让 showHelp() 只需打印出一些说明,然后在打印出帮助后,程序会自动继续进行(从你离开的同一个输入提示开始)。

完整的程序是这样的。

from random import shuffle, randrange


def showHelp():
    instructions = """\nPlayers simply guess if the next card is going to be lower or higher than previous one
and win or loose they bet according to if they were right or not.\n"""
    print(instructions)


def specialInput(prompt):
    action = input(prompt)
    if action == '--help':
        print("You need help")
        # Go to help screen
        showHelp()
        return specialInput(prompt)
    elif action == "--resume":
        print("You want to resume")
        # You can decide whether or not to do anything else here
        return specialInput(prompt)
    else:
        return action


def cardDeck():  # create deck of the cards
    cardDeck = []
    for value in range(4):  # four sets of cards
        for i in range(2, 11):  # for number values
            if value == 0:
                cardDeck.append(str(i) + '♠')
            if value == 1:
                cardDeck.append(str(i) + '♣')
            if value == 2:
                cardDeck.append(str(i) + '♦')
            if value == 3:
                cardDeck.append(str(i) + '♥')
    figures = ['J', 'Q', 'K', 'A']
    for figure in figures:  # for four set of figures
        cardDeck.append(str(figure) + '♠')
        cardDeck.append(str(figure) + '♣')
        cardDeck.append(str(figure) + '♦')
        cardDeck.append(str(figure) + '♥')
    shuffle(cardDeck)
    return cardDeck


class Player:  # define player class
    def __init__(self, nickname='Player', bankroll=100, value=0):
        self.nick = nickname
        self.bankroll = int(bankroll)
        self.value = value
        self.BetKind = ''
        self.amount = 0

    def __str__(self):
        return self.nick + ' plays'

    def win(self):

        self.bankroll += 2 * int(self.amount)

    def MassBet(self):

        for i in range(1000):
            self.amount = int(specialInput('how much do you want to bet? '))
            if self.amount <= self.bankroll:
                break
            else:
                print('You can bet only your current bank!')
        for i in range(1000):
            self.BetKind = specialInput('higher/lower [h/l] ')
            if self.BetKind == 'h' or self.BetKind == 'l':
                break
            else:
                print("Please enter only 'h' or 'l' ")
        self.bankroll -= int(self.amount)

    def GetValue(self, card):
        self.value = Values[card[0:-1]]


Deck = cardDeck()
Values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
          '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}  # define value of each card

PlayerList = []
NPlayers = int(specialInput('How many players are going to play? '))  # Here you can define players
for i in range(NPlayers):
    print('Inicialization, player ', i + 1)
    nickname = specialInput('What is your nickname? ')
    bankroll = specialInput('What is your bank? ')
    player = Player(nickname, bankroll)
    PlayerList.append(player)

oldCardsValues = [Deck[-1]]
Deck.pop()
print(Deck[45:])
round = 0
while True:
    print('You bet againts: ', oldCardsValues[-1])
    for player in PlayerList:  # define kind of the bet for each player
        print(player.nick + ', ', end='')
        player.MassBet()
    DrawCard = Deck.pop()
    if NPlayers == 1:
        print('You draw: ', DrawCard)
    else:
        print('All players bet! Draw is: ', DrawCard)
    for player in PlayerList:  # define if player won or lost
        player.GetValue(DrawCard)
        if player.BetKind == 'l':
            if Values[oldCardsValues[-1][0:-1]] > player.value:
                player.win()
                print(player.nick, 'won! New bankroll: ', player.bankroll)
            elif Values[oldCardsValues[-1][0:-1]] < player.value:
                print(player.nick, 'lost! New bankroll: ', player.bankroll)
            else:
                print('Same card', player.nick, 'lost')
        elif player.BetKind == 'h':
            if Values[oldCardsValues[-1][0:-1]] < player.value:
                player.win()
                print(player.nick, 'won! New bankroll: ', player.bankroll)
            elif Values[oldCardsValues[-1][0:-1]] > player.value:
                print(player.nick, 'lost! New bankroll: ', player.bankroll)
            else:
                print('Same card', player.nick, 'lost')
        print(player.bankroll)
        if player.bankroll <= 0:  # if player run out of cash he is out
            print(player.nick, 'I am sorry, you run out of your cash. You are out.')
            PlayerList.remove(player)
    if len(PlayerList) == 0:  # if there are no remaining players game is over
        print('No more players left. Game Over!')
        break

    round += 1
    print('current deck len: ', len(Deck), 'current round: ', round)
    MixCoeficcient = randrange(25, 35)
    if len(Deck) < MixCoeficcient:  # create new deck in random interval of the remaining cards
        Deck = cardDeck()

    oldCardsValues.append(DrawCard)
    print('-' * 40)

有一点反馈 你可能需要打印出玩家每次下注时的银行存款 这样他们就能知道自己有多少钱了

给TA打赏
共{{data.count}}人
人已打赏
未分类

Djongo使用ArrayField在makemigrations上抛出Apps No Loaded错误。

2022-9-8 5:41:37

未分类

为什么Postgres的select查询非常慢?

2022-9-8 5:41:39

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索