modal/examples/tictactoe.modal

45 lines
1.7 KiB
Plaintext

?(?-) (This example demonstrates how to implement a 2-players game of Tic Tac Toe)
<> (-- ?x) ()
<> (READ ?~) (?~)
-- (Print)
<> (?: put-str) (?:)
<> ((?0 ?1 ?2) put-row) (?0 put-str | put-str ?1 put-str | put-str ?2 put-str \n put-str)
<> ((?a ?b ?c) display) (?a put-row ?b put-row ?c put-row \n put-str (?a ?b ?c))
-- (Validation)
<> (((?x ?x ?x) ?0 ?1) ?x run) (?x victory)
<> ((?0 (?x ?x ?x) ?1) ?x run) (?x victory)
<> ((?0 ?1 (?x ?x ?x)) ?x run) (?x victory)
<> (((?x ?0 ?1) (?x ?2 ?3) (?x ?4 ?5)) ?x run) (?x victory)
<> (((?0 ?x ?1) (?2 ?x ?3) (?4 ?x ?5)) ?x run) (?x victory)
<> (((?0 ?1 ?x) (?2 ?3 ?x) (?4 ?5 ?x)) ?x run) (?x victory)
<> (((?x ?0 ?1) (?2 ?x ?3) (?4 ?5 ?x)) ?x run) (?x victory)
<> (((?0 ?1 ?x) (?2 ?x ?3) (?x ?4 ?5)) ?x run) (?x victory)
-- (Game)
<> (((?0 ?1 ?2) ?a ?b) ?x 0 0 play) (((?x ?1 ?2) ?a ?b) display ?x run wait)
<> (((?0 ?1 ?2) ?a ?b) ?x 1 0 play) (((?0 ?x ?2) ?a ?b) display ?x run wait)
<> (((?0 ?1 ?2) ?a ?b) ?x 2 0 play) (((?0 ?1 ?x) ?a ?b) display ?x run wait)
<> ((?a (?0 ?1 ?2) ?b) ?x 0 1 play) ((?a (?x ?1 ?2) ?b) display ?x run wait)
<> ((?a (?0 ?1 ?2) ?b) ?x 1 1 play) ((?a (?0 ?x ?2) ?b) display ?x run wait)
<> ((?a (?0 ?1 ?2) ?b) ?x 2 1 play) ((?a (?0 ?1 ?x) ?b) display ?x run wait)
<> ((?a ?b (?0 ?1 ?2)) ?x 0 2 play) ((?a ?b (?x ?1 ?2)) display ?x run wait)
<> ((?a ?b (?0 ?1 ?2)) ?x 1 2 play) ((?a ?b (?0 ?x ?2)) display ?x run wait)
<> ((?a ?b (?0 ?1 ?2)) ?x 2 2 play) ((?a ?b (?0 ?1 ?x)) display ?x run wait)
-- (Play)
<> (ready) (display READ stdin play)
<> (?x run wait) (READ stdin play)
<> (?x victory) ((?x wins!\n) put-str)
-- (Interface)
((Input a move, like "X 0 1":\n) put-str)
((- - -) (- - -) (- - -)) ready