io « games « js - tour - touring some programming systems
about summary refs log blame commit diff stats
path: root/js/games/nluqo.github.io/~bh/ssch10/ttt.html
blob: 8913584da885609ac9d3f67e52d37270e25dd98f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                  
<P>
<A NAME="tttttm"></A>
<P><CENTER><IMG SRC="../ss-pics/tinker.jpg" ALT="figure: tinker"></CENTER><P><CENTER>This computer, built of Tinker-Toy parts, plays
tic-tac-toe.
</CENTER><P>

<HTML>
<HEAD>
<TITLE>Simply Scheme: Introducing Computer Science ch 10: Example: Tic-Tac-Toe</TITLE>
</HEAD>
<BODY>
<HR>
<CITE>Simply Scheme:</CITE>
<CITE>Introducing Computer Science</CITE> 2/e Copyright (C) 1999 MIT
<H2>Chapter 10</H2>
<H1>Example: Tic-Tac-Toe</H1>

<TABLE width="100%"><TR><TD>
<IMG SRC="../simply.jpg" ALT="cover photo">
<TD><TABLE>
<TR><TD align="right"><CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR>University of California, Berkeley</CITE>
<TR><TD align="right"><CITE><A HREF="http://ccrma.stanford.edu/~matt">Matthew
Wright</A><BR>University of California, Santa Barbara</CITE>
<TR><TD align="right"><BR>
<TR><TD align="right"><A HREF="../pdf/ssch10.pdf">Download PDF version</A>
<TR><TD align="right"><A HREF="../ss-toc2.html">Back to Table of Contents</A>
<TR><TD align="right"><A HREF="../ssch9/bridge.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch11/part4.html"><STRONG>NEXT</STRONG></A>
<TR><TD align="right"><A HREF="http://mitpress.mit.edu/0262082810">MIT
Press web page for <CITE>Simply Scheme</CITE></A>
</TABLE></TABLE>

<HR>



<P>Now that you've learned about higher-order functions, we're going to look at
a large example that uses them extensively.  Using the
techniques you've learned so far, we're going to write a program that plays
perfect tic-tac-toe.

<P>You can load our program into Scheme by typing

<P><PRE>(load &quot;ttt.scm&quot;)
</PRE>

