home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / clips_2 / CLIPS / Examples / R-P-S < prev    next >
Encoding:
Text File  |  1993-06-02  |  4.9 KB  |  181 lines

  1.  
  2. ;;;======================================================
  3. ;;;   Rock, Paper, & Scissors Game
  4. ;;;     
  5. ;;;     Plays a children's game in which
  6. ;;;        Rock smashes scissors,
  7. ;;;        Scissors cut paper, and
  8. ;;;        Paper covers rock.
  9. ;;;     Demonstrates a use for the random
  10. ;;;     conflict resolution strategy.
  11. ;;;
  12. ;;;     CLIPS Version 6.0 Example
  13. ;;;
  14. ;;;     To execute, merely load, reset and run.
  15. ;;;======================================================
  16.  
  17. ;;;****************
  18. ;;;* DEFFUNCTIONS *
  19. ;;;****************
  20.  
  21. (deffunction yes-or-no-p (?question)
  22.   (bind ?x bogus)
  23.   (while (and (neq ?x yes) (neq ?x y) (neq ?x no) (neq ?x n))
  24.      (format t "%s(Yes or No) " ?question)
  25.      (bind ?x (lowcase (sym-cat (read)))))
  26.   (if (or (eq ?x yes) (eq ?x y)) then TRUE else FALSE))
  27.  
  28. ;;;*************
  29. ;;;* TEMPLATES *
  30. ;;;*************
  31.  
  32. (deftemplate win-totals
  33.   (slot human (type INTEGER) (default 0))
  34.   (slot computer (type INTEGER) (default 0))
  35.   (slot ties (type INTEGER) (default 0)))
  36.  
  37. (deftemplate results
  38.    (slot winner (type SYMBOL) (allowed-symbols rock paper scissors))
  39.    (slot loser (type SYMBOL) (allowed-symbols rock paper scissors))
  40.    (slot why (type STRING)))
  41.  
  42. ;;;*****************
  43. ;;;* INITIAL STATE *
  44. ;;;*****************
  45.  
  46. (deffacts information
  47.   (results (winner rock) (loser scissors) (why "Rock smashes scissors"))
  48.   (results (winner scissors) (loser paper) (why "Scissors cut paper"))
  49.   (results (winner paper) (loser rock) (why "Paper covers rock"))
  50.   (valid-answer rock r rock)
  51.   (valid-answer paper p paper)
  52.   (valid-answer scissors s scissors))
  53.  
  54. ;;;****************
  55. ;;;* STARTUP RULE *
  56. ;;;****************
  57.  
  58. (defrule startup
  59.   =>
  60.   (printout t "Lets play a game!" crlf crlf)
  61.   (printout t "You choose rock, paper, or scissors," crlf)
  62.   (printout t "and I'll do the same." crlf crlf)
  63.   (printout t "Rock smashes scissors!" crlf)
  64.   (printout t "Paper covers rock!" crlf)
  65.   (printout t "Scissors cut paper!" crlf crlf)
  66.   (set-strategy random)
  67.   (assert (win-totals))
  68.   (assert (get-human-move)))
  69.  
  70. ;;;********************
  71. ;;;* HUMAN MOVE RULES *
  72. ;;;********************
  73.  
  74. (defrule get-human-move
  75.   (get-human-move)
  76.   =>
  77.   (printout t "Rock (R), Paper (P), or Scissors (S) ? ")
  78.   (assert (human-choice (read))))
  79.  
  80. (defrule good-human-move
  81.   ?f1 <- (human-choice ?choice)
  82.   (valid-answer ?answer $? =(lowcase ?choice) $?)
  83.   ?f2 <- (get-human-move)
  84.   =>
  85.   (retract ?f1 ?f2)
  86.   (assert (human-choice ?answer))
  87.   (assert (get-computer-move)))
  88.  
  89. (defrule bad-human-move
  90.   ?f1 <- (human-choice ?choice)
  91.   (not (valid-answer ?answer $? =(lowcase ?choice) $?))
  92.   ?f2 <- (get-human-move)
  93.   =>
  94.   (retract ?f1 ?f2)
  95.   (assert (get-human-move)))
  96.  
  97. ;;;***********************
  98. ;;;* COMPUTER MOVE RULES *
  99. ;;;***********************
  100.  
  101. (defrule computer-picks-rock
  102.    ?f1 <- (get-computer-move)
  103.    =>
  104.    (printout t "Computer chooses rock" crlf)
  105.    (retract ?f1)
  106.    (assert (computer-choice rock))
  107.    (assert (determine-results)))
  108.  
  109. (defrule computer-picks-paper
  110.    ?f1 <- (get-computer-move)
  111.    =>
  112.    (printout t "Computer chooses paper" crlf)
  113.    (retract ?f1)
  114.    (assert (computer-choice paper))
  115.    (assert (determine-results)))
  116.  
  117. (defrule computer-picks-scissors
  118.    ?f1 <- (get-computer-move)
  119.    =>
  120.    (printout t "Computer chooses scissors" crlf)
  121.    (retract ?f1)
  122.    (assert (computer-choice scissors))
  123.    (assert (determine-results)))
  124.  
  125. (defrule computer-wins
  126.   ?f1 <- (determine-results)
  127.   ?f2 <- (computer-choice ?cc)
  128.   ?f3 <- (human-choice ?hc)
  129.   ?w <- (win-totals (computer ?cw))
  130.   (results (winner ?cc) (loser ?hc) (why ?explanation))
  131.   =>
  132.   (retract ?f1 ?f2 ?f3)
  133.   (modify ?w (computer (+ ?cw 1)))
  134.   (format t "%s%n" ?explanation)
  135.   (printout t "Computer wins!" t)
  136.   (assert (determine-play-again)))
  137.  
  138. ;;;***************************
  139. ;;;* WIN DETERMINATION RULES *
  140. ;;;***************************
  141.  
  142. (defrule human-wins
  143.   ?f1 <- (determine-results)
  144.   ?f2 <- (computer-choice ?cc)
  145.   ?f3 <- (human-choice ?hc)
  146.   ?w <- (win-totals (human ?hw))
  147.   (results (winner ?hc) (loser ?cc) (why ?explanation))
  148.   =>
  149.   (retract ?f1 ?f2 ?f3)
  150.   (modify ?w (human (+ ?hw 1)))
  151.   (format t "%s%n" ?explanation)
  152.   (printout t "You win!" t)
  153.   (assert (determine-play-again)))
  154.  
  155. (defrule tie
  156.   ?f1 <- (determine-results)
  157.   ?f2 <- (computer-choice ?cc)
  158.   ?f3 <- (human-choice ?cc)
  159.   ?w <- (win-totals (ties ?nt))
  160.   =>
  161.   (retract ?f1 ?f2 ?f3)
  162.   (modify ?w (ties (+ ?nt 1)))
  163.   (printout t "We tie." t)
  164.   (assert (determine-play-again)))
  165.  
  166. ;;;*******************
  167. ;;;* PLAY AGAIN RULE *
  168. ;;;*******************
  169.  
  170. (defrule play-again
  171.   ?f1 <- (determine-play-again)
  172.   (win-totals (computer ?ct) (human ?ht) (ties ?tt))
  173.   =>
  174.   (retract ?f1)
  175.   (assert (get-human-move))
  176.   (if (not (yes-or-no-p "Play again? ")) 
  177.      then 
  178.      (printout t crlf "You won " ?ht " game(s)." t)
  179.      (printout t "Computer won " ?ct " game(s)." t)
  180.      (printout t "We tied " ?ct " game(s)." t t)
  181.      (halt)))