0
該項目:在python中編寫一個程序,其中虛擬經銷商Jake與虛擬球員:Mike和Will進行比賽。 Mike和Will可以在不同的支付率下以不同的結果進行賭注。這將允許比較各種策略。你應該跟蹤每個玩家的銀行擲骰子(包括經銷商) 這個遊戲是用一個7號模子玩的,數字[0-6]數字[1,2,3]是藍色和數字[4,5,6]是綠色的。獲取相互作用的功能
校驗正確自付:2/1 正確的顏色自付:2/1 確切個數自付:5/1
下面是修改@Harvey夏天建議初稿。有關我如何改進代碼結構和性能的意見,敬請欣賞。
from random import choice
from random import randint
class die_face():
# This class is used to define the properties linked to each outcome on the dice.
def __init__(self, num, colour, parity):
self.num = num
self.colour = colour
self.parity = parity
# Determine the properties linked to each outcome on the dice.
zero = die_face(0, 'none', 'none')
one = die_face(1, 'blue', 'odd')
two = die_face(2, 'blue', 'even')
three = die_face(3, 'blue', 'odd')
four = die_face(4, 'green', 'even')
five = die_face(5, 'green', 'odd')
six = die_face(6, 'green', 'even')
options = [zero, one, two, three, four, five, six,]
class bet():
# Define the bets
def __init__(self, bet_type, odds):
self.bet_type = bet_type
self.odds = odds
num_bet = bet('num', 5)
colour_bet = bet('colour', 2)
parity_bet = bet('parity', 2)
class broker():
# Define the properties of the broker.
def __init__(self, name, balance):
self.name = name
self.balance = balance
def __str__(self):
result = "Name: {} \n" \
"Balance: {}" .format(self.name, self.balance)
return result
def modify_balance(self, amount):
self.balance += amount
main_broker = broker('Main',1e3)
def random_strategy():
# Bet a random amount on a random game with a random guess.
guess = 'empty'
game_mode= choice([num_bet, colour_bet, parity_bet])
if game_mode == num_bet:
guess = randint(0,6)
elif game_mode == colour_bet:
guess = choice(['blue','green'])
elif game_mode == parity_bet:
guess = choice(['even','odd'])
value = randint(1,10)
return game_mode , value, guess
class player():
# This class defines each player
def __init__(self, name, strategy, bank_roll):
self.name = name
self.strategy = strategy
self.bank_roll = bank_roll
def modify_balance(self, amount):
self.bank_roll += amount
def __str__(self):
result = "Name: {} \n" \
"Bank Roll: {}" .format(self.name, self.bank_roll)
return result
def play(self):
return self.strategy()
# Add the players
Will = player("Will",random_strategy,100)
def dealer(type, bet_value, guess):
#Roll the dice
correct = choice(options)
#Return amount based on Win or Lose
if type == num_bet.bet_type:
if correct.num == guess:
return num_bet.odds * bet_value - bet_value
else:
return -bet_value
if type == colour_bet.bet_type:
if correct.colour == guess:
return colour_bet.odds * bet_value - bet_value
else:
return -bet_value
if type == parity_bet.bet_type:
if correct.parity == guess:
return parity_bet.odds * bet_value - bet_value
else:
return -bet_value
def main_play(player):
# Collect the bets from the players
bets = player.play()
# Roll and return bets
amount = dealer(bets[0].bet_type, bets[1], bets[2])
# Distribute the money
main_broker.modify_balance(amount*-1)
player.modify_balance(amount)
print(player)
print(main_broker)
那麼這個經銷商和玩家會如何互動呢?它只是一個跟蹤經紀人交易的工具嗎? – shogun000 2015-01-15 16:45:01
投注表或棋盤就是執行一個遊戲並在遊戲過程中作爲資金的持有者。所以:1)投注,2)擲骰子,3)基於結果付出,重複。每個玩家都有一個賭注。經銷商下注0 - 它是支付的桌子,而不是經銷商。 (但莊家有特殊的規則可供玩。)所以投注表就是執行轉牌的地方。這使得穩定的狀態驅動的機器。 – Harvey 2015-01-15 18:35:00