Page 215 - Learn To Program With Scratch
P. 215

Unscramble

            Unscramble .sb2  Our last example presents another word game that is a little more challeng-
                          ing to play. We’ll start with an English word, scramble its letters, and ask the
                          player to guess the original word.
                             Let’s start by creating a procedure that rearranges the characters of a
                          given string in random order. The caller sets the input string (strIn), and
                          the procedure, named Randomize, modifies it so that its characters are
                          shuffled around, as illustrated in Figure 8-10.


                                        Randomize

                          abcdefg   strIn                cbeagdf



                          Figure 8-10: Illustrating the Randomize procedure

                             We’ll pick a random letter from strIn and append that letter to a tempo-
                          rary string, str1. (This temporary string, which starts off empty, is where we’ll
                          store the scrambled word as we build it.) We’ll then remove that letter from
                          strIn so we don’t reuse it and repeat the whole process until strIn is empty.
                          The Randomize procedure implements these steps as shown in Figure 8-11.






                                                                      
                                                                      

                                                                      
                                                                      

                                                                      
                                                                      


                          Figure 8-11: The Randomize procedure
                             First, Randomize sets len to the length of the input string, strIn, and
                          empties the temporary string, str1 u. The procedure then starts a repeat
                          loop to assemble the scrambled word v. The repeat count equals the length
                          of the input string. For each loop iteration, we pick a random position in
                          strIn w and append that letter to str1 x. Note that we used length of in step
                          w because strIn and its length will change inside the loop. After that, we
                          call a procedure named Remove to delete the character we just used from
                          strIn y. When the loop finishes shuffling letters around, strIn is set to the
                          scrambled word (str1) z.

                                                                               String Processing   193

                                           www.it-ebooks.info
   210   211   212   213   214   215   216   217   218   219   220