Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import random
  2.  
  3. RED = [3,3,3,3,3,6]
  4. BLUE = [2,2,2,5,5,5]
  5. OLIVE = [1,4,4,4,4,4]
  6. DICE_NAMES = ["Red", "Blue", "Olive"]
  7.  
  8. def init_die():
  9. siemenluku = int(input("Give a seed for the dice.\n"))
  10. random.seed(siemenluku)
  11.  
  12. def roll(die):
  13. if die == 0:
  14. return RED[random.randint(0,5)]
  15. elif die == 1:
  16. return BLUE[random.randint(0,5)]
  17. elif die == 2:
  18. return OLIVE[random.randint(0,5)]
  19.  
  20. def simulate_singles(die1, die2, rolls):
  21. w1 = 0
  22. w2 = 0
  23. d = 0
  24. i = 0
  25. p = 0
  26. while i < rolls:
  27. q = roll(die1)
  28. w = roll(die2)
  29. if q > w:
  30. w1 += 1
  31. elif q < w:
  32. w2 += 1
  33. elif q == w:
  34. d += 1
  35. i += 1
  36. return (w1, w2, d)
  37.  
  38. def simulate_doubles(die1, die2, rolls):
  39. w1 = 0
  40. w2 = 0
  41. d = 0
  42. i = 0
  43. while i < rolls:
  44. q = roll(die1)
  45. r = roll(die1)
  46. w = roll(die2)
  47. t = roll(die2)
  48. if q + r > w + t:
  49. w1 += 1
  50. elif q + r < w + t:
  51. w2 += 1
  52. elif q + r == w + t:
  53. d += 1
  54. i += 1
  55. return w1, w2, d
  56.  
  57. def simulate_and_print_result(die1, die2, rolls, simulation_function, header):
  58. wins1, wins2, draws = simulation_function(die1, die2, rolls)
  59. print(header)
  60. print("Player 1 used {:s} die and won {:d} times, so {:.1f}% of the rolls.".format(DICE_NAMES[die1],wins1,wins1/rolls*100))
  61. print("Player 2 used {:s} die and won {:d} times, so {:.1f}% of the rolls.".format(DICE_NAMES[die2],wins2,wins2/rolls*100))
  62. if draws != 0:
  63. print("{:d} draws, so {:.2f}% of the rolls.".format(draws, draws/rolls*100))
  64.  
  65. def main():
  66. print("Welcome to a non-transitive dice simulation.")
  67. init_die()
  68. print("The dice:")
  69. print("{:d} for {:s}: {:}".format(0 ,DICE_NAMES[0], RED))
  70. print("{:d} for {:s}: {:}".format(1 ,DICE_NAMES[1], BLUE))
  71. print("{:d} for {:s}: {:}".format(2 ,DICE_NAMES[2], OLIVE))
  72. choice1 = int(input("Choose a die for player 1:\n"))
  73. choice2 = int(input("Choose a die for player 2:\n"))
  74. rolls = int(input("How many rolls to simulate?\n"))
  75. simulate_and_print_result(choice1, choice2, rolls, simulate_singles, "Singles:")
  76. simulate_and_print_result(choice1, choice2, rolls, simulate_doubles, "Doubles:")
  77. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement