954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Improve scrambled text game

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/2008/05/jumbled-words-game-downloads/

Any suggestions to improve the code are welcome. :idea:

The source code goes like:

# Author: Anurag Bhandari | Contact: anurag.bhd@gmail.com
#
# Program: Jumbled Words Game | Version: 1.0 | Licence: GPLv2
#
# It's sure an interesting thing to do in free time

import wx

# First, we define a dictionary of jumbled words
dict = {"special":"licapes", "verify":"rfvyie", "guava":"augav", "begin":"ngibe",
        "compression":"serniospmoc", "praying":"igyaprn", "linux":"uilxn",
        "opposite":"seopopit", "depricated": "cpdirdaete",
        "leonardo da vinci":"o draconian devil", "the mona lisa":"oh lame saint",
        "dreaming":"earimngd", "tormented":"nemortted", "flabbergasted": "tasedbgaberlaf",
        "meticuluos":"ecusoulmit", "rectify":"fitecry", "mobile":"ibelmo",
        "computer":"mertopcu", "granular":"lrgaruna", "metro":"torem",
        "outrageous":"gretsouauo", "nothing":"hotning", "appreciable":"celbepiapra", "vandalism":"misdanlav", "tropical":"ptorlica"}

def func():
    'This is the core function of the program'
    # The result of popitem() method is a tuple
    # The following is an example of "sequence unpacking"
    word, jumbled = dict.popitem()
    return word, jumbled
    
def guess(event):
    ans = input_word.GetValue()
    if(ans == query[0]):
        result.SetLabel(label="Congrats! You got it right.")
    else:
        result.SetLabel(label="Sorry, wrong answer. Better luck next time!")

def next(event):
    # After a person clicks the Start button for the first time, this will happen
    nextButton.SetLabel("Next")
    guessButton.Enable()
    hintButton.Enable()
    input_word.SetValue("")
    # Unless we define the variable "query" as global, the function "guess" won't be able to access it
    global query
    query = func()
    if(dict!={}):
        question.SetLabel(label="The jumbled word is: "+query[1])
        result.SetLabel(label="Waiting for your input...")
    else:
        question.SetLabel(label="Game Over!")
        result.SetLabel(label="Yup, the game is finally over!")
        guessButton.Disable()
        nextButton.Disable()
        hintButton.Disable()

def hint(event):
    input_word.SetValue(query[0])
        
