home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 May / CHIPCD5_98.iso / coreldrw / Scripts / animals.csc next >
Text File  |  1997-05-23  |  6KB  |  215 lines

  1. REM Computer guesses your animals and learns them.
  2. REM Animals.csc August 10, 1996
  3. REM Copyright 1996 Corel Corporation. All rights reserved.
  4.  
  5. ' FUNCTION declarations
  6. DECLARE SUB Intro()
  7. DECLARE SUB EndMessage()
  8. DECLARE SUB LoadDatabase( filename$ )
  9. DECLARE SUB SaveDatabase( filename$ )
  10. DECLARE FUNCTION QuestionUser() AS BOOLEAN
  11. DECLARE SUB ShowSuggestion()
  12. DECLARE FUNCTION Guess( a% ) AS INTEGER
  13. DECLARE SUB FindAnimal()
  14.  
  15. ' GLOBAL variables and constants
  16. GLOBAL CONST MAXDATA%=999       ' Size of database
  17. GLOBAL CONST YES%=6             ' Messagebox yes value
  18. GLOBAL CONST YESNOBUTTONS%=4    ' messagebox YES/NO buttons
  19. GLOBAL CONST TITLE$="Animal Game" ' Game title
  20. GLOBAL anim(MAXDATA) AS STRING  ' holds animal database
  21. GLOBAL animsize AS INTEGER      ' Size of animal database                
  22.  
  23. ' local main variable
  24. CONST FILENAME$ = "animals.gme" ' name of the file
  25.  
  26. '==========================================================
  27. ' Display intro
  28. Intro
  29. ' Fill up databASe
  30. LoadDatabase(FILENAME)
  31. ' question loop
  32. DO 
  33. LOOP WHILE QuestionUser()
  34.  
  35. IF MESSAGEBOX("Do you want to save this game?",TITLE,YESNOBUTTONS)=YES THEN
  36.     ' Save database
  37.     SaveDatabase(FILENAME)
  38. END IF
  39. ' end message
  40. EndMessage
  41.  
  42. '==========================================================
  43. ' Display introduction message
  44. SUB Intro
  45.     DIM temp AS STRING    ' holds temporary string
  46.     temp = "Play GUESS THE ANIMAL with Corel SCRIPT" + CHR(13)
  47.     temp = temp + "Think of an animal and the computer will try to guess it..."
  48.     MESSAGE temp
  49. END SUB
  50.  
  51. '==========================================================
  52. ' Display end message
  53. SUB EndMessage
  54.     DIM temp AS STRING    ' holds temporary string
  55.     temp = "Thank you for playing."
  56.     MESSAGE  temp 
  57. END SUB
  58.  
  59. '==========================================================
  60. ' Try to load database from file.
  61. ' if file is currupted or not present, assign default values
  62. SUB LoadDatabase( filename$ )
  63.     ON ERROR GOTO errorhandler
  64.     DIM a AS INTEGER        ' loop var
  65.  
  66.     OPEN filename FOR INPUT AS 1
  67.     INPUT #1, animsize
  68.     FOR a=1 TO animsize
  69.         INPUT #1, anim(a)
  70.     NEXT a
  71.     CLOSE #1
  72.     
  73.     subend:
  74.     EXIT SUB
  75.     errorhandler:
  76.         ' We could not load database
  77.         ' Assign default values
  78.         CLOSE #1
  79.         animsize=3
  80.         anim(1)="?002003Does it swim?"
  81.         anim(2)="!Gold fish"
  82.         anim(3)="!Hawk"
  83.         resume at subend
  84. END SUB
  85.  
  86. '==========================================================
  87. ' Try to save database to file.
  88. SUB SaveDatabase( filename$ )
  89.     ON ERROR GOTO errorhandler
  90.     DIM a AS INTEGER        ' loop var
  91.  
  92.     OPEN filename FOR OUTPUT AS 1
  93.     WRITE #1, animsize
  94.     FOR a = 1 TO animsize
  95.         WRITE #1, anim(a)
  96.     NEXT a
  97.     CLOSE #1
  98.     
  99.     subend:
  100.     EXIT SUB
  101.     errorhandler:
  102.         ' We could not save database
  103.         CLOSE #1
  104.         MESSAGE "Error saving game!"
  105.         RESUME AT subend
  106. END SUB
  107.  
  108. '==========================================================
  109. ' Question user on a animal
  110. FUNCTION QuestionUser AS BOOLEAN
  111.     DIM ans AS INTEGER            ' holds the answer value
  112.  
  113.     ans = MESSAGEBOX("Are you thinking of an animal?",TITLE,YESNOBUTTONS)
  114.     IF ans=YES THEN
  115.         ' answer is YES
  116.         FindAnimal
  117.     ELSE
  118.         ' answer is NO
  119.         ans = MESSAGEBOX("Can I give you some suggestions?",TITLE,YESNOBUTTONS)
  120.         IF ans=YES THEN ShowSuggestion
  121.     END IF    
  122.     
  123.     ' Decide IF we want to continue
  124.     IF ans<>YES THEN 
  125.         QuestionUser=FALSE
  126.     ELSE
  127.         QuestionUser = TRUE
  128.     END IF
  129. END FUNCTION
  130.  
  131. '==========================================================
  132. ' Prints suggestion list
  133. SUB ShowSuggestion
  134.     DIM mess AS STRING    ' Message buffer
  135.     DIM a AS INTEGER        ' loop counter
  136.     DIM num AS INTEGER  ' number of elements in array
  137.     DIM ani AS INTEGER  ' number of animals found
  138.     
  139.     ani=0
  140.     mess = "Here are some suggestions:" + CHR(13) + CHR(13) + CHR(10)
  141.     
  142.     ' Prints all animals
  143.     FOR a = 1 TO animsize
  144.         IF LEFT( anim(a),1)="!" THEN 
  145.             mess = mess + MID( anim(a),2)
  146.             ani=ani+1
  147.     
  148.             ' Format string
  149.             IF ani MOD 5 = 0 THEN
  150.                 mess = mess + CHR(13)
  151.             ELSE
  152.                 mess = mess + CHR(9)
  153.             END IF
  154.         END IF
  155.     NEXT a
  156.     MESSAGE mess
  157. END SUB
  158.  
  159. '==========================================================
  160. ' Try to guess animal
  161. FUNCTION Guess( question% ) AS INTEGER
  162.     IF MESSAGEBOX(MID(anim(question),8),TITLE,YESNOBUTTONS)=YES THEN
  163.         Guess = CINT(MID(anim(question),2,3))
  164.     ELSE
  165.         Guess = CINT(MID(anim(question),5,3))
  166.     END IF
  167. END FUNCTION
  168.  
  169. '==========================================================
  170. ' Find next question and guess
  171. SUB FindAnimal
  172.     DIM mess AS STRING      ' Message buffer
  173.     DIM g AS INTEGER        ' guess
  174.     DIM answer AS INTEGER   ' tempo answer
  175.     DIM newval AS INTEGER   ' next empty space
  176.     DIM animal AS STRING    ' player animal
  177.     DIM question AS STRING  ' player question
  178.  
  179.     ' Start guessing with first question
  180.     g = 1
  181.     ' find animal
  182.     WHILE LEFT(anim(g),1) = "?"
  183.         g = Guess(g)            
  184.     WEND
  185.     ' Verify guess
  186.     answer=MESSAGEBOX("The animal you were thinking of was a " + MID(anim(g),2) + "?", TITLE,YESNOBUTTONS)
  187.     IF answer=YES OR animsize>MAXDATA-2 THEN
  188.         ' We have it
  189.         MESSAGE "Why not think of another animal?"
  190.     ELSE
  191.         ' Enter in database
  192.         DO
  193.             animal=INPUTBOX("This animal you were thinking of was a ")
  194.         LOOP WHILE animal=""
  195.         mess = "Please enter a question that would distinguish a " 
  196.         mess = mess + animal + " from a " + MID(anim(g),2)
  197.         DO
  198.             question=INPUTBOX(mess)
  199.         LOOP WHILE question=""
  200.         newval = animsize + 1
  201.         answer = MESSAGEBOX("For a " + animal + " the answer would be?",TITLE,YESNOBUTTONS)
  202.         IF answer=YES THEN
  203.             anim(newval) = "!" + animal
  204.             anim(newval+1) = anim(g)
  205.         ELSE
  206.             anim(newval+1) = "!" + animal
  207.             anim(newval) = anim(g)
  208.         END IF
  209.         '  Make sure we have leading 0
  210.         anim(g) = "?" + RIGHT(newval+1000,3) + RIGHT(newval+1001,3) + question
  211.         animsize = newval+1
  212.     END IF
  213. END SUB
  214.  
  215.