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