home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / rexx / guess.cmd < prev    next >
OS/2 REXX Batch file  |  1999-05-11  |  5KB  |  138 lines

  1. /******************************************************************************/
  2. /*  guess                    Object REXX Samples                              */
  3. /*                                                                            */
  4. /*  An animal guessing game                                                   */
  5. /*                                                                            */
  6. /*  This samples creates a simple node class and uses it to create a logic    */
  7. /*  tree.  The logic tree is filled in by playing a simple guessing game.     */
  8. /******************************************************************************/
  9. topic = 'animal'
  10.  
  11. firstNode = .node~new                       /* Create a new instance of node  */
  12.                                             /* to start our tree.             */
  13. firstNode~setGuess('bear')
  14.  
  15. say 'Think of' article(topic) 'for me to guess!'
  16.  
  17. do until userQuits
  18.   firstNode~interact                        /* Invoke the interact method on  */
  19.                                             /* the node class to handle user  */
  20.                                             /* interaction.                   */
  21.   userQuits = \prompt('Wanna try another' topic'?')
  22. end
  23.  
  24. say ''
  25. say 'Here is the decision tree I built:'
  26. firstNode~dump(1)
  27.  
  28. say 'Thanks for playing!'
  29. return
  30.  
  31. /* An example of using the ::routine directive to define a subroutine used by */
  32. /* several of the following methods.                                          */
  33. ::routine prompt
  34. use arg question
  35. say question '(Y/N)'
  36. pull reply
  37. return reply = 'Y'
  38.  
  39. /* An example of using the ::routine directive to define a subroutine used by */
  40. /* several of the following methods.                                          */
  41. ::routine article
  42. use arg noun
  43. firstLetter = left(translate(noun), 1)
  44. if pos(firstLetter, 'AEIOU') = 0 then
  45.   return 'a' noun
  46. else
  47.   return 'an' noun
  48.  
  49. /* Define a new class 'node' and make it visible outside of this program      */
  50. ::class node public
  51.  
  52. /* Define the init method on the node class.  This method initializes the     */
  53. /* state data for an instance of the node class.                              */
  54. ::method init
  55. expose yesNode noNode myGuess myQuestion
  56. yesNode = .nil
  57. noNode = .nil
  58. myGuess = .nil
  59. myQuestion = .nil
  60. return self
  61.  
  62. /* Define the dump method on the node class.  This method prints out the      */
  63. /* decision tree built by the program.                                        */
  64. ::method dump
  65. expose yesNode noNode myGuess myQuestion
  66. use arg tab
  67. if myGuess = .nil then
  68.   do
  69.     yesNode~dump(tab + 5)
  70.     say left('',tab) myQuestion
  71.     noNode~dump(tab + 5)
  72.   end
  73. else
  74.   do
  75.     say left('',tab) '<'myGuess'>'
  76.   end
  77.  
  78. /* Define the setGuess method on the node class.  This method gets the next   */
  79. /* guess from the user.                                                       */
  80. ::method setGuess
  81. expose myGuess
  82. use arg myGuess
  83.  
  84. /* Define the interact method on the node class.  This method handles the     */
  85. /* interaction between the program and the user.  Note the use of 'self' to   */
  86. /* execute methods on this instance of node.                                  */
  87. ::method interact
  88. expose myGuess
  89. if myGuess = .nil then
  90.   self~askQuestion
  91. else
  92.   self~guess
  93.  
  94. /* Define the guess method on the node class.  This method tries to guess the */
  95. /* user's article and asks the user for more information if it fails.         */
  96. ::method guess
  97. expose myGuess
  98. say "I bet you're thinking of" article(myGuess)'.'
  99. if prompt('Right?') then
  100.   say 'Cool!  I guessed your secret!'
  101. else
  102.   self~learnNew
  103.  
  104. /* Define the learnNew method on the node class.  This method gets additional */
  105. /* information about the user's article.                                      */
  106. ::method learnNew
  107. expose myGuess myQuestion yesNode noNode
  108. say 'What were you thinking of?'
  109. parse pull newGuess
  110. say 'Oops!  I never heard of that.'
  111. say 'Please enter a question with a yes or no answer that would help me'
  112. say 'tell' article(myGuess) 'from' article(newGuess)'.'
  113. parse pull myQuestion
  114. yesNode = .node~new
  115. noNode = .node~new
  116. if prompt('Is the answer yes for' article(newGuess)'?') then
  117.   do
  118.     yesNode~setGuess(newGuess)
  119.     noNode~setGuess(myGuess)
  120.   end
  121. else
  122.   do
  123.     yesNode~setGuess(myGuess)
  124.     noNode~setGuess(newGuess)
  125.   end
  126. say "Thanks, I'll remember that for next time!"
  127. myGuess = .nil
  128. return
  129.  
  130. /* Define the askQuestion method on the node class.  This method branches     */
  131. /* down either the yes or no fork based on the user's response.               */
  132. ::method askQuestion
  133. expose myQuestion yesNode noNode
  134. if prompt(myQuestion) then
  135.   yesNode~interact
  136. else
  137.   noNode~interact
  138.