landmark.el 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. ;;; landmark.el --- neural-network robot that learns landmarks
  2. ;; Copyright (C) 1996-1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Terrence Brannon (was: <brannon@rana.usc.edu>)
  4. ;; Created: December 16, 1996 - first release to usenet
  5. ;; Keywords: games, gomoku, neural network, adaptive search, chemotaxis
  6. ;;;_* Usage
  7. ;;; Just type
  8. ;;; M-x eval-buffer
  9. ;;; M-x landmark-test-run
  10. ;; This file is part of GNU Emacs.
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;; Landmark is a relatively non-participatory game in which a robot
  23. ;; attempts to maneuver towards a tree at the center of the window
  24. ;; based on unique olfactory cues from each of the 4 directions. If
  25. ;; the smell of the tree increases, then the weights in the robot's
  26. ;; brain are adjusted to encourage this odor-driven behavior in the
  27. ;; future. If the smell of the tree decreases, the robots weights are
  28. ;; adjusted to discourage a correct move.
  29. ;; In laymen's terms, the search space is initially flat. The point
  30. ;; of training is to "turn up the edges of the search space" so that
  31. ;; the robot rolls toward the center.
  32. ;; Further, do not become alarmed if the robot appears to oscillate
  33. ;; back and forth between two or a few positions. This simply means
  34. ;; it is currently caught in a local minimum and is doing its best to
  35. ;; work its way out.
  36. ;; The version of this program as described has a small problem. a
  37. ;; move in a net direction can produce gross credit assignment. for
  38. ;; example, if moving south will produce positive payoff, then, if in
  39. ;; a single move, one moves east,west and south, then both east and
  40. ;; west will be improved when they shouldn't
  41. ;; Many thanks to Yuri Pryadkin <yuri@rana.usc.edu> for this
  42. ;; concise problem description.
  43. ;;;_* Require
  44. (eval-when-compile (require 'cl))
  45. ;;;_* From Gomoku
  46. ;;; Code:
  47. (defgroup landmark nil
  48. "Neural-network robot that learns landmarks."
  49. :prefix "landmark-"
  50. :group 'games)
  51. ;;;_ + THE BOARD.
  52. ;; The board is a rectangular grid. We code empty squares with 0, X's with 1
  53. ;; and O's with 6. The rectangle is recorded in a one dimensional vector
  54. ;; containing padding squares (coded with -1). These squares allow us to
  55. ;; detect when we are trying to move out of the board. We denote a square by
  56. ;; its (X,Y) coords, or by the INDEX corresponding to them in the vector. The
  57. ;; leftmost topmost square has coords (1,1) and index landmark-board-width + 2.
  58. ;; Similarly, vectors between squares may be given by two DX, DY coords or by
  59. ;; one DEPL (the difference between indexes).
  60. (defvar landmark-board-width nil
  61. "Number of columns on the Landmark board.")
  62. (defvar landmark-board-height nil
  63. "Number of lines on the Landmark board.")
  64. (defvar landmark-board nil
  65. "Vector recording the actual state of the Landmark board.")
  66. (defvar landmark-vector-length nil
  67. "Length of landmark-board vector.")
  68. (defvar landmark-draw-limit nil
  69. ;; This is usually set to 70% of the number of squares.
  70. "After how many moves will Emacs offer a draw?")
  71. (defvar landmark-cx 0
  72. "This is the x coordinate of the center of the board.")
  73. (defvar landmark-cy 0
  74. "This is the y coordinate of the center of the board.")
  75. (defvar landmark-m 0
  76. "This is the x dimension of the playing board.")
  77. (defvar landmark-n 0
  78. "This is the y dimension of the playing board.")
  79. (defun landmark-xy-to-index (x y)
  80. "Translate X, Y cartesian coords into the corresponding board index."
  81. (+ (* y landmark-board-width) x y))
  82. (defun landmark-index-to-x (index)
  83. "Return corresponding x-coord of board INDEX."
  84. (% index (1+ landmark-board-width)))
  85. (defun landmark-index-to-y (index)
  86. "Return corresponding y-coord of board INDEX."
  87. (/ index (1+ landmark-board-width)))
  88. (defun landmark-init-board ()
  89. "Create the landmark-board vector and fill it with initial values."
  90. (setq landmark-board (make-vector landmark-vector-length 0))
  91. ;; Every square is 0 (i.e. empty) except padding squares:
  92. (let ((i 0) (ii (1- landmark-vector-length)))
  93. (while (<= i landmark-board-width) ; The squares in [0..width] and in
  94. (aset landmark-board i -1) ; [length - width - 1..length - 1]
  95. (aset landmark-board ii -1) ; are padding squares.
  96. (setq i (1+ i)
  97. ii (1- ii))))
  98. (let ((i 0))
  99. (while (< i landmark-vector-length)
  100. (aset landmark-board i -1) ; and also all k*(width+1)
  101. (setq i (+ i landmark-board-width 1)))))
  102. ;;;_ + DISPLAYING THE BOARD.
  103. ;; You may change these values if you have a small screen or if the squares
  104. ;; look rectangular, but spacings SHOULD be at least 2 (MUST BE at least 1).
  105. (defconst landmark-square-width 2
  106. "*Horizontal spacing between squares on the Landmark board.")
  107. (defconst landmark-square-height 1
  108. "*Vertical spacing between squares on the Landmark board.")
  109. (defconst landmark-x-offset 3
  110. "*Number of columns between the Landmark board and the side of the window.")
  111. (defconst landmark-y-offset 1
  112. "*Number of lines between the Landmark board and the top of the window.")
  113. ;;;_ + LANDMARK MODE AND KEYMAP.
  114. (defcustom landmark-mode-hook nil
  115. "If non-nil, its value is called on entry to Landmark mode."
  116. :type 'hook
  117. :group 'landmark)
  118. (defvar landmark-mode-map
  119. (let ((map (make-sparse-keymap)))
  120. ;; Key bindings for cursor motion.
  121. (define-key map "y" 'landmark-move-nw) ; y
  122. (define-key map "u" 'landmark-move-ne) ; u
  123. (define-key map "b" 'landmark-move-sw) ; b
  124. (define-key map "n" 'landmark-move-se) ; n
  125. (define-key map "h" 'backward-char) ; h
  126. (define-key map "l" 'forward-char) ; l
  127. (define-key map "j" 'landmark-move-down) ; j
  128. (define-key map "k" 'landmark-move-up) ; k
  129. (define-key map [kp-7] 'landmark-move-nw)
  130. (define-key map [kp-9] 'landmark-move-ne)
  131. (define-key map [kp-1] 'landmark-move-sw)
  132. (define-key map [kp-3] 'landmark-move-se)
  133. (define-key map [kp-4] 'backward-char)
  134. (define-key map [kp-6] 'forward-char)
  135. (define-key map [kp-2] 'landmark-move-down)
  136. (define-key map [kp-8] 'landmark-move-up)
  137. (define-key map "\C-n" 'landmark-move-down) ; C-n
  138. (define-key map "\C-p" 'landmark-move-up) ; C-p
  139. ;; Key bindings for entering Human moves.
  140. (define-key map "X" 'landmark-human-plays) ; X
  141. (define-key map "x" 'landmark-human-plays) ; x
  142. (define-key map " " 'landmark-start-robot) ; SPC
  143. (define-key map [down-mouse-1] 'landmark-start-robot)
  144. (define-key map [drag-mouse-1] 'landmark-click)
  145. (define-key map [mouse-1] 'landmark-click)
  146. (define-key map [down-mouse-2] 'landmark-click)
  147. (define-key map [mouse-2] 'landmark-mouse-play)
  148. (define-key map [drag-mouse-2] 'landmark-mouse-play)
  149. (define-key map [remap previous-line] 'landmark-move-up)
  150. (define-key map [remap next-line] 'landmark-move-down)
  151. (define-key map [remap beginning-of-line] 'landmark-beginning-of-line)
  152. (define-key map [remap end-of-line] 'landmark-end-of-line)
  153. (define-key map [remap undo] 'landmark-human-takes-back)
  154. (define-key map [remap advertised-undo] 'landmark-human-takes-back)
  155. map)
  156. "Local keymap to use in Landmark mode.")
  157. (defvar landmark-emacs-won ()
  158. "*For making font-lock use the winner's face for the line.")
  159. (defface landmark-font-lock-face-O '((((class color)) :foreground "red")
  160. (t :weight bold))
  161. "Face to use for Emacs's O."
  162. :version "22.1"
  163. :group 'landmark)
  164. (defface landmark-font-lock-face-X '((((class color)) :foreground "green")
  165. (t :weight bold))
  166. "Face to use for your X."
  167. :version "22.1"
  168. :group 'landmark)
  169. (defvar landmark-font-lock-keywords
  170. '(("O" . 'landmark-font-lock-face-O)
  171. ("X" . 'landmark-font-lock-face-X)
  172. ("[-|/\\]" 0 (if landmark-emacs-won
  173. 'landmark-font-lock-face-O
  174. 'landmark-font-lock-face-X)))
  175. "*Font lock rules for Landmark.")
  176. (put 'landmark-mode 'front-sticky
  177. (put 'landmark-mode 'rear-nonsticky '(intangible)))
  178. (put 'landmark-mode 'intangible 1)
  179. ;; This one is for when they set view-read-only to t: Landmark cannot
  180. ;; allow View Mode to be activated in its buffer.
  181. (put 'landmark-mode 'mode-class 'special)
  182. (defun landmark-mode ()
  183. "Major mode for playing Landmark against Emacs.
  184. You and Emacs play in turn by marking a free square. You mark it with X
  185. and Emacs marks it with O. The winner is the first to get five contiguous
  186. marks horizontally, vertically or in diagonal.
  187. You play by moving the cursor over the square you choose and hitting \\[landmark-human-plays].
  188. Other useful commands:
  189. \\{landmark-mode-map}
  190. Entry to this mode calls the value of `landmark-mode-hook' if that value
  191. is non-nil. One interesting value is `turn-on-font-lock'."
  192. (interactive)
  193. (kill-all-local-variables)
  194. (setq major-mode 'landmark-mode
  195. mode-name "Landmark")
  196. (landmark-display-statistics)
  197. (use-local-map landmark-mode-map)
  198. (make-local-variable 'font-lock-defaults)
  199. (setq font-lock-defaults '(landmark-font-lock-keywords t)
  200. buffer-read-only t)
  201. (run-mode-hooks 'landmark-mode-hook))
  202. ;;;_ + THE SCORE TABLE.
  203. ;; Every (free) square has a score associated to it, recorded in the
  204. ;; LANDMARK-SCORE-TABLE vector. The program always plays in the square having
  205. ;; the highest score.
  206. (defvar landmark-score-table nil
  207. "Vector recording the actual score of the free squares.")
  208. ;; The key point point about the algorithm is that, rather than considering
  209. ;; the board as just a set of squares, we prefer to see it as a "space" of
  210. ;; internested 5-tuples of contiguous squares (called qtuples).
  211. ;;
  212. ;; The aim of the program is to fill one qtuple with its O's while preventing
  213. ;; you from filling another one with your X's. To that effect, it computes a
  214. ;; score for every qtuple, with better qtuples having better scores. Of
  215. ;; course, the score of a qtuple (taken in isolation) is just determined by
  216. ;; its contents as a set, i.e. not considering the order of its elements. The
  217. ;; highest score is given to the "OOOO" qtuples because playing in such a
  218. ;; qtuple is winning the game. Just after this comes the "XXXX" qtuple because
  219. ;; not playing in it is just losing the game, and so on. Note that a
  220. ;; "polluted" qtuple, i.e. one containing at least one X and at least one O,
  221. ;; has score zero because there is no more any point in playing in it, from
  222. ;; both an attacking and a defending point of view.
  223. ;;
  224. ;; Given the score of every qtuple, the score of a given free square on the
  225. ;; board is just the sum of the scores of all the qtuples to which it belongs,
  226. ;; because playing in that square is playing in all its containing qtuples at
  227. ;; once. And it is that function which takes into account the internesting of
  228. ;; the qtuples.
  229. ;;
  230. ;; This algorithm is rather simple but anyway it gives a not so dumb level of
  231. ;; play. It easily extends to "n-dimensional Landmark", where a win should not
  232. ;; be obtained with as few as 5 contiguous marks: 6 or 7 (depending on n !)
  233. ;; should be preferred.
  234. ;; Here are the scores of the nine "non-polluted" configurations. Tuning
  235. ;; these values will change (hopefully improve) the strength of the program
  236. ;; and may change its style (rather aggressive here).
  237. (defconst landmark-nil-score 7 "Score of an empty qtuple.")
  238. (defconst landmark-score-trans-table
  239. (let ((Xscore 15) ; Score of a qtuple containing one X.
  240. (XXscore 400) ; Score of a qtuple containing two X's.
  241. (XXXscore 1800) ; Score of a qtuple containing three X's.
  242. (XXXXscore 100000) ; Score of a qtuple containing four X's.
  243. (Oscore 35) ; Score of a qtuple containing one O.
  244. (OOscore 800) ; Score of a qtuple containing two O's.
  245. (OOOscore 15000) ; Score of a qtuple containing three O's.
  246. (OOOOscore 800000)) ; Score of a qtuple containing four O's.
  247. ;; These values are not just random: if, given the following situation:
  248. ;;
  249. ;; . . . . . . . O .
  250. ;; . X X a . . . X .
  251. ;; . . . X . . . X .
  252. ;; . . . X . . . X .
  253. ;; . . . . . . . b .
  254. ;;
  255. ;; you want Emacs to play in "a" and not in "b", then the parameters must
  256. ;; satisfy the inequality:
  257. ;;
  258. ;; 6 * XXscore > XXXscore + XXscore
  259. ;;
  260. ;; because "a" mainly belongs to six "XX" qtuples (the others are less
  261. ;; important) while "b" belongs to one "XXX" and one "XX" qtuples.
  262. ;; Other conditions are required to obtain sensible moves, but the
  263. ;; previous example should illustrate the point. If you manage to
  264. ;; improve on these values, please send me a note. Thanks.
  265. ;; As we chose values 0, 1 and 6 to denote empty, X and O squares,
  266. ;; the contents of a qtuple are uniquely determined by the sum of
  267. ;; its elements and we just have to set up a translation table.
  268. (vector landmark-nil-score Xscore XXscore XXXscore XXXXscore 0
  269. Oscore 0 0 0 0 0
  270. OOscore 0 0 0 0 0
  271. OOOscore 0 0 0 0 0
  272. OOOOscore 0 0 0 0 0
  273. 0))
  274. "Vector associating qtuple contents to their score.")
  275. ;; If you do not modify drastically the previous constants, the only way for a
  276. ;; square to have a score higher than OOOOscore is to belong to a "OOOO"
  277. ;; qtuple, thus to be a winning move. Similarly, the only way for a square to
  278. ;; have a score between XXXXscore and OOOOscore is to belong to a "XXXX"
  279. ;; qtuple. We may use these considerations to detect when a given move is
  280. ;; winning or losing.
  281. (defconst landmark-winning-threshold
  282. (aref landmark-score-trans-table (+ 6 6 6 6)) ;; OOOOscore
  283. "Threshold score beyond which an Emacs move is winning.")
  284. (defconst landmark-losing-threshold
  285. (aref landmark-score-trans-table (+ 1 1 1 1)) ;; XXXXscore
  286. "Threshold score beyond which a human move is winning.")
  287. (defun landmark-strongest-square ()
  288. "Compute index of free square with highest score, or nil if none."
  289. ;; We just have to loop other all squares. However there are two problems:
  290. ;; 1/ The SCORE-TABLE only gives correct scores to free squares. To speed
  291. ;; up future searches, we set the score of padding or occupied squares
  292. ;; to -1 whenever we meet them.
  293. ;; 2/ We want to choose randomly between equally good moves.
  294. (let ((score-max 0)
  295. (count 0) ; Number of equally good moves
  296. (square (landmark-xy-to-index 1 1)) ; First square
  297. (end (landmark-xy-to-index landmark-board-width landmark-board-height))
  298. best-square score)
  299. (while (<= square end)
  300. (cond
  301. ;; If score is lower (i.e. most of the time), skip to next:
  302. ((< (aref landmark-score-table square) score-max))
  303. ;; If score is better, beware of non free squares:
  304. ((> (setq score (aref landmark-score-table square)) score-max)
  305. (if (zerop (aref landmark-board square)) ; is it free ?
  306. (setq count 1 ; yes: take it !
  307. best-square square
  308. score-max score)
  309. (aset landmark-score-table square -1))) ; no: kill it !
  310. ;; If score is equally good, choose randomly. But first check freedom:
  311. ((not (zerop (aref landmark-board square)))
  312. (aset landmark-score-table square -1))
  313. ((zerop (random (setq count (1+ count))))
  314. (setq best-square square
  315. score-max score)))
  316. (setq square (1+ square))) ; try next square
  317. best-square))
  318. ;;;_ - INITIALIZING THE SCORE TABLE.
  319. ;; At initialization the board is empty so that every qtuple amounts for
  320. ;; nil-score. Therefore, the score of any square is nil-score times the number
  321. ;; of qtuples that pass through it. This number is 3 in a corner and 20 if you
  322. ;; are sufficiently far from the sides. As computing the number is time
  323. ;; consuming, we initialize every square with 20*nil-score and then only
  324. ;; consider squares at less than 5 squares from one side. We speed this up by
  325. ;; taking symmetry into account.
  326. ;; Also, as it is likely that successive games will be played on a board with
  327. ;; same size, it is a good idea to save the initial SCORE-TABLE configuration.
  328. (defvar landmark-saved-score-table nil
  329. "Recorded initial value of previous score table.")
  330. (defvar landmark-saved-board-width nil
  331. "Recorded value of previous board width.")
  332. (defvar landmark-saved-board-height nil
  333. "Recorded value of previous board height.")
  334. (defun landmark-init-score-table ()
  335. "Create the score table vector and fill it with initial values."
  336. (if (and landmark-saved-score-table ; Has it been stored last time ?
  337. (= landmark-board-width landmark-saved-board-width)
  338. (= landmark-board-height landmark-saved-board-height))
  339. (setq landmark-score-table (copy-sequence landmark-saved-score-table))
  340. ;; No, compute it:
  341. (setq landmark-score-table
  342. (make-vector landmark-vector-length (* 20 landmark-nil-score)))
  343. (let (i j maxi maxj maxi2 maxj2)
  344. (setq maxi (/ (1+ landmark-board-width) 2)
  345. maxj (/ (1+ landmark-board-height) 2)
  346. maxi2 (min 4 maxi)
  347. maxj2 (min 4 maxj))
  348. ;; We took symmetry into account and could use it more if the board
  349. ;; would have been square and not rectangular !
  350. ;; In our case we deal with all (i,j) in the set [1..maxi2]*[1..maxj] U
  351. ;; [maxi2+1..maxi]*[1..maxj2]. Maxi2 and maxj2 are used because the
  352. ;; board may well be less than 8 by 8 !
  353. (setq i 1)
  354. (while (<= i maxi2)
  355. (setq j 1)
  356. (while (<= j maxj)
  357. (landmark-init-square-score i j)
  358. (setq j (1+ j)))
  359. (setq i (1+ i)))
  360. (while (<= i maxi)
  361. (setq j 1)
  362. (while (<= j maxj2)
  363. (landmark-init-square-score i j)
  364. (setq j (1+ j)))
  365. (setq i (1+ i))))
  366. (setq landmark-saved-score-table (copy-sequence landmark-score-table)
  367. landmark-saved-board-width landmark-board-width
  368. landmark-saved-board-height landmark-board-height)))
  369. (defun landmark-nb-qtuples (i j)
  370. "Return the number of qtuples containing square I,J."
  371. ;; This function is complicated because we have to deal
  372. ;; with ugly cases like 3 by 6 boards, but it works.
  373. ;; If you have a simpler (and correct) solution, send it to me. Thanks !
  374. (let ((left (min 4 (1- i)))
  375. (right (min 4 (- landmark-board-width i)))
  376. (up (min 4 (1- j)))
  377. (down (min 4 (- landmark-board-height j))))
  378. (+ -12
  379. (min (max (+ left right) 3) 8)
  380. (min (max (+ up down) 3) 8)
  381. (min (max (+ (min left up) (min right down)) 3) 8)
  382. (min (max (+ (min right up) (min left down)) 3) 8))))
  383. (defun landmark-init-square-score (i j)
  384. "Give initial score to square I,J and to its mirror images."
  385. (let ((ii (1+ (- landmark-board-width i)))
  386. (jj (1+ (- landmark-board-height j)))
  387. (sc (* (landmark-nb-qtuples i j) (aref landmark-score-trans-table 0))))
  388. (aset landmark-score-table (landmark-xy-to-index i j) sc)
  389. (aset landmark-score-table (landmark-xy-to-index ii j) sc)
  390. (aset landmark-score-table (landmark-xy-to-index i jj) sc)
  391. (aset landmark-score-table (landmark-xy-to-index ii jj) sc)))
  392. ;;;_ - MAINTAINING THE SCORE TABLE.
  393. ;; We do not provide functions for computing the SCORE-TABLE given the
  394. ;; contents of the BOARD. This would involve heavy nested loops, with time
  395. ;; proportional to the size of the board. It is better to update the
  396. ;; SCORE-TABLE after each move. Updating needs not modify more than 36
  397. ;; squares: it is done in constant time.
  398. (defun landmark-update-score-table (square dval)
  399. "Update score table after SQUARE received a DVAL increment."
  400. ;; The board has already been updated when this function is called.
  401. ;; Updating scores is done by looking for qtuples boundaries in all four
  402. ;; directions and then calling update-score-in-direction.
  403. ;; Finally all squares received the right increment, and then are up to
  404. ;; date, except possibly for SQUARE itself if we are taking a move back for
  405. ;; its score had been set to -1 at the time.
  406. (let* ((x (landmark-index-to-x square))
  407. (y (landmark-index-to-y square))
  408. (imin (max -4 (- 1 x)))
  409. (jmin (max -4 (- 1 y)))
  410. (imax (min 0 (- landmark-board-width x 4)))
  411. (jmax (min 0 (- landmark-board-height y 4))))
  412. (landmark-update-score-in-direction imin imax
  413. square 1 0 dval)
  414. (landmark-update-score-in-direction jmin jmax
  415. square 0 1 dval)
  416. (landmark-update-score-in-direction (max imin jmin) (min imax jmax)
  417. square 1 1 dval)
  418. (landmark-update-score-in-direction (max (- 1 y) -4
  419. (- x landmark-board-width))
  420. (min 0 (- x 5)
  421. (- landmark-board-height y 4))
  422. square -1 1 dval)))
  423. (defun landmark-update-score-in-direction (left right square dx dy dval)
  424. "Update scores for all squares in the qtuples in range.
  425. That is, those between the LEFTth square and the RIGHTth after SQUARE,
  426. along the DX, DY direction, considering that DVAL has been added on SQUARE."
  427. ;; We always have LEFT <= 0, RIGHT <= 0 and DEPL > 0 but we may very well
  428. ;; have LEFT > RIGHT, indicating that no qtuple contains SQUARE along that
  429. ;; DX,DY direction.
  430. (cond
  431. ((> left right)) ; Quit
  432. (t ; Else ..
  433. (let (depl square0 square1 square2 count delta)
  434. (setq depl (landmark-xy-to-index dx dy)
  435. square0 (+ square (* left depl))
  436. square1 (+ square (* right depl))
  437. square2 (+ square0 (* 4 depl)))
  438. ;; Compute the contents of the first qtuple:
  439. (setq square square0
  440. count 0)
  441. (while (<= square square2)
  442. (setq count (+ count (aref landmark-board square))
  443. square (+ square depl)))
  444. (while (<= square0 square1)
  445. ;; Update the squares of the qtuple beginning in SQUARE0 and ending
  446. ;; in SQUARE2.
  447. (setq delta (- (aref landmark-score-trans-table count)
  448. (aref landmark-score-trans-table (- count dval))))
  449. (cond ((not (zerop delta)) ; or else nothing to update
  450. (setq square square0)
  451. (while (<= square square2)
  452. (if (zerop (aref landmark-board square)) ; only for free squares
  453. (aset landmark-score-table square
  454. (+ (aref landmark-score-table square) delta)))
  455. (setq square (+ square depl)))))
  456. ;; Then shift the qtuple one square along DEPL, this only requires
  457. ;; modifying SQUARE0 and SQUARE2.
  458. (setq square2 (+ square2 depl)
  459. count (+ count (- (aref landmark-board square0))
  460. (aref landmark-board square2))
  461. square0 (+ square0 depl)))))))
  462. ;;;
  463. ;;; GAME CONTROL.
  464. ;;;
  465. ;; Several variables are used to monitor a game, including a GAME-HISTORY (the
  466. ;; list of all (SQUARE . PREVSCORE) played) that allows to take moves back
  467. ;; (anti-updating the score table) and to compute the table from scratch in
  468. ;; case of an interruption.
  469. (defvar landmark-game-in-progress nil
  470. "Non-nil if a game is in progress.")
  471. (defvar landmark-game-history nil
  472. "A record of all moves that have been played during current game.")
  473. (defvar landmark-number-of-moves nil
  474. "Number of moves already played in current game.")
  475. (defvar landmark-number-of-human-moves nil
  476. "Number of moves already played by human in current game.")
  477. (defvar landmark-emacs-played-first nil
  478. "Non-nil if Emacs played first.")
  479. (defvar landmark-human-took-back nil
  480. "Non-nil if Human took back a move during the game.")
  481. (defvar landmark-human-refused-draw nil
  482. "Non-nil if Human refused Emacs offer of a draw.")
  483. (defvar landmark-emacs-is-computing nil
  484. ;; This is used to detect interruptions. Hopefully, it should not be needed.
  485. "Non-nil if Emacs is in the middle of a computation.")
  486. (defun landmark-start-game (n m)
  487. "Initialize a new game on an N by M board."
  488. (setq landmark-emacs-is-computing t) ; Raise flag
  489. (setq landmark-game-in-progress t)
  490. (setq landmark-board-width n
  491. landmark-board-height m
  492. landmark-vector-length (1+ (* (+ m 2) (1+ n)))
  493. landmark-draw-limit (/ (* 7 n m) 10))
  494. (setq landmark-emacs-won nil
  495. landmark-game-history nil
  496. landmark-number-of-moves 0
  497. landmark-number-of-human-moves 0
  498. landmark-emacs-played-first nil
  499. landmark-human-took-back nil
  500. landmark-human-refused-draw nil)
  501. (landmark-init-display n m) ; Display first: the rest takes time
  502. (landmark-init-score-table) ; INIT-BOARD requires that the score
  503. (landmark-init-board) ; table be already created.
  504. (setq landmark-emacs-is-computing nil))
  505. (defun landmark-play-move (square val &optional dont-update-score)
  506. "Go to SQUARE, play VAL and update everything."
  507. (setq landmark-emacs-is-computing t) ; Raise flag
  508. (cond ((= 1 val) ; a Human move
  509. (setq landmark-number-of-human-moves (1+ landmark-number-of-human-moves)))
  510. ((zerop landmark-number-of-moves) ; an Emacs move. Is it first ?
  511. (setq landmark-emacs-played-first t)))
  512. (setq landmark-game-history
  513. (cons (cons square (aref landmark-score-table square))
  514. landmark-game-history)
  515. landmark-number-of-moves (1+ landmark-number-of-moves))
  516. (landmark-plot-square square val)
  517. (aset landmark-board square val) ; *BEFORE* UPDATE-SCORE !
  518. (if dont-update-score nil
  519. (landmark-update-score-table square val) ; previous val was 0: dval = val
  520. (aset landmark-score-table square -1))
  521. (setq landmark-emacs-is-computing nil))
  522. (defun landmark-take-back ()
  523. "Take back last move and update everything."
  524. (setq landmark-emacs-is-computing t)
  525. (let* ((last-move (car landmark-game-history))
  526. (square (car last-move))
  527. (oldval (aref landmark-board square)))
  528. (if (= 1 oldval)
  529. (setq landmark-number-of-human-moves (1- landmark-number-of-human-moves)))
  530. (setq landmark-game-history (cdr landmark-game-history)
  531. landmark-number-of-moves (1- landmark-number-of-moves))
  532. (landmark-plot-square square 0)
  533. (aset landmark-board square 0) ; *BEFORE* UPDATE-SCORE !
  534. (landmark-update-score-table square (- oldval))
  535. (aset landmark-score-table square (cdr last-move)))
  536. (setq landmark-emacs-is-computing nil))
  537. ;;;_ + SESSION CONTROL.
  538. (defvar landmark-number-of-trials 0
  539. "The number of times that landmark has been run.")
  540. (defvar landmark-sum-of-moves 0
  541. "The total number of moves made in all games.")
  542. (defvar landmark-number-of-emacs-wins 0
  543. "Number of games Emacs won in this session.")
  544. (defvar landmark-number-of-human-wins 0
  545. "Number of games you won in this session.")
  546. (defvar landmark-number-of-draws 0
  547. "Number of games already drawn in this session.")
  548. (defun landmark-terminate-game (result)
  549. "Terminate the current game with RESULT."
  550. (setq landmark-number-of-trials (1+ landmark-number-of-trials))
  551. (setq landmark-sum-of-moves (+ landmark-sum-of-moves landmark-number-of-moves))
  552. (if (eq result 'crash-game)
  553. (message
  554. "Sorry, I have been interrupted and cannot resume that game..."))
  555. (landmark-display-statistics)
  556. ;;(ding)
  557. (setq landmark-game-in-progress nil))
  558. (defun landmark-crash-game ()
  559. "What to do when Emacs detects it has been interrupted."
  560. (setq landmark-emacs-is-computing nil)
  561. (landmark-terminate-game 'crash-game)
  562. (sit-for 4) ; Let's see the message
  563. (landmark-prompt-for-other-game))
  564. ;;;_ + INTERACTIVE COMMANDS.
  565. (defun landmark-emacs-plays ()
  566. "Compute Emacs next move and play it."
  567. (interactive)
  568. (landmark-switch-to-window)
  569. (cond
  570. (landmark-emacs-is-computing
  571. (landmark-crash-game))
  572. ((not landmark-game-in-progress)
  573. (landmark-prompt-for-other-game))
  574. (t
  575. (message "Let me think...")
  576. (let (square score)
  577. (setq square (landmark-strongest-square))
  578. (cond ((null square)
  579. (landmark-terminate-game 'nobody-won))
  580. (t
  581. (setq score (aref landmark-score-table square))
  582. (landmark-play-move square 6)
  583. (cond ((>= score landmark-winning-threshold)
  584. (setq landmark-emacs-won t) ; for font-lock
  585. (landmark-find-filled-qtuple square 6)
  586. (landmark-terminate-game 'emacs-won))
  587. ((zerop score)
  588. (landmark-terminate-game 'nobody-won))
  589. ((and (> landmark-number-of-moves landmark-draw-limit)
  590. (not landmark-human-refused-draw)
  591. (landmark-offer-a-draw))
  592. (landmark-terminate-game 'draw-agreed))
  593. (t
  594. (landmark-prompt-for-move)))))))))
  595. ;; For small square dimensions this is approximate, since though measured in
  596. ;; pixels, event's (X . Y) is a character's top-left corner.
  597. (defun landmark-click (click)
  598. "Position at the square where you click."
  599. (interactive "e")
  600. (and (windowp (posn-window (setq click (event-end click))))
  601. (numberp (posn-point click))
  602. (select-window (posn-window click))
  603. (setq click (posn-col-row click))
  604. (landmark-goto-xy
  605. (min (max (/ (+ (- (car click)
  606. landmark-x-offset
  607. 1)
  608. (window-hscroll)
  609. landmark-square-width
  610. (% landmark-square-width 2)
  611. (/ landmark-square-width 2))
  612. landmark-square-width)
  613. 1)
  614. landmark-board-width)
  615. (min (max (/ (+ (- (cdr click)
  616. landmark-y-offset
  617. 1)
  618. (let ((inhibit-point-motion-hooks t))
  619. (count-lines 1 (window-start)))
  620. landmark-square-height
  621. (% landmark-square-height 2)
  622. (/ landmark-square-height 2))
  623. landmark-square-height)
  624. 1)
  625. landmark-board-height))))
  626. (defun landmark-mouse-play (click)
  627. "Play at the square where you click."
  628. (interactive "e")
  629. (if (landmark-click click)
  630. (landmark-human-plays)))
  631. (defun landmark-human-plays ()
  632. "Signal to the Landmark program that you have played.
  633. You must have put the cursor on the square where you want to play.
  634. If the game is finished, this command requests for another game."
  635. (interactive)
  636. (landmark-switch-to-window)
  637. (cond
  638. (landmark-emacs-is-computing
  639. (landmark-crash-game))
  640. ((not landmark-game-in-progress)
  641. (landmark-prompt-for-other-game))
  642. (t
  643. (let (square score)
  644. (setq square (landmark-point-square))
  645. (cond ((null square)
  646. (error "Your point is not on a square. Retry!"))
  647. ((not (zerop (aref landmark-board square)))
  648. (error "Your point is not on a free square. Retry!"))
  649. (t
  650. (setq score (aref landmark-score-table square))
  651. (landmark-play-move square 1)
  652. (cond ((and (>= score landmark-losing-threshold)
  653. ;; Just testing SCORE > THRESHOLD is not enough for
  654. ;; detecting wins, it just gives an indication that
  655. ;; we confirm with LANDMARK-FIND-FILLED-QTUPLE.
  656. (landmark-find-filled-qtuple square 1))
  657. (landmark-terminate-game 'human-won))
  658. (t
  659. (landmark-emacs-plays)))))))))
  660. (defun landmark-human-takes-back ()
  661. "Signal to the Landmark program that you wish to take back your last move."
  662. (interactive)
  663. (landmark-switch-to-window)
  664. (cond
  665. (landmark-emacs-is-computing
  666. (landmark-crash-game))
  667. ((not landmark-game-in-progress)
  668. (message "Too late for taking back...")
  669. (sit-for 4)
  670. (landmark-prompt-for-other-game))
  671. ((zerop landmark-number-of-human-moves)
  672. (message "You have not played yet... Your move?"))
  673. (t
  674. (message "One moment, please...")
  675. ;; It is possible for the user to let Emacs play several consecutive
  676. ;; moves, so that the best way to know when to stop taking back moves is
  677. ;; to count the number of human moves:
  678. (setq landmark-human-took-back t)
  679. (let ((number landmark-number-of-human-moves))
  680. (while (= number landmark-number-of-human-moves)
  681. (landmark-take-back)))
  682. (landmark-prompt-for-move))))
  683. (defun landmark-human-resigns ()
  684. "Signal to the Landmark program that you may want to resign."
  685. (interactive)
  686. (landmark-switch-to-window)
  687. (cond
  688. (landmark-emacs-is-computing
  689. (landmark-crash-game))
  690. ((not landmark-game-in-progress)
  691. (message "There is no game in progress"))
  692. ((y-or-n-p "You mean, you resign? ")
  693. (landmark-terminate-game 'human-resigned))
  694. ((y-or-n-p "You mean, we continue? ")
  695. (landmark-prompt-for-move))
  696. (t
  697. (landmark-terminate-game 'human-resigned)))) ; OK. Accept it
  698. ;;;_ + PROMPTING THE HUMAN PLAYER.
  699. (defun landmark-prompt-for-move ()
  700. "Display a message asking for Human's move."
  701. (message (if (zerop landmark-number-of-human-moves)
  702. "Your move? (move to a free square and hit X, RET ...)"
  703. "Your move?")))
  704. (defun landmark-prompt-for-other-game ()
  705. "Ask for another game, and start it."
  706. (if (y-or-n-p "Another game? ")
  707. (if (y-or-n-p "Retain learned weights ")
  708. (landmark 2)
  709. (landmark 1))
  710. (message "Chicken!")))
  711. (defun landmark-offer-a-draw ()
  712. "Offer a draw and return t if Human accepted it."
  713. (or (y-or-n-p "I offer you a draw. Do you accept it? ")
  714. (not (setq landmark-human-refused-draw t))))
  715. (defun landmark-max-width ()
  716. "Largest possible board width for the current window."
  717. (1+ (/ (- (window-width (selected-window))
  718. landmark-x-offset landmark-x-offset 1)
  719. landmark-square-width)))
  720. (defun landmark-max-height ()
  721. "Largest possible board height for the current window."
  722. (1+ (/ (- (window-height (selected-window))
  723. landmark-y-offset landmark-y-offset 2)
  724. ;; 2 instead of 1 because WINDOW-HEIGHT includes the mode line !
  725. landmark-square-height)))
  726. (defun landmark-point-y ()
  727. "Return the board row where point is."
  728. (let ((inhibit-point-motion-hooks t))
  729. (1+ (/ (- (count-lines 1 (point)) landmark-y-offset (if (bolp) 0 1))
  730. landmark-square-height))))
  731. (defun landmark-point-square ()
  732. "Return the index of the square point is on."
  733. (let ((inhibit-point-motion-hooks t))
  734. (landmark-xy-to-index (1+ (/ (- (current-column) landmark-x-offset)
  735. landmark-square-width))
  736. (landmark-point-y))))
  737. (defun landmark-goto-square (index)
  738. "Move point to square number INDEX."
  739. (landmark-goto-xy (landmark-index-to-x index) (landmark-index-to-y index)))
  740. (defun landmark-goto-xy (x y)
  741. "Move point to square at X, Y coords."
  742. (let ((inhibit-point-motion-hooks t))
  743. (goto-char (point-min))
  744. (forward-line (+ landmark-y-offset (* landmark-square-height (1- y)))))
  745. (move-to-column (+ landmark-x-offset (* landmark-square-width (1- x)))))
  746. (defun landmark-plot-square (square value)
  747. "Draw 'X', 'O' or '.' on SQUARE depending on VALUE, leave point there."
  748. (or (= value 1)
  749. (landmark-goto-square square))
  750. (let ((inhibit-read-only t)
  751. (inhibit-point-motion-hooks t))
  752. (insert-and-inherit (cond ((= value 1) ?.)
  753. ((= value 2) ?N)
  754. ((= value 3) ?S)
  755. ((= value 4) ?E)
  756. ((= value 5) ?W)
  757. ((= value 6) ?^)))
  758. (and (zerop value)
  759. (add-text-properties (1- (point)) (point)
  760. '(mouse-face highlight
  761. help-echo "\
  762. mouse-1: get robot moving, mouse-2: play on this square")))
  763. (delete-char 1)
  764. (backward-char 1))
  765. (sit-for 0)) ; Display NOW
  766. (defun landmark-init-display (n m)
  767. "Display an N by M Landmark board."
  768. (buffer-disable-undo (current-buffer))
  769. (let ((inhibit-read-only t)
  770. (point 1) opoint
  771. (intangible t)
  772. (i m) j x)
  773. ;; Try to minimize number of chars (because of text properties)
  774. (setq tab-width
  775. (if (zerop (% landmark-x-offset landmark-square-width))
  776. landmark-square-width
  777. (max (/ (+ (% landmark-x-offset landmark-square-width)
  778. landmark-square-width 1) 2) 2)))
  779. (erase-buffer)
  780. (newline landmark-y-offset)
  781. (while (progn
  782. (setq j n
  783. x (- landmark-x-offset landmark-square-width))
  784. (while (>= (setq j (1- j)) 0)
  785. (insert-char ?\t (/ (- (setq x (+ x landmark-square-width))
  786. (current-column))
  787. tab-width))
  788. (insert-char ? (- x (current-column)))
  789. (if (setq intangible (not intangible))
  790. (put-text-property point (point) 'intangible 2))
  791. (and (zerop j)
  792. (= i (- m 2))
  793. (progn
  794. (while (>= i 3)
  795. (append-to-buffer (current-buffer) opoint (point))
  796. (setq i (- i 2)))
  797. (goto-char (point-max))))
  798. (setq point (point))
  799. (insert ?=)
  800. (add-text-properties point (point)
  801. '(mouse-face highlight help-echo "\
  802. mouse-1: get robot moving, mouse-2: play on this square")))
  803. (> (setq i (1- i)) 0))
  804. (if (= i (1- m))
  805. (setq opoint point))
  806. (insert-char ?\n landmark-square-height))
  807. (or (eq (char-after 1) ?.)
  808. (put-text-property 1 2 'point-entered
  809. (lambda (_x _y) (if (bobp) (forward-char)))))
  810. (or intangible
  811. (put-text-property point (point) 'intangible 2))
  812. (put-text-property point (point) 'point-entered
  813. (lambda (_x _y) (if (eobp) (backward-char))))
  814. (put-text-property (point-min) (point) 'category 'landmark-mode))
  815. (landmark-goto-xy (/ (1+ n) 2) (/ (1+ m) 2)) ; center of the board
  816. (sit-for 0)) ; Display NOW
  817. (defun landmark-display-statistics ()
  818. "Obnoxiously display some statistics about previous games in mode line."
  819. ;; We store this string in the mode-line-process local variable.
  820. ;; This is certainly not the cleanest way out ...
  821. (setq mode-line-process
  822. (format ": Trials: %d, Avg#Moves: %d"
  823. landmark-number-of-trials
  824. (if (zerop landmark-number-of-trials)
  825. 0
  826. (/ landmark-sum-of-moves landmark-number-of-trials))))
  827. (force-mode-line-update))
  828. (defun landmark-switch-to-window ()
  829. "Find or create the Landmark buffer, and display it."
  830. (interactive)
  831. (let ((buff (get-buffer "*Landmark*")))
  832. (if buff ; Buffer exists:
  833. (switch-to-buffer buff) ; no problem.
  834. (if landmark-game-in-progress
  835. (landmark-crash-game)) ; buffer has been killed or something
  836. (switch-to-buffer "*Landmark*") ; Anyway, start anew.
  837. (landmark-mode))))
  838. ;;;_ + CROSSING WINNING QTUPLES.
  839. ;; When someone succeeds in filling a qtuple, we draw a line over the five
  840. ;; corresponding squares. One problem is that the program does not know which
  841. ;; squares ! It only knows the square where the last move has been played and
  842. ;; who won. The solution is to scan the board along all four directions.
  843. (defun landmark-find-filled-qtuple (square value)
  844. "Return t if SQUARE belongs to a qtuple filled with VALUEs."
  845. (or (landmark-check-filled-qtuple square value 1 0)
  846. (landmark-check-filled-qtuple square value 0 1)
  847. (landmark-check-filled-qtuple square value 1 1)
  848. (landmark-check-filled-qtuple square value -1 1)))
  849. (defun landmark-check-filled-qtuple (square value dx dy)
  850. "Return t if SQUARE belongs to a qtuple filled with VALUEs along DX, DY."
  851. (let ((a 0) (b 0)
  852. (left square) (right square)
  853. (depl (landmark-xy-to-index dx dy)))
  854. (while (and (> a -4) ; stretch tuple left
  855. (= value (aref landmark-board (setq left (- left depl)))))
  856. (setq a (1- a)))
  857. (while (and (< b (+ a 4)) ; stretch tuple right
  858. (= value (aref landmark-board (setq right (+ right depl)))))
  859. (setq b (1+ b)))
  860. (cond ((= b (+ a 4)) ; tuple length = 5 ?
  861. (landmark-cross-qtuple (+ square (* a depl)) (+ square (* b depl))
  862. dx dy)
  863. t))))
  864. (defun landmark-cross-qtuple (square1 square2 dx dy)
  865. "Cross every square between SQUARE1 and SQUARE2 in the DX, DY direction."
  866. (save-excursion ; Not moving point from last square
  867. (let ((depl (landmark-xy-to-index dx dy))
  868. (inhibit-read-only t)
  869. (inhibit-point-motion-hooks t))
  870. ;; WARNING: this function assumes DEPL > 0 and SQUARE2 > SQUARE1
  871. (while (/= square1 square2)
  872. (landmark-goto-square square1)
  873. (setq square1 (+ square1 depl))
  874. (cond
  875. ((= dy 0) ; Horizontal
  876. (forward-char 1)
  877. (insert-char ?- (1- landmark-square-width) t)
  878. (delete-region (point) (progn
  879. (skip-chars-forward " \t")
  880. (point))))
  881. ((= dx 0) ; Vertical
  882. (let ((landmark-n 1)
  883. (column (current-column)))
  884. (while (< landmark-n landmark-square-height)
  885. (setq landmark-n (1+ landmark-n))
  886. (forward-line 1)
  887. (indent-to column)
  888. (insert-and-inherit ?|))))
  889. ((= dx -1) ; 1st Diagonal
  890. (indent-to (prog1 (- (current-column) (/ landmark-square-width 2))
  891. (forward-line (/ landmark-square-height 2))))
  892. (insert-and-inherit ?/))
  893. (t ; 2nd Diagonal
  894. (indent-to (prog1 (+ (current-column) (/ landmark-square-width 2))
  895. (forward-line (/ landmark-square-height 2))))
  896. (insert-and-inherit ?\\))))))
  897. (sit-for 0)) ; Display NOW
  898. ;;;_ + CURSOR MOTION.
  899. ;; previous-line and next-line don't work right with intangible newlines
  900. (defun landmark-move-down ()
  901. "Move point down one row on the Landmark board."
  902. (interactive)
  903. (if (< (landmark-point-y) landmark-board-height)
  904. (forward-line 1)));;; landmark-square-height)))
  905. (defun landmark-move-up ()
  906. "Move point up one row on the Landmark board."
  907. (interactive)
  908. (if (> (landmark-point-y) 1)
  909. (forward-line (- landmark-square-height))))
  910. (defun landmark-move-ne ()
  911. "Move point North East on the Landmark board."
  912. (interactive)
  913. (landmark-move-up)
  914. (forward-char))
  915. (defun landmark-move-se ()
  916. "Move point South East on the Landmark board."
  917. (interactive)
  918. (landmark-move-down)
  919. (forward-char))
  920. (defun landmark-move-nw ()
  921. "Move point North West on the Landmark board."
  922. (interactive)
  923. (landmark-move-up)
  924. (backward-char))
  925. (defun landmark-move-sw ()
  926. "Move point South West on the Landmark board."
  927. (interactive)
  928. (landmark-move-down)
  929. (backward-char))
  930. (defun landmark-beginning-of-line ()
  931. "Move point to first square on the Landmark board row."
  932. (interactive)
  933. (move-to-column landmark-x-offset))
  934. (defun landmark-end-of-line ()
  935. "Move point to last square on the Landmark board row."
  936. (interactive)
  937. (move-to-column (+ landmark-x-offset
  938. (* landmark-square-width (1- landmark-board-width)))))
  939. ;;;_ + Simulation variables
  940. ;;;_ - landmark-nvar
  941. (defvar landmark-nvar 0.0075
  942. "Not used.
  943. Affects a noise generator which was used in an earlier incarnation of
  944. this program to add a random element to the way moves were made.")
  945. ;;;_ - lists of cardinal directions
  946. ;;;_ :
  947. (defvar landmark-ns '(landmark-n landmark-s)
  948. "Used when doing something relative to the north and south axes.")
  949. (defvar landmark-ew '(landmark-e landmark-w)
  950. "Used when doing something relative to the east and west axes.")
  951. (defvar landmark-directions '(landmark-n landmark-s landmark-e landmark-w)
  952. "The cardinal directions.")
  953. (defvar landmark-8-directions
  954. '((landmark-n) (landmark-n landmark-w) (landmark-w) (landmark-s landmark-w)
  955. (landmark-s) (landmark-s landmark-e) (landmark-e) (landmark-n landmark-e))
  956. "The full 8 possible directions.")
  957. (defvar landmark-number-of-moves
  958. "The number of moves made by the robot so far.")
  959. ;;;_* Terry's mods to create lm.el
  960. ;;;(setq landmark-debug nil)
  961. (defvar landmark-debug nil
  962. "If non-nil, debugging is printed.")
  963. (defcustom landmark-one-moment-please nil
  964. "If non-nil, print \"One moment please\" when a new board is generated.
  965. The drawback of this is you don't see how many moves the last run took
  966. because it is overwritten by \"One moment please\"."
  967. :type 'boolean
  968. :group 'landmark)
  969. (defcustom landmark-output-moves t
  970. "If non-nil, output number of moves so far on a move-by-move basis."
  971. :type 'boolean
  972. :group 'landmark)
  973. (defun landmark-weights-debug ()
  974. (if landmark-debug
  975. (progn (landmark-print-wts) (landmark-blackbox) (landmark-print-y-s-noise)
  976. (landmark-print-smell))))
  977. ;;;_ - Printing various things
  978. (defun landmark-print-distance-int (direction)
  979. (interactive)
  980. (insert (format "%S %S " direction (get direction 'distance))))
  981. (defun landmark-print-distance ()
  982. (insert (format "tree: %S \n" (calc-distance-of-robot-from 'landmark-tree)))
  983. (mapc 'landmark-print-distance-int landmark-directions))
  984. ;;(setq direction 'landmark-n)
  985. ;;(get 'landmark-n 'landmark-s)
  986. (defun landmark-nslify-wts-int (direction)
  987. (mapcar (lambda (target-direction)
  988. (get direction target-direction))
  989. landmark-directions))
  990. (defun landmark-nslify-wts ()
  991. (interactive)
  992. (let ((l (apply 'append (mapcar 'landmark-nslify-wts-int landmark-directions))))
  993. (insert (format "set data_value WTS \n %s \n" l))
  994. (insert (format "/* max: %S min: %S */"
  995. (eval (cons 'max l)) (eval (cons 'min l))))))
  996. (defun landmark-print-wts-int (direction)
  997. (mapc (lambda (target-direction)
  998. (insert (format "%S %S %S "
  999. direction
  1000. target-direction
  1001. (get direction target-direction))))
  1002. landmark-directions)
  1003. (insert "\n"))
  1004. (defun landmark-print-wts ()
  1005. (interactive)
  1006. (with-current-buffer "*landmark-wts*"
  1007. (insert "==============================\n")
  1008. (mapc 'landmark-print-wts-int landmark-directions)))
  1009. (defun landmark-print-moves (moves)
  1010. (interactive)
  1011. (with-current-buffer "*landmark-moves*"
  1012. (insert (format "%S\n" moves))))
  1013. (defun landmark-print-y-s-noise-int (direction)
  1014. (insert (format "%S:landmark-y %S, s %S, noise %S \n"
  1015. (symbol-name direction)
  1016. (get direction 'y_t)
  1017. (get direction 's)
  1018. (get direction 'noise)
  1019. )))
  1020. (defun landmark-print-y-s-noise ()
  1021. (interactive)
  1022. (with-current-buffer "*landmark-y,s,noise*"
  1023. (insert "==============================\n")
  1024. (mapc 'landmark-print-y-s-noise-int landmark-directions)))
  1025. (defun landmark-print-smell-int (direction)
  1026. (insert (format "%S: smell: %S \n"
  1027. (symbol-name direction)
  1028. (get direction 'smell))))
  1029. (defun landmark-print-smell ()
  1030. (interactive)
  1031. (with-current-buffer "*landmark-smell*"
  1032. (insert "==============================\n")
  1033. (insert (format "tree: %S \n" (get 'z 't)))
  1034. (mapc 'landmark-print-smell-int landmark-directions)))
  1035. (defun landmark-print-w0-int (direction)
  1036. (insert (format "%S: w0: %S \n"
  1037. (symbol-name direction)
  1038. (get direction 'w0))))
  1039. (defun landmark-print-w0 ()
  1040. (interactive)
  1041. (with-current-buffer "*landmark-w0*"
  1042. (insert "==============================\n")
  1043. (mapc 'landmark-print-w0-int landmark-directions)))
  1044. (defun landmark-blackbox ()
  1045. (with-current-buffer "*landmark-blackbox*"
  1046. (insert "==============================\n")
  1047. (insert "I smell: ")
  1048. (mapc (lambda (direction)
  1049. (if (> (get direction 'smell) 0)
  1050. (insert (format "%S " direction))))
  1051. landmark-directions)
  1052. (insert "\n")
  1053. (insert "I move: ")
  1054. (mapc (lambda (direction)
  1055. (if (> (get direction 'y_t) 0)
  1056. (insert (format "%S " direction))))
  1057. landmark-directions)
  1058. (insert "\n")
  1059. (landmark-print-wts-blackbox)
  1060. (insert (format "z_t-z_t-1: %S" (- (get 'z 't) (get 'z 't-1))))
  1061. (landmark-print-distance)
  1062. (insert "\n")))
  1063. (defun landmark-print-wts-blackbox ()
  1064. (interactive)
  1065. (mapc 'landmark-print-wts-int landmark-directions))
  1066. ;;;_ - learning parameters
  1067. (defcustom landmark-bound 0.005
  1068. "The maximum that w0j may be."
  1069. :type 'number
  1070. :group 'landmark)
  1071. (defcustom landmark-c 1.0
  1072. "A factor applied to modulate the increase in wij.
  1073. Used in the function landmark-update-normal-weights."
  1074. :type 'number
  1075. :group 'landmark)
  1076. (defcustom landmark-c-naught 0.5
  1077. "A factor applied to modulate the increase in w0j.
  1078. Used in the function landmark-update-naught-weights."
  1079. :type 'number
  1080. :group 'landmark)
  1081. (defvar landmark-initial-w0 0.0)
  1082. (defvar landmark-initial-wij 0.0)
  1083. (defcustom landmark-no-payoff 0
  1084. "The amount of simulation cycles that have occurred with no movement.
  1085. Used to move the robot when he is stuck in a rut for some reason."
  1086. :type 'integer
  1087. :group 'landmark)
  1088. (defcustom landmark-max-stall-time 2
  1089. "The maximum number of cycles that the robot can remain stuck in a place.
  1090. After this limit is reached, landmark-random-move is called to push him out of it."
  1091. :type 'integer
  1092. :group 'landmark)
  1093. ;;;_ + Randomizing functions
  1094. ;;;_ - landmark-flip-a-coin ()
  1095. (defun landmark-flip-a-coin ()
  1096. (if (> (random 5000) 2500)
  1097. -1
  1098. 1))
  1099. ;;;_ : landmark-very-small-random-number ()
  1100. ;(defun landmark-very-small-random-number ()
  1101. ; (/
  1102. ; (* (/ (random 900000) 900000.0) .0001)))
  1103. ;;;_ : landmark-randomize-weights-for (direction)
  1104. (defun landmark-randomize-weights-for (direction)
  1105. (mapc (lambda (target-direction)
  1106. (put direction
  1107. target-direction
  1108. (* (landmark-flip-a-coin) (/ (random 10000) 10000.0))))
  1109. landmark-directions))
  1110. ;;;_ : landmark-noise ()
  1111. (defun landmark-noise ()
  1112. (* (- (/ (random 30001) 15000.0) 1) landmark-nvar))
  1113. ;;;_ : landmark-fix-weights-for (direction)
  1114. (defun landmark-fix-weights-for (direction)
  1115. (mapc (lambda (target-direction)
  1116. (put direction
  1117. target-direction
  1118. landmark-initial-wij))
  1119. landmark-directions))
  1120. ;;;_ + Plotting functions
  1121. ;;;_ - landmark-plot-internal (sym)
  1122. (defun landmark-plot-internal (sym)
  1123. (landmark-plot-square (landmark-xy-to-index
  1124. (get sym 'x)
  1125. (get sym 'y))
  1126. (get sym 'sym)))
  1127. ;;;_ - landmark-plot-landmarks ()
  1128. (defun landmark-plot-landmarks ()
  1129. (setq landmark-cx (/ landmark-board-width 2))
  1130. (setq landmark-cy (/ landmark-board-height 2))
  1131. (put 'landmark-n 'x landmark-cx)
  1132. (put 'landmark-n 'y 1)
  1133. (put 'landmark-n 'sym 2)
  1134. (put 'landmark-tree 'x landmark-cx)
  1135. (put 'landmark-tree 'y landmark-cy)
  1136. (put 'landmark-tree 'sym 6)
  1137. (put 'landmark-s 'x landmark-cx)
  1138. (put 'landmark-s 'y landmark-board-height)
  1139. (put 'landmark-s 'sym 3)
  1140. (put 'landmark-w 'x 1)
  1141. (put 'landmark-w 'y (/ landmark-board-height 2))
  1142. (put 'landmark-w 'sym 5)
  1143. (put 'landmark-e 'x landmark-board-width)
  1144. (put 'landmark-e 'y (/ landmark-board-height 2))
  1145. (put 'landmark-e 'sym 4)
  1146. (mapc 'landmark-plot-internal '(landmark-n landmark-s landmark-e landmark-w landmark-tree)))
  1147. ;;;_ + Distance-calculation functions
  1148. ;;;_ - square (a)
  1149. (defun square (a)
  1150. (* a a))
  1151. ;;;_ - distance (x x0 y y0)
  1152. (defun distance (x x0 y y0)
  1153. (sqrt (+ (square (- x x0)) (square (- y y0)))))
  1154. ;;;_ - calc-distance-of-robot-from (direction)
  1155. (defun calc-distance-of-robot-from (direction)
  1156. (put direction 'distance
  1157. (distance (get direction 'x)
  1158. (landmark-index-to-x (landmark-point-square))
  1159. (get direction 'y)
  1160. (landmark-index-to-y (landmark-point-square)))))
  1161. ;;;_ - calc-smell-internal (sym)
  1162. (defun calc-smell-internal (sym)
  1163. (let ((r (get sym 'r))
  1164. (d (calc-distance-of-robot-from sym)))
  1165. (if (> (* 0.5 (- 1 (/ d r))) 0)
  1166. (* 0.5 (- 1 (/ d r)))
  1167. 0)))
  1168. ;;;_ + Learning (neural) functions
  1169. (defun landmark-f (x)
  1170. (cond
  1171. ((> x landmark-bound) landmark-bound)
  1172. ((< x 0.0) 0.0)
  1173. (t x)))
  1174. (defun landmark-y (direction)
  1175. (put direction 'noise (landmark-noise))
  1176. (put direction 'y_t
  1177. (if (> (get direction 's) 0.0)
  1178. 1.0
  1179. 0.0)))
  1180. (defun landmark-update-normal-weights (direction)
  1181. (mapc (lambda (target-direction)
  1182. (put direction target-direction
  1183. (+
  1184. (get direction target-direction)
  1185. (* landmark-c
  1186. (- (get 'z 't) (get 'z 't-1))
  1187. (get target-direction 'y_t)
  1188. (get direction 'smell)))))
  1189. landmark-directions))
  1190. (defun landmark-update-naught-weights (direction)
  1191. (mapc (lambda (_target-direction)
  1192. (put direction 'w0
  1193. (landmark-f
  1194. (+
  1195. (get direction 'w0)
  1196. (* landmark-c-naught
  1197. (- (get 'z 't) (get 'z 't-1))
  1198. (get direction 'y_t))))))
  1199. landmark-directions))
  1200. ;;;_ + Statistics gathering and creating functions
  1201. (defun landmark-calc-current-smells ()
  1202. (mapc (lambda (direction)
  1203. (put direction 'smell (calc-smell-internal direction)))
  1204. landmark-directions))
  1205. (defun landmark-calc-payoff ()
  1206. (put 'z 't-1 (get 'z 't))
  1207. (put 'z 't (calc-smell-internal 'landmark-tree))
  1208. (if (= (- (get 'z 't) (get 'z 't-1)) 0.0)
  1209. (incf landmark-no-payoff)
  1210. (setf landmark-no-payoff 0)))
  1211. (defun landmark-store-old-y_t ()
  1212. (mapc (lambda (direction)
  1213. (put direction 'y_t-1 (get direction 'y_t)))
  1214. landmark-directions))
  1215. ;;;_ + Functions to move robot
  1216. (defun landmark-confidence-for (target-direction)
  1217. (apply '+
  1218. (get target-direction 'w0)
  1219. (mapcar (lambda (direction)
  1220. (*
  1221. (get direction target-direction)
  1222. (get direction 'smell)))
  1223. landmark-directions)))
  1224. (defun landmark-calc-confidences ()
  1225. (mapc (lambda (direction)
  1226. (put direction 's (landmark-confidence-for direction)))
  1227. landmark-directions))
  1228. (defun landmark-move ()
  1229. (if (and (= (get 'landmark-n 'y_t) 1.0) (= (get 'landmark-s 'y_t) 1.0))
  1230. (progn
  1231. (mapc (lambda (dir) (put dir 'y_t 0)) landmark-ns)
  1232. (if landmark-debug
  1233. (message "n-s normalization."))))
  1234. (if (and (= (get 'landmark-w 'y_t) 1.0) (= (get 'landmark-e 'y_t) 1.0))
  1235. (progn
  1236. (mapc (lambda (dir) (put dir 'y_t 0)) landmark-ew)
  1237. (if landmark-debug
  1238. (message "e-w normalization"))))
  1239. (mapc (lambda (pair)
  1240. (if (> (get (car pair) 'y_t) 0)
  1241. (funcall (car (cdr pair)))))
  1242. '(
  1243. (landmark-n landmark-move-up)
  1244. (landmark-s landmark-move-down)
  1245. (landmark-e forward-char)
  1246. (landmark-w backward-char)))
  1247. (landmark-plot-square (landmark-point-square) 1)
  1248. (incf landmark-number-of-moves)
  1249. (if landmark-output-moves
  1250. (message "Moves made: %d" landmark-number-of-moves)))
  1251. (defun landmark-random-move ()
  1252. (mapc
  1253. (lambda (direction) (put direction 'y_t 0))
  1254. landmark-directions)
  1255. (dolist (direction (nth (random 8) landmark-8-directions))
  1256. (put direction 'y_t 1.0))
  1257. (landmark-move))
  1258. (defun landmark-amble-robot ()
  1259. (interactive)
  1260. (while (> (calc-distance-of-robot-from 'landmark-tree) 0)
  1261. (landmark-store-old-y_t)
  1262. (landmark-calc-current-smells)
  1263. (if (> landmark-no-payoff landmark-max-stall-time)
  1264. (landmark-random-move)
  1265. (progn
  1266. (landmark-calc-confidences)
  1267. (mapc 'landmark-y landmark-directions)
  1268. (landmark-move)))
  1269. (landmark-calc-payoff)
  1270. (mapc 'landmark-update-normal-weights landmark-directions)
  1271. (mapc 'landmark-update-naught-weights landmark-directions)
  1272. (if landmark-debug
  1273. (landmark-weights-debug)))
  1274. (landmark-terminate-game nil))
  1275. ;;;_ - landmark-start-robot ()
  1276. (defun landmark-start-robot ()
  1277. "Signal to the Landmark program that you have played.
  1278. You must have put the cursor on the square where you want to play.
  1279. If the game is finished, this command requests for another game."
  1280. (interactive)
  1281. (landmark-switch-to-window)
  1282. (cond
  1283. (landmark-emacs-is-computing
  1284. (landmark-crash-game))
  1285. ((not landmark-game-in-progress)
  1286. (landmark-prompt-for-other-game))
  1287. (t
  1288. (let (square)
  1289. (setq square (landmark-point-square))
  1290. (cond ((null square)
  1291. (error "Your point is not on a square. Retry!"))
  1292. ((not (zerop (aref landmark-board square)))
  1293. (error "Your point is not on a free square. Retry!"))
  1294. (t
  1295. (progn
  1296. (landmark-plot-square square 1)
  1297. (landmark-store-old-y_t)
  1298. (landmark-calc-current-smells)
  1299. (put 'z 't (calc-smell-internal 'landmark-tree))
  1300. (landmark-random-move)
  1301. (landmark-calc-payoff)
  1302. (mapc 'landmark-update-normal-weights landmark-directions)
  1303. (mapc 'landmark-update-naught-weights landmark-directions)
  1304. (landmark-amble-robot)
  1305. )))))))
  1306. ;;;_ + Misc functions
  1307. ;;;_ - landmark-init (auto-start save-weights)
  1308. (defvar landmark-tree-r "")
  1309. (defun landmark-init (auto-start save-weights)
  1310. (setq landmark-number-of-moves 0)
  1311. (landmark-plot-landmarks)
  1312. (if landmark-debug
  1313. (save-current-buffer
  1314. (set-buffer (get-buffer-create "*landmark-w0*"))
  1315. (erase-buffer)
  1316. (set-buffer (get-buffer-create "*landmark-moves*"))
  1317. (set-buffer (get-buffer-create "*landmark-wts*"))
  1318. (erase-buffer)
  1319. (set-buffer (get-buffer-create "*landmark-y,s,noise*"))
  1320. (erase-buffer)
  1321. (set-buffer (get-buffer-create "*landmark-smell*"))
  1322. (erase-buffer)
  1323. (set-buffer (get-buffer-create "*landmark-blackbox*"))
  1324. (erase-buffer)
  1325. (set-buffer (get-buffer-create "*landmark-distance*"))
  1326. (erase-buffer)))
  1327. (landmark-set-landmark-signal-strengths)
  1328. (dolist (direction landmark-directions)
  1329. (put direction 'y_t 0.0))
  1330. (if (not save-weights)
  1331. (progn
  1332. (mapc 'landmark-fix-weights-for landmark-directions)
  1333. (dolist (direction landmark-directions)
  1334. (put direction 'w0 landmark-initial-w0)))
  1335. (message "Weights preserved for this run."))
  1336. (if auto-start
  1337. (progn
  1338. (landmark-goto-xy (1+ (random landmark-board-width)) (1+ (random landmark-board-height)))
  1339. (landmark-start-robot))))
  1340. ;;;_ - something which doesn't work
  1341. ; no-a-worka!!
  1342. ;(defun landmark-sum-list (list)
  1343. ; (if (> (length list) 0)
  1344. ; (+ (car list) (landmark-sum-list (cdr list)))
  1345. ; 0))
  1346. ; this a worka!
  1347. ; (eval (cons '+ list))
  1348. ;;;_ - landmark-set-landmark-signal-strengths ()
  1349. ;;; on a screen higher than wide, I noticed that the robot would amble
  1350. ;;; left and right and not move forward. examining *landmark-blackbox*
  1351. ;;; revealed that there was no scent from the north and south
  1352. ;;; landmarks, hence, they need less factoring down of the effect of
  1353. ;;; distance on scent.
  1354. (defun landmark-set-landmark-signal-strengths ()
  1355. (setq landmark-tree-r (* (sqrt (+ (square landmark-cx) (square landmark-cy))) 1.5))
  1356. (mapc (lambda (direction)
  1357. (put direction 'r (* landmark-cx 1.1)))
  1358. landmark-ew)
  1359. (mapc (lambda (direction)
  1360. (put direction 'r (* landmark-cy 1.1)))
  1361. landmark-ns)
  1362. (put 'landmark-tree 'r landmark-tree-r))
  1363. ;;;_ + landmark-test-run ()
  1364. ;;;###autoload
  1365. (defalias 'landmark-repeat 'landmark-test-run)
  1366. ;;;###autoload
  1367. (defun landmark-test-run ()
  1368. "Run 100 Landmark games, each time saving the weights from the previous game."
  1369. (interactive)
  1370. (landmark 1)
  1371. (dotimes (scratch-var 100)
  1372. (landmark 2)))
  1373. ;;;###autoload
  1374. (defun landmark (parg)
  1375. "Start or resume an Landmark game.
  1376. If a game is in progress, this command allows you to resume it.
  1377. Here is the relation between prefix args and game options:
  1378. prefix arg | robot is auto-started | weights are saved from last game
  1379. ---------------------------------------------------------------------
  1380. none / 1 | yes | no
  1381. 2 | yes | yes
  1382. 3 | no | yes
  1383. 4 | no | no
  1384. You start by moving to a square and typing \\[landmark-start-robot],
  1385. if you did not use a prefix arg to ask for automatic start.
  1386. Use \\[describe-mode] for more info."
  1387. (interactive "p")
  1388. (setf landmark-n nil landmark-m nil)
  1389. (landmark-switch-to-window)
  1390. (cond
  1391. (landmark-emacs-is-computing
  1392. (landmark-crash-game))
  1393. ((or (not landmark-game-in-progress)
  1394. (<= landmark-number-of-moves 2))
  1395. (let ((max-width (landmark-max-width))
  1396. (max-height (landmark-max-height)))
  1397. (or landmark-n (setq landmark-n max-width))
  1398. (or landmark-m (setq landmark-m max-height))
  1399. (cond ((< landmark-n 1)
  1400. (error "I need at least 1 column"))
  1401. ((< landmark-m 1)
  1402. (error "I need at least 1 row"))
  1403. ((> landmark-n max-width)
  1404. (error "I cannot display %d columns in that window" landmark-n)))
  1405. (if (and (> landmark-m max-height)
  1406. (not (eq landmark-m landmark-saved-board-height))
  1407. ;; Use EQ because SAVED-BOARD-HEIGHT may be nil
  1408. (not (y-or-n-p (format "Do you really want %d rows? " landmark-m))))
  1409. (setq landmark-m max-height)))
  1410. (if landmark-one-moment-please
  1411. (message "One moment, please..."))
  1412. (landmark-start-game landmark-n landmark-m)
  1413. (eval (cons 'landmark-init
  1414. (cond
  1415. ((= parg 1) '(t nil))
  1416. ((= parg 2) '(t t))
  1417. ((= parg 3) '(nil t))
  1418. ((= parg 4) '(nil nil))
  1419. (t '(nil t))))))))
  1420. ;;;_ + Local variables
  1421. ;;; The following `allout-layout' local variable setting:
  1422. ;;; - closes all topics from the first topic to just before the third-to-last,
  1423. ;;; - shows the children of the third to last (config vars)
  1424. ;;; - and the second to last (code section),
  1425. ;;; - and closes the last topic (this local-variables section).
  1426. ;;;Local variables:
  1427. ;;;allout-layout: (0 : -1 -1 0)
  1428. ;;;End:
  1429. (random t)
  1430. (provide 'landmark)
  1431. ;;; landmark.el ends here