It is a HangMan game meant for a bot but would work on an actual nick too. It allows users to open a query with the bot/person and type !hangman <key(if needed)> and a game of HangMan will start in that channel.
Type !hangman on it's own in a channel to start a game with a random word from a text file, it will select a word from a file called hangman.txt, remember to create that and put some words in there, !addword will add words to the list and !remword will remove. Use !hangmanhelp for more info.
Everything is explained under the command !hangmanhelp
Any ideas for improvement at all?
REMEMBER TO CREATE hangman.txt IN THE SAME DIRECTORY AS THE mIRC CLIENT YOU ARE USING THIS SCRIPT ON.
ALSO: When putting words into the file manually, you have to type each word on a new line.
Thanks
on *:TEXT:!hangman*:?: {
if (( %hangman != on )) {
if (( $2 == $null )) { msg $nick You did not specify a word or channel. | close -m $nick | halt }
if (( $3 == $null )) { msg $nick You did not specify a channel. | close -m $nick | halt }
if (( $left($3,1) != $chr(35) )) { msg $nick $3 is not a chan.| close -m $nick | halt }
if (( $2 !isalpha )) { msg $nick Sorry, the word must contain only letters | close -m $nick | halt }
if (( $3 !ischan )) { msg $nick I am not on $3 $+ , type !join $3 <key(if needed)> to get me to join | close -m $nick | halt }
set %hang.word $2
set %hang.appear $str(-,$len(%hang.word))
set %hang.lives 9
set %hangman on
set %hang.chan $3
var %l = 1
while (( %l <= $len(%hang.word) )) {
set $+(%,hang.letter.,%l) $leftind(%hang.word,%l)
set $+(%,hang.appear.,%l) -
inc %l
}
msg %hang.chan $nick has started a Hangman game: %hang.appear
close -m $nick
}
else { msg $nick There is already a game in progress on %hang.chan $+ . }
}
on *:TEXT:!letter*:#: {
if (( %hangman == on )) {
if (( $chan != %hang.chan )) { notice $nick A Hangman game is being played on %hang.chan | halt }
if (( $$2 isin %hang.chosen )) { notice $nick That letter has already been chosen. | halt }
if (( $$2 !isalpha )) || (( $len($$2) != 1 )) { notice $nick You are supposed to say !letter followed by 1 letter | halt }
set %hang.chosen %hang.chosen $$2
var %n = 1
while (( %n <= $len(%hang.word) )) {
if (( $($+(%,hang.letter.,%n),2) == $$2 )) {
set $+(%,hang.appear.,%n) $$2
}
inc %n
}
var %z = 1
while (( %z <= $len(%hang.word) )) {
var %w = %w $+ $($+(%,hang.appear.,%z),2)
inc %z
}
if (( $$2 isin %hang.word )) {
msg # $nick got a letter right!
if (( - !isin %w )) { msg # $nick got the last letter(s) right and revealed the word %hang.word $+ ! | unset %hang* | inc $+(%,$nick,.hangwords) | halt }
}
else { dec %hang.lives | msg # $nick is wrong $+ , you now have %hang.lives mistakes left. | inc $+(%,$nick,.hangwasted) }
if (( %hang.lives == 0 )) { msg # The game is now over, the word was %hang.word | unset %hang* | inc $+(%,$nick,.hanglost) | halt }
msg # The word is currently: %w
}
else { notice $nick There is currently no Hangman game being played. }
}
on *:TEXT:!word*:#: {
if (( %hangman == on )) {
if (( $chan != %hang.chan )) { notice $nick A Hangman game is being played on %hang.chan | halt }
if (( $$2 isin %hang.words )) { notice $nick that word has already been suggested | halt }
if (( $$2 == %hang.word )) {
msg # $nick is correct, the word was %hang.word
inc $+(%,$nick,.hangwords)
unset %hang*
}
else {
dec %hang.lives 2
inc $+(%,$nick,.hangwasted) 2
if (( %hang.lives == -1 )) { dec $+(%,$nick,.hangwasted) }
if (( %hang.lives < 1 )) { msg # $nick got the word wrong and the game is now over, the word was %hang.word | inc $+(%,$nick,.hanglost) | unset %hang* | halt }
msg # $nick got the word wrong and you now have %hang.lives mistakes left.
set %hang.words $addtok(%hang.words,$$2,32)
}
}
else { notice $nick There is currently no Hangman game being played. }
}
on *:TEXT:!chosenletters*:#: {
if (( %hangman != on )) { notice $nick There is currently no Hangman game being played | halt }
if (( $chan != %hang.chan )) { notice $nick A Hangman game is being played on %hang.chan | halt }
msg # These letters have already been chosen: %hang.chosen
}
on *:TEXT:!hangmanhelp*:#: {
msg $nick HANGMAN: PM this bot with !hangman <word> <channel> <key(if needed)> to start a game of hangman, in the specified channel, users can type !letter <letter> to guess a letter, if they are correct the letter will be added to the displayed letters,
msg $nick any un-displayed letters will be represented by a - symbol. Players start with 9 lives, and if a player gets a letter wrong that number is reduced by 1, if that number reaches 0, the game is over. Once a player thinks they know
msg $nick the answer they can type !word <word> to guess, if they are correct the game is won, if they are incorrect players lose 2 lives. To view a list of all chosen letters, type !chosenletters. To view the current word display, type
msg $nick !viewword. For the word to be chosen at random from a list, simply type !hangman in a channel. To addwords to this list type !addword <word>, to remove words type !remword <word>. Type !hangstats to view your stats or !hangstats <nick> to view that nick's stats.
}
on *:TEXT:!viewword*:#: {
if (( %hangman != on )) { notice $nick There is currently no Hangman game being played | halt }
if (( %hang.chan != $chan )) { notice $nick A Hangman game is being played on %hang.chan $+ . | halt }
var %z = 1
while (( %z <= $len(%hang.word) )) {
var %w = %w $+ $($+(%,hang.appear.,%z),2)
inc %z
}
msg # The current word display is %w
}
on *:TEXT:!addword*:#: {
if (( $$2 !isalpha )) { notice $nick Sorry, you can't add words that don't contain only letters. | halt }
var %s = $read(hangman.txt, w, $$2)
if (( %s != $null )) { notice $nick That word is already in the list | halt }
write hangman.txt $$2
msg # The word $$2 has been added.
}
on *:TEXT:!remword*:#: {
var %d = $read(hangman.txt, w, $$2)
if (( %d == $null )) { notice $nick That word is not in the list | halt }
write -ds $+ $$2 hangman.txt $$2
msg # The word $$2 has been removed.
}
on *:TEXT:!join*:#,?: {
if (( $$2 == #0,0 )) { unset %spy* | halt }
.join $$2 $3
close -m $nick
}
on *:TEXT:!hangman:#: {
if (( %hangman != on )) {
set %hang.word $read(hangman.txt)
set %hang.appear $str(-,$len(%hang.word))
set %hang.lives 9
set %hangman on
set %hang.chan $chan
var %l = 1
while (( %l <= $len(%hang.word) )) {
set $+(%,hang.letter.,%l) $leftind(%hang.word,%l)
set $+(%,hang.appear.,%l) -
inc %l
}
msg %hang.chan $nick has started a Hangman game: %hang.appear
}
else { msg $nick There is already a game in progress on %hang.chan $+ . }
}
on *:TEXT:!hangwords*:#: {
msg # In the random words list for hangman, there is currently $lines(hangman.txt) words.
}
on *:TEXT:!hangstats*:#: {
if (( $2 == $null )) { var %n = $nick }
else { var %n = $2 }
if (( $($+(%,%n,.hangwasted),2) == $null )) { set $+(%,%n,.hangwasted) 0 } | if (( $($+(%,%n,.hangwords),2) == $null )) { set $+(%,%n,.hangwords) 0 } | if (( $($+(%,%n,.hanglost),2) == $null )) { set $+(%,%n,.hanglost) 0 }
notice $nick Stats for %n
notice $nick Mistakes Made: $($+(%,%n,.hangwasted),2)
notice $nick Games Won: $($+(%,%n,.hangwords),2)
notice $nick Games Lost: $($+(%,%n,.hanglost),2)
}
alias leftind {
return $remove($left($$1,$$2),$left($$1,$calc( $$2 - 1 )))
}
So it looks like the below
on *:TEXT:!remword*:#: {
if ($2 !isalpha) { notice $nick Words on the list only contains letters. | halt }
var %d = $read(hangman.txt, w, $$2)
if (( %d == $null )) { notice $nick That word is not in the list | halt }
write -ds $+ $$2 hangman.txt $$2
msg # The word $$2 has been removed.
}
I added
if ($2 !isalpha) { notice $nick Words on the list only contains letters. | halt }
to the below
on *:TEXT:!remword*:#: {
var %d = $read(hangman.txt, w, $$2)
if (%d !isalpha) { msg $chan Words on the list only contains letters. | halt }
if (( %d == $null )) { notice $nick That word is not in the list | halt }
write -ds $+ $$2 hangman.txt $$2
msg # The word $$2 has been removed.
}
Since it allowed \" !remword \" to actually remove \" \"
The active channel will the one where the bot/snippet is located and the game is requested. I was suggesting take out the invitations to join particular channels on the snippet, so it will be played either in chans where bot is active, and instead have a separate request bot snippet.... just an idea to avoid the complicatios of query msgs to commence games where the bot/snippet is not available in the chan requested.
In regards to the $read, just use a wildcard search - $read(hangman.txt,w,$2) (will match exactly that word as the line)
Quite a few /if\'s can be removed by using &\'s in the matchtext for on TEXTs such as the first one which could be !hangman & &
There is no need for (( )) for the conditionals of the /if\'s (can use 1 or 0 () in some cases)