本文整理匯總了Python中die.Die.roll方法的典型用法代碼示例。如果您正苦於以下問題:Python Die.roll方法的具體用法?Python Die.roll怎麽用?Python Die.roll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類die.Die
的用法示例。
在下文中一共展示了Die.roll方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: DieRollTest
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
class DieRollTest(unittest.TestCase):
"""Test the functionality of the Die class' roll function."""
def setUp(self):
self.possible_values = [1, 2, 3, "Dog", "Cat", "Hippo"]
self.new_die = Die(self.possible_values)
def tearDown(self):
del self.possible_values
del self.new_die
def test_roll_once(self):
"""Roll the die once and ensure the returned value is in possibleValues"""
self.assertIn(self.new_die.roll(), self.possible_values,
"Rolled value was not in possibleValues of Die")
def test_rolled_value_changes(self):
"""Roll the die a number of times and make sure it changes value"""
holding_value = self.new_die.roll()
for i in range(10):
if self.new_die.roll() != holding_value:
self.assertTrue(True)
return
self.assertTrue(False, "Die Value did not change from Holding Value "
"for 10 rolls")
def test_currentValue_is_updated_to_rolled_value(self):
"""Make sure that the Die's currentValue is updated to match what is rolled."""
self.new_die.currentValue = 5
self.assertEqual(self.new_die.roll(), self.new_die.currentValue,
"Current Value was not different from rolled")
示例2: main
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
def main():
theDie = Die()
count = eval(input("How many times should I roll the die? "))
c1 = 0
c2 = 0
c3 = 0
c4 = 0
c5 = 0
c6 = 0
for i in range(count):
theDie.roll()
val = theDie.getFaceValue()
if val == 1:
c1 = c1 + 1
elif val == 2:
c2 = c2 + 1
elif val == 3:
c3 = c3 + 1
elif val == 4:
c4 = c4 + 1
elif val == 5:
c5 = c5 + 1
elif val == 6:
c6 = c6 + 1
else:
print ("Error - value of die was", val)
c1Percent = float(c1)/count * 100
c2Percent = float(c2)/count * 100
c3Percent = float(c3)/count * 100
c4Percent = float(c4)/count * 100
c5Percent = float(c5)/count * 100
c6Percent = float(c6)/count * 100
print ("Value 1 came up:", c1, "or a percentage of", c1Percent)
print ("Value 2 came up:", c2, "or a percentage of", c2Percent)
print ("Value 3 came up:", c3, "or a percentage of", c3Percent)
print ("Value 4 came up:", c4, "or a percentage of", c4Percent)
print ("Value 5 came up:", c5, "or a percentage of", c5Percent)
print ("Value 6 came up:", c6, "or a percentage of", c6Percent)
示例3: Player
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
class Player(object):
def __init__(self):
"""Has a pair of dice and an empty rolls list."""
self._die1 = Die()
self._die2 = Die()
self._rolls = []
def __str__(self):
"""Returns a string representation of the list of rolls."""
result = ""
for (v1, v2) in self._rolls:
result = result + str((v1, v2)) + " " + str(v1 + v2) + "\n"
return result
def getNumberOfRolls(self):
"""Returns the number of the rolls."""
return len(self._rolls)
def play(self):
"""Plays a game, saves the rolls for that game,
and returns True for a win and False for a loss."""
self._rolls = []
self._die1.roll()
self._die2.roll()
(v1, v2) = (self._die1.getValue(), self._die2.getValue())
self._rolls.append((v1, v2))
initialSum = v1 + v2
if initialSum in (2, 3, 12):
return False
elif initialSum in (7, 11):
return True
while True:
self._die1.roll()
self._die2.roll()
(v1, v2) = (self._die1.getValue(), self._die2.getValue())
self._rolls.append((v1, v2))
sum = v1 + v2
if sum == 7:
return False
elif sum == initialSum:
return True
示例4: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
from die import Die
import pygal
die_1 = Die()
die_2 = Die()
results = []
for roll_num in range(100):
results.append(die_1.roll() + die_2.roll())
frequencies = []
max_result = die_1.num_slides + die_2.num_slides
for value in range(2,die_1.num_slides+ die_2.num_slides):
frequencies.append(results.count(value))
hist = pygal.Bar()
hist.title = "Results of rolling two D6 1.000 times"
hist.x_labels = ['1','2','3','4','5','6','7','8','9','10','11','12']
hist.x_title = "Result"
hist.y_title = "Frequency of result"
hist.add("D6 + D6",frequencies)
hist.render_to_file('dice_visual.svg')
示例5: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
from die import Die
import pygal
"""Create a D6"""
die = Die()
"""Make some rolls, and store results in a list"""
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)
"""Analyze the results"""
frequencies = []
for value in range(1, die.num_sides + 1):
frequency = results.count(value)
frequencies.append(frequency)
#print(frequencies)
"""Visualize the results."""
hist = pygal.Bar()
hist.title = "Results of rolling on D6 1,000 times."""
hist.x_labels =['1', '2', '3', '4', '5', '6']
hist.x_title = "Result"
hist.y_title = "Frquency of Result"
hist.add('D6', frequencies)
hist.render_to_file('die_visual.svg')
示例6: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
import pygal
from die import Die
# Create a D6 and a D10.
die_1 = Die()
die_2 = Die(10)
# Make some rolls, and store results in a list.
results = []
for roll_num in range(50000):
result = die_1.roll() + die_2.roll()
results.append(result)
# Analyze the results.
frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result + 1):
frequency = results.count(value)
frequencies.append(frequency)
# print(frequencies)
# Visualize the results.
hist = pygal.Bar()
hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = Die.x_labels(2, max_result)
開發者ID:crystalDf,項目名稱:Python-Crash-Course-Chapter-15-Data-Visualization,代碼行數:33,代碼來源:different_dice_visual.py
示例7: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
import pygal
from die import Die
die1 = Die()
die2 = Die(10)
results = []
for roll_num in range(5000):
result = die1.roll() + die2.roll()
results.append(result)
frequencies = []
max_result = die1.num_sides + die2.num_sides
for value in range(2, max_result+1):
frequency = results.count(value)
frequencies.append(frequency)
# create a histogram
hist = pygal.Bar()
# histogram details/styling
hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = list(range(2, max_result+1))
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
# add a series of values to the chart
hist.add('D6 + D10', frequencies)
# render the chart to a SVG file
hist.render_to_file('diff_die_visual.svg')
示例8: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
#die_visual.py
import pygal
from die import Die
# Create a D6.
die = Die()
# Make some rolls, and store results in a list.
#results = []
#for roll_num in range(1000):
# result = die.roll()
# results.append(result)
results=[ die.roll() for result in range(1000) ]
# Analyze the results.
#frequencies = []
#for value in range(1, die.num_sides+1):
# frequency = results.count(value)
# frequencies.append(frequency)
frequencies = [ results.count(value) for value in range(1, die.num_sides+1) ]
#print(results)
print(frequencies)
# Visualize the results.
# A histogram is a bar chart showing how often certain results occur.
hist = pygal.Bar()
hist.title = "Results of rolling one D6 1000 times."
#hist.x_labels = ['1', '2', '3', '4', '5', '6']
hist.x_labels = [ num for num in range(1, 7) ]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
示例9: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
#die_visual.py
import pygal
from die import Die
# Create two D6 dice.
die_1 = Die()
die_2 = Die()
# Make some rolls, and store results in a list.
#results = []
#for roll_num in range(1000):
# result = die_1.roll() + die_2.roll()
# results.append(result)
results = [ die_1.roll() + die_2.roll() for roll_num in range(1000) ]
# Analyze the results.
#frequencies = []
max_results = die_1.num_sides + die_2.num_sides
#for value in range(2, max_results+1):
# frequency = results.count(value)
# frequencies.append(frequency)
frequencies = [ results.count(value) for value in range(2, max_results+1) ]
#print(results)
print(frequencies)
# Visualize the results.
# A histogram is a bar chart showing how often certain results occur.
hist = pygal.Bar()
hist.title = "Results of rolling two D6 1000 times."
#hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
示例10: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
import pygal
from die import Die
# 投骰子生成結果
num_sides = 6
die_1 = Die(num_sides)
die_2 = Die(num_sides)
results = [die_1.roll() + die_2.roll() for i in range(10000)]
# 統計每個點數出現的詞數
frequencies = [results.count(value) for value in range(2, (num_sides * 2) + 1)]
# 可視化統計結果(直方圖)
hist = pygal.Bar()
hist.title = "Results of rolling one D6 10000 times."
hist.x_labels = range(2, 13)
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
hist.add("D6+D6", frequencies)
hist.render_to_file("die_visual.svg")
print(frequencies)
示例11: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
from die import Die
import pygal
die = Die()
results = []
for roll_num in range(100):
results.append(die.roll())
print(results)
frequencies = []
for value in range(1,die.num_slides+1):
frequencies.append(results.count(value))
print(frequencies)
hist = pygal.Bar()
hist.title = "Results of rolling one D6 1.000 times"
hist.x_labals = ['1','2','3','4','5','6']
hist.x_title = "Result"
hist.y_title = "Frequency of result"
hist.add("D6",frequencies)
hist.render_to_file('die_visual.svg')
示例12: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
from die import Die
import pygal
die1 = Die()
die2 = Die()
results = []
for roll_num in range(1000):
result1 = die1.roll()
result2 = die2.roll()
results.append(result1 + result2)
frequencies = []
for value in range(2, die1.num_sides + die2.num_sides + 1):
frequency = results.count(value)
frequencies.append(frequency)
'''
count = 0
for i in range(len(frequencies)):
count += frequencies[i]
print(count)
'''
hist = pygal.Bar()
hist.title = "Results of rolling two d6 1000 times."
#hist.x_labels = ['1', '2', '3', '4', '5', '6']
hist.x_labels =[str(i) for i in range(2, 13)]
hist.x_title = "Result"
hist.y_title = "Frequency of result"
示例13: Die
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
"""Display the result of die rolls in this script."""
import pygal
from die import Die
# Create a two D6 die.
die1 = Die()
die2 = Die()
# die3 = Die()
# Make some rolls, and store results in a list.
results = []
for roll_num in range(10000):
result = die1.roll() * die2.roll()
results.append(result)
# Analyze the results.
frequencies = []
min1 = ((die1.num_sides + 1) - die1.num_sides)
min2 = ((die2.num_sides + 1) - die2.num_sides)
# min3 = ((die3.num_sides + 1) - die3.num_sides)
min_result = min1 + min2
max_result = die1.num_sides * die2.num_sides
for value in range(min_result, max_result+1):
frequency = results.count(value)
frequencies.append(frequency)
# Visualize the results.
hist = pygal.Bar()
示例14: __init__
# 需要導入模塊: from die import Die [as 別名]
# 或者: from die.Die import roll [as 別名]
class Game:
def __init__(self):
self.board = Board()
self.move_manager = MoveManager(self.board)
self.players = [PlayerMoveFirstPawn(p, self.board) for p in Players]
self.current = Players.black
self.die = Die()
self._retry_counter = 0
# save finishers
self.finishers = []
self.event_ready = threading.Event()
self.event_finished = threading.Event()
# Look to serialize canvas drawings
self.lock = threading.Lock()
# Thread for tk mainloop, this runs until the program gets exited
self.tk_thread = threading.Thread(target=self._tk_mainloop)
def start_tk_visualization(self):
self.tk_thread.start()
# Wait for completion of canvas initialization
self.event_ready.wait()
def next_move(self):
# while True:
# number = self.die.roll()
# self.players[self.current].move(number)
# if number is not 6:
# break
# print(self.current, "rolls again!")
number = self.die.roll()
return self._execute_move(number)
def _execute_move(self, number):
moves = self.move_manager.get_valid_moves(self.current, number)
if not self._let_player_execute_move(moves):
return False
# roll again when having max number of points
if number == MAX_DICE_NUMBER_OF_POINTS:
return True
if self._retry_counter == 0:
self._go_to_next_player()
return True
def _let_player_execute_move(self, moves):
move = self.players[self.current].choose_move(moves)
if move is not None:
self._retry_counter = 0
self.move_manager.perform_move(self.current, move)
if self.move_manager.check_if_finished(self.current):
assert self.current not in self.finishers
self.finishers.append(self.current)
if len(self.finishers) == len(Players):
return False
else:
if (self.move_manager.board.can_player_only_emerge(self.current) and
self._retry_counter < MAX_THROWS-1):
self._retry_counter += 1
else:
self._retry_counter = 0
return True
def update_canvas(self):
tasks = []
# iterate over every pawn
for player in Players:
for pawn in range(PAWN_COUNT):
tasks.append({'type': "move",
'data': (player, pawn, self.board.pawns[player][pawn][0],
self.board.pawns[player][pawn][2])})
# update every pawn on canvas
list(map(self.board_drawer.job_queue.put_nowait, tasks))
def _go_to_next_player(self):
assert len(self.finishers) < len(Players)
while True:
self.current = Players.next(self.current)
if self.current not in self.finishers:
break
def _tk_mainloop(self):
# print board on tk canvas
self.board_drawer = BoardDrawer()
# set ready event for main thread to continue initialization
self.event_ready.set()
# call mainloop()
self.board_drawer.show_board()
self.event_finished.wait()
#.........這裏部分代碼省略.........