Re: [CODE] say spell thought

From: Daniel Koepke (
Date: 11/29/98

Mundi King wrote:
> thought of two problems,
> the ofs adds one even if a match was found,
> and, the nature of the for loop keeps checking
> after finding a match, instead of starting over
> while (*(lbuf + ofs)) {
>   for (j = 0; *(syls[j].org); j++) {
>     if (!strncmp(syls[j].org, lbuf + ofs, strlen(syls[j].org))) {
>          strcat(buf, syls[j].news);
>          ofs += strlen(syls[j].org);
>          ofs-- /* to offset the ofs++ */
>          /* to start the for loop over from the top */
>          break;
>        }
>      ofs++;

The "break;" call exits the for() loop, so it doesn't keep checking
after a match is found (if it did, I'd have to put a bullet in the
original author's head). You had the right idea to begin with. :)

But there are problems with your solution. The most significant one
is that you increase ofs inside of the for() loop, which is not what
we want. We want it increased outside of the loop because if we do
it inside, "lbuf+ofs" changes each time we check for matching
syllables. That essentially breaks the code unless we're the first
entry in syls[].

To fix this, we do instead, (the first few lines are context)

         ofs += strlen(syls[j].org);
         /* to start the for loop over from the top */
       } /* end if */
     } /* end for */

     if (!*syls[j].org) /* i.e., we didn't find a match in syls[] */
   } /* end while */

The next problem is, if we just increment ofs, we drop characters we
don't find in syls[] from being printed. This may or may not bother
you, depending upon the type of game environment you're trying to
create. It's only real impact is that 'monster summon I', 'monster
summon II', etc. get encrypted as the same thing -- no difference
whatsoever. Still, that's better than an infinite loop. You could
implement a simple algorithm to mutate the character to some other,
but that generally won't give decent results. Anything more complex
probably won't be worth the effort, so the middle ground is:

      if (!*syls[j].org) { /* i.e., we didn't find a match in syls[] */
        log("No entry in syllable table for substring of '%s'", lbuf);

Something like that, anyway. That way, we get some idea of what we need
to add to the table.

Hope that helped...


     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

This archive was generated by hypermail 2b30 : 12/15/00 PST