def sort(monsterList): templist = [] for monster in range(len(monsterList)): rolled_monster = die.die(monsterList[monster].initiative) templist.append(rolled_monster) # Using attribute initiative; rolls die zipped_pairs = zip(templist, monsterList) sortedMonsters = [x for _, x in sorted(zipped_pairs)] # Sorts monsters in ascending order according to dice roll sortedMonsters.reverse() # Reverses to make list in Descending order return sortedMonsters
def attack(hero, list_of_monsters): #TODO Endurance to be change to Stamina to match other changes print("\nBATTLE ENTERED!") for monster in range(len(list_of_monsters)): go = True while go: print(f"{list_of_monsters[monster].name} Stamina: {list_of_monsters[monster].endurance}") if die.die(hero.attack) > die.die(list_of_monsters[monster].agility): # If Hero's attack is larger than monster's agility, inflict 1 dmg print("\nSuccesful roll! Monster takes 1 damage") list_of_monsters[monster].endurance -= 1 time.sleep(1) print(f"{list_of_monsters[monster].name} Stamina after attack: {list_of_monsters[monster].endurance}") if list_of_monsters[monster].endurance == 0: print(f"\n{list_of_monsters[monster].name} slain!") go = False else: print("\nAttack missed!\n") if die.die(list_of_monsters[monster].attack) > die.die(hero.agility): print("\nMonster attacks you for 1 damage!") hero.endurance -= 1 print(f"Health after attack {hero.endurance}") if hero.endurance == 0: go = False
from die import die import pygal # Create a d6. die = die() # Make some rolls , and store the 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) #Visualize the results. hist = pygal.Bar() hist.title = "Results of rolling one d6 1000 times." hist.x_labels = ['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')
import pygal from die import die die_1 = die() die_2 = die() results = [] for row_num in range(1000): result = die_1.roll() + die_2.roll() results.append(result) 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) hist = pygal.Bar() hist.title = 'Results of rolling two D6 dice 1000 times.' hist.x_labels = [ '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13' ] hist.x_title = 'Result' hist.y_title = 'Frequency of Result'
pygame.init() font = pygame.font.SysFont('Arial', 25) pygame.display.set_caption('Yahtzee') mainWindowSize = windowWidth, windowHeight = 170 + 150 * 5, 147 + 5 white = 255, 255, 255 grey = 205, 201, 201 black = 0, 0, 0 btnsize = 10, 10, 150, 50 screen = pygame.display.set_mode(mainWindowSize) screen.fill(white) #inicialize dice dice = [die.die(), die.die(), die.die(), die.die(), die.die()] roll = 1 for n in range(0, 5): dice[n].setLocation(170 + 150 * n, 5) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos #roll button click if (x < 140 and y < 60) and (x > 10 and y > 10): roll += 1 if roll > 3:
def attack(hero, list_of_monsters): #TODO Endurance to be change to Stamina to match other changes menu.clear() menu.printLogo() while list_of_monsters: input(f""" You charge heroically towards the {list_of_monsters[0].name}! Press any key to enter battle""") menu.clear() menu.printLogo() print(""" (User turn)""") menu.roll_dice() time.sleep(1) menu.clear() menu.printLogo() go = True shield_block = True while go: if die.die(hero.attack) > die.die(list_of_monsters[0].agility): if isinstance(hero, Heroes.Thief) and (random.randint(0, 100) <= 25): #Thief Special Ability print(""" As Thief you inflicted Critical Damage! Causing 2 Damage!""") list_of_monsters[0].endurance -= 2 else: # If Hero's attack is larger than monster's agility, inflict 1 dmg print(""" (User turn)""") print(f""" {list_of_monsters[0].name} health: {list_of_monsters[0].endurance} VS Your health: {hero.endurance}""") print(""" Successfull roll! Monster takes 1 damage Nice hit! """) list_of_monsters[0].endurance -= 1 time.sleep(2) if list_of_monsters[0].endurance <= 0: time.sleep(0.5) print(f""" {list_of_monsters[0].name} slain! GG WP""") del list_of_monsters[0] input(""" press any key to continue""") menu.clear() menu.printLogo() go = False break else: input(f""" The {list_of_monsters[0].name} swings at you! Press any key to defend yourself!""") menu.clear() menu.printLogo() print(""" (Enemy turn)""") menu.roll_dice() time.sleep(1) else: print(""" (User turn)""") print(""" Your attack missed!""") if die.die(list_of_monsters[0].attack) > die.die(hero.agility): #menu.clear() #menu.printLogo print(""" (Enemy turn)""") menu.roll_dice() time.sleep(2) menu.clear() menu.printLogo() print(f""" {list_of_monsters[0].name} health: {list_of_monsters[0].endurance} VS Your health: {hero.endurance}""") time.sleep(1) print(""" Monster attacks you for 1 damage!""") time.sleep(2) menu.clear() menu.printLogo() if isinstance(hero, Heroes.Knight) and shield_block == True: #Knight special abilitie print(""" First attack blocked by your shield!""") shield_block = False else: hero.endurance -= 1 print(f""" {list_of_monsters[0].name} health: {list_of_monsters[0].endurance} VS Your health: {hero.endurance}""") input(""" OUCH! That hurt... Press any key to start your turn""") menu.clear() menu.printLogo() print(""" (User turn)""") menu.roll_dice() time.sleep(1) menu.clear() menu.printLogo() elif list_of_monsters[0].endurance > 0: menu.clear() menu.printLogo() print(""" (Enemy turn)""") print(f""" {list_of_monsters[0].name} health: {list_of_monsters[0].endurance} VS Your health: {hero.endurance}""") print(""" Monster attack missed """) time.sleep(1.5) input(""" That was a close one! Press any key to start your turn""") menu.clear() menu.printLogo() print(""" (User turn)""") menu.roll_dice() time.sleep(1) menu.clear() menu.printLogo() if hero.endurance == 0: go = False
def handle_message(self, message, bot_handler): content = False text = message['content'] # detects help message if "help" in text: content = """ Name: Randy Synopsis: {curly braces define optional input} ------------ Coin; @randy {<times> }c{oins weight <heads>:<tails>:<clumsy>} Examples; long:- `@randy 20 coin weight 20:10:30`, short:- `@randy 20cw2:1:3` ------------ Dice; @randy {<times> }d{ice <faces>,<stop>,<step> weight <number> <load>:<rest>:<clumsy> <operand>} Examples; long:- `@randy 20 dice 16,24,2 weight 16 20:10:30 +89`, short:- `@randy 20d16,24,2w16 20:10:30+89` ------------ Times - the number of times the action will be performed, with dice all results will be automatically accumulated and a final result given. Coin - the command used to define a coin flip. Dice - the command used to define a dice roll. Faces - the number of faces you want the die to have, becomes dice start value when stop and step are defined, with a default of 1. Stop - defines what number to stop at when deciding the size of the dice. Both start and stop are inclusive so put the number you would expect on the dice. Step - defines what the increments on the dice should be. Weight - the command given to inform that you wish to edit the weightings. Number - the number on the dice you wish to load. This must be followed by a space and then the ratio else it will default to 2:1. Heads - the weighting you wish heads to have. Weightings should be integer ratios, larger numbers mean it is more likely lower is less likely. Load - the weighting you wish the load number to have. Tails - the weighting you wish tails to have. Rest - the weighting you wish the rest of the numbers to have. Clumsy - this is a weighting with a default of 0 that allows the potential for the item to be lost during operation. Operand - this defines an operand `+`, `-`, `/`, or `*` that you can perform upon the accumulated dice rolls. This is mostly useful for applying buffs or debuffs. """ # prints purpose elif "purpose" in text: content = "My purpose is as a random item generator, I provide coin flips and dice rolls upon request, try `help` to to learn my commands." # responds to null input so you can check it's listening elif text == "" or text == "?": content = "yes?" else: command = expression.expression() matches = command.get_message(text) if matches == None: matches = [] # detects coin input if len(matches) == 4: times = test.test(matches[0]) heads = test.test(matches[1]) tails = test.test(matches[2]) clumsy = test.test(matches[3]) operand = "" item = coin.coin() results = item.parseCoin(times, heads, tails, clumsy) slug = "You flipped a coin " + str( times.value) + " times, the result was:\n" # detects dice input elif len(matches) == 10: times = test.test(matches[0]) faces = test.test(matches[1]) stop = test.test(matches[2]) step = test.test(matches[3]) number = test.test(matches[4]) load = test.test(matches[5]) rest = test.test(matches[6]) clumsy = test.test(matches[7]) operand = matches[8] value = test.test(matches[9]) try: if stop.value is None: faces.ifExists(6) item = die.createSimpleDie(faces.value) else: faces.ifExists(1) stop.ifExists(6) step.ifExists(1) item = die.die(faces.value, stop.value, step.value) except TypeError as error: content = str(error) else: try: results = item.parseDice(times, faces, number, load, rest, clumsy) except TypeError as error: content = str(error) else: slug = "You rolled a D" + str(len( item.weights.keys())) + " " + str( times.value) + " times, the result was:\n" # catches any other kind of input else: content = "I'm sorry what was that? I don't understand.\nType `help` to see the commands I know." # parses output in message friendly way if content == False: content = "" acc = 0 for item in results: if isinstance(item, int): acc += item content += ", " + str(item) content = content[2:] content = slug + content if operand is None and len(matches) == 10: content += " = " + str(acc) elif operand is not None and len(operand) != 0: value.ifExists(1) try: if operand == "+": acc += value.value elif operand == "-": acc -= value.value elif operand == "*": acc *= value.value elif operand == "/": acc /= value.value except ZeroDivisionError: content = "divide by zero? how dumb do you think I am?!" else: content += " " + operand + " " + str( value.value) + " = " + str(acc) # sends completed message and ends bot_handler.send_reply(message, content) return
def test_roll_die(self): test_die = die.die(6, 6) random.seed(1) self.assertEqual(2, test_die.roll_die())
def test_get_number_of_sides(self): test_die = die.die(7, 6) self.assertEqual(7, test_die.get_number_of_sides())
def test_set_face_value(self): test_die = die.die(6, 6) test_die.set_face_value(10) self.assertEqual(10, test_die.get_face_value())
def test_get_face_value(self): test_die = die.die(6, 7) self.assertEqual(7, test_die.get_face_value())
def test_set_number_of_sides(self): test_die = die.die(6, 6) test_die.set_number_of_sides(10) self.assertEqual(10, test_die.get_number_of_sides())
def throws(): l = [] for i in range(10000): k = die(6) l.append(k) return average(l)
def test(): dice = [die(3), die(5)] print dice[0].getValue() dice[0].roll() print dice[0].getValue()