Hang Man

By Meta on Jun 25, 2011

Just a semi-quick hangman snippet I've developed over the past couple of days. The coding is not the most elegant in the world, but I will probably improve it and/or shorten it later on.

Features:

  • Customizable max guesses, max word/phrase length, and game / startup timeouts
  • Simultaneous multi-server games
  • Two second nick-based flood control

Usage:

This snippet works best with a bot. To begin, load it into a new remote script. (and of course, press Ok so that the on *:START event initiates.)

For users who are not the bot, type:

..hmstart to start a game

..hmstop to stop a game (if you are the host)

..hmguess <letter/number> to guess a letter/number

..hmsolve <word/phrase> to solve

For more help, type ..hmhelp to get a list of commands and how to play.


Notes:

Requires at least whatever version it is that added $v1 and $v2 identifiers. Maybe an even later version. I'd recommend using the latest version for this script.

Requires the use of the $server identifier. Make sure the server/network you are on supports it.

There may or may not be an error on line 79. I didn't notice one though.

There must be at least three people in a channel to start a game. (The bot, the host, and a player)

When solving, you only need to include (basic) letters, numbers, spaces, -'s, and _'s. Everything else can be replaced with spaces. (That is not neccesary though. If you want to include every character, go ahead.)

Letters/Numbers will be displayed as *'s. Everything else will show through.

The word/phrase limit includes spaces.

The settings dialog specifically prevents you from entering a value that is not a number over 0. Not of particular importance, but some may find it useful to look at the script and learn an easy way to specify the allowed characters in a dialog editbox.


If you find any errors or have suggestions, just let me know.

on *:START: {
  hmake hang 100
  hmake hm:spec 100
  if ($isfile(hmspec.hsh)) hload hm:spec hmspec.hsh
  else {
    hadd hm:spec maxguess 6 | hadd hm:spec maxlen 100 | hadd hm:spec timeoutinit 120 | hadd hm:spec timeoutgame 300
    echo -aec info * Hangman Script Loaded.
    echo -aec info * maxguess = $hm().maxguess $+ , maxlen = $hm().maxlen $+ , timeoutinit = $hm().timeoutinit $+ , timeoutgame = $hm().timeoutgame
    echo -aec info * To change these settings, right click in a channel/query/status window, or go to the commands menu at the top, and select "Hangman > Settings"
  }
}

on *:EXIT: hsave hm:spec hmspec.hsh

; --::--::--::--::--

alias hm {
  if ($isid) {
    if ($istok(word letters host guess chan,$prop,32)) return $hget(hang,$+($prop,:,$$1))
    elseif ($istok(maxguess maxlen timeoutinit timeoutgame,$prop,32)) return $hget(hm:spec,$prop)
    else return $iif($hget(hang,$+(:,$$server)),$true,$false)
  }
  else {
    if ($1 == -d) hdel -w hang $+(*:,$server)
    if ($1 == -x) dialog -m hmspec hmspec
    if ($1 == -r) && ($input(Reset all hangman settings to their default values?,y,Hangman)) {
      hadd hm:spec maxguess 6 | hadd hm:spec maxlen 100 | hadd hm:spec timeoutinit 120 | hadd hm:spec timeoutgame 300
      echo -aec info * All hangman settings reset to default.
      hsave hm:spec hmspec.hsh
    }
  }
}

