;---------------------------------------------------> ;+ ;NAME: ; HANGMAN ; ;PURPOSE: ; Play the Hang Man word game. ; ;CALLING SEQUENCE: ; HANGMAN ; ;REQUIRED INPUTS: ; none ; ;KEYWORDS: ; none ; ;OUTPUTS: ; none ; ;EXAMPLE: ; IDL> hangman ; IDL begins the game engine. Follow the command line prompts. ;HISTORY: ; Written - Paul Higgins - 21 January 2008 ;- ;---------------------------------------------------> ;--<< Returns a 2 element array with a game word and the type of ;--<< word it is function hangman_wordlist, wordsdone ;WORDSDONE is a pointer variable which keeps track of which ;gamewords have been completed. if the number of elements reaches ;11, then the game ends. wdone=*wordsdone if n_elements(wdone) eq 11 then begin print, [[' '], ['All words have been completed!'], [' ']] return,'' endif ;loops through generating integers which correspond to game words ;untill wword comes up as -1, meaning that the word has not been ;played yet. This loops keeps the player from getting repeat words. wword=1 while wword ne -1 do begin x = round(randomu(seed)*9)+1 wword=where(wdone eq x) endwhile ;update the pointer *wordsdone=[wdone, x] ;case structure listing all of the playable words case x of 1: word = ['physics','a noun.'] 2: word = ['astronomy','a noun.'] 3: word = ['berkeley','a proper noun.'] 4: word = ['infrared','a noun.'] 5: word = ['unix','a proper noun.'] 6: word = ['aquarius','a proper noun.'] 7: word = ['telescope','a noun.'] 8: word = ['scientist','a noun.'] 9: word = ['programming','a gerund.'] 10: word = ['array','a noun.'] endcase return, word end ;---------------------------------------------------> ;--<< generates user defined PSYMBOLs for use in plotting ;--<< the hangman's body pro hangman_usersym, noose=noose, body=body ;define 2 different polygons, a square and an oval. if keyword_set(body) then poly=[[0,2,3,4,4,3,2,0,-2,-3,-4,-4,-3,-2], $ [4,4,3,2,0,-2,-3,-4,-4,-3,-2,0,2,3]] if keyword_set(noose) then poly=[[-4,-4,4,4],[-4,4,4,-4]] ;set the user symbol based on the specified keyword. usersym, poly[*,1], poly[*,0], /fill, color=255 return end ;---------------------------------------------------> ;--<< Plots more of the hangman's body with each wrong letter. ;--<< each CASE entry plots part of the body. pro hangman_wrongletter,wrongletter wset,0 hangman_usersym,/body ;WRONGLETTER is the number of wronng answers the player has guessed. ;The hangman can only survive 6 wrong answers, case wrongletter of 1: tvcircle,35,360,290,!red 2: oplot,[10,10,10],[4,5,6],psym=8 3: oplot,[8,9,10,11,12],[6,6,6,6,6],psym=8 4: oplot,[7,13],[5,5],psym=8 5: oplot,[9,11],[3,3],psym=8 6: oplot,[7,8,12,13],[2,2,2,2,2],psym=8 endcase return end ;---------------------------------------------------> ;--<< Split a word into an array of characters. function hangman_charsplit, word ;how many letters are there nchar = strlen(word) ;loop through each character of the word and stick it ;onto the character array wordarr='' for i=0,nchar-1 do begin wordarr=[wordarr,strmid(word,i,1)] endfor ;clip off the initial '' element. nlett=n_elements(wordarr) wordarr=wordarr[1:nlett-1] return,wordarr end ;---------------------------------------------------> pro hangman print,[[' '],$ ['XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'],$ [' '],$ ['Interactive Data HangMan Version 1.0 - By: Paul Higgins'],$ [' '],$ ['XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'],$ [' ']] wait,1 beginnew='' print, 'Begin NEW Game? (Y/N)' read,beginnew if strlowcase(beginnew) eq 'y' then playgame=1 else playgame=0 ;Clean up pointer garbage incase theres some pointers from a ;previous game sitting around. ptr_garbage=ptr_valid() heap_free,ptr_garbage ;initialize a pointer to remember the words which have already ;been played. wordcomplete = ptr_new(/allocate_heap) *wordcomplete = 0 ;initialize the game window, set the user defined plot symbols, ;make sure there will be no axes as in a data plot. window,0,xs=500,ys=500 hangman_usersym, /noose blankticks=['','','','','','','','','',''] !y.tickname=blankticks !x.tickname=blankticks window,1,ys=100,xs=400 ;initialize the game switches gameover=0 round=1 score=0 ;begin the game loop while playgame eq 1 do begin print,[[' '],['Round '+strcompress(round,/remo)],[' ']] ;generate a game word, and if all the words have been played its gameover word=hangman_wordlist(wordcomplete) if word[0] eq '' then gameover=1 ;say what type of word it is (noun/ verb etc) nletters=strlen(word[0]) showword=strarr(nletters)+'_' print,[[' '],['The word is '+word[1]],[' ']] wait,1 ;initialize the gallows (no man is hanging) wset,0 plot, findgen(15), /xstyle, /ystyle, color=0 oplot, [3,4,5,6,7], [1,1,1,1,1], ps=8;, /noerase oplot, [5,5,5,5,5,5,5,5,5], [2,3,4,5,6,7,8,9,10], ps=8;, /noerase oplot, [6,7,8,9],[10,10,10,10], ps=8;, /noerase xyouts,1,14,'Letters Guessed:',/data, charsize=2 ;show the word as "_ _ _ _" with the spaces filled in with the letters ;that the player has guessed correctly. wset,1 xyouts,.1,.5,strjoin(showword,' '),charsize=2,/norm ;split the word into an array of letters wordarr=hangman_charsplit(word[0]) ;initialize the guessing turn switches turncont=1 turn=1 wrongletter=0 ;begin the turns loop for guessing the current word while turncont eq 1 do begin print,[[' '],['Turn '+strcompress(turn,/remo)],[' ']] ;prompt the player for a letter, and make sure they guess only one! oneletter=0 while oneletter eq 0 do begin print,[[' '],['Guess a letter... (A-Z)'],[' ']] letterg='' read,letterg if strlen(letterg) ne 1 then print,[[' '], $ ['Hey! One letter only!'],[' ']] else oneletter=1 endwhile wait,1 ;update the "Guessed Letters" in the game screen wset,0 xyouts, turn+1, 12, letterg, charsize=2, /data ;check to see if the letter is in the word's letter array wletter = where(wordarr eq letterg) if wletter[0] ne -1 then letterexist=1 else letterexist=0 ;if it exists then show where the letter goes in the word. if letterexist eq 1 then begin print,[[' '],['There exists '+strupcase(letterg)+'.'],[' ']] showword[wletter] = letterg wait,1 wset,1 erase xyouts, .1, .5, strjoin(showword,' '), /norm, charsize=2 ;if they're wrong then show a body part of the hangman endif else begin print,[[' '],['There is no '+strupcase(letterg)+'!'],[' ']] wrongletter=wrongletter+1 hangman_wrongletter,wrongletter endelse ;check to see how many letters are left to guess wshow = where(showword eq '_') if wshow[0] eq -1 then begin turncont=0 winround=1 endif ;check to see if the hangman died if wrongletter ge 6 then begin turncont=0 winround=0 print,[[' '],['The man is manifested!'],[' ']] wait,1 endif ;loop around to the next guessing turn turn=turn+1 endwhile ;if the word was completed successfully, then add one to the score if winround eq 1 then begin score=score+1 print,[[' '],['You win this round! SCORE = '+strcompress(score,/remo)],[' ']] endif ;if the hangman died... if winround eq 0 then begin print,[[' '], $ ['You LOSE this round... SCORE = '+strcompress(score,/remo)],[' ']] endif wait,1 ;keep playing? qcont='' print,[[' '],['Continue? (Y/N)'],[' ']] read,qcont if strlowcase(qcont) ne 'y' then playgame=0 round=round+1 ;if there are no more words to play, prompt for a reset. if gameover eq 1 then begin print,[[' '],['Game Over! Begin NEW game? (Y/N)'],[' ']] beginnew='' read,beginnew if strlowcase(beginnew) eq 'y' then begin gameover=0 round=1 score=0 *wordcomplete = 0 endif else break endif ;reinitialize everything hangman_usersym, /noose erase,1 ;end the game round loop endwhile wait,1 print,[[' '],['Good Bye!'],[' ']] end ;--------------------------------------------------->