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