;; --------------------------------------------- ;; John Paxton ;; September 25, 2007 ;; CS 436 ;; go.l ;; --------------------------------------------- ;; This file contains a solution to the first ;; CS 436 programming assignment. ;; --------------------------------------------- (defvar *size* 5) ;; number of stones to produce a win ;; --------------------------------------------- ;; five-in-row-p ;; --------------------------------------------- ;; board: an n by n matrix representing the current go board ;; x: the x position of the last stone played ;; y: the y position of the last stone played ;; --------------------------------------------- ;; Return true if the last stone played produces ;; a vertical, horizontal or diagonal line of 5 ;; or more stones in a row. ;; --------------------------------------------- (defun five-in-row-p (board x y) (or (five-aux-p board x y 0 1) ;; up-down (five-aux-p board x y 1 0) ;; right-left (five-aux-p board x y 1 1) ;; diagonal 1 (five-aux-p board x y 1 -1) ;; diagonal 2 ) ) ;; --------------------------------------------- ;; five-aux-p ;; --------------------------------------------- ;; board: an n by n matrix representing the current go board ;; x: x position of last stone played ;; y: y position of last stone played ;; x-inc: how to change x ;; y-inc: how to change y ;; --------------------------------------------- ;; Returns t if the number of like colored stones in ;; a single line are greater than or equal to *size* ;; --------------------------------------------- (defun five-aux-p (board x y x-inc y-inc) (>= (+ 1 (count-stones (aref board x y) board x y x-inc y-inc) (count-stones (aref board x y) board x y (- x-inc) (- y-inc)) ) *size*) ) ;; --------------------------------------------- ;; count-stones ;; --------------------------------------------- ;; color: the color of the last stone played ;; board: an n by n matrix representing the current go board ;; x: the last x position checked ;; y: the last y position checked ;; x-inc: how to change x ;; y-inc: how to change y ;; result: the number of like colored stones in a line ;; --------------------------------------------- ;; Return the number of stones in a line that match "color" ;; --------------------------------------------- (defun count-stones (color board x y x-inc y-inc &optional (result 0)) (setf x (+ x x-inc)) (setf y (+ y y-inc)) (if (and (legal-p (array-dimension board 0) x) (legal-p (array-dimension board 1) y) (equal (aref board x y) color)) (count-stones color board x y x-inc y-inc (+ result 1)) result ) ) ;; --------------------------------------------- ;; legal-p ;; --------------------------------------------- ;; max-value: the largest legal value ;; value: the value to check ;; --------------------------------------------- ;; Return true if 0 <= value < max-value ;; --------------------------------------------- (defun legal-p (max-value value) (and (>= value 0) (< value max-value)) )