(load "a4code") (load "connect4") ; first four moves (X, O, X, O) will only be searched to depth one (define depth-one-moves 4) (define (c4-eval board current-player max-player) (let ((m (apply + (second board)))) (if (> m depth-one-moves) (real-c4-eval board current-player max-player) (check-openings m board current-player max-player)))) ; the objective in this procedure is to recognize whether the pattern ; on the current board (which is after your player has made a move) is ; a desirable pattern or not. Otherwise, refer it to the general ; evaluation function. (define (check-openings num-moves board current-player max-player) (case num-moves ; for the first move (X), play in column 2 ((1) (if (char=? (string-ref (first board) 41) #\X) 10 -10)) ; for the second move (O), play in column 6, regardless of whether ; X played there or not ((2) (if (or (char=? (string-ref (first board) 45) #\O) (char=? (string-ref (first board) 37) #\O)) 10 -10)) ; for the third move (X): if X played in column 2 and O played in ; column 1, then play in column 6 ((3) (if (and (char=? (string-ref (first board) 41) #\X) (char=? (string-ref (first board) 40) #\O) (char=? (string-ref (first board) 45) #\X)) 10 (real-c4-eval board current-player max-player))) ; for the fourth move: if X played two in the same column, then ; block it ((4) (let checkcol ((c 1)) (cond ((> c 7) (real-c4-eval board current-player max-player)) ((and (char=? (string-ref (first board) (+ 39 c)) #\X) (char=? (string-ref (first board) (+ 31 c)) #\X) (char=? (string-ref (first board) (+ 23 c)) #\O)) 10) (else (checkcol (+ c 1)))))))) (define (real-c4-eval board current-player max-player) (let ((winner (c4-end? board))) (cond ((equal? winner max-player) 1000) ((equal? winner (other-player max-player)) -1000) ((equal? winner 'draw) 0) (else (let ((max-c-threes (cadr (open-columns board max-player))) (min-c-threes (cadr (open-columns board (other-player max-player)))) (max-r-threes (apply + (map cadr (open-rows board max-player)))) (min-r-threes (apply + (map cadr (open-rows board (other-player max-player)))))) (+ max-c-threes max-r-threes (- min-c-threes) (- min-r-threes)))))))