DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Improve scrambled text game (http://www.daniweb.com/forums/thread145632.html)

iamgame Sep 13th, 2008 11:02 am
Re: Projects for the Beginner
 
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. :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!

Paul Thompson Sep 14th, 2008 5:54 am
Re: Improve scrambled text game
 
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]

iamgame Sep 14th, 2008 10:42 am
Re: Improve scrambled text game
 
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. :)

Ene Uran Sep 14th, 2008 12:53 pm
Re: Improve scrambled text game
 
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')

iamgame Sep 14th, 2008 5:39 pm
Re: Improve scrambled text game
 
Ok, cosmetics are in; I'll note them too.

Ene Uran Sep 14th, 2008 10:37 pm
Re: Improve scrambled text game
 
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'}
"""

iamgame Sep 15th, 2008 4:10 pm
Re: Improve scrambled text game
 
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. :)

vegaseat Sep 21st, 2008 12:39 pm
Re: Improve scrambled text game
 
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

iamgame Sep 21st, 2008 6:12 pm
Re: Improve scrambled text game
 
Quote:

Originally Posted by vegaseat (Post 696076)
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:

Quote:

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?

Gribouillis Sep 21st, 2008 6:40 pm
Re: Improve scrambled text game
 
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.


All times are GMT -4. The time now is 10:27 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC