Improve scrambled text game

Thread Solved

Join Date: Sep 2008
Posts: 10
Reputation: iamgame is an unknown quantity at this point 
Solved Threads: 0
iamgame's Avatar
iamgame iamgame is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #1
Sep 13th, 2008
Anyway, sometime back, I wrote a little and simple GUI application (using wPython) which I call the Jumbled Words game. This was my first step into wxPython. Not a serious project at all, but hey, a nice project for a Python newbie like me at that time.

You can download it (Windows executable; setup/installer) from here:
http://anurag.granularproject.org/20...ame-downloads/

Any suggestions to improve the code are welcome.

The source code goes like:

  1. # Author: Anurag Bhandari | Contact: anurag.bhd@gmail.com
  2. #
  3. # Program: Jumbled Words Game | Version: 1.0 | Licence: GPLv2
  4. #
  5. # It's sure an interesting thing to do in free time
  6.  
  7. import wx
  8.  
  9. # First, we define a dictionary of jumbled words
  10. dict = {"special":"licapes", "verify":"rfvyie", "guava":"augav", "begin":"ngibe",
  11. "compression":"serniospmoc", "praying":"igyaprn", "linux":"uilxn",
  12. "opposite":"seopopit", "depricated": "cpdirdaete",
  13. "leonardo da vinci":"o draconian devil", "the mona lisa":"oh lame saint",
  14. "dreaming":"earimngd", "tormented":"nemortted", "flabbergasted": "tasedbgaberlaf",
  15. "meticuluos":"ecusoulmit", "rectify":"fitecry", "mobile":"ibelmo",
  16. "computer":"mertopcu", "granular":"lrgaruna", "metro":"torem",
  17. "outrageous":"gretsouauo", "nothing":"hotning", "appreciable":"celbepiapra", "vandalism":"misdanlav", "tropical":"ptorlica"}
  18.  
  19. def func():
  20. 'This is the core function of the program'
  21. # The result of popitem() method is a tuple
  22. # The following is an example of "sequence unpacking"
  23. word, jumbled = dict.popitem()
  24. return word, jumbled
  25.  
  26. def guess(event):
  27. ans = input_word.GetValue()
  28. if(ans == query[0]):
  29. result.SetLabel(label="Congrats! You got it right.")
  30. else:
  31. result.SetLabel(label="Sorry, wrong answer. Better luck next time!")
  32.  
  33. def next(event):
  34. # After a person clicks the Start button for the first time, this will happen
  35. nextButton.SetLabel("Next")
  36. guessButton.Enable()
  37. hintButton.Enable()
  38. input_word.SetValue("")
  39. # Unless we define the variable "query" as global, the function "guess" won't be able to access it
  40. global query
  41. query = func()
  42. if(dict!={}):
  43. question.SetLabel(label="The jumbled word is: "+query[1])
  44. result.SetLabel(label="Waiting for your input...")
  45. else:
  46. question.SetLabel(label="Game Over!")
  47. result.SetLabel(label="Yup, the game is finally over!")
  48. guessButton.Disable()
  49. nextButton.Disable()
  50. hintButton.Disable()
  51.  
  52. def hint(event):
  53. input_word.SetValue(query[0])
  54.  
  55. app = wx.App()
  56. # The definition of splash screen is done after an object for the wx.App class has been defined. Same applies to all the other widgets
  57. splash_image = wx.Image("splash.bmp", wx.BITMAP_TYPE_BMP)
  58. bmp = splash_image.ConvertToBitmap()
  59. wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1)
  60. wx.Yield()
  61. win = wx.Frame(None, title="Jumbled Words Game", size=(460, 345))
  62. win.SetIcon(wx.Icon('star.ico', wx.BITMAP_TYPE_ICO))
  63. win.CenterOnScreen()
  64. bkg = wx.Panel(win)
  65. guessButton = wx.Button(bkg, label='Guess')
  66. guessButton.Bind(wx.EVT_BUTTON, guess)
  67. guessButton.SetDefault()
  68. # Unless the player has pressed the Start button, the Guess button will be disabled
  69. guessButton.Disable()
  70. nextButton = wx.Button(bkg, label='Start')
  71. nextButton.Bind(wx.EVT_BUTTON, next)
  72. hintButton = wx.Button(bkg, label='Hint')
  73. hintButton.Bind(wx.EVT_BUTTON, hint)
  74. hintButton.Disable()
  75. input_word = wx.TextCtrl(bkg)
  76. question = wx.StaticText(bkg, label="Welcome to jumbled words game\nTotal words: 25", style=wx.ALIGN_CENTER)
  77. # We define some stylish fonts for the welcome message / game questions
  78. font = wx.Font(pointSize=18, family=wx.DECORATIVE, style=wx.NORMAL, weight=wx.NORMAL)
  79. question.SetFont(font)
  80. result = wx.StaticText(bkg, label="Waiting for the initial result...", style=wx.ALIGN_CENTER)
  81. hbox = wx.BoxSizer()
  82. hbox.Add(input_word, proportion=1, flag=wx.EXPAND)
  83. hbox.Add(guessButton, proportion=0, flag=wx.LEFT, border=5)
  84. hbox.Add(nextButton, proportion=0, flag=wx.LEFT, border=5)
  85. hbox.Add(hintButton, proportion=0, flag=wx.LEFT, border=5)
  86. vbox = wx.BoxSizer(wx.VERTICAL)
  87. vbox.Add(question, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  88. vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
  89. vbox.Add(result, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
  90. bkg.SetSizer(vbox)
  91. win.Show()
  92. app.MainLoop()
Editor: Was moved to the main Python forum, to allow posting of improvements!
Last edited by vegaseat; Sep 13th, 2008 at 6:53 pm. Reason: was moved
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 893
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 143
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Improve scrambled text game

 
0
  #2
Sep 14th, 2008
It would be cool if you could have a text file full of all of the words so the user could add their own.

Your text file could look like this:
Python:yothnp
spam:apms

This was the user could add their own and you could just import it to a dictionary at the start like:
  1. f = open("Words.txt")
  2. for thing in f.split(":"):
  3. dictionary[thing[0]]=thing[1]
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 10
Reputation: iamgame is an unknown quantity at this point 
Solved Threads: 0
iamgame's Avatar
iamgame iamgame is offline Offline
Newbie Poster

Re: Improve scrambled text game

 
0
  #3
Sep 14th, 2008
I was thinking on the same lines, Paul. Thanks for the suggestion and the code.

I'll implement this in the program as soon as I can and post an update here after that.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Improve scrambled text game

 
0
  #4
Sep 14th, 2008
Nice game!
Just a few cosmetic things, there is a lot of empty space, so I would set the height of the frame to 180, and also use a little color with:
bkg.SetBackgroundColour('yellow')
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 10
Reputation: iamgame is an unknown quantity at this point 
Solved Threads: 0
iamgame's Avatar
iamgame iamgame is offline Offline
Newbie Poster

Re: Improve scrambled text game

 
0
  #5
Sep 14th, 2008
Ok, cosmetics are in; I'll note them too.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Improve scrambled text game

 
0
  #6
Sep 14th, 2008
You can also let Python do the jumbling of the words and create the dictionary of word:jumble pairs:
  1. # create a dictionary of random jumbled words for a game
  2.  
  3. import random
  4.  
  5. def jumble_word(word):
  6. # create a list of characters of the word
  7. char_list = list(word)
  8. # shuffle sequence in place
  9. random.shuffle(char_list)
  10. # joint to form a word again
  11. return "".join(char_list)
  12.  
  13. # add more words to the list as needed
  14. words = ['armada', 'bubble', 'letter', 'banana', 'radio', 'hammer',
  15. 'spark', 'pearl', 'target', 'zappy', 'zipper', 'organist',
  16. 'kitchen', 'ruler', 'motorist', 'polar', 'garage', 'window']
  17.  
  18. # create a list of jumbled words
  19. jumbles = []
  20. for word in words:
  21. jumbles.append(jumble_word(word))
  22.  
  23. # create a dictionary from the two list
  24. words_dict = dict(zip(words, jumbles))
  25.  
  26. print words_dict
  27.  
  28. """
  29. possible output -->
  30. {'polar': 'aropl', 'target': 'rgaett', 'ruler': 'rleur',
  31. 'garage': 'eaaggr', 'bubble': 'beblub', 'window': 'nowdiw',
  32. 'pearl': 'aprel', 'zipper': 'pperzi', 'radio': 'idoar',
  33. 'motorist': 'rtotimos', 'letter': 'rettel', 'zappy': 'pzpay',
  34. 'armada': 'ardaam', 'spark': 'akprs', 'kitchen': 'theinck',
  35. 'hammer': 'mrameh', 'banana': 'bnanaa', 'organist': 'soiatgnr'}
  36. """
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 10
Reputation: iamgame is an unknown quantity at this point 
Solved Threads: 0
iamgame's Avatar
iamgame iamgame is offline Offline
Newbie Poster

Re: Improve scrambled text game

 
0
  #7
Sep 15th, 2008
Automatic jumbling seems like a really nice idea. I wanted to do that earlier also, but had no clue on how to begin.

The module "random" seems an interesting lot to play with.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Improve scrambled text game

 
0
  #8
Sep 21st, 2008
I think at one time you asked about module MD5 to send on a password, you might want to take a look at this post:
http://www.daniweb.com/forums/post220840-56.html
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 10
Reputation: iamgame is an unknown quantity at this point 
Solved Threads: 0
iamgame's Avatar
iamgame iamgame is offline Offline
Newbie Poster

Re: Improve scrambled text game

 
0
  #9
Sep 21st, 2008
Originally Posted by vegaseat View Post
I think at one time you asked about module MD5 to send on a password, you might want to take a look at this post:
http://www.daniweb.com/forums/post220840-56.html
Well yes, I did ask about md5 once and the question was something like "is it possible to reverse md5 hashes to original text?". Now, what I know is that this isn't possible, but you wrote about "decoding" the password encrypted using md5 in one of your posts:

With more and more folks thinking it their business to snoop around in your e-mail, write a little Python program to encode regular text. It is assumed that you will send this text to a friend, who has Python installed and can run a program you send beforehand to decode the encrypted text (after using the correct password of course). Use Python module md5 to encrypt the password too.
Now, as we can see in the program (http://www.daniweb.com/forums/post220840-56.html), the person in not actually decoding the md5 hash, but is comparing his own "md5"ed input with the md5 hash generated by the other person at first place.

So, I guess I can assume that md5 is not reversible, right?
Last edited by iamgame; Sep 21st, 2008 at 6:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 888
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 209
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Practically a Posting Shark

Re: Improve scrambled text game

 
0
  #10
Sep 21st, 2008
An alternative to using a password to communicate with your friend is to send them a picture of your favorite pet and to use the sha128 digest of the picture as a key, together with a standard encryption algorithm (RSA or a better one). This way, only the people with your pet's picture can decipher your messages, which is quite secure I think.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC