;ELC ;;; compiled by jwz@thalidomide on Thu Mar 24 13:23:45 1994 ;;; from file /th/jwz/emacs19/lisp/games/gomoku.el ;;; emacs version 19.10 Lucid (beta9). ;;; bytecomp version 2.22; 22-dec-93. ;;; optimization is on. ;;; this file uses opcodes which do not exist in Emacs 18. (if (and (boundp 'emacs-version) (or (and (boundp 'epoch::version) epoch::version) (string-lessp emacs-version "19"))) (error "This file was compiled for Emacs 19.")) (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.") (byte-code " \"#########################" [gomoku-mode-map make-sparse-keymap set-keymap-name define-key "y" gomoku-move-nw "u" gomoku-move-ne "b" gomoku-move-sw "n" gomoku-move-se "h" gomoku-move-left "l" gomoku-move-right "j" gomoku-move-down "k" gomoku-move-up "" "" "" "" "X" gomoku-human-plays "x" " " "p" "b" gomoku-human-takes-back "r" gomoku-human-resigns "e" gomoku-emacs-plays [up] [down] [left] [right] [kp_enter] [insert]] 4) (fset 'gomoku-mode #[nil " !!" [gomoku-mode major-mode "Gomoku" mode-name gomoku-display-statistics use-local-map gomoku-mode-map run-hooks gomoku-mode-hook] 2 "\ 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." nil]) (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?") (fset 'gomoku-xy-to-index #[(x y) " _\n\\\\" [y gomoku-board-width x] 3 "\ Translate X, Y cartesian coords into the corresponding board index."]) (fset 'gomoku-index-to-x #[(index) " T" [index gomoku-board-width] 2 "\ Return corresponding x-coord of board INDEX."]) (fset 'gomoku-index-to-y #[(index) " T" [index gomoku-board-width] 2 "\ Return corresponding y-coord of board INDEX."]) (fset 'gomoku-init-board #[nil " \" S X I I T Sh* W I \\\\m)" [make-vector gomoku-vector-length 0 gomoku-board ii i gomoku-board-width -1 1] 4 "\ Create the gomoku-board vector and fill it with initial values."]) (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 (byte-code " \n ƉƉ Ɖ\nƉ&" [vector nil-score Xscore XXscore XXXscore XXXXscore 0 Oscore OOscore OOOscore OOOOscore] 32) "\ 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.") (fset 'gomoku-strongest-square #[nil "‰\" \"ʼn \n  X  H W  H V  HU\n    I  HU  I\nT\n!U   T ." [0 gomoku-xy-to-index 1 gomoku-board-width gomoku-board-height nil score best-square end square count score-max gomoku-score-table gomoku-board -1 random] 7 "\ Compute index of free square with highest score, or nil if none."]) (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.") (fset 'gomoku-init-score-table #[nil " \nU U! _\"ˉ   Tҥ Tҥ^ ^  XX\"TlT[X X\"TlT[.!  " [gomoku-saved-score-table gomoku-board-width gomoku-saved-board-width gomoku-board-height gomoku-saved-board-height copy-sequence gomoku-score-table make-vector gomoku-vector-length nil-score 20 nil maxj2 maxi2 maxj maxi j i 2 4 1 gomoku-init-square-score] 7 "\ Create the score table vector and fill it with initial values."]) (fset 'gomoku-nb-qtuples #[(i j) " S^\n Z^ S^ Z^\\]^ \\]^^ ^\\]^^ ^\\]^\\\\\\\\," [4 i gomoku-board-width j gomoku-board-height down up right left 3 8 -12] 7 "\ Return the number of qtuples containing square I,J."]) (fset 'gomoku-init-square-score #[(i j) " ZT\n ZT \" H_ \n \"I\n \"I\n \"I\n \"I+" [gomoku-board-width i gomoku-board-height j gomoku-nb-qtuples gomoku-score-trans-table 0 sc jj ii gomoku-score-table gomoku-xy-to-index] 5 "\ Give initial score to square I,J and to its mirror images."]) (fset 'gomoku-update-score-table #[(square dval) " ! !\nZ] Z]\n\nZZ^  ZZ^ & &] ^ Ɖ& Z\n\nZ]]\nZ ZZ^^ &." [gomoku-index-to-x square x gomoku-index-to-y y -4 1 imin jmin 0 gomoku-board-width 4 imax gomoku-board-height jmax gomoku-update-score-in-direction dval 5 -1] 7 "\ Update score table after SQUARE received a DVAL increment."]) (fset 'gomoku-update-score-in-direction #[(left right square dx dy dval) " V‰\n \" _\\ _\\_\\  X  H\\ \\ jX H ZHZU X HU  H \\I \\ \\ \\ H[ H\\\\\\#." [left right nil delta count square2 square1 square0 depl gomoku-xy-to-index dx dy square 4 0 gomoku-board gomoku-score-trans-table dval gomoku-score-table] 7 "\ 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."]) (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.") (fset 'gomoku-start-game #[(n m) "   \\ T_T __ʥ   \" ̉" [t gomoku-emacs-is-computing gomoku-game-in-progress n gomoku-board-width m gomoku-board-height 2 gomoku-vector-length 7 10 gomoku-draw-limit nil gomoku-game-history 0 gomoku-number-of-moves gomoku-number-of-human-moves gomoku-emacs-played-first gomoku-human-took-back gomoku-human-refused-draw gomoku-init-display gomoku-init-score-table gomoku-init-board] 3 "\ Initialize a new game on an N by M board."]) (fset 'gomoku-play-move #[(square val &optional dont-update-score) "\nU T U HB\nB\n T\n\" \nI \n\" IЉ" [t gomoku-emacs-is-computing val 1 gomoku-number-of-human-moves gomoku-number-of-moves 0 gomoku-emacs-played-first square gomoku-score-table gomoku-game-history gomoku-plot-square gomoku-board dont-update-score gomoku-update-score-table -1 nil] 3 "\ Go to SQUARE, play VAL and update everything."]) (fset 'gomoku-take-back #[nil "\n@@ HUS\nA S \" I [\" AI+Ή" [t gomoku-emacs-is-computing gomoku-game-history last-move square gomoku-board oldval 1 gomoku-number-of-human-moves gomoku-number-of-moves gomoku-plot-square 0 gomoku-update-score-table gomoku-score-table nil] 4 "\ Take back last move and update everything."]) (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.") (fset 'gomoku-terminate-game #[(result) "\n= T WǪɪ\n˪ ͪUU VҪ\n=T\nժ ֪\n= T\n=T\n۪ ܪ\n=T\nߪ ઁ\n= ! &)" [nil message result emacs-won gomoku-number-of-wins 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 !" gomoku-emacs-played-first "I won... Playing first did not help you much !" gomoku-number-of-losses 0 gomoku-number-of-draws 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 gomoku-game-in-progress] 2 "\ Terminate the current game with RESULT."]) (fset 'gomoku-crash-game #[nil "!! " [nil gomoku-emacs-is-computing gomoku-terminate-game crash-game sit-for 4 gomoku-prompt-for-other-game] 2 "\ What to do when Emacs detects it has been interrupted."]) (fset 'gomoku #[(&optional n m) "   W! W!V\" V  \"! *! \"! ! " [gomoku-switch-to-window gomoku-emacs-is-computing gomoku-crash-game gomoku-game-in-progress gomoku-max-width gomoku-max-height max-height max-width n m 1 error "I need at least 1 column" "I need at least 1 row" "I cannot display %d columns in that window" gomoku-saved-board-height 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] 4 "\ 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 \\\\[gomoku-human-plays]. Use \\[describe-mode] for more info." nil]) (fset 'gomoku-emacs-plays #[nil " !lj  !  H \"Y \" !U!V ! *" [gomoku-switch-to-window gomoku-emacs-is-computing gomoku-crash-game gomoku-game-in-progress gomoku-prompt-for-other-game message "Let me think..." nil score square gomoku-strongest-square gomoku-terminate-game nobody-won gomoku-score-table gomoku-play-move 6 gomoku-winning-threshold gomoku-find-filled-qtuple gomoku-cross-winning-qtuple emacs-won 0 gomoku-number-of-moves gomoku-draw-limit gomoku-human-refused-draw gomoku-offer-a-draw draw-agreed gomoku-prompt-for-move] 4 "\ Compute Emacs next move and play it." nil]) (fset 'gomoku-human-plays #[nil " ʼn ! HU!H\"Y\" ! *" [gomoku-switch-to-window gomoku-emacs-is-computing gomoku-crash-game gomoku-game-in-progress gomoku-prompt-for-other-game nil score square gomoku-point-square error "Your point is not on a square. Retry !" gomoku-board 0 "Your point is not on a free square. Retry !" gomoku-score-table gomoku-play-move 1 gomoku-loosing-threshold gomoku-find-filled-qtuple gomoku-cross-winning-qtuple gomoku-terminate-game human-won gomoku-emacs-plays] 4 "\ 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." nil]) (fset 'gomoku-human-takes-back #[nil " !!  U!!  U t) " [gomoku-switch-to-window gomoku-emacs-is-computing gomoku-crash-game gomoku-game-in-progress message "Too late for taking back..." sit-for 4 gomoku-prompt-for-other-game gomoku-number-of-human-moves 0 "You have not played yet... Your move ?" "One moment, please..." t gomoku-human-took-back number gomoku-take-back gomoku-prompt-for-move] 2 "\ Signal to the Gomoku program that you wish to take back your last move." nil]) (fset 'gomoku-human-resigns #[nil " !!!! !" [gomoku-switch-to-window gomoku-emacs-is-computing gomoku-crash-game gomoku-game-in-progress 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] 2 "\ Signal to the Gomoku program that you may want to resign." nil]) (fset 'gomoku-prompt-for-move #[nil " Uê! q)" [message gomoku-number-of-human-moves 0 "Your move ? (move to a free square and hit X, RET ...)" "Your move ?" other-buffer] 3 "\ Display a message asking for Human's move."]) (fset 'gomoku-prompt-for-other-game #[nil "! \"!" [y-or-n-p "Another game " gomoku gomoku-board-width gomoku-board-height message "Chicken !"] 3 "\ Ask for another game, and start it."]) (fset 'gomoku-offer-a-draw #[nil "!‰" [y-or-n-p "I offer you a draw. Do you accept it " t gomoku-human-refused-draw] 2 "\ Offer a draw and return T if Human accepted it."]) (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.") (fset 'gomoku-max-width #[nil " !\nZ\nZZ T" [window-width selected-window gomoku-x-offset 1 gomoku-square-width] 2 "\ Largest possible board width for the current window."]) (fset 'gomoku-max-height #[nil " !\nZ\nZZ T" [window-height selected-window gomoku-y-offset 2 gomoku-square-height] 2 "\ Largest possible board height for the current window."]) (fset 'gomoku-point-x #[nil "iZY U T X )" [gomoku-x-offset col 0 gomoku-square-width gomoku-board-width] 3 "\ Return the board column where point is, or nil if it is not a board column."]) (fset 'gomoku-point-y #[nil "`\"\nZZY U TX )" [count-lines 1 gomoku-y-offset row 0 gomoku-square-height gomoku-board-height] 4 "\ Return the board row where point is, or nil if it is not a board row."]) (fset 'gomoku-point-square #[nil "  \n \"*" [nil y x gomoku-point-x gomoku-point-y gomoku-xy-to-index] 3 "\ Return the index of the square point is on, or nil if not on the board."]) (fset 'gomoku-goto-square #[(index) "\n!\n!\"" [gomoku-goto-xy gomoku-index-to-x index gomoku-index-to-y] 4 "\ Move point to square number INDEX."]) (fset 'gomoku-goto-xy #[(x y) " \n S_\\\\!S_\\!" [goto-line gomoku-y-offset gomoku-square-height y 1 move-to-column gomoku-x-offset gomoku-square-width x] 4 "\ Move point to square at X, Y coords."]) (fset 'gomoku-plot-square #[(square value) " ! UŪ UǪ!!" [gomoku-goto-square square gomoku-put-char value 1 88 6 79 46 sit-for 0] 3 "\ Draw 'X', 'O' or '.' on SQUARE (depending on VALUE), leave point there."]) (fset 'gomoku-put-char #[(char) "\nc!!)" [t inhibit-read-only char delete-char 1 backward-char] 2 "\ Draw CHAR on the Gomoku screen."]) (fset 'gomoku-init-display #[(n m) "p! ĉ\nS\"PS\"\"\"RS\n_\\\\\"PS\"\"PS\"\"\"\",TإTإ\"!)" [buffer-disable-undo t inhibit-read-only erase-buffer nil string4 string3 string2 string1 make-string gomoku-square-width 32 "." apply concat make-list n gomoku-x-offset "\n" 1 gomoku-square-height m gomoku-y-offset gomoku-goto-xy 2 sit-for 0] 6 "\ Display an N by M Gomoku board."]) (fset 'gomoku-display-statistics #[nil "U $ U # UǪ Uɪ \" !" [gomoku-number-of-draws 0 format ": Won %d, lost %d, drew %d" gomoku-number-of-wins gomoku-number-of-losses ": Won %d, lost %d" "" 1 ": Already won one" ": Won %d in a row" mode-line-process set-buffer-modified-p buffer-modified-p] 5 "\ Obnoxiously display some statistics about previous games in mode line."]) (fset 'gomoku-switch-to-window #[nil "!\n! ! )" [get-buffer "*Gomoku*" buff switch-to-buffer gomoku-game-in-progress gomoku-crash-game gomoku-mode] 3 "\ Find or create the Gomoku buffer, and display it." nil]) (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).") (fset 'gomoku-find-filled-qtuple #[(square value) " \n$ \n$ \nÉ$ \n$" [gomoku-check-filled-qtuple square value 1 0 -1] 5 "\ Return T if SQUARE belongs to a qtuple filled with VALUEs."]) (fset 'gomoku-check-filled-qtuple #[(square value dx dy) " \" \n V  Z HU S d \\\nW \\HU\nT\nc\nU  _\\ \n_\\  ." [0 square gomoku-xy-to-index dx dy nil a+4 depl right left b a -4 value gomoku-board 4 gomoku-winning-qtuple-beg gomoku-winning-qtuple-end gomoku-winning-qtuple-dx gomoku-winning-qtuple-dy t] 8 "\ Return T if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."]) (fset 'gomoku-cross-winning-qtuple #[nil " \n $" [gomoku-cross-qtuple gomoku-winning-qtuple-beg gomoku-winning-qtuple-end gomoku-winning-qtuple-dx gomoku-winning-qtuple-dy] 5 "\ Cross winning qtuple, as found by `gomoku-find-filled-qtuple'."]) (fset 'gomoku-cross-qtuple #[(square1 square2 dx dy) " \n\" U ! \\ U\nU  \nW T u!k)M U\nU   W T !!j)' U\nU\nҥ! ҥ!! U\nU\nҥu ҥ!!*!" [gomoku-xy-to-index dx dy depl square1 square2 gomoku-goto-square 1 0 n gomoku-square-width gomoku-put-char 45 gomoku-square-height next-line 124 -1 backward-char 2 47 92 sit-for] 3 "\ Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."]) (fset 'gomoku-move-left #[nil "  ê V !)" [gomoku-point-x x backward-char 1 gomoku-square-width 0] 3 "\ Move point backward one column on the Gomoku board." nil]) (fset 'gomoku-move-right #[nil " ª W u)" [gomoku-point-x x 1 gomoku-board-width gomoku-square-width 0] 3 "\ Move point forward one column on the Gomoku board." nil]) (fset 'gomoku-move-down #[nil "  ê W !)" [gomoku-point-y y next-line 1 gomoku-board-height gomoku-square-height 0] 3 "\ Move point down one row on the Gomoku board." nil]) (fset 'gomoku-move-up #[nil "  ê V !)" [gomoku-point-y y previous-line 1 gomoku-square-height 0] 3 "\ Move point up one row on the Gomoku board." nil]) (fset 'gomoku-move-ne #[nil " " [gomoku-move-up gomoku-move-right] 1 "\ Move point North East on the Gomoku board." nil]) (fset 'gomoku-move-se #[nil " " [gomoku-move-down gomoku-move-right] 1 "\ Move point South East on the Gomoku board." nil]) (fset 'gomoku-move-nw #[nil " " [gomoku-move-up gomoku-move-left] 1 "\ Move point North West on the Gomoku board." nil]) (fset 'gomoku-move-sw #[nil " " [gomoku-move-down gomoku-move-left] 1 "\ Move point South West on the Gomoku board." nil]) (provide 'gomoku)