]>
jfr.im git - irc/evilnet/x3.git/blob - src/plugins/hangman/plugin.py
16 target
= '' #channel or user's nick who we are playing with
20 dictionary
= "/usr/share/dict/words"
22 def __init__(self
, irc
, target
, length
=0):
26 if(length
> 3 and length
< 100):
29 self
.length
= random
.randrange(5, 9)
30 # What constitutes a valid word?
31 self
.valid
= re
.compile(r
"^[a-zA-Z]+$")
35 self
.reply("You can only play with 3 or more letters")
39 if(self
.newword(self
.length
)):
40 self
.reply("HANGMAN is starting!")
43 self
.reply("Aborting game")
48 if(len(self
.word
) == self
.length
and self
.valid
.search(self
.word
)):
52 def newword(self
, length
):
54 for line
in open(self
.dictionary
, "r"):
59 raise Exception("Dictionary has too few words")
61 while((not self
.validword())): #failsafe dont loop forever...
64 self
.reply("Error finding a %s letter word"%length
)
66 #raise(Exception("DictError", "Unable to find %s letter word"%length))
68 randline
= random
.randrange(1, numlines
-1)
69 for line
in open(self
.dictionary
, 'r'):
71 self
.word
= line
.rstrip()
72 if(not self
.validword() and i
< randline
+ 50):
75 break # give up on this block and try again
77 if(len(self
.word
) < 3):
78 self
.reply("Unable to find a word in the dictionary!")
87 if(i
in self
.guesses
or not i
.isalpha()):
90 mask
.append(self
.maskchar
)
93 def manpart(self
, part
, num
):
99 def printstatus(self
):
100 print("DEBUG: the word is '%s'"%self
.word
)
101 self
.reply(" /---%s "%( self
.manpart(",", 1 )) )
102 self
.reply(" | %s Make "%( self
.manpart("o",2)) )
103 self
.reply(" | %s%s%s your "%( self
.manpart("/",4), self
.manpart("|",3), self
.manpart("\\", 5) ) )
104 self
.reply(" | %s %s guess! "%( self
.manpart("/",6), self
.manpart("\\",7) ))
106 self
.reply(self
.maskedword())
108 if(self
.won() == True):
109 self
.reply("YOU WON! FOR NOW!!")
110 elif(self
.won() == False):
111 self
.reply("Your DEAD! DEAAAAAAAD!")
119 if(not i
in self
.guesses
.keys()):
123 def guess(self
, irc
, letter
):
126 if(self
.won() != None):
127 self
.reply("This game is over. Start another!")
130 self
.reply("Guess a single letter only, please.")
132 if(not letter
.isalpha()):
133 self
.reply("Letters only. Punctuation will be filled in for you.")
135 if(letter
in self
.guesses
):
136 self
.reply("Pay attention! %s has already been guessed! I'm hanging you anyway!"%letter
)
141 self
.guesses
[letter
] = True
143 if(self
.won() != None):
145 elif(self
.word
.find(letter
) >= 0):
146 self
.reply("YOU GOT ONE! But I'll hang you yet!!")
148 self
.reply("NO! MuaHaHaHaHa!")
153 def reply(self
, msg
):
154 self
.irc
.send_target_privmsg(self
.irc
.service
, self
.target
, msg
)
159 def __init__(self
, handler
, irc
):
160 self
.handler
= handler
161 self
.name
= "hangman"
163 handler
.addcommand(self
.name
, "start", self
.start
)
164 handler
.addcommand(self
.name
, "end", self
.end
)
165 handler
.addcommand(self
.name
, "guess", self
.guess
)
167 self
.games
= {} # list of game objects
169 def target(self
, irc
):
175 def start(self
, irc
, arg
):
176 playwith
= self
.target(irc
)
177 if(playwith
in self
.games
.keys() and self
.games
[playwith
].won() == None):
178 irc
.reply("There is a game is in progress here, End it before you start another.")
182 self
.games
[playwith
] = Game(irc
, playwith
, arg
)
184 self
.games
[playwith
] = Game(irc
, playwith
)
186 def end(self
, irc
, unused
):
187 playwith
= self
.target(irc
)
188 if(self
.target(irc
) in self
.games
.keys()):
189 self
.games
[playwith
].reply("Game ended by %s"%irc.caller
)
190 del(self
.games
[playwith
])
192 irc
.reply("No game here to end")
194 def guess(self
, irc
, arg
):
195 playwith
= self
.target(irc
)
196 if(self
.target(irc
) in self
.games
.keys()):
197 self
.games
[playwith
].guess(irc
, arg
)
199 irc
.reply("No game here in progress. Start one!")
201 def dance(self
, irc
, args
):
203 user
= _svc
.get_user(nick
)
206 if(user
and "account" in user
):
207 reply
+= " Mr. %s"%user["account"]
209 reply
+= " we can dance"
217 def nickof(self
, irc
, bot
):
218 info
= _svc
.get_info()
220 if(bot
and bot
in info
.keys()):
221 irc
.reply("%s has nick %s"%(bot
, info
[bot
]))
223 irc
.reply("I dunno. Try %s"%str
(info
.keys()))