Sunday, February 24, 2013

Living my LiveCode Dream: Encryption Version 2.0



In my last post, I made it clear that updating line 7 in the code to use different "encryption" techniques is a very poor strategy. And, what about giving the user the option of what to encrypt?

So, version 2.0 is here to the rescue!


As this screenshot shows, the user now has control over the two options for what to reveal in the target word, or to choose neither. To do this, I added two new check box buttons: btnNumbers and btnCapitals. I tried to use good naming conventions, hence I put the prefix "btn" (short for "button" of course) in front of each. The important thing to remember with a check box button is that when it is checked, the "hilite" of it is true; likewise, when not checked, its "hilite" is false.

OK, let's take a look at the modified code of the button "encrypt":

-----------------------------------------------------------------------------------------------

global varChar, varLength, varWord, varWordEncrypt, varEncrypt

on showCapitals
   if (charToNum(char varChar of varWord) >64) and (charToNum(char varChar of varWord) <91) then
      put varWordEncrypt & char varChar of varWord into varWordEncrypt
      put false into varEncrypt
   end if
end showCapitals

on showNumbers
   if (charToNum(char varChar of varWord) >47 and charToNum(char varChar of varWord) <58) then
      put varWordEncrypt & char varChar of varWord into varWordEncrypt
      put false into varEncrypt
   end if
end showNumbers

on mouseUp
   put empty into varWordEncrypt
   put line 1 of field "target" into varWord
   put the length of varWord into varLength
   repeat with varChar = 1 to varLength
      put true into varEncrypt
      if hilite of button "btnCapitals" is true then showCapitals
      if hilite of button "btnNumbers" is true then showNumbers
      if varEncrypt is true then put varWordEncrypt & "*" into varWordEncrypt
   end repeat
   put varWordEncrypt into line 1 of field "result"
end mouseUp

-----------------------------------------------------------------------------------------------

First, notice that I had to declare a bunch of global variables. This was not necessary in version 1.0 because everything was happening inside of a single handler (i.e. on mouseUp). Now, these variables are used in a bunch of places, so they need to be global variables.

The main difference between the code here and that in version 1.0 shown in my previous post is that line 7 is now replaced by lines 7 and 8:

     if hilite of button "btnCapitals" is true then showCapitals
     if hilite of button "btnNumbers" is true then showNumbers

Each of these two lines first check to see if the check box buttons are checked and if they are (i.e. if true), then each executes a custom procedure: showCapitals and showNumbers. Think of custom procedures as "teaching" LiveCode new vocabulary words to add to its "language" (yes, think of yourself as a language teacher and LiveCode is your student).

When a new word is encountered by LiveCode, it checks to see if the word has been previously "defined." As you can see, both have been defined just above the "on mouseUp" script. If you compare the script of these two procedures to line 7 in version 1.0, you will find they are almost identical.

But, you will notice I had to create a new variable: "varEncrypt". This is another one of those true/false variables. Notice that at the start of each repeat loop, it is set to true. Also notice that if either the showCapitals or showNumbers procedure is executed, then it is set to false. This is important because if either gets executed, we obviously don't want the character to be displayed as an asterisk. Consequently, line 9 in the "on mouseUp" script will only execute when varEncrypt is true.

Finally, I added the following code to the card script:

on openCard
   set the hilite of button "btnNumbers" to false
   set the hilite of button "btnCapitals" to false
end openCard

This script just turns both check box buttons off when the card is opened. That seemed the best way to start.

I hope you will agree that this version 2.0 is a huge improvement over version 1.0. The use of the showCapitals and showNumbers procedures is much cleaner and easier to follow. If I wanted to add an option to only show vowels, I would just add another check box button called something like "btnVowels" and a procedure called something like "showVowels," then insert the following line of code into the button script:

      if hilite of button "btnVowels" is true then showVowels

And, giving the user the option to control the code always fills me with delight because it transforms a simple piece of code into a powerful tool.

In my next post, we'll finally do the obvious: write some code that reveals one of the target word's characters at random. I can hardly wait.

No comments:

Post a Comment