User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 374,570 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,525 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Feb 19th, 2008
Views: 1,267
Hi all,

Have you ever heard of "Petals Around the Rose?" It's a logic puzzle. The way it works is, I roll five dice, then tell you what the "score" is for this round. I can repeat this for you as many times as you want. It's your job to figure out the algorithm that connects the roll of the dice to the score, and then determine the score for yourself on new rolls. There are three hints:

(i) the name of the puzzle is significant,
(ii) the answer will always be an even positive number or zero, and
(iii) people who know the secret are supposed to keep quiet about it!

So what we have here is an implementation of Petals Around the Rose, written using Tkinter. You click a button to roll the dice, which display in a canvas, along with the answer. Can you figure out the mystery?

I mean, you could just look at the code, but that would be cheating! Try to discern the pattern for yourself first by running the program, then peek under the hood.
Last edited : Feb 19th, 2008.
python Syntax
  1. #!/usr/bin/python
  2. # petals.py is an implementation of Petals Around the Rose
  3. # -----------------------------------------------------------------------------
  4. import sys, random
  5. from Tkinter import *
  6. # -----------------------------------------------------------------------------
  7. # GLOBALS
  8. # The title and version
  9. title, version = "Petals Around the Rose", "v1.0"
  10.  
  11. # Lists of the possible faces of the dice and the colors
  12. faces = [ 1, 2, 3, 4, 5, 6 ]
  13. colors = [ ("#A54","#721"), ("#4A5","#172"), ("#45A","#127") ]
  14.  
  15. # A dictionary which maps die faces to dot configurations (offsets)
  16. faces_to_dots = { 1:[(23,23)],
  17. 2:[(12,12), (33,33)],
  18. 3:[(23,23), (10,10), (36,36)],
  19. 4:[(12,12), (33,33), (12,33), (33,12)],
  20. 5:[(23,23), (10,10), (36,36), (10,36), (36,10)],
  21. 6:[(12,12), (33,33), (12,33), (33,12), (12,22), (33,22)] }
  22.  
  23. # -----------------------------------------------------------------------------
  24. # CLASSES
  25. # The PetalEngine class is the application class
  26. class PetalEngine:
  27.  
  28. # Instance variables
  29. # root is the Tkinter root
  30. # frame is the main frame of the application
  31. # canvas is the canvas on which the roll results are drawn
  32. # answer is a string variable used to display the solution to the roll
  33.  
  34. # Initialize new PetalEngines
  35. def __init__(self, root):
  36. self.root = root
  37. self.root.title(title+" "+version)
  38. self.root.resizable(0, 0)
  39. self.make_widgets()
  40. self.root.mainloop()
  41.  
  42. # Make and bind all Tkinter widgets
  43. def make_widgets(self):
  44.  
  45. # Make the main frame
  46. self.frame = Frame(self.root)
  47. self.frame.pack()
  48.  
  49. # Make the screen widget
  50. frame = Frame(self.frame)
  51. label = Label(frame, text="Roll Outcome")
  52. self.canvas = Canvas(frame, width=350, height=70)
  53. frame.pack(); label.pack(); self.canvas.pack()
  54.  
  55. # Make the answer widget
  56. frame = Frame(self.frame)
  57. self.answer = StringVar()
  58. self.answer.set("The answer for this roll is: ")
  59. entry = Entry(frame, textvariable=self.answer)
  60. entry.config(state=DISABLED)
  61. frame.pack(fill="x", expand=True); entry.pack(fill="x")
  62.  
  63. # Make the roller widget
  64. frame = Frame(self.frame)
  65. button = Button(frame, text="Roll Dice", command=self.roll)
  66. frame.pack(fill="x", expand=True); button.pack(fill="x")
  67.  
  68. # Roll the dice
  69. def roll(self):
  70.  
  71. # Pick the dice (faces and colors)
  72. dice = []
  73. for die in range(5):
  74. dice.append((random.sample(faces, 1)[0],
  75. random.sample(colors, 1)[0]))
  76.  
  77. # Print them to the screen
  78. for die in range(len(dice)):
  79. self.show_die(dice[die], 10+die*70)
  80.  
  81. # Determine and print the answer
  82. solution = 0
  83. for die in dice:
  84. if die[0]%2 == 1: solution += (die[0]-1)
  85. self.answer.set("The answer for this roll is: "+str(solution))
  86.  
  87. # Print a die on the screen
  88. def show_die(self, die, x_coordinate):
  89. x1, y1, x2, y2 = x_coordinate, 10, x_coordinate+50, 60
  90. self.canvas.create_rectangle(x1, y1, x2, y2, fill=die[1][1])
  91. self.canvas.create_rectangle(x1+5, y1+5, x2-5, y2-5, fill=die[1][0])
  92. for dot in faces_to_dots[die[0]]:
  93. self.canvas.create_oval(x1+dot[0], y1+dot[1],
  94. x1+dot[0]+5, y1+dot[1]+5, fill="white")
  95.  
  96. # -----------------------------------------------------------------------------
  97. # FUNCTIONS
  98. # Main function
  99. def main(args):
  100. pe = PetalEngine(Tk())
  101. sys.exit(0)
  102.  
  103. # -----------------------------------------------------------------------------
  104. # The following code is executed upon command-line invocation
  105. if __name__ == "__main__": main(sys.argv)
  106.  
  107. # -----------------------------------------------------------------------------
  108. # EOF
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC