gpt4 book ai didi

python - 记录掷骰子游戏的输赢

转载 作者:行者123 更新时间:2023-11-28 22:03:32 25 4
gpt4 key购买 nike

所以我必须创建一个考虑到作业赌注的掷骰子游戏。到目前为止,我的代码的工作原理是掷骰子是正确的,并且作业要求的其他小花絮。但是现在我不知道如何将每场比赛记录为玩家或计算机的输赢,以便将彩池添加到赢家的钱中。我意识到我的代码是半成品,还没有完成,也没有按原样运行,但我真的需要某人的帮助。谢谢,麻烦您了。以下是我的作业的更具体说明:

http://www.ics.uci.edu/~kay/courses/i42/hw/labA.html

import random

def craps():
print("Welcome to Sky Masterson's Craps Game")
handle_commands()

def handle_commands(): # Collection -> Collection (plus interaction)
""" Display menu to user, accept and process commands
"""

playerInitial = 500
compInitial = 500

MENU = "How much would you like to bet?: "
while True:
bet = float(input(MENU))
if bet <= playerInitial:
human_game()
elif bet > playerInitial:
print("Sorry, you can't bet more than you have")

def handle_commands2():


MENU2 = "Would you like to play again? (y or n): "

while True:
response = input (MENU2)
if response=="y":
counter = counter + multipleGames()
elif response=="n":
while ( counter < 2000):
roll = random.randint(1, 6) + random.randint(1,6)
updateCount(roll)
counter += 1
print ("Thank you for playing." + "\n" + "\n" + "Distribution of dice rolls: " + "\n")
return
else:
invalid_command(response)



def invalid_command(reponse):
"""print message for invalid menu command.
"""
print("Sorry; '" + response + "' isn't a valid command. Please try again.")



def play_game():
"""prints shooters roll results
"""
diceRoll = 0
roll = random.randint(1, 6) + random.randint(1, 6)
updateCount(roll)
diceRoll = diceRoll + 1

point = 0
print("The roll is " + str(roll))
response = (roll)
if response== 7 or response== 11:
print("Natural; shooter wins" + "\n" + "Thank you for playing")
handle_commands2()

elif response== 2 or response== 3 or response== 12:
print("Crapped out; shooter loses" + "\n" + "Thank you for playing")
handle_commands2()
else:
print("The point is " + str(roll))
point = roll
secondRoll = 0
handle_commands()

while (secondRoll !=point) and (secondRoll != 7):
secondRoll = random.randint(1, 6) + random.randint(1, 6)
updateCount(secondRoll)
diceRoll += 1
print("The roll is " + str(secondRoll))
handle_commands()
if secondRoll== point:
print ("Made the point; shooter wins." + "\n" + "Thank you for playing")
handle_commands2()
elif (secondRoll == 7):
print ("Crapped out; shooter loses." + "\n" + "Thank you for playing")
handle_commands2()
return diceRoll


def multipleGames():
gameCounter = 0
while (gameCounter <= 2000):
print("Your game: ")
gameCounter += play_game()
print("\n")
print("Computer's game: ")
gameCounter += play_game()
print( "\n")
return gameCounter

def updateCount(point):
count =List[point] + 1
List[point] = count


List = {2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0}

def human_game():
playerInitial = 500
compInitial = 500
while True:
play_game()
if

playerInitial += bet
compInitial += bet
counter = 0
counter = counter + multipleGames()
playerInitial -= bet






craps()
for point in List:
print("%2d" %(point) + ": " + "%3d" %(List[point]) + " " + "(" + ("%2d" % (int((List[point])/2000*100)))+ "%" + ")" + " " + ("*" *(int((List[point])/2000*100))))

最佳答案

使用类:

import random

class Human:
def __init__(self):
self.name = 'Human'
self.wins = []
self.losses = []
self.bets = []
self.total = 0

class Computer:
def __init__(self):
self.name = 'Computer'
self.wins = []
self.losses = []
self.bets = []
self.total = 0

class Game:
def __init__(self):
self.rolls = []
self.currentPlayer = None

def roll(self):
self.rolls.append(random.randint(1, 6))

if __name__ == '__main__':
human = Human()
computer = Computer()
game = Game()

game.roll()
print games.rolls

我不会为您编写所有代码,但使用类会让事情变得很多更简单。

关于python - 记录掷骰子游戏的输赢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9027996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com