on $*:TEXT:/^\.\.hm(start|guess|solve|stop|cancel|help|phrase)/Si:*: {
  if (!%..flood [ $+ [ $nick ] ] ) {
    inc -u2 %..flood [ $+ [ $nick ] ]
    var %..host $hm($server).host, %..chan $hm($server).chan, %..word $hm($server).word, %..letters $hm($server).letters
    if ($target ischan) {
      if ($regml(1) == help) {
        .notice $nick The object of the game is to guess letters or numbers until you are able to decypher the word/phrase created by the host.
        .notice $nick To start a game of hangman, type ..hmstart -- Once you do this I will query you with further instructions.
        .notice $nick After a game has started, if you are the host, type ..hmstop at any time to stop the current game.
        .notice $nick If you are not the host however, type ..hmguess <letter/number> or ..hmsolve <word/phrase> to guess or try and solve.
        .notice $nick ...Remember! You only have a limited number of guesses before... 
      }
      if ($regml(1) == start) {
        if ($nick(#,0) < 3) .notice $nick Sorry, but there must be at least three people in the channel to play.
        elseif (!%..host) {
          hadd hang $+(host:,$server) $nick | hadd hang $+(chan:,$server) #
          msg # $nick is now the host of hangman. Please wait while $nick comes up with a word/phrase for you to guess.
          .msg $nick To begin, type: ..hmphrase Word/phrase $(|,) Category $(|,) Hint here. (Word/Phrase can be up to $hm().maxlen characters long)
          .msg $nick To cancel, type ..hmcancel
          $+(.timer:hmcancel:,$server) 1 $hm().timeoutinit msg # No response from host. Cancelling game. $(|) hm -d
        }
        else .notice $nick Sorry $nick $+ , but a game is already in progress.
      }
      elseif (# == %..chan) {
        if ($regml(1) == stop) {
          if ($nick == %..host) {
            hm -d
            msg # Hangman game stopped by the host. ( $+ $nick $+ )
          }
          else .notice Only $v1 can stop the game.
        }
        if ($regml(1) == guess) {
          $+(.timer:hmtimeout:,$server) 1 $hm().timeoutgame msg %..chan Hangman game timed out. $(|) hm -d
          if ($nick != %..host) {
            if ($len($2) > 1) .notice $nick One letter or number only please. If you would like to solve, type ..hmsolve answer
            elseif ($2 !isalnum) .notice $nick You must guess either a letter or a number.
            elseif ($2 isin $hm($server).letters) .notice $nick That letter or number has already been picked.
            else {
              hadd hang $+(letters:,$server) $hm($server).letters $+ $2
              msg # $iif($2 !isin $hm($server).word,Nope.) $regsubex($hm($server).word,/([^ $+ $hm($server).letters $+ \W])/gSi,*) $(|) $hm($server).letters
              if ($2 !isin $hm($server).word) {
                hinc hang $+(guess:,$server) 1
                msg # $calc($hm().maxguess - $hm($server).guess) $iif($calc($hm().maxguess - $hm($server).guess) == 1,try,tries) left.
              }
            }
            if ($hm($server).guess == $hm().maxguess) || ($regsubex($hm($server).word,/([^ $+ $hm($server).letters $+ \W])/gSi,*) == $hm($server).word) {
              msg # $iif($hm($server).guess == $hm().maxguess,Oh $+ $chr(44) too bad... The word/phrase was: $hm($server).word,Congrats $+ $chr(44) $nick $+ ! You won hangman!)
              hm -d
              $+(.timer:hmtimeout:,$server) off
            }
          }
          else .notice $nick You can't guess! You're the host!
        }
        if ($regml(1) == solve) {
          $+(.timer:hmtimeout:,$server) 1 $hm().timeoutgame msg %..chan Hangman game timed out. $(|) hm -d
          if ($nick != %..host) {
            if ($2 == $null) .notice $nick Forgetting something, $nick $+ ?
            elseif ($regsubex($2-,/[^\w -]/gSi,$chr(32)) != $regsubex($hm($server).word,/[^\w -]/gSi,$chr(32))) {
              hinc hang $+(guess:,$server) 1
              msg # Nope, sorry. $calc($hm().maxguess - $hm($server).guess) $iif($calc($hm().maxguess - $hm($server).guess) == 1,try,tries) left.
            }
            if ($hm($server).guess == $hm().maxguess) || ($regsubex($2-,/[^\w -]/gSi,$chr(32)) == $regsubex($hm($server).word,/[^\w -]/gSi,$chr(32))) {
              msg # $iif($hm($server).guess == $hm().maxguess,Oh $+ $chr(44) too bad... The word/phrase was: $hm($server).word,Congrats $+ $chr(44) $nick $+ ! You won hangman!)
              hm -d
              $+(.timer:hmtimeout:,$server) off
            }
          }
          else .notice $nick You can't solve! You're the host!
        }
      }
    }
    elseif ($target == $me) && ($nick == %..host) && ($timer($+(:hmcancel:,$server))) {
      if ($regml(1) == cancel) {
        msg %..chan Hangman game cancelled by the host. ( $+ $nick $+ ) | close -m $nick
        $+(.timer:hmcancel:,$server) off | hm -d
      }
      if ($regml(1) == phrase) {
        tokenize 124 $2-
        if ($len($regsubex(nospace,$1,/\s+$/,)) > $hm().maxlen) .msg $nick Word/Phrase is $calc($v1 - $v2) characters too long.
        elseif (!$regsubex($1,/ /g,)) .msg $nick Sorry? 
        else {
          hadd hang $+(word:,$server) $1
          .msg $nick Thanks $nick $+ . I am now starting the game. | close -m $nick
          msg %..chan Hangman game is now starting. Here is the word/phrase:
          msg %..chan $regsubex(word,$hm($server).word,/([a-z0-9])/gSi,*)
          msg %..chan Category: $iif($2,$2,None) -- Hint: $iif($3,$3,None)
          $+(.timer:hmcancel:,$server) off | $+(.timer:hmtimeout:,$server) 1 $hm().timeoutgame msg %..chan Hangman game timed out. $(|) hm -d
        }
      }
    }
  }
}

; --::--::--::--::--

dialog -l hmspec {
  title Hangman Settings
  size -1 -1 100 96
  option dbu

  button Ok, 1, 8 80 40 12, Ok
  button Cancel, 2, 52 80 40 12, cancel

  text Max Wrong Guesses:, 3, 23 11 54 8
  edit $hm().maxguess, 4, 77 10 15 10

  text Max Word/Phrase Length:, 5, 11 27 66 8
  edit $hm().maxlen, 6, 77 26 15 10 

  text Startup Timeout (Seconds):, 7, 8 43 68 8
  edit $hm().timeoutinit, 8, 77 42 15 10

  text Game Timeout (Seconds):, 9, 12 59 64 8
  edit $hm().timeoutgame, 10, 77 58 15 10
}

on *:DIALOG:hmspec:EDIT:4,6,8,10: {
  did -ra hmspec $did $regsubex($did($did),/^[^1-9]+|\D/g,)
  did $iif(!$did($did),-b,-e) hmspec 1
}

on *:DIALOG:hmspec:SCLICK:1: {
  if ($numtok($did(4) $did(6) $did(8) $did(10),32) < 4) halt
  hadd hm:spec maxguess $did(4) | hadd hm:spec maxlen $did(6) | hadd hm:spec timeoutinit $did(8) | hadd hm:spec timeoutgame $did(10)
  hsave hm:spec hmspec.hsh
  echo -aec info * maxguess = $hm().maxguess $+ , maxlen = $hm().maxlen $+ , timeoutinit = $hm().timeoutinit $+ , timeoutgame = $hm().timeoutgame
}

; --::--::--::--::--

menu channel,query,status,menubar {
  - 
  Hangman
  .Settings:hm -x
  .Reset:hm -r 
}

Comments

Sign in to comment.
napa182   -  Jun 28, 2011

Nice version of a hang man snippet
Keep it up... 8/10 +Like

 Respond  
irchainscriptz   -  Jun 26, 2011

Kool i'll try it later on.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.