app = wx.App()
# 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
splash_image = wx.Image("splash.bmp", wx.BITMAP_TYPE_BMP)
bmp = splash_image.ConvertToBitmap()
wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1)
wx.Yield()
win = wx.Frame(None, title="Jumbled Words Game", size=(460, 345))
win.SetIcon(wx.Icon('star.ico', wx.BITMAP_TYPE_ICO))
win.CenterOnScreen()
bkg = wx.Panel(win)
guessButton = wx.Button(bkg, label='Guess')
guessButton.Bind(wx.EVT_BUTTON, guess)
guessButton.SetDefault()
# Unless the player has pressed the Start button, the Guess button will be disabled
guessButton.Disable()
nextButton = wx.Button(bkg, label='Start')
nextButton.Bind(wx.EVT_BUTTON, next)
hintButton = wx.Button(bkg, label='Hint')
hintButton.Bind(wx.EVT_BUTTON, hint)
hintButton.Disable()
input_word = wx.TextCtrl(bkg)
question = wx.StaticText(bkg, label="Welcome to jumbled words game\nTotal words: 25", style=wx.ALIGN_CENTER)
# We define some stylish fonts for the welcome message / game questions
font = wx.Font(pointSize=18, family=wx.DECORATIVE, style=wx.NORMAL, weight=wx.NORMAL)
question.SetFont(font)
result = wx.StaticText(bkg, label="Waiting for the initial result...", style=wx.ALIGN_CENTER)
hbox = wx.BoxSizer()
hbox.Add(input_word, proportion=1, flag=wx.EXPAND)
hbox.Add(guessButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(nextButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(hintButton, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(question, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
vbox.Add(result, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()


Editor: Was moved to the main Python forum, to allow posting of improvements!

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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:

f = open("Words.txt")
for thing in f.split(":"):
    dictionary[thing[0]]=thing[1]
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

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. :)

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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')

Ene Uran
Posting Virtuoso
1,722 posts since Aug 2005
Reputation Points: 625
Solved Threads: 212
 

Ok, cosmetics are in; I'll note them too.

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

You can also let Python do the jumbling of the words and create the dictionary of word:jumble pairs:

# create a dictionary of random jumbled words for a game

import random

def jumble_word(word):
    # create a list of characters of the word
    char_list = list(word)
    # shuffle sequence in place
    random.shuffle(char_list)
    # joint to form a word again
    return "".join(char_list)

# add more words to the list as needed
words = ['armada', 'bubble', 'letter', 'banana', 'radio', 'hammer',
    'spark', 'pearl', 'target', 'zappy', 'zipper', 'organist',
    'kitchen', 'ruler', 'motorist', 'polar', 'garage', 'window']

# create a list of jumbled words
jumbles = []
for word in words:
    jumbles.append(jumble_word(word))

# create a dictionary from the two list
words_dict = dict(zip(words, jumbles))

print words_dict

"""
possible output -->
{'polar': 'aropl', 'target': 'rgaett', 'ruler': 'rleur',
'garage': 'eaaggr', 'bubble': 'beblub', 'window': 'nowdiw',
'pearl': 'aprel', 'zipper': 'pperzi', 'radio': 'idoar',
'motorist': 'rtotimos', 'letter': 'rettel', 'zappy': 'pzpay',
'armada': 'ardaam', 'spark': 'akprs', 'kitchen': 'theinck',
'hammer': 'mrameh', 'banana': 'bnanaa', 'organist': 'soiatgnr'}
"""
Ene Uran
Posting Virtuoso
1,722 posts since Aug 2005
Reputation Points: 625
Solved Threads: 212
 

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. :)

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 
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?

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Gribouillis
Posting Maven
Moderator
2,781 posts since Jul 2008
Reputation Points: 1,024
Solved Threads: 691
 
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.

Seems to me like a nice technique. It is steganography, right?

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 
Seems to me like a nice technique. It is steganography, right?

As far as I understand, steganography would be to change each byte of image file just one small amount, so the picture essentially looks the same. Comparing it with the original picture would reveal the secret message hidden in the small changes.So, I guess I can assume that md5 is not reversible, right?That is the idea, and the reason md5 is used for encrypting the password itself. Encryption of the text itself can be donw with a number of Python tools. Very simple one would be XOR encryption against the looped password. I think there is code snippet under Python for that. Check out: http://www.daniweb.com/code/snippet688.html

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Ok, I've updated the code with suggestions of forum members here.

Changelog:All words now read from a text file, so adding more words is easier
Words are jumbled automatically by the program (using the random module), instead of pre-defined dictionary of words and corresponding jumbled words
The main window is now colored and smaller in size
The program also displays the score of the player

Download the source-code attached with this post. To execute the program, simply double-click the JumbledGUI.pyw file, given that you have Python and wxPython runtimes already installed.

As of now, I am aware of only one problem in the program. The score system, though works fine normally, is a bit flawed. Try it to check the problem. Do tell me if you can think of a better way of implementing the score system.

Attachments JumbledWordsGame-Source.zip (45.48KB)
iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Ok, the score problem is solved now. I solved it by taking a :flag" variable. :)

So, rather than downloading the attachment in my previous post, download this updated one.

Attachments JumbledWordsGame-Source.zip (40.59KB)
iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Update:

The improved Jumbled Words Game is available for download in various forms:

http://anurag.granularproject.org/2008/09/improved-jumbled-words-game/

Thanks again to all the members here who helped me improve it. :)

iamgame
Newbie Poster
9 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You