Tutorial 020  Anagrams - truth testing and much shuffling


The following programs illustrate use of arrays, strings, loops, conditional statements and much more.

Have fun.

Anagram your Name or any other word(s) :

      REM Anagram
      REM Richard Weston 3rd april 2003
      REM (after Alan Thomas)
      MODE8
      DIM A(30)
      REPEAT
        ON
        INPUTTAB(1,1)"Enter your name/word : "word$
        OFF
        L=LEN(word$)
        REPEAT
          PRINT'" Anagrams of '";word$;"' are :"'
          FOR n=1 TO 27
            B$=""
            FOR i= 1 TO L
              A(i)=i
            NEXT
            FOR i = L TO 2 STEP -1
              x=RND(i)
              temp=A(x)
              A(x)=A(i)
              A(i)=temp
            NEXT i
            FOR i= 1 TO L
              B$=B$+MID$(word$,A(i),1)
            NEXT i
            COLOUR RND(7)
            PRINTTAB(n*2) B$
          NEXT n
          COLOUR15
          PRINT" Press <RETURN> for more anagrams or <SPACE> for another go"
          G$=GET$ : CLS
        UNTIL G$=" "
      UNTIL FALSE


Anotated Listing of "Anagram your Name"
     
      REM Anagram
      REM Richard Weston 3rd april 2003
      REM (after Alan Thomas)
      MODE8
      DIM A(30) **** To save the positions of the letters in the word to be "anagramised" ***
      REPEAT
        ON
        INPUTTAB(1,1)"Enter your name/word : "word$
        OFF *** flashing cursor distracts ***
        L=LEN(word$) *** number of letters in the word ***
        REPEAT
          PRINT'" Anagrams of '";word$;"' are :"'
          FOR n=1 TO 27 *** to fit the screen without overflowing ***
            B$="" *** an empty string to start off ***
            FOR i= 1 TO L
              A(i)=i *** initial letter positions ***
            NEXT
            FOR i = L TO 2 STEP -1 *** We've seen this randomising routine before when dealing cards ***
              x=RND(i)
              temp=A(x)
              A(x)=A(i)
              A(i)=temp
            NEXT i                            ***Here it scrambles the letter positions ***
            FOR i= 1 TO L
              B$=B$+MID$(word$,A(i),1) *** Prints the letters of the word in the new shuffled order ***
            NEXT i
            COLOUR RND(7)
            PRINTTAB(n*2) B$ *** spreads the words prettily ***
          NEXT n
          COLOUR15
          PRINT" Press <RETURN> for more anagrams or <SPACE> for another go"
          G$=GET$ : CLS
        UNTIL G$=" " *** in fact pressing any key other than <SPACE> will give more anagrams *** 
      UNTIL FALSE



Anagram Game ( Listing for ease of copying)

How good are you at spotting anagrams?

      REM Anagram Game
      REM Richard Weston
      REM 3rd March 2003
      MODE8
      DIM A(20),word$(100)
      score=0
      i=0
      REPEAT
        i=i+1
        READ word$(i)
      UNTIL word$(i)="*"
      nwords=i-1
      :
      PROCshufflewords
      :
      FOR n= 1 TO nwords
        word$=word$(n)
        PROCanagram
      NEXT n
      END
      :
      DEF PROCanagram
      REPEAT
        CLS
        COLOUR15
        PRINTTAB(1,1)"A N A G R A M  G A M E"'
        COLOUR9
        PRINTTAB(1)"Just press <enter> if you need to give up!"'
        COLOUR14
        PRINTTAB(1)"Question ";n;" of ";nwords;
        COLOUR15
        PRINT"  Score = ";score;" " '
        FOR z=1 TO 14
          PROCnewanagram
          COLOUR RND(7)
          PRINTTAB(1+(z-1)*5) B$
        NEXT
        COLOUR7
        ON
        INPUT''TAB(20)"are anagrams of : "guess$
        OFF
        IF guess$ = word$ THEN
          PRINT'TAB(38)"Right!"
          score+=1
        ENDIF
        IF guess$<>word$ THEN
          IF guess$ ="" THEN
            COLOUR17
            PRINT'TAB(38,VPOS-2)word$
          ELSE  PRINT'TAB(38)"Try again!"
          ENDIF
        ENDIF
        delay=INKEY(100)
      UNTIL guess$ = word$ OR guess$=""
      ENDPROC
      :
      DEF PROCshufflewords
      FOR i = nwords TO 2 STEP -1
        x=RND(i)
        temp$=word$(x)
        word$(x)=word$(i)
        word$(i)=temp$
      NEXT i
      ENDPROC
      :
      DEF PROCnewanagram
      L=LEN(word$)
      B$=""
      FOR i= 1 TO L
        A(i)=i
      NEXT
      FOR i = L TO 2 STEP -1
        x=RND(i)
        temp=A(x)
        A(x)=A(i)
        A(i)=temp
      NEXT i
      FOR i= 1 TO L
        B$=B$+MID$(word$,A(i),1)
      NEXT i
      ENDPROC
      :
      DATA banana, apple, carrot, aerial, parrot, gorilla, thistle, house
      DATA wrong, right, rabid, angry, comet, program, dictator, meteorite, rifle
      DATA computer, wrist, cranium, brain, nerve, spoof, sunshine, planet, satellite
      DATA soapy, green, yellow, elephant, haircut, scissors, mouse, photograph, scanner
      DATA aspirin, delight, happiness, victory, peace, femur, bliss, heaven, nirvana
      DATA ankle, kneecap, elbow, aardvark, abacus, enzyme, protein, nucleus, electron
      DATA positron, dinosaur, extinct, mantra, pigsty, temple, ziggurat, windmill
      DATA haddock, plaice, lobster, miniskirt, teacher, professor, diamond, zircon
      DATA criminal, judge, adjective, organ, violin, banjo, tempo, ballet, sextet
      DATA vocalist, clarinet, virtuoso, guitar, saxaphone, quartet, magician, spaniel
      DATA cabbage, broccoli, drought, tractor, haystack, telescope, asteroid, supernova
      :
      REM: Don't delete the following asterisk - its the end marker
      DATA *



