Connect 4 board analysis example

Here's an example of using the open-rows, open-columns, open-diagonals functions. Please make sure that you've gotten the most recent versions of the a4support.com and connect4.scm files because some errors have been corrected.

Getting a board state

First, you want to get a board state that you can work with. Instead of creating a state manually, you can simply play against the random player. Or you can play both sides of the game if you want to control the board that you get. By giving the play-c4 function an optional third argument, it will print the raw state of the board which you can then use for your experiments.

(load "a4code")
;Loading "a4code.com" -- done
;Value: inf:=
(load "connect4")
;Loading "connect4.scm" -- done
;Value: count-re-matches

(play-c4 random-player human-player #t)

...

    +---------------+
   6|               |
   5|           X   |
   4|           X   |
   3|     O   X O   |
   2|     O   O X   |
   1|     O   X X   |
    +---------------+
      1 2 3 4 5 6 7
("       |     X |     X |  O XO |  O OX |  O XX " (0 0 3 0 3 5 0))


You are playing O

Which column do you want to play in? (1-7 inclusive)
Enter your move and press return (or C-x C-e in Emacs/Edwin
;Quit!

Analyzing a board state

You can now define a variable to be this board state by cutting and pasting the raw board state.

(define b '("       |     X |     X |  O XO |  O OX |  O XX " (0 0 3 0 3 5 0)))
;Value: b

(print-board b)
    +---------------+
   6|               |
   5|           X   |
   4|           X   |
   3|     O   X O   |
   2|     O   O X   |
   1|     O   X X   |
    +---------------+
      1 2 3 4 5 6 7
;No value

(open-rows b 'x)
;Value 1: ((0 0) (0 0) (1 0))
;
; This reflects that X has a row of two open on both sides in the
; first row and no other open rows of two or three pieces
;

(open-rows b 'o)
;Value 2: ((0 0) (0 0) (0 0))
;
;  This reflects that O has no rows of two or three pieces.
;

(open-columns b 'x)
;Value 3: (1 0)
;
;  This indicates the open column of two X pieces in column 6.
;

(open-columns b 'o)
;Value 4: (0 1)
;
;  This indicates the open column of three O pieces in column 3.
;

(open-diagonals b 'x)
;Value 5: ((1 0) (0 0) (2 0))
;
;  This shows that X has a diagonal of two pieces open at the top:
;  (column, row) = (5, 1) - (6, 2).  And X has two diagonals 
;  of two pieces open at both ends: (6, 2) - (5, 3) and (5, 3) - (6, 4)
;

(open-diagonals b 'o)
;Value 6: ((0 0) (0 0) (1 0))
;
;  This shows that O has a diagonal of two pieces open at both ends:
;  (5, 2) - (6, 3)
;

whuang@cs.rpi.edu; email
Last updates: September 30; September 26; September 25, 2000