
(provide (quote gomoku))

(defvar gomoku-mode-hook nil "\
If non-nil, its value is called on entry to Gomoku mode.")

(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" (quote gomoku-move-nw)) (define-key gomoku-mode-map "u" (quote gomoku-move-ne)) (define-key gomoku-mode-map "b" (quote gomoku-move-sw)) (define-key gomoku-mode-map "n" (quote gomoku-move-se)) (define-key gomoku-mode-map "h" (quote gomoku-move-left)) (define-key gomoku-mode-map "l" (quote gomoku-move-right)) (define-key gomoku-mode-map "j" (quote gomoku-move-down)) (define-key gomoku-mode-map "k" (quote gomoku-move-up)) (define-key gomoku-mode-map "" (quote gomoku-move-down)) (define-key gomoku-mode-map "" (quote gomoku-move-up)) (define-key gomoku-mode-map "" (quote gomoku-move-right)) (define-key gomoku-mode-map "" (quote gomoku-move-left)) (define-key gomoku-mode-map "X" (quote gomoku-human-plays)) (define-key gomoku-mode-map "x" (quote gomoku-human-plays)) (define-key gomoku-mode-map "" (quote gomoku-human-plays)) (define-key gomoku-mode-map "p" (quote gomoku-human-plays)) (define-key gomoku-mode-map "b" (quote gomoku-human-takes-back)) (define-key gomoku-mode-map "r" (quote gomoku-human-resigns)) (define-key gomoku-mode-map "e" (quote gomoku-emacs-plays)) (if (featurep (quote keypad)) (let (keys) (if (setq keys (function-key-sequence 117)) (define-key gomoku-mode-map keys (quote gomoku-move-up))) (if (setq keys (function-key-sequence 100)) (define-key gomoku-mode-map keys (quote gomoku-move-down))) (if (setq keys (function-key-sequence 108)) (define-key gomoku-mode-map keys (quote gomoku-move-left))) (if (setq keys (function-key-sequence 114)) (define-key gomoku-mode-map keys (quote gomoku-move-right))))))

(defun gomoku-mode nil "\
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 RET,
x, .. or whatever has been set locally.

Other useful commands:

C-c r	Indicate that you resign,
C-c t	Take back your last move,
C-c e	Ask for Emacs to play (thus passing).

Commands:
\\{gomoku-mode-map}
Entry to this mode calls the value of gomoku-mode-hook
if that value is non-nil." (interactive) (byte-code "Èŉ 
!!" [major-mode mode-name gomoku-mode-map nil gomoku-mode "Gomoku" gomoku-display-statistics use-local-map run-hooks gomoku-mode-hook] 4))

(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." (byte-code "	\"
#" [y gomoku-board-width x + *] 5))

(defun gomoku-index-to-x (index) "\
Return corresponding x-coord of board INDEX." (byte-code "	T\"" [index gomoku-board-width %] 3))

(defun gomoku-index-to-y (index) "\
Return corresponding y-coord of board INDEX." (byte-code "	T\"" [index gomoku-board-width /] 3))

(defun gomoku-init-board nil "\
Create the gomoku-board vector and fill it with initial values." (byte-code "	\"	S
X' 
II
TS *
	WA 
I
#+ )" [gomoku-board gomoku-vector-length i ii gomoku-board-width make-vector 0 -1 + 1] 6))

(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 nil "\
Compute index of free square with highest score, or nil if none." (byte-code "\"\"
Xy 
HWp 
HVL 	
H!D 
I 
Ip 	
H!?^ 
Ip 		T!Up 

T ." [score-max count square end gomoku-board-width gomoku-board-height best-square score gomoku-score-table gomoku-board 0 gomoku-xy-to-index 1 nil zerop -1 random-number] 9))

(defun random-number (n) "\
Return a random integer between 0 and N-1 inclusive." (byte-code " \"W [ " [n % random 0] 4))

(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 nil "\
Create the score table vector and fill it with initial values." (byte-code " 	
U U ! \"\"
		T\"
T\"
^^։X ։		X} 	\"	T	d TW 
X ։		X 	\"	T	 T .!	" [gomoku-saved-score-table gomoku-board-width gomoku-saved-board-width gomoku-board-height gomoku-saved-board-height gomoku-score-table gomoku-vector-length nil-score i j maxi maxj maxi2 maxj2 copy-sequence make-vector * 20 nil / 2 4 1 gomoku-init-square-score] 11))

(defun gomoku-nb-qtuples (i j) "\
Return the number of qtuples containing square I,J." (byte-code "	S^	Z^S^Z^
\\]^\\]^^
^\\]^
^^\\]^%," [left i right gomoku-board-width up j down gomoku-board-height 4 + -12 3 8] 8))

(defun gomoku-init-square-score (i j) "\
Give initial score to square I,J and to its mirror images." (byte-code "	
ZTZT
\"H\"
\"I\"I
\"I\"I+" [ii gomoku-board-width i jj gomoku-board-height j sc gomoku-score-trans-table gomoku-score-table * gomoku-nb-qtuples 0 gomoku-xy-to-index] 9))

(defun gomoku-update-score-table (square dval) "\
Update score table after SQUARE received a DVAL increment." (byte-code "	!	!Z]
Z]#^
#^		&		&]^		&
ZZ#Z
##		&." [x square y imin jmin imax gomoku-board-width jmax gomoku-board-height dval gomoku-index-to-x gomoku-index-to-y -4 1 0 - 4 gomoku-update-score-in-direction max min 5 -1] 17))

(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." (byte-code "	V 	
\"\"\\	\"\\\"\\ԉX[ H\\\\> X HZHZ!? X H! H\\I\\ \\H[H#\\\\ ." [left right t depl square0 square1 square2 count delta dx dy square gomoku-board gomoku-score-trans-table dval gomoku-score-table nil gomoku-xy-to-index * 4 0 zerop +] 12))

(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." (byte-code "\\T\"T#\"	ʉ\"  ʉ" [gomoku-emacs-is-computing t gomoku-game-in-progress gomoku-board-width n gomoku-board-height m gomoku-vector-length gomoku-draw-limit gomoku-game-history nil gomoku-number-of-moves gomoku-number-of-human-moves gomoku-emacs-played-first gomoku-human-took-back gomoku-human-refused-draw * 2 / 7 10 0 gomoku-init-display gomoku-init-score-table gomoku-init-board] 8))

(defun gomoku-play-move (square val &optional dont-update-score) "\
Go to SQUARE, play VAL and update everything." (byte-code "
U T ! HBBT
\"	
I
C ˂O 
\"Iˉ" [gomoku-emacs-is-computing t val gomoku-number-of-human-moves gomoku-number-of-moves gomoku-emacs-played-first gomoku-game-history square gomoku-score-table gomoku-board dont-update-score nil 1 zerop gomoku-plot-square gomoku-update-score-table -1] 6))

(defun gomoku-take-back nil "\
Take back last move and update everything." (byte-code "@
@HU SAS\"I[\"	
AI+ʉ" [gomoku-emacs-is-computing t last-move gomoku-game-history square oldval gomoku-board gomoku-number-of-human-moves gomoku-number-of-moves gomoku-score-table nil 1 gomoku-plot-square 0 gomoku-update-score-table] 5))

(defvar gomoku-number-of-wins 0 "\
Number of games already won in this session.")

(defvar gomoku-number-of-losses 0 "\
Number of games already lost 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." (byte-code "	=O 
TW ΂J  ςJ ' ЂJ ?1 тJ !B !B 
VI ԂJ Չ 	=s Td ׂn m ؂n ى 	= 
Tۉ 	= T ݂  ނ ߉ 	= T     	=   ! ˉ
)" [message result gomoku-number-of-wins gomoku-number-of-moves gomoku-human-refused-draw gomoku-human-took-back gomoku-emacs-played-first gomoku-number-of-losses gomoku-number-of-draws t gomoku-game-in-progress nil emacs-won 20 "This was a REALLY QUICK win." "I won... Too bad you refused my offer of a draw !" "I won... Taking moves back will not help you !" "I won... Playing first did not help you much !" zerop 1 "I'm becoming tired of winning..." "I won." human-won "OK, you won this one. I, for one, never take my moves back..." "OK, you won this one... so what ?" "OK, you won this one. Now, let me play first just once." human-resigned "So you resign... That's just one more win for me." nobody-won "This is a draw. I, for one, never take my moves back..." "This is a draw... Just chance, I guess." "This is a draw. Now, let me play first just once." draw-agreed "Draw agreed. I, for one, never take my moves back..." "Draw agreed. You were lucky." "Draw agreed. Now, let me play first just once." crash-game "Sorry, I have been interrupted and cannot resume that game..." gomoku-display-statistics ding] 7))

(defun gomoku-crash-game nil "\
What to do when Emacs detects it has been interrupted." (byte-code "!! " [gomoku-emacs-is-computing nil gomoku-terminate-game crash-game sit-for 4 gomoku-prompt-for-other-game] 4))

(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.

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 RET,
x, .. or whatever has been set locally.
Use C-h m for more info." (interactive) (byte-code "Ȉ    	?     
( W5 !K WA !K 
VK \"Vb \"?b \"!?h *!\"!~     !    " [gomoku-emacs-is-computing gomoku-game-in-progress max-width max-height n m gomoku-saved-board-height t nil gomoku-switch-to-window gomoku-crash-game gomoku-max-width gomoku-max-height 1 error "I need at least 1 column" "I need at least 1 row" "I cannot display %d columns in that window" equal y-or-n-p format "Do you really want %d rows " message "One moment, please..." gomoku-start-game "Do you allow me to play first " gomoku-emacs-plays gomoku-prompt-for-move "Shall we continue our game " gomoku-human-resigns] 19))

(defun gomoku-emacs-plays nil "\
Compute Emacs next move and play it." (interactive) (byte-code "ʈ   x 	?  x ! ?0 !w H\"YP \" !w !\\ !w Vl 	?l  u !w  *" [gomoku-emacs-is-computing gomoku-game-in-progress t square score gomoku-score-table gomoku-winning-threshold gomoku-number-of-moves gomoku-draw-limit gomoku-human-refused-draw nil gomoku-switch-to-window gomoku-crash-game gomoku-prompt-for-other-game message "Let me think..." gomoku-strongest-square gomoku-terminate-game nobody-won gomoku-play-move 6 gomoku-find-filled-qtuple gomoku-cross-winning-qtuple emacs-won zerop gomoku-offer-a-draw draw-agreed gomoku-prompt-for-move] 16))

(defun gomoku-human-plays nil "\
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) (byte-code "Ȉ   a 	?  a  ?, !` H!?; !` H\"YR \"^  !`  *" [gomoku-emacs-is-computing gomoku-game-in-progress t square score gomoku-board gomoku-score-table gomoku-loosing-threshold nil gomoku-switch-to-window gomoku-crash-game gomoku-prompt-for-other-game gomoku-point-square error "Your point is not on a square. Retry !" zerop "Your point is not on a free square. Retry !" gomoku-play-move 1 gomoku-find-filled-qtuple gomoku-cross-winning-qtuple gomoku-terminate-game human-won gomoku-emacs-plays] 13))

(defun gomoku-human-takes-back nil "\
Signal to the Gomoku program that you wish to take back your last move." (interactive) (byte-code "ƈ   F 	?  !! F 
!, !F !É

UB  6 ) " [gomoku-emacs-is-computing gomoku-game-in-progress gomoku-number-of-human-moves t gomoku-human-took-back number nil gomoku-switch-to-window gomoku-crash-game message "Too late for taking back..." sit-for 4 gomoku-prompt-for-other-game zerop "You have not played yet... Your move ?" "One moment, please..." gomoku-take-back gomoku-prompt-for-move] 11))

(defun gomoku-human-resigns nil "\
Signal to the Gomoku program that you may want to resign." (interactive) (byte-code "È   3 	? !3 !% !3 !0  3 !" [gomoku-emacs-is-computing gomoku-game-in-progress t nil gomoku-switch-to-window gomoku-crash-game message "There is no game in progress" y-or-n-p "You mean, you resign " gomoku-terminate-game human-resigned "You mean, we continue " gomoku-prompt-for-move] 9))

(defun gomoku-prompt-for-move nil "\
Display a message asking for Human's move." (byte-code "! Â ! q)" [gomoku-number-of-human-moves message zerop "Your move ? (move to a free square and hit X, RET ...)" "Your move ?" other-buffer] 4))

(defun gomoku-prompt-for-other-game nil "\
Ask for another game, and start it." (byte-code "! 	\" !" [gomoku-board-width gomoku-board-height y-or-n-p "Another game " gomoku message "Chicken !"] 4))

(defun gomoku-offer-a-draw nil "\
Offer a draw and return T if Human accepted it." (byte-code "! " [gomoku-human-refused-draw t nil y-or-n-p "I offer you a draw. Do you accept it "] 3))

(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 nil "\
Largest possible board width for the current window." (byte-code " !$	\"T" [gomoku-x-offset gomoku-square-width / - window-width selected-window 1] 8))

(defun gomoku-max-height nil "\
Largest possible board height for the current window." (byte-code " !$	\"T" [gomoku-y-offset gomoku-square-height / - window-height selected-window 2] 8))

(defun gomoku-point-x nil "\
Return the board column where point is, or nil if it is not a board column." (byte-code "i	ZY 
\"! 
\"TX  )" [col gomoku-x-offset gomoku-square-width gomoku-board-width 0 zerop % /] 5))

(defun gomoku-point-y nil "\
Return the board row where point is, or nil if it is not a board row." (byte-code "`\"	#Y! 
\"!! 
\"TX% )" [row gomoku-y-offset gomoku-square-height gomoku-board-height - count-lines 1 0 zerop % /] 7))

(defun gomoku-point-square nil "\
Return the index of the square point is on, or nil if not on the board." (byte-code "    	\"*" [x y nil gomoku-point-x gomoku-point-y gomoku-xy-to-index] 5))

(defun gomoku-goto-square (index) "\
Move point to square number INDEX." (byte-code "!!\"" [index gomoku-goto-xy gomoku-index-to-x gomoku-index-to-y] 5))

(defun gomoku-goto-xy (x y) "\
Move point to square at X, Y coords." (byte-code "	
S\"#!S\"\\!" [gomoku-y-offset gomoku-square-height y gomoku-x-offset gomoku-square-width x goto-line + 1 * move-to-column] 8))

(defun gomoku-plot-square (square value) "\
Draw 'X', 'O' or '.' on SQUARE (depending on VALUE), leave point there." (byte-code "!	U Ƃ 	U Ȃ !!" [square value t gomoku-goto-square gomoku-put-char 1 88 6 79 46 sit-for 0] 4))

(defun gomoku-put-char (char) "\
Draw CHAR on the Gomoku screen." (byte-code "  	c!! " [buffer-read-only char toggle-read-only delete-char 1 backward-char] 5))

(defun gomoku-init-display (n m) "\
Display an N by M Gomoku board." (byte-code "p!
   S\"PS	\"\"\"	RS\"#\"
PS
\"\"	P	S\"\"

\"\"	#, T\"	T\"\"!" [buffer-read-only string1 string2 string3 string4 gomoku-square-width n gomoku-x-offset gomoku-square-height m gomoku-y-offset buffer-flush-undo toggle-read-only erase-buffer nil make-string 32 "." apply concat make-list "
" + 1 * insert gomoku-goto-xy / 2 sit-for 0] 24))

(defun gomoku-display-statistics nil "\
Obnoxiously display some statistics about previous games in mode line." (byte-code "	!? 
	$7 !? 
#7 
!) ɂ7 
U3 ˂7 
\" !" [mode-line-process gomoku-number-of-draws gomoku-number-of-wins gomoku-number-of-losses t zerop format ": Won %d, lost %d, drew %d" ": Won %d, lost %d" "" 1 ": Already won one" ": Won %d in a row" set-buffer-modified-p buffer-modified-p] 9))

(defun gomoku-switch-to-window nil "\
Find or create the Gomoku buffer, and display it." (interactive) (byte-code "! ! 	  ! )" [buff gomoku-game-in-progress nil get-buffer "*Gomoku*" switch-to-buffer gomoku-crash-game gomoku-mode] 6))

(defvar gomoku-winning-qtuple-beg nil "\
First square of the winning qtuple.")

(defvar gomoku-winning-qtuple-end nil "\
Last square of the winning qtuple.")

(defvar gomoku-winning-qtuple-dx nil "\
Direction of the winning qtuple (along the X axis).")

(defvar gomoku-winning-qtuple-dy nil "\
Direction of the winning qtuple (along the Y axis).")

(defun gomoku-find-filled-qtuple (square value) "\
Return T if SQUARE belongs to a qtuple filled with VALUEs." (byte-code "	$! 	$! 	$! 	$" [square value gomoku-check-filled-qtuple 1 0 -1] 8))

(defun gomoku-check-filled-qtuple (square value dx dy) "\
Return T if SQUARE belongs to a qtuple filled  with VALUEs along DX, DY." (byte-code "\"V# 	

ZHU. S \\	WH 	
\\HUS 	T6 	Uv \"\\	\"\\." [a b left square right depl dx dy a+4 value gomoku-board gomoku-winning-qtuple-beg gomoku-winning-qtuple-end gomoku-winning-qtuple-dx gomoku-winning-qtuple-dy t 0 gomoku-xy-to-index nil -4 4 *] 7))

(defun gomoku-cross-winning-qtuple nil "\
Cross winning qtuple, as found by gomoku-find-filled-qtuple." (byte-code "	
$" [gomoku-winning-qtuple-beg gomoku-winning-qtuple-end gomoku-winning-qtuple-dx gomoku-winning-qtuple-dy gomoku-cross-qtuple] 5))

(defun gomoku-cross-qtuple (square1 square2 dx dy) "\
Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction." (byte-code "	
\"U? !\\	U  
U@ W< T!!% ) 	UI 
Ui We T!!N ) 	Ur 
U \"!\"!! 	U 
U \"!\"!! ))!" [depl dx dy square1 square2 n gomoku-square-width gomoku-square-height gomoku-xy-to-index gomoku-goto-square 1 0 forward-char gomoku-put-char 45 next-line 124 -1 backward-char / 2 47 92 sit-for] 18))

(defun gomoku-move-left nil "\
Move point backward one column on the Gomoku board." (interactive) (byte-code "È ? Ƃ V 	 !)" [x gomoku-square-width t nil gomoku-point-x backward-char 1 0] 4))

(defun gomoku-move-right nil "\
Move point forward one column on the Gomoku board." (interactive) (byte-code "Ĉ ? ǂ 	W 
 !)" [x gomoku-board-width gomoku-square-width t nil gomoku-point-x forward-char 1 0] 4))

(defun gomoku-move-down nil "\
Move point down one row on the Gomoku board." (interactive) (byte-code "Ĉ ? ǂ 	W 
 !)" [y gomoku-board-height gomoku-square-height t nil gomoku-point-y next-line 1 0] 4))

(defun gomoku-move-up nil "\
Move point up one row on the Gomoku board." (interactive) (byte-code "È ? Ƃ V 	 !)" [y gomoku-square-height t nil gomoku-point-y previous-line 1 0] 4))

(defun gomoku-move-ne nil "\
Move point North East on the Gomoku board." (interactive) (byte-code "  " [nil gomoku-move-up gomoku-move-right] 3))

(defun gomoku-move-se nil "\
Move point South East on the Gomoku board." (interactive) (byte-code "  " [nil gomoku-move-down gomoku-move-right] 3))

(defun gomoku-move-nw nil "\
Move point North West on the Gomoku board." (interactive) (byte-code "  " [nil gomoku-move-up gomoku-move-left] 3))

(defun gomoku-move-sw nil "\
Move point South West on the Gomoku board." (interactive) (byte-code "  " [nil gomoku-move-down gomoku-move-left] 3))
