Trivia hint function

Trivia hint function

mIRC snippet that creates a trivia hint answer.

Postat de Copyright Categorie Review user Vizualizari Data
BLKoldSUN genius_at_work games Cod netestat 320 2024-07-30 19:52:41

/*
1stHint: io******
2ndHint: iop*****
3rdHint: ioph****

Usage: $disphint(<num>,<char>,<answer>)
or: /disphint <num> <char> <answer>

<num> = number of characters to show (0 shows no hint characters)
<char> = character to 'cover' hidden chars (can't be: º ¬ a-z 0-9)
<answer> = answer string (can contain any character including spaces, commas and º ¬)


The code also supports a 'vowel' feature. If you populate the %sVowels global variable with aeiou (or any other letter/number) all of those characters present in the answer will be shown.

The code doesn't hide special characters (commas, question marks, etc) (anything not a-z 0-9). If you want to limit the length of answers which can be hinted, your code should do that before allowing the hint to be shown.

*/

alias disphint {
  ;$1 = Hint count, $2 = Hidden Character, $3- = Hint string

  tokenize 32 $1-

  var %nHintNum = $1
  var %cHintChar = $2
  var %sAnswer = $3-

  if (%nHintNum !isnum) { echo 4 -s Invalid hint count | return }
  if ($regex(%cHintChar,/^([a-z0-9º¬]|$)/i)) { echo 4 -s Invalid hint hidden character | return }
  if (%sAnswer == $null) { echo 4 -s Invalid hint string | return }

  var %sClue = %sAnswer
  var %nLength = $len(%sAnswer)

  %sClue = $regsubex(%sClue,/[^a-zA-Z0-9 ]/g,º)
  %sClue = $regsubex(%sClue,/ /g,¬)
  %sClue = $regsubex(%sClue,/[a-zA-Z0-9]/g,%cHintChar)

  if ($hget(hint)) hfree hint
  hmake hint 2

  ;;; Create hash table and insert vowels
  var %i = 0
  while (%i < %nLength) {
    inc %i
    hadd hint $+(a.,%i) $mid(%sAnswer,%i,1)
    hadd hint $+(c.,%i) $mid(%sClue,%i,1)
    if ($hget(hint,$+(a.,%i)) isin %sVowels) {
      hadd hint $+(c.,%i) $mid(%sAnswer,%i,1)
    }
  }

  hadd hint $+(p.1) 1
  var %j = 0, %nCurPart = 1
  while (%j < %nLength) {
    inc %j
    if ($hget(hint,$+(c.,%j)) != ¬) continue
    inc %j
    if (%j <= %nLength) {
      inc %nCurPart
      hadd hint $+(p.,%nCurPart) %j
    }
  }

  ;;; Calculate hint order
  var %nClues = $count(%sClue,%cHintChar)
  var %n = 0, %m = 0, %L = 1, %p, %h

  while (%L) {
    inc %n

    %p = $hget(hint,$+(p.,%n))
    %h = $hget(hint,$+(c.,%p))

    if (%p > %nLength) {
      %n = 0
      continue
    }
    if (%n > %nCurPart) {
      %n = 0
    }
    if (%m >= %nClues) {
      %L = 0
      continue
    }

    if (%h == %cHintChar) {
      inc %m
      hadd hint $+(h.,%m) %p
      hinc hint $+(p.,%n) 1
    }
    elseif (%h == º) {
      hinc hint $+(p.,%n) 1
      dec %n
      continue
    }
    elseif (%h == ¬) {
      continue
    }
    else {
      hinc hint $+(p.,%n) 1
    }
  }

  var %c, %i = 0
  while (%i < %nHintNum) {
    inc %i
    %c = $hget(hint,$+(h.,%i))
    hadd hint $+(c.,%c) $hget(hint,$+(a.,%c))
  }

  ;;; Build string from hash table values
  var %sClue, %h, %s = 0
  var %i = 0
  while (%i < %nLength) {
    inc %i
    %h = $hget(hint,$+(c.,%i))
    if (%h == ¬) { 
      %s = 1
      continue
    }
    else {
      if (%h == º) %h = $hget(hint,$+(a.,%i))
      %sClue = %sClue $+ $iif(%s,$chr(32)) $+ %h
      %s = 0
    }
  }

  if ($isid) return %sClue
  else echo -a %sClue
}