Annotated Listing of Anagram Game


      REM Anagram Game
      REM Richard Weston
      REM 3rd March 2003
      MODE8
      DIM A(20),word$(100) ***Array A(20) holds up to twenty letter positions of a word; word$(100) holds up to 100 words
      score=0 *** you gain a point for each correct anagram solved ***
      i=0

      REPEAT  ***counts the number of words in the data statement at the end of the program.... ***
        i=i+1
        READ word$(i)   *** ...... and loads them into the string array word$(100) ***
      UNTIL word$(i)="*"  *** is used as an end marker ***
      nwords=i-1 *** number of words (less one so as not to count the asterisk, *) ***
      :
      PROCshufflewords *** shuffles the word positions so each time you play the words come up in a different order ***
      :
      FOR n= 1 TO nwords *** This is the main program loop"
        word$=word$(n)
        PROCanagram
      NEXT n
      END *** after here we define the procedures ***
      :
      DEF PROCanagram
      REPEAT *** until you guess right or give up! ***
        CLS
        COLOUR15
        PRINTTAB(1,1)"A N A G R A M  G A M E"'
        COLOUR9
        PRINTTAB(1)"Just press <enter> if you need to give up!"'
        COLOUR14
        PRINTTAB(1)"Question ";n;" of ";nwords;
        COLOUR15
        PRINT"  Score = ";score;" " '
        FOR z=1 TO 14
          PROCnewanagram
          COLOUR RND(7)
          PRINTTAB(1+(z-1)*5) B$  *** Prints anagrams diagonally to be seen more easily ***
        NEXT
        COLOUR7
        ON *** cursor on ***
        INPUT''TAB(20)"are anagrams of : "guess$    *** enter your guess ***
        OFF  *** cursor off ***
        IF guess$ = word$ THEN
          PRINT'TAB(38)"Right!"
          score+=1 *** This is equivalent to score=score + 1 ***
        ENDIF
        IF guess$<>word$ THEN                *** Guess not equal to the original word in the DATA statement ***
          IF guess$ ="" THEN                       *** Pressing <ENTER> alone gives a "null string" ***
            COLOUR17                                 *** nice and bright ***
            PRINT'TAB(38,VPOS-2)word$  *** VPOS is useful for moving the cursor back up ***
          ELSE  PRINT'TAB(38)"Try again!"
          ENDIF
        ENDIF
        delay=INKEY(100) *** Gives time to read the comment ***
      UNTIL guess$ = word$ OR guess$=""
      ENDPROC
      :
      DEF PROCshufflewords *** randomises the order the words come up in the game ***
      FOR i = nwords TO 2 STEP -1
        x=RND(i)
        temp$=word$(x)
        word$(x)=word$(i)
        word$(i)=temp$
      NEXT i
      ENDPROC
      :
      DEF PROCnewanagram  *** randomises the letter positions in the word ***
      L=LEN(word$)
      B$=""
      FOR i= 1 TO L
        A(i)=i
      NEXT
      FOR i = L TO 2 STEP -1
        x=RND(i)
        temp=A(x)
        A(x)=A(i)
        A(i)=temp
      NEXT i
      FOR i= 1 TO L
        B$=B$+MID$(word$,A(i),1)
      NEXT i
      ENDPROC
      :
 ***** Note that DATA statements need commas between the entries but not at the end of the lines ***

 ***** You can add more words and delete those you don't want *****

***** Don't forget to alter the DIM word$(100) to word$(200) if you want up to 200 words ! ***

      DATA banana, apple, carrot, aerial, parrot, gorilla, thistle, house
      DATA wrong, right, rabid, angry, comet, program, dictator, meteorite, rifle
      DATA computer, wrist, cranium, brain, nerve, spoof, sunshine, planet, satellite
      DATA soapy, green, yellow, elephant, haircut, scissors, mouse, photograph, scanner
      DATA aspirin, delight, happiness, victory, peace, femur, bliss, heaven, nirvana
      DATA ankle, kneecap, elbow, aardvark, abacus, enzyme, protein, nucleus, electron
      DATA positron, dinosaur, extinct, mantra, pigsty, temple, ziggurat, windmill
      DATA haddock, plaice, lobster, miniskirt, teacher, professor, diamond, zircon
      DATA criminal, judge, adjective, organ, violin, banjo, tempo, ballet, sextet
      DATA vocalist, clarinet, virtuoso, guitar, saxaphone, quartet, magician, spaniel
      DATA cabbage, broccoli, drought, tractor, haystack, telescope, asteroid, supernova
      :
      REM: Don't delete the following asterisk - its the end marker
      DATA *

   

Next Tutorial

Richard Weston's Homepage