本文整理汇总了Python中dice.Dice类的典型用法代码示例。如果您正苦于以下问题:Python Dice类的具体用法?Python Dice怎么用?Python Dice使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dice类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_double_roll
def test_double_roll(sides, rolls):
""" Check that the probability for the sum of two n-sided dice matches
the expected distribution.
"""
# Store the expected probabilities for the sum of two dice.
exp = {}
for x in range(2, 2*sides + 1):
exp[x] = prob_double_roll(x, sides)
# Create a dictionary to hold the tally for each outcome.
tally = {}
for key in exp:
tally[key] = 0
# Initialise the dice.
dice = Dice(sides)
# Roll two dice 'rolls' times.
for i in range(0, rolls):
# Sum the value of the two dice rolls.
roll_sum = dice.roll() + dice.roll()
# Increment the tally for the outcome.
tally[roll_sum] += 1
# Compute the probabilities and check with expected values.
for key in tally:
average = tally[key] / rolls
assert average == pytest.approx(exp[key], rel=1e-2)
示例2: end_turn
def end_turn(self):
for c in self.conditions.values():
if c['cond_type'] == 's':
r = None
if self.pc:
print '{}: save against {}.'.format(self, c['name'])
r = easyinput.input_int('saving throw')
else:
save_die = Dice.from_str('1d20')
r = save_die.roll()['total']
if r >= 10:
self.remove_condition(c['index'])
print '{} successfully saved against {}.'.format(self, c['name'])
else:
print '{} failed a save against {}.'.format(self, c['name'])
elif c['cond_type'] == 't' and c['end_type'] == 'e':
if c['duration'] <= 0:
self.remove_condition(c['index'])
else:
print '{} is still affected by {} ({} round{} left).'.format(self, c['name'], c['duration'], 's'[c['duration']==1:])
for r in self.recharges.values():
if r['used']:
if r['just_used']:
r['just_used'] = False
continue
# Roll to recharge
d = Dice.from_str('1d6')
n = d.roll()['total']
if n >= r['value']:
r['used'] = False
print '{} can use {} again!'.format(self, r['name'])
示例3: round
def round(player):
print "\nYour turn, %s." % player.name
dice = Dice()
roll1 = dice.first_roll()
roll2 = dice.second_roll(roll1)
roll3 = dice.third_roll(roll2)
player.cat_choice(roll3)
player.round += 1
示例4: __init__
def __init__(self, n, parent):
Frame.__init__(self, parent)
Dice.__init__(self, n)
self.loadimages()
self.n = n
self.labels = [Label(self) for i in range(self.n)]
for i in range(self.n):
self.setimage(i)
self.labels[i].pack(side="left")
示例5: __init__
class ChuckALuck:
def __init__(self):
self.dice = Dice(3)
self.score = 0
def play(self):
choice = int(input("Choose a number (0 to stop): "))
while choice != 0:
self.dice.rollall()
print("The roll:", self.dice)
matches = self.dice.count(choice)
self.score += matches if matches > 0 else -1
print("Current score:", self.score)
choice = int(input("Choose a number (0 to stop): "))
print("Thanks for playing.")
示例6: __init__
def __init__(self):
self.car_dice = Dice()
self.car_dice1 = Dice()
self.car_dice2 = Dice()
self.name = "Car"
#self.distance = 0
self.position = 0
示例7: __init__
def __init__(self, game, point_config='STD_CONFIG', uid=1, max_turns=3):
self.game = game
self.id = uid
self.turn = 0
self.points = Points(config=point_config)
self.dice = Dice()
self.max_turns = max_turns
示例8: __init__
def __init__(self, parent, hpRoll=10,
Str=12, Dex=12,
Con=12, Int=12,
Wis=12, Cha=12):
self.parent = parent
self.mbox = msgBox()
self.dice = Dice()
self.Str = Str
self.Dex = Dex
self.Con = Con
self.Int = Int
self.Wis = Wis
self.Cha = Cha
self.baseAttack = 2
self.baseDefense = 2
self.attackRoll
self.hp = hpRoll + bonus[str(Con)]
self.maxHP = self.hp
self.mp = baseMP + bonus[str(Int)]
self.maxMP = self.mp
self.xp = 0
self.xpForNextLevel = 1000
示例9: start
def start(self):
sleep(1)
print("Game is about to start!")
sleep(2)
while len(self.winners) < 1:
sleep(1)
print("---------- ROUND " + str(self.round) + " ----------")
for player in self.players:
results = []
for i in range(player.dice):
result = Dice.roll()
results.append(result)
if result == 6:
player.dice -= 1
print(player.name + " rolled the numbers " + str(results))
print(player.name + " has " + str(player.dice) + " dice remaining.")
if player.dice < 1:
self.winners.append(player)
self.round +=1
print("--------------------------")
print("Game Over!")
sleep(1)
for player in self.winners:
print(player.name + " has " + str(player.dice) + " remaining dice and won the game!")
示例10: __init__
class PokerApp:
def __init__(self, interface):
self.dice = Dice()
self.money = 100
self.interface = interface
def run(self):
while self.money >= 10 and self.interface.wantToPlay():
self.playRound()
self.interface.close()
def playRound(self):
self.money = self.money - 10
self.interface.setMoney(self.money)
self.doRolls()
result, score = self.dice.score()
self.interface.showResult(result, score)
self.money = self.money + score
self.interface.setMoney(self.money)
def doRolls(self):
self.dice.rollAll()
roll = 1
self.interface.setDice(self.dice.values())
toRoll = self.interface.chooseDice()
while roll < 3 and toRoll != []:
self.dice.roll(toRoll)
roll = roll + 1
self.interface.setDice(self.dice.values())
if roll < 3:
toRoll = self.interface.chooseDice()
示例11: Vehicle
class Vehicle(object):
def __init__(self):
self.vehicle_dice = Dice()
self.name = "Vehicle"
self.distance = 0
def run(self):
self.distance += self.vehicle_dice.get_value()
def get_distance(self):
return self.distance
示例12: play
def play(self):
self.dice = Dice()
while True:
try:
self.command_prompt()
except WrongCommandException:
print_message('wrongcommand')
except TurnEndException:
break
self.turn = 0
示例13: Bike
class Bike(Vehicle):
def __init__(self):
self.bike_dice = Dice()
self.name = "Bike"
#self.distance = 0
self.position = 0
def run(self):
# self.distance += self.motor_dice.get_value()
# self.distance += self.motor_dice1.get_value()
space = self.bike_dice.get_value()
# + self.car_dice1.get_value() + self.car_dice2.get_value()
return space
示例14: __init__
class PokerApp:
def __init__(self, interface):
self.dice = Dice()
self.money = 100
self.interface = interface
def processScore(self, score):
h = HighScores()
if h.isElgible(score):
nameentry = GraphWin("Name Entry", 200, 100)
entry = Entry(Point(50, 50), 10)
entry.draw(nameentry)
okbutton = Button(nameentry, Point(150, 50), 90, 50, "Save Name")
okbutton.activate()
while 1:
m = nameentry.getMouse()
if okbutton.clicked(m):
h.addToList(score, entry.getText())
nameentry.close()
return
def run(self):
while self.money >= 10:
result = self.interface.wantToPlay()
if result == "Roll Dice":
self.playRound()
elif result == "Help":
h = HelpScreen()
h.DoEvents()
elif result == "Quit":
self.processScore(self.money)
break
self.interface.close()
def playRound(self):
self.money = self.money - 10
self.interface.setMoney(self.money)
self.doRolls()
result, score = self.dice.score()
self.interface.showResult(result, score)
self.money = self.money + score
self.interface.setMoney(self.money)
def doRolls(self):
self.dice.rollAll()
roll = 1
self.interface.setDice(self.dice.values())
toRoll = self.interface.chooseDice()
while roll < 3 and toRoll != []:
self.dice.roll(toRoll)
roll = roll + 1
self.interface.setDice(self.dice.values())
if roll < 3:
toRoll = self.interface.chooseDice()
示例15: gen_siblings
def gen_siblings(self):
d = Dice()
roll = d.roll(1, 10)
self.relations = []
self.siblings = []
self.gender_table = []
self.gender_table.append('Male')
self.gender_table.append('Female')
self.read_file('lifepath/family/family_siblings.txt', self.relations)
little_num = 1
big_num= 1
if roll>0 and roll<8:
for i in range(0, roll):
gender = choice(self.gender_table)
first=self.contr.get_r_name('first', gender)
second=self.contr.get_r_name('second', gender)
last=self.contr.datasets['last name']
nick = self.contr.get_r_name('nick', gender)
name = first + ' ' + second + ' ' + nick + ' ' + last.get()
age=int(self.contr.datasets['age'].get())
relative_age = d.Roll(1,2)
if relative_age ==1:
age= age-little_num
little_num = little_num +1
else:
age= age + big_num
big_num = big_num +1
relation = choice(self.relations)
member = family_member(name, relation, gender, age)
self.siblings.append(member)
self.sibling_var.set('you have ' +str(roll) + ' siblings')
self.contr.datasets['siblings'] = self.siblings
else:
self.sibling_var.set('you are the only child')
member = family_member('you are the only child','none','none',0)
self.siblings.append(member)
self.contr.datasets['siblings'] = self.siblings