<P>(See Appendix A if this doesn't work for you.)

<P><H2>A Warning</H2>

<P>Programs don't always come out right the first time.  One of our goals in
this chapter is to show you how a program is developed, so we're presenting
early versions of procedures.  These include some mistakes that we made, and
also some after-the-fact simplifications to make our explanations
easier.  If you type in these early versions, they won't work.  We will show
you how we corrected these &quot;bugs&quot; and also will present a complete, correct
version at the end of the chapter.

<P>To indicate the unfinished versions of procedures, we'll use comments like
&quot;first version&quot; or &quot;not really part of game.&quot;

<P><H2>Technical Terms in Tic-Tac-Toe</H2>

<P>We'll number the squares of the board this way:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>1<TD>2<TD>3<TR><TD>4<TD>5<TD>6<TR><TD>7<TD>8<TD>9</TABLE>&nbsp;

<P>We'll call a partially filled-in board a &quot;position.&quot;

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD><TD><TD>o<TR><TD><TD>x<TD>o<TR><TD>x<TD><TD>x</TABLE>&nbsp;

<P>To the computer, the same position will be represented by the word <CODE>__o_xox_x</CODE>.  The nine letters of the word correspond to squares one through
nine of the board.  (We're thinking ahead to the possibility of using <CODE>item</CODE> to extract the <EM>n</EM>th square of a given position.)

<P><H2>Thinking about the Program Structure</H2>

<P>Our top-level procedure, <CODE>ttt</CODE>, will return the computer's next move 
given the current position.  It takes two arguments: the current position
and whether the computer is playing X or O.  If the computer is O and the
board looks like the one above, then we'd invoke <CODE>ttt</CODE> like this:

<P><PRE>(<A NAME="g1"></A>ttt '__o_xox_x 'o)
</PRE>

<P>Here is a sample game:

<P><PRE>&gt; (ttt '____x____ 'o)                   ; Human goes first in square 5
1                                       ; Computer moves in square 1
&gt; (ttt 'o__xx____ 'o)                   ; Human moves in square 4
6                                       ; Computer blocks in square 6
&gt; (ttt 'o_xxxo___ 'o)                   ; Human moves in square 3
7                                       ; Computer blocks again
&gt; (ttt 'o_xxxoox_ 'o)
2
</PRE>

<P>This is not a complete game program!  Later, when we talk about input and
output, you'll see how to write an interactive program that displays the
board pictorially, asks the player where to move, and so on.  For now, we'll
just write the <EM>strategy</EM> procedure that chooses the next
move.  As a paying customer, you wouldn't be satisfied with this
partial program, but from the programmer's point of view, this is the more
interesting part.

<P>Let's plan the computer's strategy in English before we start writing a
computer program.  How do <EM>you</EM> play tic-tac-toe?  You have several
strategy rules in your head, some of which are more urgent than others.  For
example, if you can win on this move, then you just do it without thinking
about anything else.  But if there isn't anything that immediate, you
consider less urgent questions, such as how this move might affect what
happens two moves later.

<P>So we'll represent this set of rules by a giant <CODE>cond</CODE> expression:

<P><PRE>(define (ttt position me)                    ;; first version
  (cond ((i-can-win?)
         (choose-winning-move))
        ((opponent-can-win?)
         (block-opponent-win))
        ((i-can-win-next-time?)
         (prepare-win))
        (else (whatever))))
</PRE>

<P>We're imagining many helper procedures.  <CODE>I-can-win?</CODE> will look at the
board and tell if the computer has an immediate winning move. If so, <CODE>choose-winning-move</CODE> will find that particular move.  <CODE>Opponent-can-win?</CODE> returns true if the human player has an immediate
winning move.  <CODE>Block-opponent-win</CODE> will return a move that prevents the
computer's opponent from winning, and so on.

<P>We didn't actually start by writing this definition of <CODE>ttt</CODE>.  The
particular names of helper procedures are just guesses, because we haven't
yet planned the tic-tac-toe strategy in detail.  But we did know that this
would be the overall structure of our program.  This big picture doesn't
automatically tell us what to do next; different programmers might fill in
the details differently.  But it's a framework to keep in mind during the
rest of the job.

<P>Our first practical step was to think about the <EM><A NAME="g2"></A><A NAME="g3"></A>data structures</EM> in our program.  A data structure is a way of
organizing several pieces of information into a big chunk.  For example, a
sentence is a data structure that combines several words in a sequence (that
is, in left-to-right order).

<P>In the first, handwavy version of <CODE>ttt</CODE>, the strategy procedures like
<CODE>i-can-win?</CODE> are called with no arguments, but of course we knew they
would need some information about the board position.  We began by thinking
about how to represent that information within the program.

<P><H2>The First Step: Triples</H2>

<P>A person looking at a tic-tac-toe board looks at the rows, columns, and
diagonals.  The question &quot;do I have a winning move?&quot; is equivalent to the
question &quot;are there three squares in a line such that two of them are mine
and the last one is blank?&quot;  In fact, nothing else matters about the game
besides these potential winning combinations.

<P>There are eight potential winning combinations: three rows, three
columns, and two diagonals.  Consider the combination containing the three
squares 1, 5, and 9.  If it contains both an <CODE>x</CODE> and an <CODE>o</CODE> then
nobody can win with this combination and there's nothing to think about.
But if it contains two <CODE>x</CODE>s and a free square, we're very interested in
the combination.  What we want to know in particular is which square is
free, since we want to move in that square to win or block.

<P>More generally, the only squares whose <EM>numbers</EM> we care about are the
ones we might want to move into, namely, the free ones.  So the only
interesting information about a square is whether it has an <CODE>x</CODE> or an
<CODE>o</CODE>, and if not, what its number is.

<P>The information that 1, 5, 9 is a potential winning combination and the
information that square 1 contains an <CODE>x</CODE>, square 5 is empty, and square
<CODE>9</CODE> contains another <CODE>x</CODE> can be combined into the single word <CODE>x5x</CODE>.  Looking at this word we can see immediately that there are two <CODE>x</CODE>s in this &quot;triple&quot; and that the free square is square 5.  So when we
want to know about a three-square combination, we will turn it into a
triple of that form.

<P>Here's a sample board position:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD><TD>x<TD>o<TR><TD><TD>x<TD><TR><TD>o<TD><TD></TABLE>&nbsp;

<P>and here is a sentence of all of its triples:

<P><PRE>(1xo 4x6 o89 14o xx8 o69 1x9 oxo)
</PRE>

<P>Take a minute to convince yourself that this sentence really does tell you
everything you need to know about the corresponding board position.  Once
our strategy procedure finds the triples for a board position, it's never
going to look at the original position again.

<P>This technique of converting data from one form to another so that it can be
manipulated more easily is an important idea in computer science.  There are
really three representations of the same thing.  There's this picture:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD><TD>x<TD>o<TR><TD><TD>x<TD><TR><TD>o<TD><TD></TABLE>&nbsp;

<P>as well as the word <CODE>_xo_x_o__</CODE> and the sentence <CODE>(1xo 4x6
o89 14o xx8 o69 1x9 oxo)</CODE>.  All three of these formats have the same
information but are convenient in different ways.  The pictorial form is
convenient because it makes sense to the person who's playing tic-tac-toe.
Unfortunately, you can't type that picture into a computer, so we need a
different format, the word <CODE>_xo_x_o__</CODE>, which contains the <EM>contents</EM> of the nine squares in the picture, but without the lines
separating the squares and without the two-dimensional shape.

<P>The third format, the sentence, is quite <EM>inconvenient</EM> for human
beings.  You'd never want to think about a tic-tac-toe board that way
yourself, because the sentence doesn't have the visual simplicity that lets
you take in a tic-tac-toe position at a glance.  But the sentence of triples
is the most convenient representation for our program.  <CODE>Ttt</CODE> will have
to answer questions like &quot;can <CODE>x</CODE> win on the next move?&quot; To do that, it
will have to consider an equivalent but more detailed question:  &quot;For each
of the eight possible winning combinations, can <CODE>x</CODE> complete that
combination on the next move?&quot; It doesn't really matter whether a
combination is a row or a column; what does matter is that each of the eight
combinations be readily available for inspection by the program.  The
sentence-of-triples representation obscures part of the available
information (which combination is where) to emphasize another part (making
the eight combinations explicit, instead of implicit in the nine boxes of
the diagram).

<P>The representation of fractions as &quot;mixed numerals,&quot; such as 2<SUP><SMALL><SMALL>1</SMALL></SMALL></SUP>/<SUB><SUB><SMALL>3</SMALL></SUB></SUB>,
and as &quot;improper fractions,&quot; such as <SUP><SMALL><SMALL>7</SMALL></SMALL></SUP>/<SUB><SUB><SMALL>3</SMALL></SUB></SUB>, is a non-programming
example of this idea about multiple representations.  A mixed numeral makes
it easier for a person to tell how big the number is, but an improper
fraction makes arithmetic easier.

<P><H2>Finding the Triples</H2>

<P>We said that we would combine the current board position with the
numbers of the squares in the eight potential winning combinations in order
to compute the things we're calling triples.  That was our first task in
writing the program.

<P>Our program will start with this sentence of all the winning combinations:

<P><PRE>(123 456 789 147 258 369 159 357)
</PRE>

<P>and a position word such as <CODE>_xo_x_o__</CODE>; it will return a
sentence of triples such as

<P><PRE>(1xo 4x6 o89 14o xx8 o69 1x9 oxo)
</PRE>

<P>All that's necessary is to replace some of the numbers with <CODE>x</CODE>s and
<CODE>o</CODE>s.  This kind of word-by-word translation in a sentence is a good job
for <CODE>every</CODE>.

<P><PRE>(define (find-triples position)              ;; first version
  (every substitute-triple '(123 456 789 147 258 369 159 357)))
</PRE>

<P>We've made up a name <CODE>substitute-triple</CODE> for a procedure we haven't
written yet.  This is perfectly OK, as long as we write it before we try to
invoke <CODE>find-triples</CODE>.  The <CODE>substitute-triple</CODE> function will take
three digits, such as <CODE>258</CODE>, and return a triple, such as <CODE>2x8</CODE>:

<P><PRE>(define (substitute-triple combination)      ;; first version
  (every substitute-letter combination))
</PRE>

<P>This procedure uses <CODE>every</CODE> to call <CODE>substitute-letter</CODE> on
all three letters.

<P>There's a small problem, though.  <CODE>Every</CODE> always returns a sentence, and
we want our triple to be a word.  For example, we want to turn the potential
winning combination <CODE>258</CODE> into the word <CODE>2x8</CODE>, but <CODE>every</CODE> would
return the sentence <CODE>(2 x 8)</CODE>.  So here's our next version of <CODE>substitute-triple</CODE>:

<P><PRE>(define (substitute-triple combination)      ;; second version
  (accumulate word (every substitute-letter combination)))
</PRE>

<P><CODE>Substitute-letter</CODE> knows that letter number 3 of the word that
represents the board corresponds to the contents of square 3 of the board.
This means that it can just call <CODE>item</CODE> with the given square number and
the board to find out what's in that square.  If it's empty, we return the
square number itself; otherwise we return the contents of the square.

<P><PRE>(define (substitute-letter square)           ;; first version
  (if (equal? '_ (item square position))
      square
      (item square position)))
</PRE>

<P>Whoops!  Do you see the problem?

<P><PRE>&gt; (substitute-letter 5)
ERROR: Variable POSITION is unbound.
</PRE>

<P><H2>Using <CODE>Every</CODE> with Two-Argument Procedures</H2>

<P>Our procedure only takes one argument, <CODE>square</CODE>, but it needs to know
the position so it can find out what's in the given square.  So here's the
real <CODE>substitute-letter</CODE>:

<P><PRE>(define (<A NAME="g4"></A>substitute-letter square position)
  (if (equal? '_ (item square position))
      square
      (item square position)))

&gt; (substitute-letter 5 '_xo_x_o__)
X

&gt; (substitute-letter 8 '_xo_x_o__)
8
</PRE>

<P>Now <CODE>substitute-letter</CODE> can do its job, since it has access to
the position.  But we'll have to modify <CODE>substitute-triple</CODE> to invoke
<CODE>substitute-letter</CODE> with two arguments.

<P>This is a little tricky.  Let's look again at the way we're using <CODE>substitute-letter</CODE> inside <CODE>substitute-triple</CODE>:

<P><PRE>(define (substitute-triple combination)      ;; second version again
  (accumulate word (every substitute-letter combination)))
</PRE>

<P>By giving <CODE>substitute-letter</CODE> another argument, we have made
this formerly correct procedure incorrect.  The first argument to <CODE>every</CODE>
must be a function of one argument, not two.  This is exactly the kind of
situation in which <CODE>lambda</CODE> can help us:  We have a function of two
arguments, and we need a function of one argument that does the same thing,
but with one of the arguments fixed.

<P>The procedure returned by

<P><PRE>(lambda (square) (substitute-letter square position))
</PRE>

<P>does exactly the right thing; it takes a square as its argument
and returns the contents of the position at that square.

<P>Here's the final version of <CODE>substitute-triple</CODE>:

<P><PRE>(define (<A NAME="g5"></A>substitute-triple combination position)
  (accumulate word
	      (every (lambda (square)
		       (substitute-letter square position))
		     combination)))

&gt; (substitute-triple 456 '_xo_x_o__)
&quot;4X6"

&gt; (substitute-triple 147 '_xo_x_o__)
&quot;14O"

&gt; (substitute-triple 357 '_xo_x_o__)
OXO
</PRE>

<P>As you can see, Scheme prints some of these words with
double-quote marks.  The rule is that a word that isn't a number but begins
with a digit must be double-quoted.  But in the finished program we're not
going to print such words at all; we're just showing you the working of a
helper procedure.  Similarly, in this chapter we'll show direct invocations
of helper procedures in which some of the arguments are strings, but a
user of the overall program won't have to use this notation.

<P>We've fixed the <CODE>substitute-letter</CODE> problem by giving <CODE>substitute-triple</CODE> an extra argument, so we're going to have to go through
the same process with <CODE>find-triples</CODE>.  Here's the right version:

<P><PRE>(define (<A NAME="g6"></A>find-triples position)
  (every (lambda (comb) (substitute-triple comb position))
         '(123 456 789 147 258 369 159 357)))
</PRE>

<P>It's the same trick.  <CODE>Substitute-triple</CODE> is a procedure
of two arguments.  We use <CODE>lambda</CODE> to transform it into a procedure
of one argument for use with <CODE>every</CODE>.

<P>We've now finished <CODE>find-triples</CODE>, one of the most important procedures in
the game.

<P><PRE>&gt; (find-triples '_xo_x_o__)
(&quot;1XO&quot; &quot;4X6&quot; O89 &quot;14O&quot; XX8 O69 &quot;1X9&quot; OXO)

&gt; (find-triples 'x_____oxo)
(X23 456 OXO X4O &quot;25X&quot; &quot;36O&quot; X5O &quot;35O&quot;)
</PRE>

<P>Here again are the jobs of all three procedures we've written so far:

<P><TABLE>
<TR><TD><CODE>Substitute-letter&nbsp;&nbsp;</CODE>
<TD>finds the letter in a single square.
<TR><TD><CODE>Substitute-triple</CODE>
<TD>finds all three leters corresponding to three squares.
<TR><TD><CODE>Find-triples</CODE>
<TD>finds all the letters in all eight winning combinations.
</TABLE>


<P>We've done all this because we think that the rest of the program can use
the triples we've computed as data.  So we'll just compute the triples once
for all the other procedures to use:

<P><PRE>(define (<A NAME="g7"></A>ttt position me)
  (ttt-choose (find-triples position) me))

(define (ttt-choose triples me)              ;; first version
  (cond ((i-can-win? triples me)
         (choose-winning-move triples me))
        ((opponent-can-win? triples me)
         (block-opponent-win triples me))
        &hellip;))
</PRE>

<P><H2>Can the Computer Win on This Move?</H2>

<P>The obvious next step is to write <CODE>i-can-win?</CODE>, a procedure that should
return <CODE>#t</CODE> if the computer can win on the current move&mdash;that is, if
the computer already has two squares of a triple whose third square is empty.
The triples <CODE>x6x</CODE> and <CODE>oo7</CODE> are examples.

<P>So we need a function that takes a word and a letter as arguments
and counts how many times that letter appears in the word.  The
<CODE>appearances</CODE> primitive that we used in Chapter 2 (and
that you re-implemented in Exercise <A HREF="../ssch9/lambda.html#appear">9.10</A>) will do the job:

<P><PRE>&gt; (appearances 'o 'oo7)
2

&gt; (appearances 'x 'oo7)
0
</PRE>

<P>The computer &quot;owns&quot; a triple if the computer's letter appears twice and
the opponent's letter doesn't appear at all.  (The second condition is
necessary to exclude cases like <CODE>xxo</CODE>.)

<P><PRE>(define (<A NAME="g8"></A>my-pair? triple me)
  (and (= (appearances me triple) 2)
       (= (appearances (opponent me) triple) 0)))
</PRE>

<P>Notice that we need a function <CODE>opponent</CODE> that returns the opposite
letter from ours.

<P><PRE>(define (<A NAME="g9"></A>opponent letter)
  (if (equal? letter 'x) 'o 'x))

&gt; (opponent 'x)
O

&gt; (opponent 'o)
X

&gt; (my-pair? 'oo7 'o)
#T

&gt; (my-pair? 'xo7 'o)
#F

&gt; (my-pair? 'oox 'o)
#F
</PRE>

<P>Finally, the computer can win if it owns any of the triples:

<P><PRE>(define (i-can-win? triples me)                ;; first version
  (not (empty?
	(keep (lambda (triple) (my-pair? triple me))
	      triples))))

&gt; (i-can-win? '(&quot;1xo&quot; &quot;4x6&quot; o89 &quot;14o&quot; xx8 o69 &quot;1x9&quot; oxo) 'x)
#T

&gt; (i-can-win? '(&quot;1xo&quot; &quot;4x6&quot; o89 &quot;14o&quot; xx8 o69 &quot;1x9&quot; oxo) 'o)
#F
</PRE>

<P>By now you're accustomed to this trick with <CODE>lambda</CODE>.  <CODE>My-pair?</CODE> takes a triple and the computer's letter as arguments, but we
want a function of one argument for use with <CODE>keep</CODE>.

<P><H2>If So, in Which Square?</H2>

<P>Suppose <CODE>i-can-win?</CODE> returns <CODE>#t</CODE>.  We then have to find the
particular square that will win the game for us.  This will involve a
repetition of some of the same work we've already done:

<P><PRE>(define (choose-winning-move triples me)     ;; not really part of game
  (keep number? (first (keep (lambda (triple) (my-pair? triple me))
                             triples))))
</PRE>

<P>We again use <CODE>keep</CODE> to find the triples with two of the
computer's letter, but this time we extract the number from the first such
winning triple.

<P>We'd like to avoid this inefficiency.  As it turns out, generations of Lisp
programmers have been in just this bind in the past, and so they've invented
a kludge<A NAME="text1" HREF="ttt.html#ft1">[1]</A> to get around it.

<P>Remember we told you that everything other than <CODE>#f</CODE> counts as true?
We'll take advantage of that by having a single procedure that returns the
number of a winning square if one is available, or <CODE>#f</CODE> otherwise.  In
Chapter 6 we called such a procedure a &quot;semipredicate.&quot; The kludgy
part is that <A NAME="g10"></A><CODE>cond</CODE> accepts a clause containing a single expression
instead of the usual two expressions; if the expression has any true value,
then <CODE>cond</CODE> returns that value.  So we can say

<P><PRE>(define (ttt-choose triples me)              ;; second version
  (cond ((i-can-win? triples me))
        ((opponent-can-win? triples me))
        &hellip;))
</PRE>

<P>where each <CODE>cond</CODE> clause invokes a semipredicate.  We then
modify <CODE>i-can-win?</CODE> to have the desired behavior:

<P><PRE>(define (<A NAME="g11"></A>i-can-win? triples me)
  (choose-win
   (keep (lambda (triple) (my-pair? triple me))
	 triples)))

(define (<A NAME="g12"></A>choose-win winning-triples)
  (if (empty? winning-triples)
      #f
      (keep number? (first winning-triples))))

&gt; (i-can-win? '(&quot;1xo&quot; &quot;4x6&quot; o89 &quot;14o&quot; xx8 o69 &quot;1x9&quot; oxo) 'x)
8

&gt; (i-can-win? '(&quot;1xo&quot; &quot;4x6&quot; o89 &quot;14o&quot; xx8 o69 &quot;1x9&quot; oxo) 'o)
#F
</PRE>

<P>

<P>By this point, we're starting to see the structure of the overall program.
There will be several procedures, similar to <CODE>i-can-win?</CODE>, that will try
to choose the next move.  <CODE>I-can-win?</CODE> checks to see if the computer can
win on this turn, another procedure will check to see if the computer should
block the opponent's win next turn, and other procedures will check for
other possibilities.  Each of these procedures will be what we've been
calling &quot;semipredicates.&quot; That is to say, each will return the number of
the square where the computer should move next, or <CODE>#f</CODE> if it can't
decide.  All that's left is to figure out the rest of the computer's
strategy and write more procedures like <CODE>i-can-win?</CODE>.

<P><H2>Second Verse, Same as the First</H2>

<P>Now it's time to deal with the second possible strategy case:  The computer
can't win on this move, but the opponent can win unless we block a triple
right now.

<P>(What if the computer and the opponent both have immediate winning triples?
In that case, we've already noticed the computer's win, and by winning the
game we avoid having to think about blocking the opponent.)

<P>Once again, we have to go through the complicated business of finding
triples that have two of the opponent's letter and none of the computer's
letter&mdash;but it's already done!

<P>

<P><PRE>(define (<A NAME="g13"></A>opponent-can-win? triples me)
  (i-can-win? triples (opponent me)))

&gt; (opponent-can-win? '(&quot;1xo&quot; &quot;4x6&quot; o89 &quot;14o&quot; xx8 o69 &quot;1x9&quot; oxo) 'x)
#F

&gt; (opponent-can-win? '(&quot;1xo&quot; &quot;4x6&quot; o89 &quot;14o&quot; xx8 o69 &quot;1x9&quot; oxo) 'o)
8
</PRE>

<P>

<P>

<P>Is that amazing or what?

<P>
<H2>Now the Strategy Gets Complicated</H2>

<P>Since our goal here is to teach programming, rather than tic-tac-toe
strategy, we're just going to explain the strategy we use and not give the
history of how we developed it.

<P>The third step, after we check to see if either player can win on the next
move, is to look for a situation in which a move that we make now will give
rise to <EM>two</EM> winning triples next time.  Here's an example:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>x<TD>o<TD><TR><TD><TD>x<TD><TR><TD><TD><TD>o</TABLE>&nbsp;

<P>Neither <CODE>x</CODE> nor <CODE>o</CODE> can win on this move.  But if the computer is
playing <CODE>x</CODE>, moving in square 4 or square 7 will produce a situation
with two winning triples.  For example, here's what happens if we move in
square 7:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>x<TD>o<TD><TR><TD><TD>x<TD><TR><TD>x<TD><TD>o</TABLE>&nbsp;

<P>From this position, <CODE>x</CODE> can win by moving either in square
3 or in square 4.  It's <CODE>o</CODE>'s turn, but <CODE>o</CODE> can block only one of
these two possibilities.  By contrast, if (in the earlier position) <CODE>x</CODE>
moves in square 3 or square 6, that would create a single winning triple for
next time, but <CODE>o</CODE> could block it.

<P>In other words, we want to find <EM>two</EM> triples in which one square is
taken by the computer and the other two are free, with one free square
shared between the two triples.  (In this example, we might find the two
triples <CODE>x47</CODE> and <CODE>3x7</CODE>; that would lead us to move in square 7, the
one that these triples have in common.)  We'll call a situation like this a
&quot;fork,&quot; and we'll call the common square the &quot;pivot.&quot; This
isn't standard terminology; we just made up these terms to make it easier to
talk about the strategy.

<P>In order to write the strategy procedure <CODE>i-can-fork?</CODE> we assume that
we'll need a procedure <CODE>pivots</CODE> that returns a sentence of all pivots of
forks currently available to the computer.  In this board, 4 and 7 are the
pivots, so the <CODE>pivots</CODE> procedure would return the sentence <CODE>(4 7)</CODE>.  If we assume <CODE>pivots</CODE>, then writing <CODE>i-can-fork?</CODE> is
straightforward:

<P><PRE>(define (<A NAME="g14"></A>i-can-fork? triples me)
  (first-if-any (pivots triples me)))

(define (<A NAME="g15"></A>first-if-any sent)
  (if (empty? sent)
      #f
      (first sent)))
</PRE>

<P><H2>Finding the Pivots</H2>

<P><CODE>Pivots</CODE> should return a sentence containing the pivot numbers.  Here's
the plan.  We'll start with the triples:

<P><PRE>(xo3 4x6 78o x47 ox8 36o xxo 3x7)
</PRE>

<P>We <CODE>keep</CODE> the ones that have an <CODE>x</CODE> and two numbers:

<P><PRE>(4x6 x47 3x7)
</PRE>

<P>We mash these into one huge word:

<P><PRE>4x6x473x7
</PRE>

<P>We sort the digits from this word into nine &quot;buckets,&quot; one for
each digit:

<P><PRE>(&quot;&quot; &quot;&quot; 3 44 &quot;&quot; 6 77 &quot;&quot; &quot;&quot;)
</PRE>

<P>We see that there are no ones or twos, one three, two fours, and
so on.  Now we can easily see that four and seven are the pivot squares.

<P>Let's write the procedures to carry out that plan.  <CODE>Pivots</CODE> has to find
all the triples with one computer-owned square and two free squares, and
then it has to extract the square numbers that appear in more than one
triple.

<P><PRE>(define (<A NAME="g16"></A>pivots triples me)
  (repeated-numbers (keep (lambda (triple) (my-single? triple me))
			  triples)))

(define (<A NAME="g17"></A>my-single? triple me)
  (and (= (appearances me triple) 1)
       (= (appearances (opponent me) triple) 0)))

&gt; (my-single? &quot;4x6&quot; 'x)
#T

&gt; (my-single? 'xo3 'x)
#F

&gt; (keep (lambda (triple) (my-single? triple 'x))
	(find-triples 'xo__x___o))
(&quot;4X6&quot; X47 &quot;3X7&quot;)
</PRE>

<P><CODE>My-single?</CODE> is just like <CODE>my-pair?</CODE> except that it looks
for one appearance of the letter instead of two.

<P><CODE>Repeated-numbers</CODE> takes a sentence of triples as its argument and has
to return a sentence of all the numbers that appear in more than one
triple.

<P><PRE>(define (<A NAME="g18"></A>repeated-numbers sent)
  (every first
         (keep (lambda (wd) (&gt;= (count wd) 2))
	       (sort-digits (accumulate word sent)))))
</PRE>

<P>We're going to read this procedure inside-out, starting with the
<CODE>accumulate</CODE> and working outward.

<P>Why is it okay to <CODE>accumulate word</CODE> the sentence?  Suppose that a number
appears in two triples.  All we need to know is that number, not the
particular triples through which we found it.  Therefore, instead of writing
a program to look through several triples separately, we can just as well
combine the triples into one long word, keep only the digits of that word,
and simply look for the ones that appear more than once.

<P><PRE>&gt; (accumulate word '(&quot;4x6&quot; x47 &quot;3x7&quot;))
&quot;4X6X473X7"
</PRE>

<P>We now have one long word, and we're looking for repeated digits.  Since
this is a hard problem, let's start with the subproblem of finding all the
copies of a particular digit.

<P><PRE>(define (<A NAME="g19"></A>extract-digit desired-digit wd)
  (keep (lambda (wd-digit) (equal? wd-digit desired-digit)) wd))

&gt; (extract-digit 7 &quot;4x6x473x7&quot;)
77

&gt; (extract-digit 2 &quot;4x6x473x7&quot;)
&quot;"
</PRE>

<P>Now we want a sentence where the first word is all the <CODE>1</CODE>s, the second
word is all the <CODE>2</CODE>s, etc.  We could do it like this:

<P><PRE>(se (extract-digit 1 &quot;4x6x473x7&quot;)
    (extract-digit 2 &quot;4x6x473x7&quot;)
    (extract-digit 3 &quot;4x6x473x7&quot;)
    &hellip;)
</PRE>

<P>but that wouldn't be taking advantage of the power of computers to
do that sort of repetitious work for us.  Instead, we'll use <CODE>every</CODE>:

<P><PRE>(define (<A NAME="g20"></A>sort-digits number-word)
  (every (lambda (digit) (extract-digit digit number-word))
	 '(1 2 3 4 5 6 7 8 9)))
</PRE>

<P><CODE>Sort-digits</CODE> takes a word full of numbers and returns a sentence whose
first word is all the ones, second word is all the twos, etc.<A NAME="text2" HREF="ttt.html#ft2">[2]</A>

<P><PRE>&gt; (sort-digits 123456789147258369159357)
(111 22 333 44 5555 66 777 88 999)

&gt; (sort-digits &quot;4x6x473x7&quot;)
(&quot;&quot; &quot;&quot; 3 44 &quot;&quot; 6 77 &quot;&quot; &quot;&quot;)
</PRE>

<P>Let's look at <CODE>repeated-numbers</CODE> again:

<P><PRE>(define (repeated-numbers sent)
  (every first
	 (keep (lambda (wd) (&gt;= (count wd) 2))
	       (sort-digits (accumulate word sent)))))

&gt; (repeated-numbers '(&quot;4x6&quot; x47 &quot;3x7&quot;))
(4 7)

&gt; (keep (lambda (wd) (&gt;= (count wd) 2))
	'(&quot;&quot; &quot;&quot; 3 44 &quot;&quot; 6 77 &quot;&quot; &quot;&quot;))
(44 77)

&gt; (every first '(44 77))
(4 7)
</PRE>

<P>This concludes the explanation of <CODE>pivots</CODE>.  Remember that <CODE>i-can-fork?</CODE> chooses the first pivot, if any, as the computer's move.

<P><H2>Taking the Offensive</H2>

<P>Here's the final version of <CODE>ttt-choose</CODE> with all the clauses shown:

<P><PRE>(define (<A NAME="g21"></A>ttt-choose triples me)
  (cond ((i-can-win? triples me))
        ((opponent-can-win? triples me))
        ((i-can-fork? triples me))
        ((i-can-advance? triples me))
        (else (best-free-square triples))))
</PRE>

<P>You already know about the first three possibilities.

<P>Just as the second possibility was the &quot;mirror image&quot; of the first
(blocking an opponent's move of the same sort the computer just attempted), 
it would make sense for the fourth possibility to be blocking the creation
of a fork by the opponent.  That would be easy to do:

<P><PRE>(define (opponent-can-fork? triples me)      ;; not really part of game
  (i-can-fork? triples (opponent me)))
</PRE>

<P>Unfortunately, although the programming works, the strategy doesn't.  Maybe
the opponent has <EM>two</EM> potential forks; we can block only one of
them.  (Why isn't that a concern when blocking the opponent's wins?  It <EM>is</EM> a concern, but if we've allowed the situation to reach the point where
there are two ways the opponent can win on the next move, it's too late to
do anything about it.)

<P>Instead, our strategy is to go on the offensive.  If we can get two in a row
on this move, then our opponent will be forced to block on the next move,
instead of making a fork.  However, we want to make sure that we don't
accidentally force the opponent <EM>into</EM> making a fork.

<P>Let's look at this board position again, but from <CODE>o</CODE>'s point of view:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>x<TD>o<TD><TR><TD><TD>x<TD><TR><TD><TD><TD>o</TABLE>&nbsp;

<P><CODE>X</CODE>'s pivots are 4 and 7, as we discussed earlier; <CODE>o</CODE>
couldn't take both those squares.  Instead, look at the triples <CODE>369</CODE>
and <CODE>789</CODE>, both of which are singles that belong to <CODE>o</CODE>.  So <CODE>o</CODE>
should move in one of the squares 3, 6, 7, or 8, forcing <CODE>x</CODE> to block
instead of setting up the fork.  But <CODE>o</CODE> <EM>shouldn't</EM> move in square
8, like this:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>x<TD>o<TD><TR><TD><TD>x<TD><TR><TD><TD>o<TD>o</TABLE>&nbsp;

<P>because that would force <CODE>x</CODE> to block in square 7, setting up
a fork!

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>x<TD>o<TD><TR><TD><TD>x<TD><TR><TD>x<TD>o<TD>o</TABLE>&nbsp;

<P>The structure of the algorithm is much like that of the other strategy
possibilities.  We use <CODE>keep</CODE> to find the appropriate triples, take the
first such triple if any, and then decide which of the two empty squares in
that triple to move into.

<P><PRE>(define (<A NAME="g22"></A>i-can-advance? triples me)
  (best-move (keep (lambda (triple) (my-single? triple me)) triples)
             triples
             me))

(define (<A NAME="g23"></A>best-move my-triples all-triples me)
  (if (empty? my-triples)
      #f
      (best-square (first my-triples) all-triples me)))
</PRE>

<P><CODE>Best-move</CODE> does the same job as <CODE>first-if-any</CODE>, which we
saw earlier, except that it also invokes <CODE>best-square</CODE> on the first
triple if there is one.  

<P>Since we've already chosen the relevant triples before we get to <CODE>best-move</CODE>, you may be wondering why it needs <EM>all</EM> the triples as an
additional argument.  The answer is that <CODE>best-square</CODE> is going to look
at the board position from the opponent's point of view to look for forks.

<P><PRE>(define (<A NAME="g24"></A>best-square my-triple triples me)
  (best-square-helper (pivots triples (opponent me))
		      (keep number? my-triple)))

(define (<A NAME="g25"></A>best-square-helper opponent-pivots pair)
  (if (member? (first pair) opponent-pivots)
      (first pair)
      (last pair)))
</PRE>

<P>We <CODE>keep</CODE> the two numbers of the triple that we've already selected.  We
also select the opponent's possible pivots from among all the triples.  If
one of our two possible moves is a potential pivot for the opponent, that's
the one we should move into, to block the fork.  Otherwise, we arbitrarily
pick the second (<CODE>last</CODE>) free square.

<P><PRE>&gt; (best-square &quot;78o&quot; (find-triples 'xo__x___o) 'o)
7

&gt; (best-square &quot;36o&quot; (find-triples 'xo__x___o) 'o)
6

&gt; (best-move '(&quot;78o&quot; &quot;36o&quot;) (find-triples 'xo__x___o) 'o)
7

&gt; (i-can-advance? (find-triples 'xo__x___o) 'o)
7
</PRE>

<P>What if <EM>both</EM> of the candidate squares are pivots for the opponent?
In that case, we've picked a bad triple; moving in either square will make
us lose.  As it turns out, this can occur only in a situation like the
following:

<P>&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>x<TD><TD><TR><TD><TD>o<TD><TR><TD><TD><TD>x</TABLE>&nbsp;

<P>If we chose the triple <CODE>3o7</CODE>, then either move will force the
opponent to set up a fork, so that we lose two moves later.  Luckily,
though, we can instead choose a triple like <CODE>2o8</CODE>.  We can move in
either of those squares and the game will end up a tie.

<P>In principle, we should analyze a candidate triple to see if both free
squares create forks for the opponent.  But since we happen to know that
this situation arises only on the diagonals, we can be lazier.  We just list
the diagonals <EM>last</EM> in the procedure <CODE>find-triples</CODE>.  Since we
take the first available triple, this ensures that we won't take a diagonal
if there are any other choices.<A NAME="text3" HREF="ttt.html#ft3">[3]</A>

<P><H2>Leftovers</H2>

<P>If all else fails, we just pick a square.  However, some squares are better
than others.  The center square is part of four triples, the corner squares
are each part of three, and the edge squares each a mere two.

<P>So we pick the center if it's free, then a corner, then an edge.

<P><PRE>(define (<A NAME="g26"></A>best-free-square triples)
  (first-choice (accumulate word triples)
		'(5 1 3 7 9 2 4 6 8)))

(define (<A NAME="g27"></A>first-choice possibilities preferences)
  (first (keep (lambda (square) (member? square possibilities))
	       preferences)))

&gt; (first-choice 123456789147258369159357 '(5 1 3 7 9 2 4 6 8))
5

&gt; (first-choice &quot;1xo4x6o8914oxx8o691x9oxo&quot; '(5 1 3 7 9 2 4 6 8))
1

&gt; (best-free-square (find-triples '_________))
5

&gt; (best-free-square (find-triples '____x____))
1
</PRE>

<P><H2>Complete Program Listing</H2>

<P><P><P><PRE>
;;; ttt.scm
;;; Tic-Tac-Toe program

(define (ttt position me)
  (ttt-choose (find-triples position) me))

(define (find-triples position)
  (every (lambda (comb) (substitute-triple comb position))
         '(123 456 789 147 258 369 159 357)))

(define (substitute-triple combination position)
  (accumulate word
	      (every (lambda (square)
		       (substitute-letter square position))
		     combination) ))

(define (substitute-letter square position)
  (if (equal? '_ (item square position))
      square
      (item square position) ))

(define (ttt-choose triples me)
  (cond ((i-can-win? triples me))
        ((opponent-can-win? triples me))
        ((i-can-fork? triples me))
        ((i-can-advance? triples me))
        (else (best-free-square triples)) ))

(define (i-can-win? triples me)
  (choose-win
   (keep (lambda (triple) (my-pair? triple me))
         triples)))

(define (my-pair? triple me)
  (and (= (appearances me triple) 2)
       (= (appearances (opponent me) triple) 0)))

(define (opponent letter)
  (if (equal? letter 'x) 'o 'x))

(define (choose-win winning-triples)
  (if (empty? winning-triples)
      #f
      (keep number? (first winning-triples)) ))

(define (opponent-can-win? triples me)
  (i-can-win? triples (opponent me)) )

(define (i-can-fork? triples me)
  (first-if-any (pivots triples me)) )

(define (first-if-any sent)
  (if (empty? sent)
      #f
      (first sent) ))

(define (pivots triples me)
  (repeated-numbers (keep (lambda (triple) (my-single? triple me))
                          triples)))

(define (my-single? triple me)
  (and (= (appearances me triple) 1)
       (= (appearances (opponent me) triple) 0)))

(define (repeated-numbers sent)
  (every first
         (keep (lambda (wd) (>= (count wd) 2))
               (sort-digits (accumulate word sent)) )))

(define (sort-digits number-word)
  (every (lambda (digit) (extract-digit digit number-word))
         '(1 2 3 4 5 6 7 8 9) ))

(define (extract-digit desired-digit wd)
  (keep (lambda (wd-digit) (equal? wd-digit desired-digit)) wd))

(define (i-can-advance? triples me)
  (best-move (keep (lambda (triple) (my-single? triple me)) triples)
             triples
             me))

(define (best-move my-triples all-triples me)
  (if (empty? my-triples)
      #f
      (best-square (first my-triples) all-triples me) ))

(define (best-square my-triple triples me)
  (best-square-helper (pivots triples (opponent me))
		      (keep number? my-triple)))

(define (best-square-helper opponent-pivots pair)
  (if (member? (first pair) opponent-pivots)
      (first pair)
      (last pair)))

(define (best-free-square triples)
  (first-choice (accumulate word triples)
                '(5 1 3 7 9 2 4 6 8)))

(define (first-choice possibilities preferences)
  (first (keep (lambda (square) (member? square possibilities))
               preferences)))
</PRE><P>


<P><H2>Exercises</H2>

<P><B>10.1</B>&nbsp;&nbsp;The <CODE>ttt</CODE> procedure assumes that nobody has won the game
yet.  What happens if you invoke <CODE>ttt</CODE> with a board position in which
some player has already won?  Try to figure it out by looking through the
program before you run it.

<P>A complete tic-tac-toe program would need to stop when one of the two
players wins.  Write a predicate <CODE><A NAME="g28"></A>already-won?</CODE> that takes a
board position and a letter (<CODE>x</CODE> or <CODE>o</CODE>) as its arguments and returns
<CODE>#t</CODE> if that player has already won.
<A NAME="tttwon"></A>


<P>
<B>10.2</B>&nbsp;&nbsp;The program also doesn't notice when the game has ended in a tie, that is,
when all nine squares are already filled.  What happens now if you ask it to
move in this situation?

<P>Write a procedure <CODE><A NAME="g29"></A>tie-game?</CODE> that returns <CODE>#t</CODE> in this case.
<A NAME="ttttied"></A>


<P>
<B>10.3</B>&nbsp;&nbsp;A real human playing tic-tac-toe would look at a board like this:

<P>
&nbsp;<TABLE rules="all" frame="void" border="2"><TR><TD>o<TD>x<TD>o<TR><TD>o<TD>x<TD>x<TR><TD>x<TD>o<TD></TABLE>&nbsp;

<P>

<P>and notice that it's a tie, rather than moving in square <CODE>9</CODE>.
Modify <CODE>tie-game?</CODE> from Exercise <A HREF="ttt.html#ttttied">10.2</A> to notice this situation and
return <CODE>#t</CODE>.

<P>(Can you improve the program's ability to recognize ties even further?
What about boards with two free squares?)


<P>
<B>10.4</B>&nbsp;&nbsp;Here are some possible changes to the rules of tic-tac-toe:

<P><P>
<TABLE><TR><TH align="right" valign="top">&bull;<TD>&nbsp;&nbsp;&nbsp;&nbsp;<TD valign="top">What if you could
win a game by having three squares forming an L shape in a corner, such
as squares 1, 2, and 4?

</TABLE><TABLE><TR><TH align="right" valign="top">&bull;<TD>&nbsp;&nbsp;&nbsp;&nbsp;<TD valign="top">What if the diagonals didn't win?

</TABLE><TABLE><TR><TH align="right" valign="top">&bull;<TD>&nbsp;&nbsp;&nbsp;&nbsp;<TD valign="top">What if you could win by having <EM>four</EM>
squares in a corner, such as 1, 2, 4, and 5?

</TABLE><P>
Answer the following questions for each of these modifications separately:
What would happen if we tried to implement the change merely by changing the
quoted sentence of potential winning combinations in <CODE>find-triples</CODE>?
Would the program successfully follow the rules as modified?


<P>
<B>10.5</B>&nbsp;&nbsp;Modify <CODE>ttt</CODE> to play chess.


<P>


<HR>
<A NAME="ft1" HREF="ttt.html#text1">[1]</A> A kludge is a programming trick that doesn't follow
the rules but works anyway somehow.  It doesn't rhyme with &quot;sludge&quot;; it's
more like &quot;clue&quot; followed by &quot;j&quot; as in &quot;Jim.&quot;<P>
<A NAME="ft2" HREF="ttt.html#text2">[2]</A> Brian
thinks this is a kludge, but Matt thinks it's brilliant and elegant.<P>
<A NAME="ft3" HREF="ttt.html#text3">[3]</A> Matt thinks this is a
kludge, but Brian thinks it's brilliant and elegant.<P>
<P><A HREF="../ss-toc2.html">(back to Table of Contents)</A><P>
<A HREF="../ssch9/bridge.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch11/part4.html"><STRONG>NEXT</STRONG></A>

<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>, 
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>