More CL cleanups and reduction of use of cl.el.
[emacs.git] / lisp / play / tetris.el
blobb811a21605b4f0f4c259b1d26c773ea1788046fd
1 ;;; tetris.el --- implementation of Tetris for Emacs
3 ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
6 ;; Version: 2.01
7 ;; Created: 1997-08-13
8 ;; Keywords: games
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl-lib))
31 (require 'gamegrid)
33 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (defgroup tetris nil
36 "Play a game of Tetris."
37 :prefix "tetris-"
38 :group 'games)
40 (defcustom tetris-use-glyphs t
41 "Non-nil means use glyphs when available."
42 :group 'tetris
43 :type 'boolean)
45 (defcustom tetris-use-color t
46 "Non-nil means use color when available."
47 :group 'tetris
48 :type 'boolean)
50 (defcustom tetris-draw-border-with-glyphs t
51 "Non-nil means draw a border even when using glyphs."
52 :group 'tetris
53 :type 'boolean)
55 (defcustom tetris-default-tick-period 0.3
56 "The default time taken for a shape to drop one row."
57 :group 'tetris
58 :type 'number)
60 (defcustom tetris-update-speed-function
61 'tetris-default-update-speed-function
62 "Function run whenever the Tetris score changes.
63 Called with two arguments: (SHAPES ROWS)
64 SHAPES is the number of shapes which have been dropped.
65 ROWS is the number of rows which have been completed.
67 If the return value is a number, it is used as the timer period."
68 :group 'tetris
69 :type 'function)
71 (defcustom tetris-mode-hook nil
72 "Hook run upon starting Tetris."
73 :group 'tetris
74 :type 'hook)
76 (defcustom tetris-tty-colors
77 ["blue" "white" "yellow" "magenta" "cyan" "green" "red"]
78 "Vector of colors of the various shapes in text mode."
79 :group 'tetris
80 :type (let ((names `("Shape 1" "Shape 2" "Shape 3"
81 "Shape 4" "Shape 5" "Shape 6" "Shape 7"))
82 (result nil))
83 (while names
84 (add-to-list 'result
85 (cons 'choice
86 (cons :tag
87 (cons (car names)
88 (mapcar (lambda (color)
89 (list 'const color))
90 (defined-colors)))))
92 (setq names (cdr names)))
93 result))
95 (defcustom tetris-x-colors
96 [[0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
97 "Vector of colors of the various shapes."
98 :group 'tetris
99 :type 'sexp)
101 (defcustom tetris-buffer-name "*Tetris*"
102 "Name used for Tetris buffer."
103 :group 'tetris
104 :type 'string)
106 (defcustom tetris-buffer-width 30
107 "Width of used portion of buffer."
108 :group 'tetris
109 :type 'number)
111 (defcustom tetris-buffer-height 22
112 "Height of used portion of buffer."
113 :group 'tetris
114 :type 'number)
116 (defcustom tetris-width 10
117 "Width of playing area."
118 :group 'tetris
119 :type 'number)
121 (defcustom tetris-height 20
122 "Height of playing area."
123 :group 'tetris
124 :type 'number)
126 (defcustom tetris-top-left-x 3
127 "X position of top left of playing area."
128 :group 'tetris
129 :type 'number)
131 (defcustom tetris-top-left-y 1
132 "Y position of top left of playing area."
133 :group 'tetris
134 :type 'number)
136 (defvar tetris-next-x (+ (* 2 tetris-top-left-x) tetris-width)
137 "X position of next shape.")
139 (defvar tetris-next-y tetris-top-left-y
140 "Y position of next shape.")
142 (defvar tetris-score-x tetris-next-x
143 "X position of score.")
145 (defvar tetris-score-y (+ tetris-next-y 6)
146 "Y position of score.")
148 ;; It is not safe to put this in /tmp.
149 ;; Someone could make a symlink in /tmp
150 ;; pointing to a file you don't want to clobber.
151 (defvar tetris-score-file "tetris-scores"
152 ;; anybody with a well-connected server want to host this?
153 ;(defvar tetris-score-file "/anonymous@ftp.pgt.com:/pub/cgw/tetris-scores"
154 "File for holding high scores.")
156 ;; ;;;;;;;;;;;;; display options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 (defvar tetris-blank-options
159 '(((glyph colorize)
160 (t ?\040))
161 ((color-x color-x)
162 (mono-x grid-x)
163 (color-tty color-tty))
164 (((glyph color-x) [0 0 0])
165 (color-tty "black"))))
167 (defvar tetris-cell-options
168 '(((glyph colorize)
169 (emacs-tty ?O)
170 (t ?\040))
171 ((color-x color-x)
172 (mono-x mono-x)
173 (color-tty color-tty)
174 (mono-tty mono-tty))
175 ;; color information is taken from tetris-x-colors and tetris-tty-colors
178 (defvar tetris-border-options
179 '(((glyph colorize)
180 (t ?\+))
181 ((color-x color-x)
182 (mono-x grid-x)
183 (color-tty color-tty))
184 (((glyph color-x) [0.5 0.5 0.5])
185 (color-tty "white"))))
187 (defvar tetris-space-options
188 '(((t ?\040))
190 nil))
192 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 (defconst tetris-shapes
195 [[[[0 0] [1 0] [0 1] [1 1]]]
197 [[[0 0] [1 0] [2 0] [2 1]]
198 [[1 -1] [1 0] [1 1] [0 1]]
199 [[0 -1] [0 0] [1 0] [2 0]]
200 [[1 -1] [2 -1] [1 0] [1 1]]]
202 [[[0 0] [1 0] [2 0] [0 1]]
203 [[0 -1] [1 -1] [1 0] [1 1]]
204 [[2 -1] [0 0] [1 0] [2 0]]
205 [[1 -1] [1 0] [1 1] [2 1]]]
207 [[[0 0] [1 0] [1 1] [2 1]]
208 [[1 0] [0 1] [1 1] [0 2]]]
210 [[[1 0] [2 0] [0 1] [1 1]]
211 [[0 0] [0 1] [1 1] [1 2]]]
213 [[[1 0] [0 1] [1 1] [2 1]]
214 [[1 0] [1 1] [2 1] [1 2]]
215 [[0 1] [1 1] [2 1] [1 2]]
216 [[1 0] [0 1] [1 1] [1 2]]]
218 [[[0 0] [1 0] [2 0] [3 0]]
219 [[1 -1] [1 0] [1 1] [1 2]]]]
220 "Each shape is described by a vector that contains the coordinates of
221 each one of its four blocks.")
223 ;;the scoring rules were taken from "xtetris". Blocks score differently
224 ;;depending on their rotation
226 (defconst tetris-shape-scores
227 [[6] [6 7 6 7] [6 7 6 7] [6 7] [6 7] [5 5 6 5] [5 8]] )
229 (defconst tetris-shape-dimensions
230 [[2 2] [3 2] [3 2] [3 2] [3 2] [3 2] [4 1]])
232 (defconst tetris-blank 7)
234 (defconst tetris-border 8)
236 (defconst tetris-space 9)
238 (defun tetris-default-update-speed-function (_shapes rows)
239 (/ 20.0 (+ 50.0 rows)))
241 ;; ;;;;;;;;;;;;; variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 (defvar tetris-shape 0)
244 (defvar tetris-rot 0)
245 (defvar tetris-next-shape 0)
246 (defvar tetris-n-shapes 0)
247 (defvar tetris-n-rows 0)
248 (defvar tetris-score 0)
249 (defvar tetris-pos-x 0)
250 (defvar tetris-pos-y 0)
251 (defvar tetris-paused nil)
253 (make-variable-buffer-local 'tetris-shape)
254 (make-variable-buffer-local 'tetris-rot)
255 (make-variable-buffer-local 'tetris-next-shape)
256 (make-variable-buffer-local 'tetris-n-shapes)
257 (make-variable-buffer-local 'tetris-n-rows)
258 (make-variable-buffer-local 'tetris-score)
259 (make-variable-buffer-local 'tetris-pos-x)
260 (make-variable-buffer-local 'tetris-pos-y)
261 (make-variable-buffer-local 'tetris-paused)
263 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 (defvar tetris-mode-map
266 (let ((map (make-sparse-keymap 'tetris-mode-map)))
267 (define-key map "n" 'tetris-start-game)
268 (define-key map "q" 'tetris-end-game)
269 (define-key map "p" 'tetris-pause-game)
271 (define-key map " " 'tetris-move-bottom)
272 (define-key map [left] 'tetris-move-left)
273 (define-key map [right] 'tetris-move-right)
274 (define-key map [up] 'tetris-rotate-prev)
275 (define-key map [down] 'tetris-rotate-next)
276 map))
278 (defvar tetris-null-map
279 (let ((map (make-sparse-keymap 'tetris-null-map)))
280 (define-key map "n" 'tetris-start-game)
281 map))
283 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 (defun tetris-display-options ()
286 (let ((options (make-vector 256 nil)))
287 (dotimes (c 256)
288 (aset options c
289 (cond ((= c tetris-blank)
290 tetris-blank-options)
291 ((and (>= c 0) (<= c 6))
292 (append
293 tetris-cell-options
294 `((((glyph color-x) ,(aref tetris-x-colors c))
295 (color-tty ,(aref tetris-tty-colors c))
296 (t nil)))))
297 ((= c tetris-border)
298 tetris-border-options)
299 ((= c tetris-space)
300 tetris-space-options)
302 '(nil nil nil)))))
303 options))
305 (defun tetris-get-tick-period ()
306 (if (boundp 'tetris-update-speed-function)
307 (let ((period (apply tetris-update-speed-function
308 tetris-n-shapes
309 tetris-n-rows nil)))
310 (and (numberp period) period))))
312 (defun tetris-get-shape-cell (block)
313 (aref (aref (aref tetris-shapes
314 tetris-shape) tetris-rot)
315 block))
317 (defun tetris-shape-width ()
318 (aref (aref tetris-shape-dimensions tetris-shape) 0))
320 (defun tetris-shape-rotations ()
321 (length (aref tetris-shapes tetris-shape)))
323 (defun tetris-draw-score ()
324 (let ((strings (vector (format "Shapes: %05d" tetris-n-shapes)
325 (format "Rows: %05d" tetris-n-rows)
326 (format "Score: %05d" tetris-score))))
327 (dotimes (y 3)
328 (let* ((string (aref strings y))
329 (len (length string)))
330 (dotimes (x len)
331 (gamegrid-set-cell (+ tetris-score-x x)
332 (+ tetris-score-y y)
333 (aref string x)))))))
335 (defun tetris-update-score ()
336 (tetris-draw-score)
337 (let ((period (tetris-get-tick-period)))
338 (if period (gamegrid-set-timer period))))
340 (defun tetris-new-shape ()
341 (setq tetris-shape tetris-next-shape)
342 (setq tetris-rot 0)
343 (setq tetris-next-shape (random 7))
344 (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2))
345 (setq tetris-pos-y 0)
346 (if (tetris-test-shape)
347 (tetris-end-game)
348 (tetris-draw-shape)
349 (tetris-draw-next-shape)
350 (tetris-update-score)))
352 (defun tetris-draw-next-shape ()
353 (dotimes (x 4)
354 (dotimes (y 4)
355 (gamegrid-set-cell (+ tetris-next-x x)
356 (+ tetris-next-y y)
357 tetris-blank)))
358 (dotimes (i 4)
359 (let ((tetris-shape tetris-next-shape)
360 (tetris-rot 0))
361 (gamegrid-set-cell (+ tetris-next-x
362 (aref (tetris-get-shape-cell i) 0))
363 (+ tetris-next-y
364 (aref (tetris-get-shape-cell i) 1))
365 tetris-shape))))
367 (defun tetris-draw-shape ()
368 (dotimes (i 4)
369 (let ((c (tetris-get-shape-cell i)))
370 (gamegrid-set-cell (+ tetris-top-left-x
371 tetris-pos-x
372 (aref c 0))
373 (+ tetris-top-left-y
374 tetris-pos-y
375 (aref c 1))
376 tetris-shape))))
378 (defun tetris-erase-shape ()
379 (dotimes (i 4)
380 (let ((c (tetris-get-shape-cell i)))
381 (gamegrid-set-cell (+ tetris-top-left-x
382 tetris-pos-x
383 (aref c 0))
384 (+ tetris-top-left-y
385 tetris-pos-y
386 (aref c 1))
387 tetris-blank))))
389 (defun tetris-test-shape ()
390 (let ((hit nil))
391 (dotimes (i 4)
392 (unless hit
393 (setq hit
394 (let* ((c (tetris-get-shape-cell i))
395 (xx (+ tetris-pos-x
396 (aref c 0)))
397 (yy (+ tetris-pos-y
398 (aref c 1))))
399 (or (>= xx tetris-width)
400 (>= yy tetris-height)
401 (/= (gamegrid-get-cell
402 (+ xx tetris-top-left-x)
403 (+ yy tetris-top-left-y))
404 tetris-blank))))))
405 hit))
407 (defun tetris-full-row (y)
408 (let ((full t))
409 (dotimes (x tetris-width)
410 (if (= (gamegrid-get-cell (+ tetris-top-left-x x)
411 (+ tetris-top-left-y y))
412 tetris-blank)
413 (setq full nil)))
414 full))
416 (defun tetris-shift-row (y)
417 (if (= y 0)
418 (dotimes (x tetris-width)
419 (gamegrid-set-cell (+ tetris-top-left-x x)
420 (+ tetris-top-left-y y)
421 tetris-blank))
422 (dotimes (x tetris-width)
423 (let ((c (gamegrid-get-cell (+ tetris-top-left-x x)
424 (+ tetris-top-left-y y -1))))
425 (gamegrid-set-cell (+ tetris-top-left-x x)
426 (+ tetris-top-left-y y)
427 c)))))
429 (defun tetris-shift-down ()
430 (dotimes (y0 tetris-height)
431 (when (tetris-full-row y0)
432 (setq tetris-n-rows (1+ tetris-n-rows))
433 (cl-loop for y from y0 downto 0 do
434 (tetris-shift-row y)))))
436 (defun tetris-draw-border-p ()
437 (or (not (eq gamegrid-display-mode 'glyph))
438 tetris-draw-border-with-glyphs))
440 (defun tetris-init-buffer ()
441 (gamegrid-init-buffer tetris-buffer-width
442 tetris-buffer-height
443 tetris-space)
444 (let ((buffer-read-only nil))
445 (if (tetris-draw-border-p)
446 (cl-loop for y from -1 to tetris-height do
447 (cl-loop for x from -1 to tetris-width do
448 (gamegrid-set-cell (+ tetris-top-left-x x)
449 (+ tetris-top-left-y y)
450 tetris-border))))
451 (dotimes (y tetris-height)
452 (dotimes (x tetris-width)
453 (gamegrid-set-cell (+ tetris-top-left-x x)
454 (+ tetris-top-left-y y)
455 tetris-blank)))
456 (if (tetris-draw-border-p)
457 (cl-loop for y from -1 to 4 do
458 (cl-loop for x from -1 to 4 do
459 (gamegrid-set-cell (+ tetris-next-x x)
460 (+ tetris-next-y y)
461 tetris-border))))))
463 (defun tetris-reset-game ()
464 (gamegrid-kill-timer)
465 (tetris-init-buffer)
466 (setq tetris-next-shape (random 7))
467 (setq tetris-shape 0
468 tetris-rot 0
469 tetris-pos-x 0
470 tetris-pos-y 0
471 tetris-n-shapes 0
472 tetris-n-rows 0
473 tetris-score 0
474 tetris-paused nil)
475 (tetris-new-shape))
477 (defun tetris-shape-done ()
478 (tetris-shift-down)
479 (setq tetris-n-shapes (1+ tetris-n-shapes))
480 (setq tetris-score
481 (+ tetris-score
482 (aref (aref tetris-shape-scores tetris-shape) tetris-rot)))
483 (tetris-update-score)
484 (tetris-new-shape))
486 (defun tetris-update-game (tetris-buffer)
487 "Called on each clock tick.
488 Drops the shape one square, testing for collision."
489 (if (and (not tetris-paused)
490 (eq (current-buffer) tetris-buffer))
491 (let (hit)
492 (tetris-erase-shape)
493 (setq tetris-pos-y (1+ tetris-pos-y))
494 (setq hit (tetris-test-shape))
495 (if hit
496 (setq tetris-pos-y (1- tetris-pos-y)))
497 (tetris-draw-shape)
498 (if hit
499 (tetris-shape-done)))))
501 (defun tetris-move-bottom ()
502 "Drop the shape to the bottom of the playing area."
503 (interactive)
504 (unless tetris-paused
505 (let ((hit nil))
506 (tetris-erase-shape)
507 (while (not hit)
508 (setq tetris-pos-y (1+ tetris-pos-y))
509 (setq hit (tetris-test-shape)))
510 (setq tetris-pos-y (1- tetris-pos-y))
511 (tetris-draw-shape)
512 (tetris-shape-done))))
514 (defun tetris-move-left ()
515 "Move the shape one square to the left."
516 (interactive)
517 (unless tetris-paused
518 (tetris-erase-shape)
519 (setq tetris-pos-x (1- tetris-pos-x))
520 (if (tetris-test-shape)
521 (setq tetris-pos-x (1+ tetris-pos-x)))
522 (tetris-draw-shape)))
524 (defun tetris-move-right ()
525 "Move the shape one square to the right."
526 (interactive)
527 (unless tetris-paused
528 (tetris-erase-shape)
529 (setq tetris-pos-x (1+ tetris-pos-x))
530 (if (tetris-test-shape)
531 (setq tetris-pos-x (1- tetris-pos-x)))
532 (tetris-draw-shape)))
534 (defun tetris-rotate-prev ()
535 "Rotate the shape clockwise."
536 (interactive)
537 (unless tetris-paused
538 (tetris-erase-shape)
539 (setq tetris-rot (% (+ 1 tetris-rot)
540 (tetris-shape-rotations)))
541 (if (tetris-test-shape)
542 (setq tetris-rot (% (+ 3 tetris-rot)
543 (tetris-shape-rotations))))
544 (tetris-draw-shape)))
546 (defun tetris-rotate-next ()
547 "Rotate the shape anticlockwise."
548 (interactive)
549 (unless tetris-paused
550 (tetris-erase-shape)
551 (setq tetris-rot (% (+ 3 tetris-rot)
552 (tetris-shape-rotations)))
553 (if (tetris-test-shape)
554 (setq tetris-rot (% (+ 1 tetris-rot)
555 (tetris-shape-rotations))))
556 (tetris-draw-shape)))
558 (defun tetris-end-game ()
559 "Terminate the current game."
560 (interactive)
561 (gamegrid-kill-timer)
562 (use-local-map tetris-null-map)
563 (gamegrid-add-score tetris-score-file tetris-score))
565 (defun tetris-start-game ()
566 "Start a new game of Tetris."
567 (interactive)
568 (tetris-reset-game)
569 (use-local-map tetris-mode-map)
570 (let ((period (or (tetris-get-tick-period)
571 tetris-default-tick-period)))
572 (gamegrid-start-timer period 'tetris-update-game)))
574 (defun tetris-pause-game ()
575 "Pause (or resume) the current game."
576 (interactive)
577 (setq tetris-paused (not tetris-paused))
578 (message (and tetris-paused "Game paused (press p to resume)")))
580 (defun tetris-active-p ()
581 (eq (current-local-map) tetris-mode-map))
583 (put 'tetris-mode 'mode-class 'special)
585 (define-derived-mode tetris-mode nil "Tetris"
586 "A mode for playing Tetris."
588 (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
590 (use-local-map tetris-null-map)
592 (unless (featurep 'emacs)
593 (setq mode-popup-menu
594 '("Tetris Commands"
595 ["Start new game" tetris-start-game]
596 ["End game" tetris-end-game
597 (tetris-active-p)]
598 ["Pause" tetris-pause-game
599 (and (tetris-active-p) (not tetris-paused))]
600 ["Resume" tetris-pause-game
601 (and (tetris-active-p) tetris-paused)])))
603 (setq show-trailing-whitespace nil)
605 (setq gamegrid-use-glyphs tetris-use-glyphs)
606 (setq gamegrid-use-color tetris-use-color)
608 (gamegrid-init (tetris-display-options)))
610 ;;;###autoload
611 (defun tetris ()
612 "Play the Tetris game.
613 Shapes drop from the top of the screen, and the user has to move and
614 rotate the shape to fit in with those at the bottom of the screen so
615 as to form complete rows.
617 tetris-mode keybindings:
618 \\<tetris-mode-map>
619 \\[tetris-start-game] Starts a new game of Tetris
620 \\[tetris-end-game] Terminates the current game
621 \\[tetris-pause-game] Pauses (or resumes) the current game
622 \\[tetris-move-left] Moves the shape one square to the left
623 \\[tetris-move-right] Moves the shape one square to the right
624 \\[tetris-rotate-prev] Rotates the shape clockwise
625 \\[tetris-rotate-next] Rotates the shape anticlockwise
626 \\[tetris-move-bottom] Drops the shape to the bottom of the playing area
629 (interactive)
631 (select-window (or (get-buffer-window tetris-buffer-name)
632 (selected-window)))
633 (switch-to-buffer tetris-buffer-name)
634 (gamegrid-kill-timer)
635 (tetris-mode)
636 (tetris-start-game))
638 (random t)
640 (provide 'tetris)
642 ;;; tetris.el ends here