オセロ必勝法 Part3

(defun Othello ()
  (setq table nil)
  (setq sq 8)
  (setq r nil)
  (defun initable ()
    (defun initableat ()
      (cond ((not table) (setq table '((0 0))))
            ((= (caar table) (1- sq)) (setq table (push `(0 ,(1+ (cadr (car table)))) table)))
            (t (setq table (push `(,(1+ (caar table)) ,(cadr (car table))) table)))))
    (defun nest (f n)
      (if (zerop n) nil
        (progn (funcall f)
               (nest f (1- n)))))
    (nest 'initableat (* sq sq))
    (setq table (reverse table))
    (setq table (mapcar (lambda (x) (list x 0)) table)))
  (defun setdisk (x y disk)
    (setq table (mapcar (lambda (z) (cond ((and (eq (caar z) x)
                                                (eq (cadr (car z)) y))
                                           (list (list x y) disk))
                                          (t z)))
                        table)))
  (defun subsetdisk (x y disk)
    (setq subtable (mapcar (lambda (z) (cond ((and (eq (caar z) x)
                                                   (eq (cadr (car z)) y))
                                              (list (list x y) disk))
                                             (t z)))
                           subtable)))
  (defun begintable ()
    (initable)
    (setdisk 3 3 nil)
    (setdisk 3 4 t)
    (setdisk 4 3 t)
    (setdisk 4 4 nil)
    (display))
  (defun neighbor (x)
    (cond ((= x 0) '(-1 -1))
          ((= x 1) '(0 -1))
          ((= x 2) '(1 -1))
          ((= x 3) '(-1 0))
          ((= x 4) '(1 0))
          ((= x 5) '(-1 1))
          ((= x 6) '(0 1))
          ((= x 7) '(1 1))))
  (defun getdisk (x y)
    (cadr (elt table (+ (* sq y) x))))
  (defun turnover (x y color)
    (let ((dir nil)
          (flag nil)
          (i 0)
          (subtable table)
          (nei 8)
          (sq 8))
      (if (not (checkturnover x y color)) '(cant put here)
        (progn (setdisk x y color)
               (defun turnoverat (x y color dir flag)
                 (let ((neix (+ x (car (neighbor dir))))
                       (neiy (+ y (cadr (neighbor dir)))))
                   (if (or (> 0 (+ (* sq neiy) neix))
                           (<= (* sq sq) (+ (* sq neiy) neix))
                           (eq (getdisk neix neiy) 0))
                       (setq subtable table)
                     (if (not flag)
                         (if (eq (getdisk neix neiy) (not color))
                             (progn (subsetdisk neix neiy color)
                                    (turnoverat neix neiy color dir t)))
                       (cond ((eq (getdisk neix neiy) 0) (setq subtable table))
                             ((eq (getdisk neix neiy) (not color))
                              (progn (subsetdisk neix neiy color)
                                     (turnoverat neix neiy color dir t)))
                             ((eq (getdisk neix neiy) color) (setq table subtable)))))))
               (while (< i nei)
                 (turnoverat x y color i flag)
                 (setq i (1+ i)))
               (display)))))
  (defun checkturnover (x y color)
    (let ((dir nil)
          (flag nil)
          (i 0)
          (subtable table)
          (nei 8)
          (sq 8)
          (checkflag nil))
      (defun checkturnoverat (x y color dir flag)
        (let ((neix (+ x (car (neighbor dir))))
              (neiy (+ y (cadr (neighbor dir)))))
          (if (or (> 0 (+ (* sq neiy) neix))
                  (<= (* sq sq) (+ (* sq neiy) neix))
                  (eq (getdisk neix neiy) 0))
              nil
            (if (not flag)
                (if (eq (getdisk neix neiy) (not color))
                    (checkturnoverat neix neiy color dir t))
              (cond ((eq (getdisk neix neiy) 0))
                    ((eq (getdisk neix neiy) (not color)) (checkturnoverat neix neiy color dir t))
                    ((eq (getdisk neix neiy) color) (setq checkflag t)))))))
      (while (< i nei)
        (checkturnoverat x y color i flag)
        (setq i (1+ i)))
      checkflag))
  (defun displaycolor ()
    (mapcar (lambda (x) (cond ((eq (cadr x) 0) 0)
                              ((eq (cadr x) nil) '●)
                              ((eq (cadr x) t) '○)
                              (t '.)))
            table))
  (defun cutoff (table i j)
    (cond ((eq i nil) (progn (setq i 0)
                             (setq j 0)
                             (setq p nil)
                             (setq q nil)
                             (cutoff table i j)))
          ((< i sq) (progn (push (car table) p)
                           (cutoff (cdr table) (1+ i) j)))
          ((< j sq) (progn (push (reverse p) q)
                    (setq i 0)
                    (setq p nil)
                    (cutoff table i (1+ j))))
          (t (reverse q))))
  (defun displaynewline (table)
    (if (eq table nil) nil
      (progn (setq r (concat r (format "%s" (car table)) (string ?\n)))
             (displaynewline (cdr table))))
    r)
  (defun display ()
    (setq r (string ?\n))
    (displaynewline (cutoff (displaycolor) nil nil))))

石をひっくり返す関数と石を置けるかどうかチェックする関数と、
石を表示する関数を作りました。

表示例

(Othello)
display
(begintable)
"
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 ● ○ 0 0 0)
(0 0 0 ○ ● 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
"
(turnover 3 2 t)
"
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 ○ 0 0 0 0)
(0 0 0 ○ ○ 0 0 0)
(0 0 0 ○ ● 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
"
(turnover 2 2 nil)
"
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 ● ○ 0 0 0 0)
(0 0 0 ● ○ 0 0 0)
(0 0 0 ○ ● 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
"
(turnover 0 0 nil)
(cant put here)

次で終わると思います。
遅れてゴメンネ(><)