home *** CD-ROM | disk | FTP | other *** search
- [GATO.CNV]
- [G. Cisneros, 15.1.85]
- [[This program plays tic-tac-toe on a board numbered as follows:
-
- 1 | 2 | 3
- ---+---+---
- 4 | 5 | 6
- ---+---+---
- 7 | 8 | 9
- ]]
-
- [get a move from the console]
- (()(
- ((%r,TTY:Your turn; give me a number between 1 and 9: )) N
- )(0 1)(
- ((and,<[9]>,<0>):(and,(IVL,1,9,),<1>)<>,<<
- >>(IF,(2 3),<0>,(and,<[(#m,<1>)]>,<2>) <3>,<<
- >><2>X<3>,(%t,Not available)(t,<0>):(N))):
- (<[9]><>,(T,(t,<=>)(%t)));
- (<0>:,<0>:(N)):
- )) j
-
- [display the board]
- (()()()(
- (,<=>(WHILE,(0 1 2 3),<=>,<:0:><:1:><:2:><3>,<<
- >>(%t, <0> | <1> | <2>(if,<3>,<[3]>,(^MJ)---+---+---,)),<<
- >><3>,));
- )) t
-
- [check whether winning is possible or an opponent win must be stopped]
- (()()(0 1 2 3)(
- ((and,(OR,,<[3]>,<[6]>)(or,00 ,0 0, 00),<0><[3]>)<1>,<<
- >>(,(t,<0>000<1>))(G));
- ((and,(OR,,<[1]>,<[2]>)<<
- >>(and,(or,0<[2]>0<[2]> ,0<[2]> <[2]>0, <[2]>0<[2]>0),<1>),<<
- >><0><1>)<2>,(,(t,<0>(IF,(3 4),<1>,(and,<[6]>,<3>) ,<3>0,<<
- >><=>,(and,<[3]>,<3>) <4>,<3>0<4>,<<
- >><=>, <4>,0<4>)<2>))(G));
- (0(and,<[3]>,<0>)0(and,<[3]>,<1>) ,(,(t,0<0>0<1>0))(G));
- ( (and,<[3]>,<0>)0(and,<[3]>,<1>)0,(,(t,0<0>0<1>0))(G));
- ((and,<[2]>,<0>)0<:1:>0<:2:> <3>,(,(t,<0>0<1>0<2>0<3>))(G));
- ((and,<[2]>,<0>) <:1:>0<:2:>0<3>,(,(t,<0>0<1>0<2>0<3>))(G));
- ((and,(OR,,<[3]>,<[6]>)(or,XX ,X X, XX),<0><[3]>,<0><1>)<2>,<<
- >><0>(IF,(3 4),<1>,<3> <4>,<3>0<4>)<2>:);
- ((and,(OR,,<[1]>,<[2]>)<<
- >>(and,(or,X<[2]>X<[2]> ,X<[2]> <[2]>X, <[2]>X<[2]>X),<1>),<<
- >><0><1>)<2>,<0>(IF,(3 4),<1>,(and,<[6]>,<3>) ,<3>0,<<
- >><=>,(and,<[3]>,<3>) <4>,<3>0<4>,<<
- >><=>, <4>,0<4>)<2>:);
- (X(and,<[3]>,<0>)X(and,<[3]>,<1>) ,X<0>X<1>0:);
- ( (and,<[3]>,<0>)X(and,<[3]>,<1>)X,0<0>X<1>X:);
- ((and,<[2]>,<0>)X<:1:>X<:2:> <3>,<0>X<1>X<2>0<3>:);
- ((and,<[2]>,<0>) <:1:>X<:2:>X<3>,<0>0<1>X<2>X<3>:);
- )) T
-
- [first response]
- (()()(0 1)(
- ( X ,(j,(t,0 X ):));
- ((and,<[4]>,<0>) (and,<[4]>,<1>),(j,(t,<0>0<1>):));
- )) A
-
- [second response: prevent double plays]
- (()()(0 1 2 3)(
- (<-->:,(j,(t,<=>)));
- ((and,(OR,X<[7]>X,<[2]>X<[3]>X<[2]>),<:1:> <0>),(j,(t,<1>0<0>):));
- ((and, <[3]>,<0>)(and,(or,X<[2]>,<[2]>X),<:1:><:2:><:3:>),<<
- >>(j,(t,<0>(if,<1>, ,0)<2>(if,<3>, ,0)):));
- ((and,(or,X<[2]>,<[2]>X),<:1:><:2:><:3:>)(and,<[3]> ,<0>),<<
- >>(j,(t,(if,<1>, ,0)<2>(if,<3>, ,0)<0>):));
- ((and,(or,X<[8]>,<[6]>X<[2]>),<:0:><:1:> 0 <:2:><:3:> ),<<
- >>(j,(t,(if,<0>, ,0)<1> 0 (if,<2>, ,0)<3> ):));
- ((and,(or,<[2]>X<[6]>,<[8]>X), <:0:><:1:> 0 <:2:><:3:>),<<
- >>(j,(t, <0>(if,<1>, ,0) 0 <2>(if,<3>, ,0)):));
- ( (and,<[3]>,<0>)<:1:><:2:><:3:>,<<
- >>(j,(t, <0>(if,<0>,X0,0,<1>)<2>(if,<0>,X0,<3>,0)):));
- (<:1:><:2:><:3:>(and,<[3]><0>) ,<<
- >>(j,(t,(if,<0>,X0,0,<1>)<2>(if,<0>,X0,<3>,0)<0> ):));
- ( <0>,(j,(t,0<0>):));
- ((and,<[2]>,<0>) <1>,(j,(t,<0>0<1>):));
- )) B
-
- [third response: choose the best position if stopping was not needed]
- (()()(0 1 2 3)(
- (<>,);
- (<-->:,(j,(t,<=>)));
- ((and,<[3]>,<0>) 0 <1>,(j,(t,<0>00 <1>):));
- (<:0:> (and,<[2]>,<1>)0(and,<[2]>,<2>) <:3:>,(j,(t,<0>0<1>0<2> <3>):));
- ( <0>,(j,(t,0<0>):));
- (<:0:><:1:> <2>,(j,(t,<0><1>0<2>):));
- )) C
-
- [last response: necessarily a tie]
- (()()(0 1)(
- (<>,);
- (<-->:,(,(t,<=>))(%t,Tie));
- (<0> <1>,(,(t,<0>0<1>))(%t,Tie));
- )) D
-
- [main program]
- ((
- ((and,<[1]>,<0>)) 0
- ((and,<[1]>,<1>)) 1
- ((and,<[1]>,<2>)) 2
- ((and,<[1]>,<3>)) 3
- )(
- ((%t, ====(>) I win! (<)====)) G
- ((%r,TTY:t terminates<,> p plays<,> (<)return(>) gives directions: )) E
- )()(
- (<>,(%t,This program plays tic-tac-toe on a board numbered(^MJ)<<
- >> from 1 to 9 as follows:(^MJMJ) 1 | 2 | 3(^MJ)---+---+---<<
- >>(^MJ) 4 | 5 | 6(^MJ)---+---+---(^MJ) 7 | 8 | 9)(E)):
- ((or,P,p),(D,(C,(B,(A,(j, :)))))(E)):
- ((or,T,t),Stop);
- (,(E)):
- ))
-
- [end]