TicTacToe

The program below implements a two-player tictactoe game.

Listing 8 Simple two player tictactoe game.
 1#lang racket
 2
 3(define (tictactoe cross?)
 4  (let play-turn ([board "........."] [cross? cross?])
 5    (displayln (string-append (substring board 0 3) "\n"
 6                              (substring board 3 6) "\n"
 7                              (substring board 6 9) "\n"))
 8    (if (string-contains? board ".")
 9        (if (or (string=? "xxx" (substring board 0 3))
10                (string=? "xxx" (substring board 4 6))
11                (string=? "xxx" (substring board 7 9))
12                (string=? "xxx" (string-append (substring board 0 1)
13                                               (substring board 4 5)
14                                               (substring board 8 9)))
15                (string=? "xxx" (string-append (substring board 2 3)
16                                               (substring board 4 5)
17                                               (substring board 6 7)))
18                (string=? "xxx" (string-append (substring board 0 1)
19                                               (substring board 3 4)
20                                               (substring board 6 7)))
21                (string=? "xxx" (string-append (substring board 1 2)
22                                               (substring board 4 5)
23                                               (substring board 7 8)))
24                (string=? "xxx" (string-append (substring board 2 3)
25                                               (substring board 5 6)
26                                               (substring board 8 9))))
27            'x-won
28            (if (or (string=? "ooo" (substring board 0 3))
29                    (string=? "ooo" (substring board 4 6))
30                    (string=? "ooo" (substring board 7 9))
31                    (string=? "ooo" (string-append (substring board 0 1)
32                                                   (substring board 4 5)
33                                                   (substring board 8 9)))
34                    (string=? "ooo" (string-append (substring board 2 3)
35                                                   (substring board 4 5)
36                                                   (substring board 6 7)))
37                    (string=? "ooo" (string-append (substring board 0 1)
38                                                   (substring board 3 4)
39                                                   (substring board 6 7)))
40                    (string=? "ooo" (string-append (substring board 1 2)
41                                                   (substring board 4 5)
42                                                   (substring board 7 8)))
43                    (string=? "ooo" (string-append (substring board 2 3)
44                                                   (substring board 5 6)
45                                                   (substring board 8 9))))
46                'o-won
47                (let ([next-pos (begin (display "What's your move? ")
48                                       (- (read) 1))])
49                  (if (equal? (string-ref board next-pos) #\.)
50                      (play-turn (string-append (substring board 0 next-pos)
51                                                (if cross? "x" "o")
52                                                (substring board (+ 1 next-pos) 9))
53                                 (not cross?))
54                      (begin (displayln "Invalid position. Try again.")
55                             (play-turn board cross?))))))
56        (displayln "Game over"))))
57                         
58                
59                
60            

Task

Go through the same process as with the The number guessing game to arrive at a legible program for playing the same game. Remember that when you’re starting with this, you probably don’t “understand” the program and that’s ok and expected. Your goal is to “interrogate” and along the way edit/rewrite parts of the program step by step to arrive at a final version that is more easily understood. For reference about documenting abstractions (a.k.a. “procedures” or “functions”) see Systematic program design.

Tip

Approach the process using the scientific method. You’re welcome to make “guesses” (i.e. “hypotheses”) as to what a particular form means, but it would then be up to you to verify, at some point, whether your guess turned out to be valid.

Tip

A good rule of thumb is that when the pattern of an expression repeats in some sense, there is perhaps a concept waiting to be abstracted from the repetitions.