home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / o / ops5.zip / TTT.OPS < prev    next >
Text File  |  1992-05-31  |  10KB  |  309 lines

  1. (strategy mea)
  2. (watch 0)
  3.  
  4.  
  5. (literalize task
  6.     actor)
  7. (literalize position
  8.     row column value identity)
  9. (literalize opposite
  10.     of is)
  11. (literalize player
  12.     with-mark is)
  13. (literalize move
  14.     status whose-turn input)
  15. (vector-attribute input)
  16.  
  17.  
  18.  
  19. (p start
  20. ;     generate the wm-elements defining the "board" and find out whether
  21. ;     the human wants his mark to be x or o
  22.      (ready)
  23.     -->
  24.      (make task ^actor referee)
  25.      (make position ^row 1 ^column 1 ^value | | ^identity top-left)
  26.      (make position ^row 1 ^column 2 ^value | | ^identity top-middle)
  27.      (make position ^row 1 ^column 3 ^value | | ^identity top-right)
  28.      (make position ^row 2 ^column 1 ^value | | ^identity middle-left)
  29.      (make position ^row 2 ^column 2 ^value | | ^identity center)
  30.      (make position ^row 2 ^column 3 ^value | | ^identity middle-right)
  31.      (make position ^row 3 ^column 1 ^value | | ^identity bottom-left)
  32.      (make position ^row 3 ^column 2 ^value | | ^identity bottom-middle)
  33.      (make position ^row 3 ^column 3 ^value | | ^identity bottom-right)
  34.      (make opposite ^of x ^is o)
  35.      (make opposite ^of o ^is x)
  36.      (write (crlf) Do you want to be x or "o?  " )
  37.      (make player ^with-mark (accept) ^is human) )
  38.  
  39. (make ready)
  40.  
  41. (p pop
  42.      ; if there is nothing more to do in the most recently generated task,
  43.      ; delete the task
  44.      (task)
  45.     -->
  46.      (remove 1) )
  47.  
  48.  
  49. (p referee--display-the-board
  50.      ; after each move, display the board
  51.      (task ^actor referee)
  52.      (move ^status made ^whose-turn <mark>)
  53.      (opposite ^of <mark> ^is <opponent-mark>)
  54.      (position ^row 1 ^column 1 ^value <l1>)
  55.      (position ^row 1 ^column 2 ^value <m1>)
  56.      (position ^row 1 ^column 3 ^value <r1>)
  57.      (position ^row 2 ^column 1 ^value <l2>)
  58.      (position ^row 2 ^column 2 ^value <m2>)
  59.      (position ^row 2 ^column 3 ^value <r2>)
  60.      (position ^row 3 ^column 1 ^value <l3>)
  61.      (position ^row 3 ^column 2 ^value <m3>)
  62.      (position ^row 3 ^column 3 ^value <r3>)
  63.     -->
  64.      (modify 2 ^status unmade ^whose-turn <opponent-mark>)
  65.      (write (crlf) (crlf) (crlf)
  66.         (tabto 12) <l1> (tabto 15) "|" (tabto 18) <m1>
  67.         (tabto 21) "|" (tabto 24) <r1>
  68.         (tabto 10) -----------------
  69.         (tabto 12) <l2> (tabto 15) "|" (tabto 18) <m2>
  70.         (tabto 21) "|" (tabto 24) <r2>
  71.         (tabto 10) -----------------
  72.         (tabto 12) <l3> (tabto 15) "|" (tabto 18) <m3>
  73.         (tabto 21) "|" (tabto 24) <r3>) )
  74.  
  75. (p referee--prepare-for-first-move
  76. ;     identify the mark of the computer and create the move wm-element that
  77. ;     will drive the game
  78.      (task ^actor referee)
  79.      (player ^with-mark <mark> ^is human)
  80.      (opposite ^of <mark> ^is <other-mark>)
  81.     -->
  82.      (write (crlf) (crlf) When you are asked where you want your |mark,|
  83.         enter two |numbers.|
  84.         (crlf) The first number should be the row you |want,| the
  85.         second |number, the column.|)
  86.      (make player ^with-mark <other-mark> ^is computer)
  87.      (make move ^status unmade ^whose-turn x) )
  88.  
  89. (p referee--get-a-good-mark
  90. ;     if the human says he wants to be something other than x or o, make
  91. ;     him x
  92.      (task ^actor referee)
  93.      (player ^with-mark <mark> ^is human)
  94.      - (opposite ^of <mark>)
  95.     -->
  96.      (modify 2 ^with-mark x)
  97.      (write (crlf) (crlf) Try to remember that |you're x.|) )
  98.  
  99. (p referee--next-move
  100. ;     if it's time for the next move to be made, generate the appropriate
  101. ;     subtask
  102.      (task ^actor referee)
  103.      (move ^status unmade ^whose-turn <mark>)
  104.      (player ^with-mark <mark> ^is <who>)
  105.     -->
  106.      (make task ^actor <who>) )
  107.  
  108. (p referee--recognize-column-win
  109. ;     if someone has filled a column, note that fact
  110.      (task ^actor referee)
  111.      (move ^status unmade ^whose-turn <mark>)
  112.      (opposite ^of <mark> ^is <other-mark>)
  113.      (player ^with-mark <other-mark>)
  114.      (position ^column <c> ^value <other-mark>)
  115.      - (position ^column <c> ^value <> <other-mark>)
  116.     -->
  117.      (remove 2)
  118.      (make player ^with-mark <other-mark> ^is winner) )
  119.  
  120. (p referee--recognize-row-win
  121. ;     if someone has filled a row, note that fact
  122.      (task ^actor referee)
  123.      (move ^status unmade ^whose-turn <mark>)
  124.      (opposite ^of <mark> ^is <other-mark>)
  125.      (player ^with-mark <other-mark>)
  126.      (position ^row <r> ^value <other-mark>)
  127.      - (position ^row <r> ^value <> <other-mark>)
  128.     -->
  129.      (remove 2)
  130.      (make player ^with-mark <other-mark> ^is winner) )
  131.  
  132. (p referee--recognize-diagonal-win
  133. ;     if someone has filled a diagonal, note that fact
  134.      (task ^actor referee)
  135.      (move ^status unmade ^whose-turn <mark>)
  136.      (opposite ^of <mark> ^is <other-mark>)
  137.      (player ^with-mark <other-mark>)
  138.      (position ^row 2 ^column 2 ^value <other-mark>)
  139.      (position ^row {<r> <> 2} ^column {<c> <> 2}
  140.            ^identity <id> ^value <other-mark>)
  141.      (position ^row <c> ^column <r>
  142.            ^identity <> <id> ^value <other-mark>)
  143.     -->
  144.      (remove 2)
  145.      (make player ^with-mark <other-mark> ^is winner) )
  146.  
  147. (p referee--human-wins
  148. ;     if the human won, let him know
  149.      (task ^actor referee)
  150.      (player ^with-mark <other-mark> ^is winner)
  151.      (player ^with-mark <other-mark> ^is human)
  152.     -->
  153.      (write (crlf) (crlf) You |win.| (crlf) (crlf)) )
  154.  
  155. (p referee--computer-wins
  156. ;     if the computer won, let the human know
  157.      (task ^actor referee)
  158.      (player ^with-mark <other-mark> ^is winner)
  159.      (player ^with-mark <other-mark> ^is computer)
  160.     -->
  161.      (write (crlf) (crlf) I |win.| (crlf) (crlf)) )
  162.  
  163. (p referee--draw
  164. ;     if there are no empty spaces, the game is a draw
  165.      (task ^actor referee)
  166.      (move ^status unmade ^whose-turn <mark>)
  167.      (player ^with-mark <mark>)
  168.      - (position ^value | |)
  169.     -->
  170.      (write (crlf) (crlf) We |drew.| (crlf) (crlf))
  171.      (remove 2) )
  172.  
  173. (p referee--cleanup
  174. ;     if the game is over, delete all of the wm-elements
  175.      (task ^actor referee)
  176.      - (move)
  177.      (<> task)
  178.     -->
  179.      (remove 2) )
  180.  
  181.  
  182. (p human--ask-for-next-move
  183.      ; get the position (row and column) where the human wants his mark
  184.      (task ^actor human)
  185.      (move ^status unmade ^input nil)
  186.     -->
  187.       (write (crlf) (crlf) Where do you want your "mark?  ")
  188.      (modify 2 ^input (acceptline)) )
  189.  
  190. (p human--accept-move
  191.      ; if the move is legal, accept it
  192.      ; the move wm-element is remade so that the value of ^input becomes
  193.      ; nil (there are 2 simpler but less educational ways of achieving
  194.      ; this same end)
  195.      (task ^actor human)
  196.      (move ^status unmade ^whose-turn <mark>
  197.            ^input {<row> >= 0 <= 3} {<column> >= 0 <= 3} nil)
  198.      (position ^row <row> ^column <column> ^value | |)
  199.     -->
  200.      (remove 2)
  201.      (make move (substr 2 2 input) ^status made ^input nil)
  202.      (modify 3 ^value <mark>) )
  203.  
  204. (p human--reject-attempt-to-overwrite
  205.      ; if the position specified is not empty, complain
  206.      ; the move condition element in this rule differs from the move
  207.      ; condition in the previous rule only so you can see two equivalent
  208.      ; ways of expressing the same condition
  209.      (task ^actor human)
  210.      (move ^status unmade
  211.            ^input <row> <column> nil ^input << 1 2 3 >> << 1 2 3 >>)
  212.      (position ^row <row> ^column <column> ^value {<mark> <> | |})
  213.     -->
  214.      (write (crlf) (crlf) There is already an <mark> in <row> <column>)
  215.      (modify 2 ^input nil nil) )
  216.  
  217. (p human--reject-out-of-bounds-move
  218.      ; if the row or column specified is not within bounds or if more than
  219.      ; two numbers have been entered, complain
  220.      ; the move wm-element is remade so that the value of ^input becomes
  221.      ; nil (there is a simpler but less educational way of achieving this
  222.      ; same end)
  223.      (task ^actor human)
  224.      (move ^status unmade ^input <> nil)
  225.     -->
  226.      (write (crlf) (crlf) (substr 2 input inf) is not a legal |move.|)
  227.      (remove 2)
  228.      (make move (substr 2 2 input) ^input nil) )
  229.  
  230.  
  231. (p computer--select-move
  232.      ; select any empty position
  233.      (task ^actor computer)
  234.      (move ^status unmade ^whose-turn <mark>)
  235.      - (position ^row 2 ^column 2 ^value | |)
  236.      (position ^row <r> ^column <c> ^value | |)
  237.     -->
  238.      (modify 2 ^status made)
  239.      (modify 3 ^value <mark>) )
  240.  
  241. (p computer--select-center
  242.      ; select the center if it's available
  243.      (task ^actor computer)
  244.      (move ^status unmade ^whose-turn <mark>)
  245.      (position ^row 2 ^column 2 ^value | |)
  246.     -->
  247.      (modify 2 ^status made)
  248.      (modify 3 ^value <mark>) )
  249.  
  250. (p computer--block-column-win
  251.      ; if the human has two in a column, block
  252.      (task ^actor computer)
  253.      (move ^status unmade ^whose-turn <mark>)
  254.      (position ^row <r> ^column <c>
  255.            ^value {<other-mark> <> <mark> <> | |})
  256.      (position ^column <c> ^value | |)
  257.      (position ^row <> <r> ^column <c> ^value <other-mark>)
  258.     -->
  259.      (modify 2 ^status made)
  260.      (modify 4 ^value <mark>) )
  261.  
  262. (p computer--block-row-win
  263.      ; if the human has two in a row, block
  264.      (task ^actor computer)
  265.      (move ^status unmade ^whose-turn <mark>)
  266.      (position ^row <r> ^column <c>
  267.            ^value {<other-mark> <> <mark> <> | |})
  268.      (position ^row <r> ^value | |)
  269.      (position ^row <r> ^column <> <c> ^value <other-mark>)
  270.     -->
  271.      (modify 2 ^status made)
  272.      (modify 4 ^value <mark>) )
  273.  
  274. (p computer--block-diagonal-win
  275.      ; if the human has two on a diagonal, block
  276.      (task ^actor computer)
  277.      (move ^status unmade ^whose-turn <mark>)
  278.      (position ^row 2 ^column 2
  279.            ^value {<other-mark> <> <mark> <> | |})
  280.      (position ^row {<r> <> 2} ^column {<c> <> 2} ^value | |)
  281.      (position ^row <c> ^column <r> ^value <other-mark>)
  282.     -->
  283.      (modify 2 ^status made)
  284.      (modify 4 ^value <mark>) )
  285.  
  286. (p computer--possible-column
  287.      ; if the computer has one mark in an otherwise empty column, put
  288.      ; another mark in that column
  289.      (task ^actor computer)
  290.      (move ^status unmade ^whose-turn <mark>)
  291.      (position ^row <r> ^column <c> ^value <mark>)
  292.      - (position ^row <> <r> ^column <c> ^value <> | |)
  293.      (position ^row <> <r> ^column <c> ^value | |)
  294.     -->
  295.      (modify 2 ^status made)
  296.      (modify 4 ^value <mark>) )
  297.  
  298. (p computer--possible-row
  299.      ; if the computer has one mark in an otherwise empty row, put
  300.      ; another mark in that row
  301.      (task ^actor computer)
  302.      (move ^status unmade ^whose-turn <mark>)
  303.      (position ^row <r> ^column <c> ^value <mark>)
  304.      - (position ^row <r> ^column <> <c> ^value <> | |)
  305.      (position ^row <r> ^column <> <c> ^value | |)
  306.     -->
  307.      (modify 2 ^status made)
  308.      (modify 4 ^value <mark>) )
  309.