def get_attribute_level(): initial_value = 10 die_12 = Die(12) score_of_12 = die_12.roll() die_4 = Die(4) score_of_4 = die_4.roll() change = floor(score_of_12 / score_of_4) return int(initial_value + change)
def get_attribute_level(): initial_value = 10 die_12 = Die(12) score_of_12 = die_12.roll() die_4 = Die(4) score_of_4 = die_4.roll() change = floor(score_of_12 / score_of_4) return int(initial_value + change)
def __init__(self, Name, hp_dice, base_attack, base_defense, xp_value, renown_value): self.Name=Name self.xp_value=xp_value self.renown_value=renown_value dice = Die() self.hpmax=dice.roll(hp_dice, 6) self.current_hp = self.hpmax self.base_attack=base_attack self.base_defense=base_defense self.equipment=[] self.equipped_items=[] self.gold=dice.roll(5, 6)
def attack(self): attack_value=self.base_attack for item in self.equipped_items: attack_value+=item.attack_value dice=Die() attack=dice.roll(attack_value, 6) return attack
def encounter(character_1, character_2): strength_modifier = calculate_attribute_modifier( character_1.get_strength(), character_2.get_strength()) skill_modifier = calculate_attribute_modifier(character_1.get_skill(), character_2.get_skill()) print "Stength modifier: %d" % strength_modifier print "Skill modifier: %d" % skill_modifier die = Die(6) die_score_1 = die.roll() die_score_2 = die.roll() print "Die score 1: %d, die score 2: %d" % (die_score_1, die_score_2) if die_score_1 == die_score_2: print "Tie!" else: if die_score_1 > die_score_2: print "Player 1 wins!" character_1.set_strength(character_1.get_strength() + strength_modifier) character_1.set_skill(character_1.get_skill() + skill_modifier) character_2.set_strength(character_2.get_strength() - strength_modifier) character_2.set_skill(character_2.get_skill() - skill_modifier) else: print "Player 2 wins!" character_1.set_strength(character_1.get_strength() - strength_modifier) character_1.set_skill(character_1.get_skill() - skill_modifier) character_2.set_strength(character_2.get_strength() + strength_modifier) character_2.set_skill(character_2.get_skill() + skill_modifier)
class YahtzeeGame: def __init__(self, num_of_dice: int = 5): self.num_of_dice = num_of_dice self.die = Die() def roll(self, num: int) -> [int]: '''Rolls num amount of dice''' return [self.die.roll() for i in range(num)]
def test_die(self): print("test_die") print( "Testing a six-sided die by creating and rolling. The result must be between 1 and 6 (inclusive)." ) die = Die(6) face_value = die.roll() self.assertGreater(face_value, 0) self.assertLessEqual(face_value, 6)
def fight(): from enemylist import enemylist # PICKS AN ENEMY FROM ENEMYLIST FILE opponent=random.choice(enemylist) print "You are fighting "+opponent.Name, "\n\tOpponent's max HP:", opponent.hpmax, "\n\tYour HP:", playerone.current_hp while opponent.current_hp > 0 or playerone.current_hp > 0: dice = Die() player_initiative = dice.roll(1, 21) opponent_initiative = dice.roll(1, 21) if player_initiative > opponent_initiative: print "Player goes first." player_attacks(playerone, opponent) if opponent.current_hp <= 0: won_fight(playerone, opponent) break enemy_attacks(opponent, playerone) if playerone.current_hp <= 0: playerone.current_hp = playerone.hpmax print "You were defeated. Returning to main menu" break print "Your hp: ", playerone.current_hp, "Enemy's hp: ", opponent.current_hp elif opponent_initiative>player_initiative: print "Opponent goes first." enemy_attacks(opponent, playerone) if playerone.current_hp <= 0: playerone.current_hp = playerone.hpmax print"You were defeated. Returning to main menu" player_attacks(playerone, opponent) if opponent.current_hp <= 0: won_fight(playerone, opponent) break print "Your hp: ", playerone.current_hp, "Enemy's hp: ", opponent.current_hp print "END OF TURN\n-----------------------------"
def __init__(self, name, level): self.level=level self.name=name die = Die() self.hpmax=die.roll(15, 6) self.current_hp = self.hpmax self.base_attack=3 self.base_defense=2 self.equipment=[] self.equipped_items=[] self.gold=50 self.xp=0 self.renown=0 self.next_levelup = 100
def encounter(character_1, character_2): strength_modifier = calculate_attribute_modifier( character_1.get_strength(), character_2.get_strength()) skill_modifier = calculate_attribute_modifier( character_1.get_skill(), character_2.get_skill()) print "Stength modifier: %d" % strength_modifier print "Skill modifier: %d" % skill_modifier die = Die(6) die_score_1 = die.roll() die_score_2 = die.roll() print "Die score 1: %d, die score 2: %d" % (die_score_1, die_score_2) if die_score_1 == die_score_2: print "Tie!" else: if die_score_1 > die_score_2: print "Player 1 wins!" character_1.set_strength(character_1.get_strength() + strength_modifier) character_1.set_skill(character_1.get_skill() + skill_modifier) character_2.set_strength(character_2.get_strength() - strength_modifier) character_2.set_skill(character_2.get_skill() - skill_modifier) else: print "Player 2 wins!" character_1.set_strength(character_1.get_strength() - strength_modifier) character_1.set_skill(character_1.get_skill() - skill_modifier) character_2.set_strength(character_2.get_strength() + strength_modifier) character_2.set_skill(character_2.get_skill() + skill_modifier)
from dice import Die from plotly.graph_objs import Bar, Layout from plotly import offline die = Die() results = [] for roll_num in range(1000): result = die.roll() results.append(result) frequencies = [] for value in range(1, die.num_sides + 1): frequency = results.count(value) frequencies.append(frequency) x_values = list(range(1, die.num_sides + 1)) data = [Bar(x=x_values, y=frequencies)] x_axis_config = {'title': 'Result'} y_axis_config = {'title': 'Frequency'} my_layout = Layout(title='My Graph', xaxis=x_axis_config, yaxis=y_axis_config) offline.plot({'data': data, 'layout': my_layout}, filename='d6.html')
class GameBoard: def __init__(self, num_players: int, player_names: List[str]): LOG.info("Call to GameBoard.__init__") self.red_deck = CardDeck( "People" ) # Are these the right color to category mappings? If we want to stick with colors, that's fine self.white_deck = CardDeck("Events") self.blue_deck = CardDeck("Places") self.green_deck = CardDeck("Independence Day") self.die = Die(num_sides=6) self.game_positions = GamePositions() self.players = [] self.current_player = None # It might be useful to have this property to easily access the player whose turn it is self.direction = "" # The direction the player has chosen to move (not yet sure what values this can take) self.pixel_to_position_scaling_factor = 78 # Multiple a game_position location (in matrix) by this number to get the pixel location equivalent self.pixel_to_position_offset = ( 12, 12 ) # add these x and y values to the scaled pixel location to get starting square (since it isn't in top left corner) colors = ['red', 'white', 'blue', 'green'] for player_num in range(0, num_players): self.players.append( Mover(name=player_names[player_num], mover_color=colors[player_num], start_pos_x=0, start_pos_y=0)) #self.GUI = GameBoardGUI() #self.GUI.render( # self, # self.players, # self.pixel_to_position_scaling_factor, # self.pixel_to_position_offset # ) def main_gameplay_loop_GUI(self): self.win_x = 829 self.win_y = 830 self.window = Tk() self.load = im.open('game_board.jpg') self.photoImage = ImageTk.PhotoImage(self.load) # Draw board (stationary) self.window.title("Trivial Purfuit") self.window.configure(background='black') self.canvas = Canvas(self.window, width=self.win_x, height=self.win_y) self.canvas.create_image(self.win_x / 2, self.win_y / 2, image=self.photoImage) self.canvas.grid() # make label self.label = Label( self.window, text="", ) self.label.grid(row=1, column=0) self.set_current_player(self.players[0]) # make buttons b = Button(self.window, text="Roll Die", padx=2, command=self.present_die_GUI) b.grid(row=3, column=0) b = Button(self.window, text="Forward", padx=2, command=self.set_start_direction_fwd) b.grid(row=4, column=1) b = Button(self.window, text="Reverse", padx=2, command=self.set_start_direction_rev) b.grid(row=4, column=0) # update label self.set_label_text( self.current_player.mover_color + " player's turn. Roll the die and select a direction to move!") # Draw movers self.draw_movers(self.players, self.pixel_to_position_scaling_factor, self.pixel_to_position_offset) self.window.mainloop() # not sure where this lives. Here? def set_start_direction_fwd(self): print('Setting direction to "Forward"') self.set_label_text('Setting direction to "Forward"') self.game_positions.start_direction = "fwd" self.move_player() def set_start_direction_rev(self): print('Setting direction to "Reverse"') self.set_label_text('Setting direction to "Reverse"') self.game_positions.start_direction = "rev" self.move_player() def move_player(self): new_x_pos, new_y_pos = self.game_positions.find_next_position( self.current_player.get_pos()[0], self.current_player.get_pos()[1], self.die.last_roll, self.players) self.current_player.update_pos(new_x_pos, new_y_pos) self.draw_movers(self.players, self.pixel_to_position_scaling_factor, self.pixel_to_position_offset) gp_type = self.game_positions.get_position_type(new_x_pos, new_y_pos) # Mapp 4 character game_positions to game_board position type type = GAME_POSITION_TYPE_MAP[gp_type] if type == 'center': colors = ['red', 'white', 'blue', 'green'] n = len(colors) - 1 i = random.randint(0, n) type = colors[i] if type != 'roll_again': card = self.draw_card_by_type(type) # card = self.MINIMAL_INCREMENT_draw_card_by_type(type) self.game_positions.render(self.players) self.display_question(card) self.ask_user_answer() answered_correct = self.display_answer(card) if (new_x_pos, new_y_pos ) in self.game_positions.get_headquarter_positions(): is_full = self.current_player.add_wedge(type) if is_full: self.report_end_of_game() # should be a conditional self.report_end_of_turn() def set_label_text(self, text): # make label #self.label.config(text=text) self.label['text'] = text def main_gameplay_loop(self): while True: for player in self.players: # I think this should be where the gui is incorporated self.take_turn(player) self.game_positions.render(self.players) self.GUI.render(self.players, self.pixel_to_position_scaling_factor, self.pixel_to_position_offset) def present_die(self): roll_amount = input( "Press Enter to roll the die. Type quit to exit game.") if roll_amount == 'quit': exit() else: value = self.die.roll() print("Die face value: ", value) return value def present_die_GUI(self): value = self.die.roll() self.set_label_text("Die face value: " + str(value)) print("Die face value: ", value) return value def take_turn(self, current_player: Mover): self.set_current_player(current_player) type = 'roll_again' answered_correct = False while type == 'roll_again' or answered_correct: self.game_positions.render(self.players) val = self.present_die() new_x_pos, new_y_pos = self.game_positions.find_next_position( current_player.get_pos()[0], current_player.get_pos()[1], val, self.players) current_player.update_pos(new_x_pos, new_y_pos) # Currently game_positions stores types of positions as 4 character stings gp_type = self.game_positions.get_position_type( new_x_pos, new_y_pos) # Mapp 4 character game_positions to game_board position type type = GAME_POSITION_TYPE_MAP[gp_type] if type == 'center': colors = ['red', 'white', 'blue', 'green'] n = len(colors) - 1 i = random.randint(0, n) type = colors[i] if type != 'roll_again': card = self.draw_card_by_type(type) #card = self.MINIMAL_INCREMENT_draw_card_by_type(type) self.game_positions.render(self.players) self.display_question(card) self.ask_user_answer() answered_correct = self.display_answer(card) # logic either needs to sit here to only add a wedge if it is isn't already owned OR let the mover worry about that (latter seems better) if (new_x_pos, new_y_pos ) in self.game_positions.get_headquarter_positions(): is_full = self.current_player.add_wedge(type) if is_full: self.report_end_of_game() # should be a conditional self.report_end_of_turn() return def display_question(self, card): self.set_label_text(card.type + " question: " + card.question) print(card.type, "question:", card.question) def ask_user_answer(self): pass # Ask user to press correct or incorrect button (enter logic to enable these buttons here) #input("Press Enter to see the answer.") def report_end_of_turn(self): input(self.current_player.name + ", your turn is now over. Press Enter to finish.") def report_end_of_game(self, winner): input(winner + " has won the game! Press Enter to finish.") self.end_game( ) # this call might better live outside of this method, like in the calling method (presumably the main gameplay loop) def display_answer(self, card): print("Answer:", card.answer) self.set_label_text("Answer: " + card.answer) #self.set_label_text( ''' val = input("Did " + self.current_player.name + " answer the question correctly? [y/n]\n") while val not in ['y', 'n']: val = input("Did " + self.current_player.name + " answer the question correctly? [y/n]\n") if val == 'y': return True if val == 'n': return False ''' def draw_board(self): # for target increment pass def set_current_player(self, player): print('It is ' + player.name + '\'s turn!') self.current_player = player def end_game( self ): # kick off the sequence of ending the game (proclaim the winner, etc) exit def MINIMAL_INCREMENT_draw_card_by_type(self, type): return Card("place_holder_type", "place_holder_question", "place_holder_answer", "easiest") def draw_card_by_type(self, type): # Move this logic to game board if type == "red": return self.red_deck.deal_card() if type == "white": return self.white_deck.deal_card() if type == "blue": return self.blue_deck.deal_card() if type == "green": return self.green_deck.deal_card() def draw_movers(self, players, pixel_to_position_scaling_factor: float, pixel_to_position_offset: tuple): for player in players: self.draw_mover(player, pixel_to_position_scaling_factor, pixel_to_position_offset) #self.window.update() def draw_mover(self, mover, pixel_to_position_scaling_factor: float, pixel_to_position_offset: tuple): self.canvas.create_oval( pixel_to_position_offset[0] + mover.curr_x_pos * pixel_to_position_scaling_factor, pixel_to_position_offset[1] + mover.curr_y_pos * pixel_to_position_scaling_factor, pixel_to_position_offset[0] + mover.curr_x_pos * pixel_to_position_scaling_factor + 25, pixel_to_position_offset[1] + mover.curr_y_pos * pixel_to_position_scaling_factor + 25, outline=mover.mover_color, fill='grey', width=2) for wedge in mover.wedges: if wedge == "red": start = 0 elif wedge == "yellow": start = 90 elif wedge == "green": start = 180 elif wedge == "blue": start = 270 self.canvas.create_arc( pixel_to_position_offset[0] + mover.curr_x_pos * pixel_to_position_scaling_factor, pixel_to_position_offset[1] + mover.curr_y_pos * pixel_to_position_scaling_factor, pixel_to_position_offset[0] + mover.curr_x_pos * pixel_to_position_scaling_factor + 40, pixel_to_position_offset[1] + mover.curr_y_pos * pixel_to_position_scaling_factor + 40, start=start, extent=90, outline=mover.mover_color, fill=wedge, width=2)
import pygal from dice import Die #创建一个6面骰子 D6 = Die() results = [] #投掷100次,并将结果保存到一个列表中 for roll_num in range(100): result = D6.roll() results.append(result) #分析结果,分析1-6个数字出现的次数 frequencies = [] for value in range(1, D6.num_sides + 1):#取左不取右 frequency = results.count(value) frequencies.append(frequency) #对上面得到的结果进行可视化 hist = pygal.Bar() #设置属性,注意这里是属性,不是方法函数 hist.title = 'Results of Rolling one D6 100 times' hist.x_labels = list(range(1, D6.num_sides+1)) hist.x_title = 'Result' hist.y_title = 'Frequence of Result' #传递值 hist.add('D6', frequencies)#D6设置标签
class Angry_dice: """ Class that controls the flow of an Angry Dice player. Arguments: stage = Whetherhich stage the player is currently in. die_a = The first Die object. die_b = The second Die object. cheating = Whether or not the player held a 6 (bool). """ def __init__(self): self.stage = 1 self.die_a = Die(["1", "2", "ANGRY", "4", "5", "6"]) self.die_b = Die(["1", "2", "ANGRY", "4", "5", "6"]) self.cheating = False def play(self): """Controls the actual flow of the game.""" input(""" Welcome to Angry Dice! Roll the two dice until you get thru the 3 Stages! Stage 1 you need to roll 1 & 2 Stage 2 you need to roll ANGRY & 4 Stage 3 you need to roll 5 & 6 You can lock a die needed for your current stage and just roll the other one, but beware! If you ever get 2 ANGRY's at once, you have to restart to Stage 1! Also, you can never lock a 6! That's cheating! To rol the dice, simply input the name of the die you want to roll. Their names are a and b. Press ENTER to start! """) self.cheating = self.roll_parse("ab") done = False while not done: self.print_hand() decision = input("Roll dice: ") self.cheating = self.roll_parse(decision) done = self.advance_check() self.print_hand() print("You've won! Calm down!") def roll_parse(self, string): """ Takes an input string and rolls the appropriate dice. Returns whether or not the player was cheating with that roll (bool). Arguments: string = an input string from the user. VALUES = values you're allowed to hold in each stage. """ VALUES = [["1", "2"], ["ANGRY", "4"], ["5"]] cheating = False if "a" not in string: self.die_a.held = True if self.die_a.value not in VALUES[self.stage - 1]: cheating = True else: self.die_a.held = False if "b" not in string: self.die_b.held = True if self.die_b.value not in VALUES[self.stage - 1]: cheating = True else: self.die_b.held = False if not self.die_a.held: self.die_a.roll() if not self.die_b.held: self.die_b.roll() return cheating def print_hand(self): """ Prints to dice currently held, and whether or not the player has cheated. Arguments: none """ if self.cheating: print("You're cheating!") print("until you reroll it!") print(""" You rolled: a = [ {} ] b = [ {} ] You are in Stage {} """.format(self.die_a, self.die_b, self.stage)) def advance_check(self): """Checks conditions of each stage.""" values = [self.die_a.value, self.die_b.value] if self.stage == 3: if not self.cheating and "5" in values and "6" in values: return True if self.stage == 2 and "ANGRY" in values and "4" in values: self.stage = 3 if self.stage == 1 and "1" in values and "2" in values: self.stage = 2 if self.die_a.value == self.die_b.value == "ANGRY": print("WOW, you're ANGRY!") self.stage = 1 return False
import pygal from dice import Die d1 = Die() d2 = Die() results = [] for roll_num in range(1000): result = d1.roll() * d2.roll() results.append(result) frequencies = [] max_result = d1.num_sides * d2.num_sides for value in range(1, max_result + 1): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = 'Result of rolling D6*D6 1000times' hist.x_title = 'Result' hist.y_title = 'Frequency of Result' hist.x_labels = list(range(1, max_result + 1)) hist.add('D6-D6', frequencies) hist.render_to_file('D6-D6.svg')
while active: print("Welcome to dice 2.0.") size = input("How many sides? (6/10/20/Custom): ").lower() while size not in size_list: size = input("How many sides? (6/10/20/Custom): ").lower() counter = 0 if size == 'custom': sides = int(input("Custom sides (0-100): ")) dice = Die(sides) elif size == '6': dice = dice_1 elif size == '10': dice = dice_10 else: dice = dice_20 number = int(input("How many would you like to roll at once? (1-20): ")) for _ in range(number): counter += dice.roll() print(f"Your roll is: {counter}") again = input("Roll again? (Y/N): ").lower() if again not in "ynYN": again = input("Roll again? (Y/N): ").lower() if again == 'y': continue elif again == 'n': active = False
def test_die_roll_in_range(self): die = Die(rnd.randint(1,101)) self.assertIn(die.roll(), range(1, die.sides+1))
class Player: def __init__(self, name): self.name = name self.round = 1 self.die1 = Die() self.die2 = Die() def roll_round(self): self.die1.roll() self.die2.roll() self.display_dice() query = input('Would you like to hold die A or B or press Enter to continue? ') if query.upper() == 'A': if self.die1.held == True: self.die1.held = False else: if self.die1.value != 6: self.die1.held = True else: print('You can not hold a 6.') elif query.upper() == 'B': if self.die2.held: self.die2.held = False else: if self.die2.value != 6: self.die2.held = True else: print('You can not hold a 6.') def display_dice(self): separator = ' | ' spaces = ' ' * int(len(self.die1.art[0]) / 2) print(spaces + "A" + spaces + separator + spaces + "B") print(self.die1.art[0] + separator + self.die2.art[0]) print(self.die1.art[1] + separator + self.die2.art[1]) print(self.die1.art[2] + separator + self.die2.art[2]) print(self.die1.art[3] + separator + self.die2.art[3]) print(self.die1.art[4] + separator + self.die2.art[4]) if self.die1.held: print(" HELD " + separator + spaces + " ") elif self.die2.held: print(" " + separator + " HELD ") def check_win(self): if self.die1.value == 3 and self.die2.value == 3: print("Why so angry? Go back to round One") self.round = 1 self.die1.held = False self.die2.held = False elif self.round == 1: if self.die1.value + self.die2.value == 3: print("Welcome to round 2, {}".format(self.name)) self.round = 2 self.die1.held = False self.die2.held = False elif self.round == 2: if self.die1.value == 3 and self.die2.value == 4 or self.die1.value == 4 and self.die2.value == 3: print("Welcome to round 3, {}".format(self.name)) self.round = 3 self.die1.held = False self.die2.held = False elif self.round == 3: if self.die1.value + self.die2.value == 11: print("You win, {}!".format(self.name)) quit()
import matplotlib.pyplot as plt from dice import Die d = Die() results = [] for roll_num in range(500): result = d.roll() results.append(result) frequencies = [] for value in range(1, d.num_sides + 1): frequency = results.count(value) frequencies.append(frequency) x_values = list(range(1, d.num_sides + 1)) y_values = frequencies plt.plot(x_values, y_values, linewidth=2) plt.title('Result of Rolling D6 500 times', fontsize=24) plt.xlabel('Result', fontsize=14) plt.ylabel('Frequency of Rusult', fontsize=14) plt.axis([0, 6, 0, 200]) plt.tick_params(axis='both', which='major', labelsize=14) plt.show()
For clarity, the listings in this section use the long form of for loops. If you’re comfortable using list comprehensions, try writing a comprehension for one or both of the loops in each of these programs. """ from plotly.graph_objs import Bar, Layout from plotly import offline from dice import Die #Create two D6 dices. die_1 = Die() die_2 = Die() #Make some rolls and store the results in a list results = [die_1.roll() + die_2.roll() for roll_num in range(1000)] max_result = die_1.roll() + die_2.roll() #Analyse the results frequencies = [results.count(value) for value in range(2, max_result + 1)] #Visualize the results. x_values = list(range(2, max_result + 1)) data = [Bar(x=x_values, y=frequencies)] x_axis_config = {'title': 'Result', 'dtick': 1} y_axis_config = {'title': 'Frequency of Result'} my_layout = Layout(title='Results of rolling two D6 dice 1000 times', xaxis=x_axis_config, yaxis=y_axis_config) offline.plot({
from plotly.graph_objs import Bar, Layout from plotly import offline from dice import Die # Create a 2 D8 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) # Analyze the results frequencies = [] max_result = die_1.num_sides * die_2.num_sides for value in range(1, max_result+1): frequency = results.count(value) frequencies.append(frequency) #Visualize the results x_values = list(range(1, max_result+1)) data = [Bar(x=x_values, y=frequencies)] x_axis_config = {'title': 'Result', 'dtick':1} y_axis_config = {'title': 'Frequency of Result'} my_layout = Layout(title='Results of multiplying the rolling two D6 1,000 times', xaxis = x_axis_config, yaxis = y_axis_config) offline.plot({'data': data, 'layout': my_layout}, filename='d6xd6.html')
class Test_Roll_Dice(unittest.TestCase): def setUp(self): self.die = Die(6) self.die_1 = Die(6) self.die_2 = Die(6) def test_six_sided_die(self): with patch('random.randint') as mocked_randint: mocked_randint.return_value = 3 self.assertEqual(self.die.roll(), 3) #test snake eyes and craps mocked_randint.return_value = 1 self.assertEqual( dice.checkSnakeEyes(self.die_1.roll(), self.die_2.roll()), "Snake Eyes!") self.assertEqual( dice.checkCraps(self.die_1.roll(), self.die_2.roll()), "Crapped out!") #test craps continued mocked_randint.return_value = 1 result_1 = self.die_1.roll() mocked_randint.return_value = 2 result_2 = self.die_2.roll() self.assertEqual(dice.checkCraps(result_1, result_2), "Crapped out!") mocked_randint.return_value = 6 self.assertEqual( dice.checkCraps(self.die_1.roll(), self.die_2.roll()), "Crapped out!") #test box cars mocked_randint.return_value = 6 self.assertEqual( dice.checkBoxCars(self.die_1.roll(), self.die_2.roll()), "Box Cars!")
from dice import Die import pygal die_1 = Die(8) die_2 = Die(8) results = [] for roll in range(1000): results.append(die_1.roll() + die_2.roll()) frequencies = [] max_result = die_1.sides + die_2.sides label = "" for value in range(2, max_result + 1): frequencies.append(results.count(value)) # label += str(value) print(frequencies) print(label) hist = pygal.Bar() hist.title = "Results of rolling two die 1000 times" hist.x_labels = [ '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16' ] hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add("Die", frequencies) hist.render_to_file("die_visual.svg")
from plotly.graph_objs import Bar, Layout from plotly import offline from dice import Die # Create a D6 and a D10 die_1 = Die() die_2 = Die() die_3 = Die() # Make some rolls, and store results in a list results = [] for roll_num in range(1000): result = die_1.roll() + die_2.roll() + die_3.roll() results.append(result) # Analyze the results frequencies = [] max_result = die_1.num_sides + die_2.num_sides + die_3.num_sides for value in range(3, max_result + 1): frequency = results.count(value) frequencies.append(frequency) #Visualize the results x_values = list(range(3, max_result + 1)) data = [Bar(x=x_values, y=frequencies)] x_axis_config = {'title': 'Result', 'dtick': 1} y_axis_config = {'title': 'Frequency of Result'} my_layout = Layout(title='Results of rolling three D6 1,000 times', xaxis=x_axis_config, yaxis=y_axis_config) offline.plot({'data': data, 'layout': my_layout}, filename='d6_d6_d6.html')
import pygal from dice import Die d7 =Die(7) d9 = Die(9) results = [] for roll_num in range(2000): result = d7.roll() + d9.roll() results.append(result) frequencies = [] max_result = d7.num_sides + d9.num_sides for value in range(2, max_result +1 ): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = 'Result of rolling D7 and D8 2000times' hist._x_title = 'Result' hist._y_title = 'Frequency of Result' hist.x_labels = list(range(2, max_result + 1)) hist.add('D7 and D9', frequencies) hist.render_to_file('D7 and D9.svg')
from plotly.graph_objs import Bar, Layout from plotly import offline from dice import Die # Create a 2 D6 dice die_1 = Die() die_2 = Die() # Make some rolls, and store results in a list results = [die_1.roll() * die_2.roll() for roll_num in range(1000)] # Analyze the results max_result = die_1.num_sides * die_2.num_sides frequencies = [results.count(value) for value in range(1, max_result + 1)] #Visualize the results x_values = list(range(1, max_result + 1)) data = [Bar(x=x_values, y=frequencies)] x_axis_config = {'title': 'Result', 'dtick': 1} y_axis_config = {'title': 'Frequency of Result'} my_layout = Layout( title='Results of multiplying the rolling two D6 1,000 times', xaxis=x_axis_config, yaxis=y_axis_config) offline.plot({'data': data, 'layout': my_layout}, filename='2d6xd6.html')
import pygal from dice import Die d1 = Die() d2 = Die() d3 = Die() results = [] for roll_num in range(1000): result = d1.roll() + d2.roll() + d3.roll() results.append(result) frequencies = [] max_result = d1.num_sides + d2.num_sides + d3.num_sides for value in range(3, max_result + 1): frequency = results.count(value) frequencies.append(frequency) hist = pygal.Bar() hist.title = 'Result of rolling Three D6 1000times' hist.x_title = 'Result' hist.y_title = 'Frequency of Result' hist.x_labels = list(range(3, max_result + 1)) hist.add('3D6', frequencies) hist.render_to_file('Three D6.svg')