]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/hangman.py
wxHandleFatalExceptions() added, documented
[wxWidgets.git] / utils / wxPython / demo / hangman.py
1 """Hangman.py, a simple wxPython game, inspired by the
2 old bsd game by Ken Arnold.
3 From the original man page:
4
5 In hangman, the computer picks a word from the on-line
6 word list and you must try to guess it. The computer
7 keeps track of which letters have been guessed and how
8 many wrong guesses you have made on the screen in a
9 graphic fashion.
10
11 That says it all, doesn't it?
12
13 Have fun with it,
14
15 Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)"""
16
17 import random,re,string
18 from wxPython.wx import *
19
20
21
22 class WordFetcher:
23 builtin_words = ' albatros banana electrometer eggshell'
24
25 def __init__(self, filename, min_length = 5):
26 self.min_length = min_length
27 print "Trying to open file %s" % (filename,)
28 try:
29 f = open(filename, "r")
30 except:
31 print "Couldn't open dictionary file %s, using builtins" % (filename,)
32 self.words = self.builtin_words
33 self.filename = None
34 return
35 self.words = f.read()
36 self.filename = filename
37 print "Got %d bytes." % (len(self.words),)
38
39 def SetMinLength(min_length):
40 self.min_length = min_length
41
42 def Get(self):
43 reg = re.compile('\s+([a-zA-Z]+)\s+')
44 n = 50 # safety valve; maximum number of tries to find a suitable word
45 while n:
46 index = int(random.random()*len(self.words))
47 m = reg.search(self.words[index:])
48 if m and len(m.groups()[0]) >= self.min_length: break
49 n = n - 1
50 if n: return string.lower(m.groups()[0])
51 return "error"
52
53
54
55 def stdprint(x):
56 print x
57
58
59
60 class URLWordFetcher(WordFetcher):
61 def __init__(self, url):
62 self.OpenURL(url)
63 WordFetcher.__init__(self, "hangman_dict.txt")
64
65 def logprint(self,x):
66 print x
67
68 def RetrieveAsFile(self, host, path=''):
69 from httplib import HTTP
70 try:
71 h = HTTP(host)
72 except:
73 self.logprint("Failed to create HTTP connection to %s... is the network available?" % (host))
74 return None
75 h.putrequest('GET',path)
76 h.putheader('Accept','text/html')
77 h.putheader('Accept','text/plain')
78 h.endheaders()
79 errcode, errmsg, headers = h.getreply()
80 if errcode != 200:
81 self.logprint("HTTP error code %d: %s" % (errcode, errmsg))
82 return None
83 f = h.getfile()
84 return f
85
86 def OpenURL(self,url):
87 from htmllib import HTMLParser
88 import formatter
89 self.url = url
90 m = re.match('http://([^/]+)(/\S*)\s*', url)
91 if m:
92 host = m.groups()[0]
93 path = m.groups()[1]
94 else:
95 m = re.match('http://(\S+)\s*', url)
96 if not m:
97 # Invalid URL
98 self.logprint("Invalid or unsupported URL: %s" % (url))
99 return
100 host = m.groups()[0]
101 path = ''
102 f = self.RetrieveAsFile(host,path)
103 if not f:
104 self.logprint("Could not open %s" % (url))
105 return
106 self.logprint("Receiving data...")
107 data = f.read()
108 tmp = open('hangman_dict.txt','w')
109 fmt = formatter.AbstractFormatter(formatter.DumbWriter(tmp))
110 p = HTMLParser(fmt)
111 self.logprint("Parsing data...")
112 p.feed(data)
113 p.close()
114 tmp.close()
115
116
117
118 class HangmanWnd(wxWindow):
119 def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize):
120 wxWindow.__init__(self, parent, id, pos, size)
121 self.SetBackgroundColour(wxNamedColour('white'))
122 if wxPlatform == '__WXGTK__':
123 self.font = wxFont(12, wxMODERN, wxNORMAL, wxNORMAL)
124 else:
125 self.font = wxFont(10, wxMODERN, wxNORMAL, wxNORMAL)
126 self.SetFocus()
127
128 def StartGame(self, word):
129 self.word = word
130 self.guess = []
131 self.tries = 0
132 self.misses = 0
133 self.Draw()
134
135 def EndGame(self):
136 self.misses = 7;
137 self.guess = map(chr, range(ord('a'),ord('z')+1))
138 self.Draw()
139
140 def HandleKey(self, key):
141 self.message = ""
142 if self.guess.count(key):
143 self.message = 'Already guessed %s' % (key,)
144 return 0
145 self.guess.append(key)
146 self.guess.sort()
147 self.tries = self.tries+1
148 if not key in self.word:
149 self.misses = self.misses+1
150 if self.misses == 7:
151 self.EndGame()
152 return 1
153 has_won = 1
154 for letter in self.word:
155 if not self.guess.count(letter):
156 has_won = 0
157 break
158 if has_won:
159 self.Draw()
160 return 2
161 self.Draw()
162 return 0
163
164 def Draw(self, dc = None):
165 if not dc:
166 dc = wxClientDC(self)
167 dc.SetFont(self.font)
168 dc.Clear()
169 (x,y) = self.GetSizeTuple()
170 x1 = x-200; y1 = 20
171 for letter in self.word:
172 if self.guess.count(letter):
173 dc.DrawText(letter, x1, y1)
174 else:
175 dc.DrawText('.', x1, y1)
176 x1 = x1 + 10
177 x1 = x-200
178 dc.DrawText("tries %d misses %d" % (self.tries,self.misses),x1,50)
179 guesses = ""
180 for letter in self.guess:
181 guesses = guesses + letter
182 dc.DrawText("guessed:", x1, 70)
183 dc.DrawText(guesses[:13], x1+80, 70)
184 dc.DrawText(guesses[13:], x1+80, 90)
185 dc.SetUserScale(x/1000.0, y/1000.0)
186 self.DrawVictim(dc)
187
188 def DrawVictim(self, dc):
189 dc.SetPen(wxPen(wxNamedColour('black'), 20))
190 dc.DrawLines([(10, 980), (10,900), (700,900), (700,940), (720,940),
191 (720,980), (900,980)])
192 dc.DrawLines([(100,900), (100, 100), (300,100)])
193 dc.DrawLine(100,200,200,100)
194 if ( self.misses == 0 ): return
195 dc.SetPen(wxPen(wxNamedColour('blue'), 10))
196 dc.DrawLine(300,100,300,200)
197 if ( self.misses == 1 ): return
198 dc.DrawEllipse(250,200,100,100)
199 if ( self.misses == 2 ): return
200 dc.DrawLine(300,300,300,600)
201 if ( self.misses == 3) : return
202 dc.DrawLine(300,300,250,550)
203 if ( self.misses == 4) : return
204 dc.DrawLine(300,300,350,550)
205 if ( self.misses == 5) : return
206 dc.DrawLine(300,600,350,850)
207 if ( self.misses == 6) : return
208 dc.DrawLine(300,600,250,850)
209
210 def OnPaint(self, event):
211 dc = wxPaintDC(self)
212 self.Draw(dc)
213
214
215
216 class HangmanDemo(HangmanWnd):
217 def __init__(self, wf, parent, id, pos, size):
218 HangmanWnd.__init__(self, parent, id, pos, size)
219 self.StartGame("dummy")
220 self.start_new = 1
221 self.wf = wf
222 self.delay = 500
223 self.timer = self.PlayTimer(self.MakeMove)
224
225 def MakeMove(self):
226 self.timer.Stop()
227 if self.start_new:
228 self.StartGame(self.wf.Get())
229 self.start_new = 0
230 self.left = list('aaaabcdeeeeefghiiiiijklmnnnoooopqrssssttttuuuuvwxyz')
231 else:
232 key = self.left[int(random.random()*len(self.left))]
233 while self.left.count(key): self.left.remove(key)
234 self.start_new = self.HandleKey(key)
235 self.timer.Start(self.delay)
236
237 def Stop(self):
238 self.timer.Stop()
239
240 class PlayTimer(wxTimer):
241 def __init__(self,func):
242 wxTimer.__init__(self)
243 self.func = func
244 self.Start(1000)
245
246 def Notify(self):
247 apply(self.func, ())
248
249
250
251 class HangmanDemoFrame(wxFrame):
252 def __init__(self, wf, parent, id, pos, size):
253 wxFrame.__init__(self, parent, id, "Hangman demo", pos, size)
254 self.demo = HangmanDemo(wf, self, -1, wxDefaultPosition, wxDefaultSize)
255
256 def OnCloseWindow(self, event):
257 self.demo.timer.Stop()
258 self.Destroy()
259
260
261
262 class AboutBox(wxDialog):
263 def __init__(self, parent,wf):
264 wxDialog.__init__(self, parent, -1, "About Hangman", wxDefaultPosition, wxSize(350,450))
265 self.wnd = HangmanDemo(wf, self, -1, wxPoint(1,1), wxSize(350,150))
266 self.static = wxStaticText(self, -1, __doc__, wxPoint(1,160), wxSize(350, 250))
267 self.button = wxButton(self, 2001, "OK", wxPoint(150,420), wxSize(50,-1))
268 EVT_BUTTON(self, 2001, self.OnOK)
269
270 def OnOK(self, event):
271 self.wnd.Stop()
272 self.EndModal(wxID_OK)
273
274
275
276 class MyFrame(wxFrame):
277 def __init__(self, parent, wf):
278 self.wf = wf
279 wxFrame.__init__(self, parent, -1, "hangman", wxDefaultPosition, wxSize(400,300))
280 self.wnd = HangmanWnd(self, -1)
281 menu = wxMenu()
282 menu.Append(1001, "New")
283 menu.Append(1002, "End")
284 menu.AppendSeparator()
285 menu.Append(1003, "Reset")
286 menu.Append(1004, "Demo...")
287 menu.AppendSeparator()
288 menu.Append(1005, "Exit")
289 menubar = wxMenuBar()
290 menubar.Append(menu, "Game")
291 menu = wxMenu()
292 #menu.Append(1010, "Internal", "Use internal dictionary", TRUE)
293 menu.Append(1011, "ASCII File...")
294 urls = [ 'wxPython home', 'http://alldunn.com/wxPython/main.html',
295 'slashdot.org', 'http://slashdot.org/',
296 'cnn.com', 'http://cnn.com',
297 'The New York Times', 'http://www.nytimes.com',
298 'De Volkskrant', 'http://www.volkskrant.nl/frameless/25000006.html',
299 'Gnu GPL', 'http://www.fsf.org/copyleft/gpl.html',
300 'Bijbel: Genesis', 'http://www.coas.com/bijbel/gn1.htm']
301 urlmenu = wxMenu()
302 for item in range(0,len(urls),2):
303 urlmenu.Append(1020+item/2, urls[item], urls[item+1])
304 urlmenu.Append(1080, 'Other...', 'Enter an URL')
305 menu.AppendMenu(1012, 'URL', urlmenu, 'Use a webpage')
306 menu.Append(1013, 'Dump', 'Write contents to stdout')
307 menubar.Append(menu, "Dictionary")
308 self.urls = urls
309 self.urloffset = 1020
310 menu = wxMenu()
311 menu.Append(1090, "About...")
312 menubar.Append(menu, "Help")
313 self.SetMenuBar(menubar)
314 self.CreateStatusBar(2)
315 EVT_MENU(self, 1001, self.OnGameNew)
316 EVT_MENU(self, 1002, self.OnGameEnd)
317 EVT_MENU(self, 1003, self.OnGameReset)
318 EVT_MENU(self, 1004, self.OnGameDemo)
319 EVT_MENU(self, 1005, self.OnWindowClose)
320 EVT_MENU(self, 1011, self.OnDictFile)
321 EVT_MENU_RANGE(self, 1020, 1020+len(urls)/2, self.OnDictURL)
322 EVT_MENU(self, 1080, self.OnDictURLSel)
323 EVT_MENU(self, 1013, self.OnDictDump)
324 EVT_MENU(self, 1090, self.OnHelpAbout)
325 EVT_CHAR(self.wnd, self.OnChar)
326 self.OnGameReset()
327
328 def OnGameNew(self, event):
329 word = self.wf.Get()
330 self.in_progress = 1
331 self.SetStatusText("",0)
332 self.wnd.StartGame(word)
333
334 def OnGameEnd(self, event):
335 self.UpdateAverages(0)
336 self.in_progress = 0
337 self.SetStatusText("",0)
338 self.wnd.EndGame()
339
340 def OnGameReset(self, event=None):
341 self.played = 0
342 self.won = 0
343 self.history = []
344 self.average = 0.0
345 self.OnGameNew(None)
346
347 def OnGameDemo(self, event):
348 frame = HangmanDemoFrame(self.wf, self, -1, wxDefaultPosition, self.GetSize())
349 frame.Show(TRUE)
350
351 def OnDictFile(self, event):
352 fd = wxFileDialog(self)
353 if (self.wf.filename):
354 fd.SetFilename(self.wf.filename)
355 if fd.ShowModal() == wxID_OK:
356 file = fd.GetPath()
357 self.wf = WordFetcher(file)
358
359 def OnDictURL(self, event):
360 item = (event.GetId() - self.urloffset)*2
361 print "Trying to open %s at %s" % (self.urls[item], self.urls[item+1])
362 self.wf = URLWordFetcher(self.urls[item+1])
363
364 def OnDictURLSel(self, event):
365 msg = wxTextEntryDialog(self, "Enter the URL of the dictionary document", "Enter URL")
366 if msg.ShowModal() == wxID_OK:
367 url = msg.GetValue()
368 self.wf = URLWordFetcher(url)
369 def OnDictDump(self, event):
370 print self.wf.words
371
372 def OnHelpAbout(self, event):
373 about = AboutBox(self, self.wf)
374 about.ShowModal()
375 about.wnd.Stop() # that damn timer won't stop!
376
377 def UpdateAverages(self, has_won):
378 if has_won:
379 self.won = self.won + 1
380 self.played = self.played+1
381 self.history.append(self.wnd.misses) # ugly
382 total = 0.0
383 for m in self.history:
384 total = total + m
385 self.average = float(total/len(self.history))
386
387 def OnChar(self, event):
388 if not self.in_progress:
389 #print "new"
390 self.OnGameNew(None)
391 return
392 key = event.KeyCode();
393 #print key
394 if key >= ord('A') and key <= ord('Z'):
395 key = key + ord('a') - ord('A')
396 key = chr(key)
397 if key < 'a' or key > 'z':
398 event.Skip()
399 return
400 res = self.wnd.HandleKey(key)
401 if res == 0:
402 self.SetStatusText(self.wnd.message)
403 elif res == 1:
404 self.UpdateAverages(0)
405 self.SetStatusText("Too bad, you're dead!",0)
406 self.in_progress = 0
407 elif res == 2:
408 self.in_progress = 0
409 self.UpdateAverages(1)
410 self.SetStatusText("Congratulations!",0)
411 if self.played:
412 percent = (100.*self.won)/self.played
413 else:
414 percent = 0.0
415 self.SetStatusText("p %d, w %d (%g %%), av %g" % (self.played,self.won, percent, self.average),1)
416
417 def OnWindowClose(self, event):
418 self.Destroy()
419
420
421
422 class MyApp(wxApp):
423 def OnInit(self):
424 if wxPlatform == '__WXGTK__':
425 defaultfile = "/usr/share/games/hangman-words"
426 elif wxPlatform == '__WXMSW__':
427 defaultfile = "c:\\windows\\hardware.txt"
428 else:
429 defaultfile = ""
430 wf = WordFetcher(defaultfile)
431 frame = MyFrame(NULL, wf)
432 self.SetTopWindow(frame)
433 frame.Show(TRUE)
434 return TRUE
435
436
437
438 if __name__ == '__main__':
439 app = MyApp(0)
440 app.MainLoop()
441
442
443 #----------------------------------------------------------------------
444
445 overview = __doc__
446
447
448 def runTest(frame, nb, log):
449 if wxPlatform == '__WXGTK__' or wxPlatform == '__WXMOTIF__':
450 defaultfile = "/usr/share/games/hangman-words"
451 elif wxPlatform == '__WXMSW__':
452 defaultfile = "c:\\windows\\hardware.txt"
453 else:
454 defaultfile = ""
455 wf = WordFetcher(defaultfile)
456 win = MyFrame(frame, wf)
457 frame.otherWin = win
458 win.Show(true)
459
460
461 #----------------------------------------------------------------------
462
463
464
465