2012-01-27 35 views
2

因此,我必須創建一個骰子游戲,考慮投注的投注。到目前爲止,我的代碼的工作原理是擲骰子是正確的,還有其他需要分配的小工具。但是現在我不知道如何將每場比賽記錄爲球員或計算機的輸贏,這樣就可以將獎池添加到贏家的錢中。我意識到我的代碼只有一半,沒有完成,並沒有按原樣運行,但我只是認真需要某人的幫助。謝謝,麻煩您了。這裏是我的任務更具體的方向:在骰子游戲中錄製勝利或損失

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)))) 

回答

5

使用類:

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 

我不會代碼的所有的它適合你,但使用類將使事情更簡單。