(defgroup gomoku nil
"Gomoku game between you and Emacs."
:prefix "gomoku-"
:group 'games)
(defcustom gomoku-mode-hook nil
"If non-nil, its value is called on entry to Gomoku mode.
One useful value to include is `turn-on-font-lock' to highlight the pieces."
:type 'hook
:group 'gomoku)
(defvar gomoku-mode-map nil
"Local keymap to use in Gomoku mode.")
(if gomoku-mode-map nil
(setq gomoku-mode-map (make-sparse-keymap))
(define-key gomoku-mode-map "y" 'gomoku-move-nw) (define-key gomoku-mode-map "u" 'gomoku-move-ne) (define-key gomoku-mode-map "b" 'gomoku-move-sw) (define-key gomoku-mode-map "n" 'gomoku-move-se) (define-key gomoku-mode-map "h" 'backward-char) (define-key gomoku-mode-map "l" 'forward-char) (define-key gomoku-mode-map "j" 'gomoku-move-down) (define-key gomoku-mode-map "k" 'gomoku-move-up)
(define-key gomoku-mode-map [kp-7] 'gomoku-move-nw)
(define-key gomoku-mode-map [kp-9] 'gomoku-move-ne)
(define-key gomoku-mode-map [kp-1] 'gomoku-move-sw)
(define-key gomoku-mode-map [kp-3] 'gomoku-move-se)
(define-key gomoku-mode-map [kp-4] 'backward-char)
(define-key gomoku-mode-map [kp-6] 'forward-char)
(define-key gomoku-mode-map [kp-2] 'gomoku-move-down)
(define-key gomoku-mode-map [kp-8] 'gomoku-move-up)
(define-key gomoku-mode-map "\C-n" 'gomoku-move-down) (define-key gomoku-mode-map "\C-p" 'gomoku-move-up)
(define-key gomoku-mode-map "X" 'gomoku-human-plays) (define-key gomoku-mode-map "x" 'gomoku-human-plays) (define-key gomoku-mode-map " " 'gomoku-human-plays) (define-key gomoku-mode-map "\C-m" 'gomoku-human-plays) (define-key gomoku-mode-map "\C-c\C-p" 'gomoku-human-plays) (define-key gomoku-mode-map "\C-c\C-b" 'gomoku-human-takes-back) (define-key gomoku-mode-map "\C-c\C-r" 'gomoku-human-resigns) (define-key gomoku-mode-map "\C-c\C-e" 'gomoku-emacs-plays)
(define-key gomoku-mode-map [kp-enter] 'gomoku-human-plays)
(define-key gomoku-mode-map [insert] 'gomoku-human-plays)
(define-key gomoku-mode-map [down-mouse-1] 'gomoku-click)
(define-key gomoku-mode-map [drag-mouse-1] 'gomoku-click)
(define-key gomoku-mode-map [mouse-1] 'gomoku-click)
(define-key gomoku-mode-map [down-mouse-2] 'gomoku-click)
(define-key gomoku-mode-map [mouse-2] 'gomoku-mouse-play)
(define-key gomoku-mode-map [drag-mouse-2] 'gomoku-mouse-play)
(substitute-key-definition 'previous-line 'gomoku-move-up
gomoku-mode-map (current-global-map))
(substitute-key-definition 'next-line 'gomoku-move-down
gomoku-mode-map (current-global-map))
(substitute-key-definition 'beginning-of-line 'gomoku-beginning-of-line
gomoku-mode-map (current-global-map))
(substitute-key-definition 'end-of-line 'gomoku-end-of-line
gomoku-mode-map (current-global-map))
(substitute-key-definition 'undo 'gomoku-human-takes-back
gomoku-mode-map (current-global-map))
(substitute-key-definition 'advertised-undo 'gomoku-human-takes-back
gomoku-mode-map (current-global-map)))
(defvar gomoku-emacs-won ()
"For making font-lock use the winner's face for the line.")
(defcustom gomoku-font-lock-O-face
(if (display-color-p)
(list (facemenu-get-face 'fg:red) 'bold))
"*Face to use for Emacs' O."
:type '(repeat face)
:group 'gomoku)
(defcustom gomoku-font-lock-X-face
(if (display-color-p)
(list (facemenu-get-face 'fg:green) 'bold))
"*Face to use for your X."
:type '(repeat face)
:group 'gomoku)
(defvar gomoku-font-lock-keywords
'(("O" . gomoku-font-lock-O-face)
("X" . gomoku-font-lock-X-face)
("[-|/\\]" 0 (if gomoku-emacs-won
gomoku-font-lock-O-face
gomoku-font-lock-X-face)))
"*Font lock rules for Gomoku.")
(put 'gomoku-mode 'front-sticky
(put 'gomoku-mode 'rear-nonsticky '(intangible)))
(put 'gomoku-mode 'intangible 1)
(put 'gomoku-mode 'mode-class 'special)
(defun gomoku-mode ()
"Major mode for playing Gomoku against Emacs.
You and Emacs play in turn by marking a free square. You mark it with X
and Emacs marks it with O. The winner is the first to get five contiguous
marks horizontally, vertically or in diagonal.
You play by moving the cursor over the square you choose and hitting \\[gomoku-human-plays].
Other useful commands:
\\{gomoku-mode-map}
Entry to this mode calls the value of `gomoku-mode-hook' if that value
is non-nil. One interesting value is `turn-on-font-lock'."
(interactive)
(setq major-mode 'gomoku-mode
mode-name "Gomoku")
(gomoku-display-statistics)
(use-local-map gomoku-mode-map)
(make-local-variable 'font-lock-defaults)
(setq font-lock-defaults '(gomoku-font-lock-keywords t))
(toggle-read-only t)
(run-hooks 'gomoku-mode-hook))
(defvar gomoku-board-width nil
"Number of columns on the Gomoku board.")
(defvar gomoku-board-height nil
"Number of lines on the Gomoku board.")
(defvar gomoku-board nil
"Vector recording the actual state of the Gomoku board.")
(defvar gomoku-vector-length nil
"Length of gomoku-board vector.")
(defvar gomoku-draw-limit nil
"After how many moves will Emacs offer a draw?")
(defun gomoku-xy-to-index (x y)
"Translate X, Y cartesian coords into the corresponding board index."
(+ (* y gomoku-board-width) x y))
(defun gomoku-index-to-x (index)
"Return corresponding x-coord of board INDEX."
(% index (1+ gomoku-board-width)))
(defun gomoku-index-to-y (index)
"Return corresponding y-coord of board INDEX."
(/ index (1+ gomoku-board-width)))
(defun gomoku-init-board ()
"Create the gomoku-board vector and fill it with initial values."
(setq gomoku-board (make-vector gomoku-vector-length 0))
(let ((i 0) (ii (1- gomoku-vector-length)))
(while (<= i gomoku-board-width) (aset gomoku-board i -1) (aset gomoku-board ii -1) (setq i (1+ i)
ii (1- ii))))
(let ((i 0))
(while (< i gomoku-vector-length)
(aset gomoku-board i -1) (setq i (+ i gomoku-board-width 1)))))
(defvar gomoku-score-table nil
"Vector recording the actual score of the free squares.")
(defconst nil-score 7 "Score of an empty qtuple.")
(defconst Xscore 15 "Score of a qtuple containing one X.")
(defconst XXscore 400 "Score of a qtuple containing two X's.")
(defconst XXXscore 1800 "Score of a qtuple containing three X's.")
(defconst XXXXscore 100000 "Score of a qtuple containing four X's.")
(defconst Oscore 35 "Score of a qtuple containing one O.")
(defconst OOscore 800 "Score of a qtuple containing two O's.")
(defconst OOOscore 15000 "Score of a qtuple containing three O's.")
(defconst OOOOscore 800000 "Score of a qtuple containing four O's.")
(defconst gomoku-score-trans-table
(vector nil-score Xscore XXscore XXXscore XXXXscore 0
Oscore 0 0 0 0 0
OOscore 0 0 0 0 0
OOOscore 0 0 0 0 0
OOOOscore 0 0 0 0 0
0)
"Vector associating qtuple contents to their score.")
(defconst gomoku-winning-threshold OOOOscore
"Threshold score beyond which an Emacs move is winning.")
(defconst gomoku-loosing-threshold XXXXscore
"Threshold score beyond which a human move is winning.")
(defun gomoku-strongest-square ()
"Compute index of free square with highest score, or nil if none."
(let ((score-max 0)
(count 0) (square (gomoku-xy-to-index 1 1)) (end (gomoku-xy-to-index gomoku-board-width gomoku-board-height))
best-square score)
(while (<= square end)
(cond
((< (aref gomoku-score-table square) score-max))
((> (setq score (aref gomoku-score-table square)) score-max)
(if (zerop (aref gomoku-board square)) (setq count 1 best-square square
score-max score)
(aset gomoku-score-table square -1))) ((not (zerop (aref gomoku-board square)))
(aset gomoku-score-table square -1))
((zerop (random (setq count (1+ count))))
(setq best-square square
score-max score)))
(setq square (1+ square))) best-square))
(defvar gomoku-saved-score-table nil
"Recorded initial value of previous score table.")
(defvar gomoku-saved-board-width nil
"Recorded value of previous board width.")
(defvar gomoku-saved-board-height nil
"Recorded value of previous board height.")
(defun gomoku-init-score-table ()
"Create the score table vector and fill it with initial values."
(if (and gomoku-saved-score-table (= gomoku-board-width gomoku-saved-board-width)
(= gomoku-board-height gomoku-saved-board-height))
(setq gomoku-score-table (copy-sequence gomoku-saved-score-table))
(setq gomoku-score-table
(make-vector gomoku-vector-length (* 20 nil-score)))
(let (i j maxi maxj maxi2 maxj2)
(setq maxi (/ (1+ gomoku-board-width) 2)
maxj (/ (1+ gomoku-board-height) 2)
maxi2 (min 4 maxi)
maxj2 (min 4 maxj))
(setq i 1)
(while (<= i maxi2)
(setq j 1)
(while (<= j maxj)
(gomoku-init-square-score i j)
(setq j (1+ j)))
(setq i (1+ i)))
(while (<= i maxi)
(setq j 1)
(while (<= j maxj2)
(gomoku-init-square-score i j)
(setq j (1+ j)))
(setq i (1+ i))))
(setq gomoku-saved-score-table (copy-sequence gomoku-score-table)
gomoku-saved-board-width gomoku-board-width
gomoku-saved-board-height gomoku-board-height)))
(defun gomoku-nb-qtuples (i j)
"Return the number of qtuples containing square I,J."
(let ((left (min 4 (1- i)))
(right (min 4 (- gomoku-board-width i)))
(up (min 4 (1- j)))
(down (min 4 (- gomoku-board-height j))))
(+ -12
(min (max (+ left right) 3) 8)
(min (max (+ up down) 3) 8)
(min (max (+ (min left up) (min right down)) 3) 8)
(min (max (+ (min right up) (min left down)) 3) 8))))
(defun gomoku-init-square-score (i j)
"Give initial score to square I,J and to its mirror images."
(let ((ii (1+ (- gomoku-board-width i)))
(jj (1+ (- gomoku-board-height j)))
(sc (* (gomoku-nb-qtuples i j) (aref gomoku-score-trans-table 0))))
(aset gomoku-score-table (gomoku-xy-to-index i j) sc)
(aset gomoku-score-table (gomoku-xy-to-index ii j) sc)
(aset gomoku-score-table (gomoku-xy-to-index i jj) sc)
(aset gomoku-score-table (gomoku-xy-to-index ii jj) sc)))
(defun gomoku-update-score-table (square dval)
"Update score table after SQUARE received a DVAL increment."
(let* ((x (gomoku-index-to-x square))
(y (gomoku-index-to-y square))
(imin (max -4 (- 1 x)))
(jmin (max -4 (- 1 y)))
(imax (min 0 (- gomoku-board-width x 4)))
(jmax (min 0 (- gomoku-board-height y 4))))
(gomoku-update-score-in-direction imin imax
square 1 0 dval)
(gomoku-update-score-in-direction jmin jmax
square 0 1 dval)
(gomoku-update-score-in-direction (max imin jmin) (min imax jmax)
square 1 1 dval)
(gomoku-update-score-in-direction (max (- 1 y) -4
(- x gomoku-board-width))
(min 0 (- x 5)
(- gomoku-board-height y 4))
square -1 1 dval)))
(defun gomoku-update-score-in-direction (left right square dx dy dval)
"Update scores for all squares in the qtuples starting between the LEFTth
square and the RIGHTth after SQUARE, along the DX, DY direction, considering
that DVAL has been added on SQUARE."
(cond
((> left right)) (t (let (depl square0 square1 square2 count delta)
(setq depl (gomoku-xy-to-index dx dy)
square0 (+ square (* left depl))
square1 (+ square (* right depl))
square2 (+ square0 (* 4 depl)))
(setq square square0
count 0)
(while (<= square square2)
(setq count (+ count (aref gomoku-board square))
square (+ square depl)))
(while (<= square0 square1)
(setq delta (- (aref gomoku-score-trans-table count)
(aref gomoku-score-trans-table (- count dval))))
(cond ((not (zerop delta)) (setq square square0)
(while (<= square square2)
(if (zerop (aref gomoku-board square)) (aset gomoku-score-table square
(+ (aref gomoku-score-table square) delta)))
(setq square (+ square depl)))))
(setq square2 (+ square2 depl)
count (+ count (- (aref gomoku-board square0))
(aref gomoku-board square2))
square0 (+ square0 depl)))))))
(defvar gomoku-game-in-progress nil
"Non-nil if a game is in progress.")
(defvar gomoku-game-history nil
"A record of all moves that have been played during current game.")
(defvar gomoku-number-of-moves nil
"Number of moves already played in current game.")
(defvar gomoku-number-of-human-moves nil
"Number of moves already played by human in current game.")
(defvar gomoku-emacs-played-first nil
"Non-nil if Emacs played first.")
(defvar gomoku-human-took-back nil
"Non-nil if Human took back a move during the game.")
(defvar gomoku-human-refused-draw nil
"Non-nil if Human refused Emacs offer of a draw.")
(defvar gomoku-emacs-is-computing nil
"Non-nil if Emacs is in the middle of a computation.")
(defun gomoku-start-game (n m)
"Initialize a new game on an N by M board."
(setq gomoku-emacs-is-computing t) (setq gomoku-game-in-progress t)
(setq gomoku-board-width n
gomoku-board-height m
gomoku-vector-length (1+ (* (+ m 2) (1+ n)))
gomoku-draw-limit (/ (* 7 n m) 10))
(setq gomoku-emacs-won nil
gomoku-game-history nil
gomoku-number-of-moves 0
gomoku-number-of-human-moves 0
gomoku-emacs-played-first nil
gomoku-human-took-back nil
gomoku-human-refused-draw nil)
(gomoku-init-display n m) (gomoku-init-score-table) (gomoku-init-board) (setq gomoku-emacs-is-computing nil))
(defun gomoku-play-move (square val &optional dont-update-score)
"Go to SQUARE, play VAL and update everything."
(setq gomoku-emacs-is-computing t) (cond ((= 1 val) (setq gomoku-number-of-human-moves (1+ gomoku-number-of-human-moves)))
((zerop gomoku-number-of-moves) (setq gomoku-emacs-played-first t)))
(setq gomoku-game-history
(cons (cons square (aref gomoku-score-table square))
gomoku-game-history)
gomoku-number-of-moves (1+ gomoku-number-of-moves))
(gomoku-plot-square square val)
(aset gomoku-board square val) (if dont-update-score nil
(gomoku-update-score-table square val) (aset gomoku-score-table square -1))
(setq gomoku-emacs-is-computing nil))
(defun gomoku-take-back ()
"Take back last move and update everything."
(setq gomoku-emacs-is-computing t)
(let* ((last-move (car gomoku-game-history))
(square (car last-move))
(oldval (aref gomoku-board square)))
(if (= 1 oldval)
(setq gomoku-number-of-human-moves (1- gomoku-number-of-human-moves)))
(setq gomoku-game-history (cdr gomoku-game-history)
gomoku-number-of-moves (1- gomoku-number-of-moves))
(gomoku-plot-square square 0)
(aset gomoku-board square 0) (gomoku-update-score-table square (- oldval))
(aset gomoku-score-table square (cdr last-move)))
(setq gomoku-emacs-is-computing nil))
(defvar gomoku-number-of-emacs-wins 0
"Number of games Emacs won in this session.")
(defvar gomoku-number-of-human-wins 0
"Number of games you won in this session.")
(defvar gomoku-number-of-draws 0
"Number of games already drawn in this session.")
(defun gomoku-terminate-game (result)
"Terminate the current game with RESULT."
(message
(cond
((eq result 'emacs-won)
(setq gomoku-number-of-emacs-wins (1+ gomoku-number-of-emacs-wins))
(cond ((< gomoku-number-of-moves 20)
"This was a REALLY QUICK win.")
(gomoku-human-refused-draw
"I won... Too bad you refused my offer of a draw !")
(gomoku-human-took-back
"I won... Taking moves back will not help you !")
((not gomoku-emacs-played-first)
"I won... Playing first did not help you much !")
((and (zerop gomoku-number-of-human-wins)
(zerop gomoku-number-of-draws)
(> gomoku-number-of-emacs-wins 1))
"I'm becoming tired of winning...")
("I won.")))
((eq result 'human-won)
(setq gomoku-number-of-human-wins (1+ gomoku-number-of-human-wins))
(concat "OK, you won this one."
(cond
(gomoku-human-took-back
" I, for one, never take my moves back...")
(gomoku-emacs-played-first
".. so what ?")
(" Now, let me play first just once."))))
((eq result 'human-resigned)
(setq gomoku-number-of-emacs-wins (1+ gomoku-number-of-emacs-wins))
"So you resign. That's just one more win for me.")
((eq result 'nobody-won)
(setq gomoku-number-of-draws (1+ gomoku-number-of-draws))
(concat "This is a draw. "
(cond
(gomoku-human-took-back
"I, for one, never take my moves back...")
(gomoku-emacs-played-first
"Just chance, I guess.")
("Now, let me play first just once."))))
((eq result 'draw-agreed)
(setq gomoku-number-of-draws (1+ gomoku-number-of-draws))
(concat "Draw agreed. "
(cond
(gomoku-human-took-back
"I, for one, never take my moves back...")
(gomoku-emacs-played-first
"You were lucky.")
("Now, let me play first just once."))))
((eq result 'crash-game)
"Sorry, I have been interrupted and cannot resume that game...")))
(gomoku-display-statistics)
(setq gomoku-game-in-progress nil))
(defun gomoku-crash-game ()
"What to do when Emacs detects it has been interrupted."
(setq gomoku-emacs-is-computing nil)
(gomoku-terminate-game 'crash-game)
(sit-for 4) (gomoku-prompt-for-other-game))
(defun gomoku (&optional n m)
"Start a Gomoku game between you and Emacs.
If a game is in progress, this command allow you to resume it.
If optional arguments N and M are given, an N by M board is used.
If prefix arg is given for N, M is prompted for.
You and Emacs play in turn by marking a free square. You mark it with X
and Emacs marks it with O. The winner is the first to get five contiguous
marks horizontally, vertically or in diagonal.
You play by moving the cursor over the square you choose and hitting
\\<gomoku-mode-map>\\[gomoku-human-plays].
Use \\[describe-mode] for more info."
(interactive (if current-prefix-arg
(list (prefix-numeric-value current-prefix-arg)
(eval (read-minibuffer "Height: ")))))
(gomoku-switch-to-window)
(cond
(gomoku-emacs-is-computing
(gomoku-crash-game))
((or (not gomoku-game-in-progress)
(<= gomoku-number-of-moves 2))
(let ((max-width (gomoku-max-width))
(max-height (gomoku-max-height)))
(or n (setq n max-width))
(or m (setq m max-height))
(cond ((< n 1)
(error "I need at least 1 column"))
((< m 1)
(error "I need at least 1 row"))
((> n max-width)
(error "I cannot display %d columns in that window" n)))
(if (and (> m max-height)
(not (eq m gomoku-saved-board-height))
(not (y-or-n-p (format "Do you really want %d rows " m))))
(setq m max-height)))
(message "One moment, please...")
(gomoku-start-game n m)
(if (y-or-n-p "Do you allow me to play first ")
(gomoku-emacs-plays)
(gomoku-prompt-for-move)))
((y-or-n-p "Shall we continue our game ")
(gomoku-prompt-for-move))
(t
(gomoku-human-resigns))))
(defun gomoku-emacs-plays ()
"Compute Emacs next move and play it."
(interactive)
(gomoku-switch-to-window)
(cond
(gomoku-emacs-is-computing
(gomoku-crash-game))
((not gomoku-game-in-progress)
(gomoku-prompt-for-other-game))
(t
(message "Let me think...")
(let (square score)
(setq square (gomoku-strongest-square))
(cond ((null square)
(gomoku-terminate-game 'nobody-won))
(t
(setq score (aref gomoku-score-table square))
(gomoku-play-move square 6)
(cond ((>= score gomoku-winning-threshold)
(setq gomoku-emacs-won t) (gomoku-find-filled-qtuple square 6)
(gomoku-terminate-game 'emacs-won))
((zerop score)
(gomoku-terminate-game 'nobody-won))
((and (> gomoku-number-of-moves gomoku-draw-limit)
(not gomoku-human-refused-draw)
(gomoku-offer-a-draw))
(gomoku-terminate-game 'draw-agreed))
(t
(gomoku-prompt-for-move)))))))))
(defun gomoku-click (click)
"Position at the square where you click."
(interactive "e")
(and (windowp (posn-window (setq click (event-end click))))
(numberp (posn-point click))
(select-window (posn-window click))
(setq click (posn-col-row click))
(gomoku-goto-xy
(min (max (/ (+ (- (car click)
gomoku-x-offset
1)
(window-hscroll)
gomoku-square-width
(% gomoku-square-width 2)
(/ gomoku-square-width 2))
gomoku-square-width)
1)
gomoku-board-width)
(min (max (/ (+ (- (cdr click)
gomoku-y-offset
1)
(let ((inhibit-point-motion-hooks t))
(count-lines 1 (window-start)))
gomoku-square-height
(% gomoku-square-height 2)
(/ gomoku-square-height 2))
gomoku-square-height)
1)
gomoku-board-height))))
(defun gomoku-mouse-play (click)
"Play at the square where you click."
(interactive "e")
(if (gomoku-click click)
(gomoku-human-plays)))
(defun gomoku-human-plays ()
"Signal to the Gomoku program that you have played.
You must have put the cursor on the square where you want to play.
If the game is finished, this command requests for another game."
(interactive)
(gomoku-switch-to-window)
(cond
(gomoku-emacs-is-computing
(gomoku-crash-game))
((not gomoku-game-in-progress)
(gomoku-prompt-for-other-game))
(t
(let (square score)
(setq square (gomoku-point-square))
(cond ((null square)
(error "Your point is not on a square. Retry !"))
((not (zerop (aref gomoku-board square)))
(error "Your point is not on a free square. Retry !"))
(t
(setq score (aref gomoku-score-table square))
(gomoku-play-move square 1)
(cond ((and (>= score gomoku-loosing-threshold)
(gomoku-find-filled-qtuple square 1))
(gomoku-terminate-game 'human-won))
(t
(gomoku-emacs-plays)))))))))
(defun gomoku-human-takes-back ()
"Signal to the Gomoku program that you wish to take back your last move."
(interactive)
(gomoku-switch-to-window)
(cond
(gomoku-emacs-is-computing
(gomoku-crash-game))
((not gomoku-game-in-progress)
(message "Too late for taking back...")
(sit-for 4)
(gomoku-prompt-for-other-game))
((zerop gomoku-number-of-human-moves)
(message "You have not played yet... Your move ?"))
(t
(message "One moment, please...")
(setq gomoku-human-took-back t)
(let ((number gomoku-number-of-human-moves))
(while (= number gomoku-number-of-human-moves)
(gomoku-take-back)))
(gomoku-prompt-for-move))))
(defun gomoku-human-resigns ()
"Signal to the Gomoku program that you may want to resign."
(interactive)
(gomoku-switch-to-window)
(cond
(gomoku-emacs-is-computing
(gomoku-crash-game))
((not gomoku-game-in-progress)
(message "There is no game in progress"))
((y-or-n-p "You mean, you resign ")
(gomoku-terminate-game 'human-resigned))
((y-or-n-p "You mean, we continue ")
(gomoku-prompt-for-move))
(t
(gomoku-terminate-game 'human-resigned))))
(defun gomoku-prompt-for-move ()
"Display a message asking for Human's move."
(message (if (zerop gomoku-number-of-human-moves)
"Your move ? (move to a free square and hit X, RET ...)"
"Your move ?"))
(save-excursion (set-buffer (other-buffer))))
(defun gomoku-prompt-for-other-game ()
"Ask for another game, and start it."
(if (y-or-n-p "Another game ")
(gomoku gomoku-board-width gomoku-board-height)
(message "Chicken !")))
(defun gomoku-offer-a-draw ()
"Offer a draw and return T if Human accepted it."
(or (y-or-n-p "I offer you a draw. Do you accept it ")
(not (setq gomoku-human-refused-draw t))))
(defconst gomoku-square-width 4
"*Horizontal spacing between squares on the Gomoku board.")
(defconst gomoku-square-height 2
"*Vertical spacing between squares on the Gomoku board.")
(defconst gomoku-x-offset 3
"*Number of columns between the Gomoku board and the side of the window.")
(defconst gomoku-y-offset 1
"*Number of lines between the Gomoku board and the top of the window.")
(defun gomoku-max-width ()
"Largest possible board width for the current window."
(1+ (/ (- (window-width (selected-window))
gomoku-x-offset gomoku-x-offset 1)
gomoku-square-width)))
(defun gomoku-max-height ()
"Largest possible board height for the current window."
(1+ (/ (- (window-height (selected-window))
gomoku-y-offset gomoku-y-offset 2)
gomoku-square-height)))
(defun gomoku-point-y ()
"Return the board row where point is."
(let ((inhibit-point-motion-hooks t))
(1+ (/ (- (count-lines 1 (point)) gomoku-y-offset (if (bolp) 0 1))
gomoku-square-height))))
(defun gomoku-point-square ()
"Return the index of the square point is on."
(let ((inhibit-point-motion-hooks t))
(gomoku-xy-to-index (1+ (/ (- (current-column) gomoku-x-offset)
gomoku-square-width))
(gomoku-point-y))))
(defun gomoku-goto-square (index)
"Move point to square number INDEX."
(gomoku-goto-xy (gomoku-index-to-x index) (gomoku-index-to-y index)))
(defun gomoku-goto-xy (x y)
"Move point to square at X, Y coords."
(let ((inhibit-point-motion-hooks t))
(goto-line (+ 1 gomoku-y-offset (* gomoku-square-height (1- y)))))
(move-to-column (+ gomoku-x-offset (* gomoku-square-width (1- x)))))
(defun gomoku-plot-square (square value)
"Draw 'X', 'O' or '.' on SQUARE depending on VALUE, leave point there."
(or (= value 1)
(gomoku-goto-square square))
(let ((inhibit-read-only t)
(inhibit-point-motion-hooks t))
(insert-and-inherit (cond ((= value 1) ?X)
((= value 6) ?O)
(?.)))
(and (zerop value)
(add-text-properties
(1- (point)) (point)
'(mouse-face highlight help-echo "mouse-2: play at this square")))
(delete-char 1)
(backward-char 1))
(sit-for 0))
(defun gomoku-init-display (n m)
"Display an N by M Gomoku board."
(buffer-disable-undo (current-buffer))
(let ((inhibit-read-only t)
(point 1) opoint
(intangible t)
(i m) j x)
(setq tab-width
(if (zerop (% gomoku-x-offset gomoku-square-width))
gomoku-square-width
(max (/ (+ (% gomoku-x-offset gomoku-square-width)
gomoku-square-width 1) 2) 2)))
(erase-buffer)
(newline gomoku-y-offset)
(while (progn
(setq j n
x (- gomoku-x-offset gomoku-square-width))
(while (>= (setq j (1- j)) 0)
(insert-char ?\t (/ (- (setq x (+ x gomoku-square-width))
(current-column))
tab-width))
(insert-char ? (- x (current-column)))
(if (setq intangible (not intangible))
(put-text-property point (point) 'intangible 2))
(and (zerop j)
(= i (- m 2))
(progn
(while (>= i 3)
(append-to-buffer (current-buffer) opoint (point))
(setq i (- i 2)))
(goto-char (point-max))))
(setq point (point))
(insert ?.)
(add-text-properties
point (point)
'(mouse-face highlight
help-echo "mouse-2: play at this square")))
(> (setq i (1- i)) 0))
(if (= i (1- m))
(setq opoint point))
(insert-char ?\n gomoku-square-height))
(or (eq (char-after 1) ?.)
(put-text-property 1 2 'point-entered
(lambda (x x) (if (bobp) (forward-char)))))
(or intangible
(put-text-property point (point) 'intangible 2))
(put-text-property point (point) 'point-entered
(lambda (x x) (if (eobp) (backward-char))))
(put-text-property (point-min) (point) 'category 'gomoku-mode))
(gomoku-goto-xy (/ (1+ n) 2) (/ (1+ m) 2)) (sit-for 0))
(defun gomoku-display-statistics ()
"Obnoxiously display some statistics about previous games in mode line."
(setq mode-line-process
(format ": Won %d, lost %d%s"
gomoku-number-of-human-wins
gomoku-number-of-emacs-wins
(if (zerop gomoku-number-of-draws)
""
(format ", drew %d" gomoku-number-of-draws))))
(force-mode-line-update))
(defun gomoku-switch-to-window ()
"Find or create the Gomoku buffer, and display it."
(interactive)
(let ((buff (get-buffer "*Gomoku*")))
(if buff (switch-to-buffer buff) (if gomoku-game-in-progress
(gomoku-crash-game)) (switch-to-buffer "*Gomoku*") (gomoku-mode))))
(defun gomoku-find-filled-qtuple (square value)
"Return T if SQUARE belongs to a qtuple filled with VALUEs."
(or (gomoku-check-filled-qtuple square value 1 0)
(gomoku-check-filled-qtuple square value 0 1)
(gomoku-check-filled-qtuple square value 1 1)
(gomoku-check-filled-qtuple square value -1 1)))
(defun gomoku-check-filled-qtuple (square value dx dy)
"Return T if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."
(let ((a 0) (b 0)
(left square) (right square)
(depl (gomoku-xy-to-index dx dy)))
(while (and (> a -4) (= value (aref gomoku-board (setq left (- left depl)))))
(setq a (1- a)))
(while (and (< b (+ a 4)) (= value (aref gomoku-board (setq right (+ right depl)))))
(setq b (1+ b)))
(cond ((= b (+ a 4)) (gomoku-cross-qtuple (+ square (* a depl)) (+ square (* b depl))
dx dy)
t))))
(defun gomoku-cross-qtuple (square1 square2 dx dy)
"Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."
(save-excursion (let ((depl (gomoku-xy-to-index dx dy))
(inhibit-read-only t)
(inhibit-point-motion-hooks t))
(while (/= square1 square2)
(gomoku-goto-square square1)
(setq square1 (+ square1 depl))
(cond
((= dy 0) (forward-char 1)
(insert-char ?- (1- gomoku-square-width) t)
(delete-region (point) (progn
(skip-chars-forward " \t")
(point))))
((= dx 0) (let ((n 1)
(column (current-column)))
(while (< n gomoku-square-height)
(setq n (1+ n))
(forward-line 1)
(indent-to column)
(insert-and-inherit ?|))))
((= dx -1) (indent-to (prog1 (- (current-column) (/ gomoku-square-width 2))
(forward-line (/ gomoku-square-height 2))))
(insert-and-inherit ?/))
(t (indent-to (prog1 (+ (current-column) (/ gomoku-square-width 2))
(forward-line (/ gomoku-square-height 2))))
(insert-and-inherit ?\\))))))
(sit-for 0))
(defun gomoku-move-down ()
"Move point down one row on the Gomoku board."
(interactive)
(if (< (gomoku-point-y) gomoku-board-height)
(next-line gomoku-square-height)))
(defun gomoku-move-up ()
"Move point up one row on the Gomoku board."
(interactive)
(if (> (gomoku-point-y) 1)
(previous-line gomoku-square-height)))
(defun gomoku-move-ne ()
"Move point North East on the Gomoku board."
(interactive)
(gomoku-move-up)
(forward-char))
(defun gomoku-move-se ()
"Move point South East on the Gomoku board."
(interactive)
(gomoku-move-down)
(forward-char))
(defun gomoku-move-nw ()
"Move point North West on the Gomoku board."
(interactive)
(gomoku-move-up)
(backward-char))
(defun gomoku-move-sw ()
"Move point South West on the Gomoku board."
(interactive)
(gomoku-move-down)
(backward-char))
(defun gomoku-beginning-of-line ()
"Move point to first square on the Gomoku board row."
(interactive)
(move-to-column gomoku-x-offset))
(defun gomoku-end-of-line ()
"Move point to last square on the Gomoku board row."
(interactive)
(move-to-column (+ gomoku-x-offset
(* gomoku-square-width (1- gomoku-board-width)))))
(provide 'gomoku)