Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- RED = [3,3,3,3,3,6]
- BLUE = [2,2,2,5,5,5]
- OLIVE = [1,4,4,4,4,4]
- DICE_NAMES = ["Red", "Blue", "Olive"]
- def init_die():
- siemenluku = int(input("Give a seed for the dice.\n"))
- random.seed(siemenluku)
- def roll(die):
- if die == 0:
- return RED[random.randint(0,5)]
- elif die == 1:
- return BLUE[random.randint(0,5)]
- elif die == 2:
- return OLIVE[random.randint(0,5)]
- def simulate_singles(die1, die2, rolls):
- w1 = 0
- w2 = 0
- d = 0
- i = 0
- p = 0
- while i < rolls:
- q = roll(die1)
- w = roll(die2)
- if q > w:
- w1 += 1
- elif q < w:
- w2 += 1
- elif q == w:
- d += 1
- i += 1
- return (w1, w2, d)
- def simulate_doubles(die1, die2, rolls):
- w1 = 0
- w2 = 0
- d = 0
- i = 0
- while i < rolls:
- q = roll(die1)
- r = roll(die1)
- w = roll(die2)
- t = roll(die2)
- if q + r > w + t:
- w1 += 1
- elif q + r < w + t:
- w2 += 1
- elif q + r == w + t:
- d += 1
- i += 1
- return w1, w2, d
- def simulate_and_print_result(die1, die2, rolls, simulation_function, header):
- wins1, wins2, draws = simulation_function(die1, die2, rolls)
- print(header)
- print("Player 1 used {:s} die and won {:d} times, so {:.1f}% of the rolls.".format(DICE_NAMES[die1],wins1,wins1/rolls*100))
- print("Player 2 used {:s} die and won {:d} times, so {:.1f}% of the rolls.".format(DICE_NAMES[die2],wins2,wins2/rolls*100))
- if draws != 0:
- print("{:d} draws, so {:.2f}% of the rolls.".format(draws, draws/rolls*100))
- def main():
- print("Welcome to a non-transitive dice simulation.")
- init_die()
- print("The dice:")
- print("{:d} for {:s}: {:}".format(0 ,DICE_NAMES[0], RED))
- print("{:d} for {:s}: {:}".format(1 ,DICE_NAMES[1], BLUE))
- print("{:d} for {:s}: {:}".format(2 ,DICE_NAMES[2], OLIVE))
- choice1 = int(input("Choose a die for player 1:\n"))
- choice2 = int(input("Choose a die for player 2:\n"))
- rolls = int(input("How many rolls to simulate?\n"))
- simulate_and_print_result(choice1, choice2, rolls, simulate_singles, "Singles:")
- simulate_and_print_result(choice1, choice2, rolls, simulate_doubles, "Doubles:")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement