我是 Python 新手,最初的几十个小时都花在了在线类(class)、家庭作业项目和一些文档阅读上。
我几乎完成了我的一门类(class)。我的最后一个项目是创建我已经完成的简单纸牌游戏(代码如下)。我想简单解释一下我的游戏是关于什么的。对于不确定数量的玩家来说,这是一款简单的“赌博”猜牌游戏。玩家只需猜测下一张牌是低于还是高于前一张牌,然后根据他们是对还是错来下注赢或输。
就我测试的代码而言,我发现了一个到目前为止我无法解决的错误 - 当所有玩家在一轮中输了时,他们并没有像他们应该的那样全部被踢出游戏)。
无论如何,我的主要问题是我不知道如何将其实现到我的代码中:
在游戏过程中的任何时候,有人应该能够输入“--help”以进入一个屏幕,在那里他们可以阅读游戏规则和如何玩的说明。阅读完后,他们应该能够输入“--resume”返回游戏并从上次中断的地方继续。
(我在这里发现了非常相同的问题:Python simple card game -)
我用谷歌搜索了一下,我发现只有一个有用的东西是 help()
功能。我试图实现它,但我不确定它是否正确选择如何执行此操作 --help 和 --resume 标志(甚至称为标志?我对所有 Python 术语都不是 100% 确定)
我的代码是:
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 ?去哪里找,甚至告诉我应该找什么?
另外,如果你想给我一些关于代码的反馈(可以用更简单的方式编写什么,或者我在 future 的编码载体或其他任何东西中应该避免什么),我将非常感激。
最佳答案
为了能够在游戏中的任何地方获得“--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)
一点反馈:您可能想在玩家每次下注时打印他们的资金,以便他们知道他们必须处理多少面团。
关于python - 猜牌游戏并帮助实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61388991/