Home‎ > ‎

Animal Wizard

Animal Wizard Knows Every Animal on the Planet !!

That is an exaggeration right now, but it does know a lot of them. 

If you tell it the first letter of the animal you are thinking of, it will guess the animal. If it does not guess it, it will add it to its list and know about it the next time you run it.

   It learns from you.

Download the "AnimalWizard.LiveCode" and "Animals.txt" files below and put them on your Desktop. (it looks for the animal's file there)

    AnimalWizard.livecode

                                      


It reads in the Animals.txt file (see below)  then asks for the first letter of an animal. It then looks up that letter in the list and asks you if that animal is the one you were thinking of. If "yes", then it gloats. If "no", it asks for the animal that you were thinking of. It adds the new animal to its list and saves the new list so it will know it the next time. By saving its list every time it hears a new animal, it gets smarter and smarter until it has every animal in its list.

   Animals.txt
        Animals.txt is just a plain text file (made using Notepad or any text editor) with each animal on a different line:
                            e.g.
alligator
armadillo
bear
beaver
butterfly
camel
chicken
chipmunk
cougar
coyote
crocodile
crab
crayfish
crow
....etc


Play with it and look at the code on the button:

                         global theAnimals
   put specialFolderPath("Desktop") & "/" & "Animals.txt" into myFile     // myFile now is "
   put URL ( "file:" & myFile ) into theAnimals
   ask "What letter does your animal start with?"
   put it into theLetter
   put false into found
   repeat for each line l in theAnimals
      if theLetter = char 1 of item 1 of theAnimals then
         answer "Is it the " & item 1 of theAnimals & "?" with "yes" or "no"

         if it is "yes" then
            answer "See, I am the wizard of all animals"
            put true into found
            break 
         end if
      end if
   end repeat
   if not found then
      ask "You got me, what was your animal?"
      put it into newAnimal
      if it is empty then break
      // add the new animal at the end of the list
      put newAnimal & return after x
      // and save it
      put specialFolderPath("Desktop") & "/" & "Animals.txt" into myFile
      put x into URL ( "file:" & myFile ) 
   end if


Explanation of the Code:


We use a global variable    

            global theAnimals

 to hold all of the animals that we read from the file "Animals.txt"

   put specialFolderPath("Desktop") & "/" & "Animals.txt" into myFile     // myFile now is "
   put URL ( "file:" & myFile ) into theAnimals

We ask the user for the first letter of their animal

   ask "What letter does your animal start with?"
   put it into theLetter
   put false into found

We then go through every Animal word in our list (theAnimals) looking for animals that start with that letter. If the user answers "Yes" we mark found as "true" and break out (we do not have to look anymore)

   repeat for each line l in theAnimals

      if theLetter = char 1 of item 1 of theAnimals then
         answer "Is it the " & item 1 of theAnimals & "?" with "yes" or "no"

         if it is "yes" then
            answer "See, I am the wizard of all animals"
            put true into found
            break 
         end if
      end if

   end repeat


When we are done with the list - if we did not guess the animal, we ask for its name and add it to our list. We then write out the new file...

   if not found then
      ask "You got me, what was your animal?"
      put it into newAnimal
      if it is empty then break

      // add the new animal at the end of the list
      put newAnimal & return after x

      // and save it
      put specialFolderPath("Desktop") & "/" & "Animals.txt" into myFile
      put x into URL ( "file:" & myFile ) 

   end if


For You To Do:
        Come up with ideas for this type of program that collects information on its own:
  1. - to register people at an event (as they type their name, it is added to the list and saved
  2. - to collect vocabulary words as you type them (with their definitions) We could take that list and created a Review program that takes the list and asks you definitions randomly from it. (a later project - also easy to do)
  3. ....(what can you come up with....?)
            

ċ
AnimalWizard.livecode
(6k)
cyril.pruszko@pgcps.org,
Mar 1, 2016, 11:23 AM
ċ
cyril.pruszko@pgcps.org,
Mar 1, 2016, 11:23 AM
Comments