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

标签 python

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

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/

相关文章:

python - 我需要虚拟环境吗?

python - 在 Django 中迭代特定组中的项目

OSX 中的 python 地穴

python - 父控件如何定位其子控件?

python - Python 函数 scipy.sparse.bmat 的 R 等价物是什么?

python - 如何更改部分 tkinter 标签的字体样式

python - 回复留言discord.py

python - opencv 锐界提取图像

python - 范围函数的正确编码

python - Heroku Scheduler vs Heroku Temporize Scheduler,有什么区别?