| C-x C-f | ファイルを開く |
| C-x C-s | ファイルを保存する |
| C-x b | バッファを切り替える |
| C-f, C-b, C-n, C-p: | カーソルを移動する |
| C-w | カット |
| M-w | コピー |
| C-y | ペースト |
| C-d | 一文字削除 |
| C-x C-e | カーソルの前の S 式を実行する |
| M-: | S 式を評価する |
| M-x COMMAND | コマンドを実行 |
| C-x k | バッファを削除する |
123 ; => 123 -123 ; => 123
(+ 1 2) ; => 3 (- 2 1) ; => 1 (* 2 3) ; => 6 (/ 6 2) ; => 3
hello ; => hello の値 foo-bar ; => foo-bar の値
; x というシンボルに 100 をいれる (setq x 100) ; => 100 x ; => 100
(set 'x 200) ; => 200 (setq xref 'y) ; => y (set xref 123) ; => 123 y ; => 123
(symbol-value 'x) ; => 200
(fset 'x 300) ; => 300
(symbol-function 'x) ; => 300
aはシンボル a の値を返すが
'aはシンボル a 自体を返す。 クオートによって値を評価するかどうかを制御できる。
; コンスセル '(1 . 2) ; リスト '(1 2 3) ; リストはコンスセルで表現できる '(1 . (2 . (3 . ())))
(car '(1 . 2)) ; => 1
(cdr '(1 . 2))
(setq c '(1 . 2)) ; => (1 . 2) (setcar c 3) ; => 3 c ; => (3 . 2)
(setq c '(1 . 2)) ; => (1 . 2) (setcar c 3) ; => 3 c ; => (1 . 3)
(setq a [1 2 3]) ; =>b [1 2 3] (aref a 0) ; => 1
(setq a [1 2 3]) ; =>b [1 2 3] (aset a 0 2) ; => 2 a ; => [2 2 3]
"foo" ; => "foo"
(substring "hello" 0 4) ; => "hell"
(concat "Hello" " world") ; => "Hello world"
(length '(1 2 3)) ; => 3 (length [1 2 3]) ; => 3 (length "foo") ; => 3
(mapcar 'identity '(1 2 3)) ; => (1 2 3) (mapcar 'identity '[1 2 3]) ; => (1 2 3) (mapcar 'identity "foo") ; => (102 111 111) (mapcar (lambda (x) (* x x)) '(1 2 3)) ; => (1 4 9)
(elt '(1 2 3) 1) ; => 2 (elt '[1 2 3] 1) ; => 2 (elt "foo" 1) ; => 111
; 二乗を求める関数 (defun square (x) (* x x)) (square 2) ; => 4
(funcall 'square 3) ; => 9 (apply 'square '(3)) ; => 9
(setq square2 (lambda (x) (* x x))) (square2 4) ; => (void-functoin square2) (funcall square2 4) ; => 16 (fset 'square2 square2) ;うまく呼べる (square2 4) ; => 16
t ; => t
nil ; => nil () ; => nil
(eq 1 1) ; => t (eq 1 2) ; => nil (eq 'foo 'foo) ; => t (eq '(1) '(1)) ; => nil (eq 1.2 1.2) ; => nil (eq "foo" "foo") ; => nil
(eql '(1) '(1)) ; => nil (eql 1.2 1.2) ; => t (eql "foo" "foo") ; => nil
(equal '(1) '(1)) ; => t (equal "foo" "foo") ; => t
(= 1 1) ; => t (= 1 2) ; => nil **** /=
(/= 1 1) ; => nil (/= 1 2) ; => t **** >/>= (> 1 2) ; => nil (>= 1 2) ; => nil **** </<= (< 1 2) ; => t (<= 1 2) ; => t
(not t) ; => nil (not nil) ; => t
(if COND THEN ELSE...)
(if (eq 1 1)
(message "OK")
(error "error")
(error "do something"))
(when COND BODY...)
(if (not COND)
(progn
BODY...)) と同等
(when (eq 1 1)
(message "do")
(message "something"))
(unless COND BODY...)
(if COND
nil
BODY...) と同等
(unless (eq 1 1)
(message "oh")
(message "my")
(message "god"))
(or CONDITIONS...) (or nil) ; => nil (or nil t) ; => t (or nil 1) ; => 1
(and CONDITIONS...) (and nil) ; => nil (and nil t) ; => nil (and 1 2) ; => 2
(while TEST BODY...) (setq i 0) (while (< i 5) (message "%s" i) (setq i (1+ i))) ; => 0 1 2 3 4
(dotimes (VAR COUNT) BODY...) (dotimes (i 5) (message "%s" i)) ; => 0 1 2 3 4
(dolist (VAR LIST) BODY...) (dolist (i '(1 2 3)) (message "%s" i)) ; => 1 2 3
(let VARLIST BODY...) (let ((x 100) (y 200)) (message "%s %s" x y)) ; => 100 200
(setq x 1) (defun scope-test () (+ x 2)) (scope-test 2) ; => 3 (let ((x 3)) (scope-test 2) ; => 5
(defun make-adder (m) (lambda (n) (+ m n))) (funcall (make-adder 1) 2) ; => (void-variable m)
(defun puyo () "ぷよぷよをはじめる" (interactive) (switch-to-buffer "*Puyo*") ...)
(defun puyo-mode () "ぷよぷよモード" (interactive) (setq major-mode 'puyo-mode) (setq mode-name "Puyo") (puyo-init))
(defun puyo-start-game () "ぷよぷよをはじめる" (interactive) (puyo-init-buffer) (gamegrid-start-timer puyo-default-tick-period 'puyo-update-game))
(defun puyo-update-game (puyo-buffer)
"ゲームをすすめる"
(let (hit)
(setq puyo-drop (+ puyo-drop (/ puyo-default-tick-period puyo-default-drop-speed)))
(when (>= puyo-drop 1)
...)))
(defun puyo-get-cell (x y)
(gamegrid-get-cell (+ puyo-top-left-x x)
(+ puyo-top-left-y y)))
(defun puyo-set-cell (x y color)
(gamegrid-set-cell (+ puyo-top-left-x x)
(+ puyo-top-left-y y)
color))
(puyo-set-cell 0 0 1)
(puyo-set-cell 1 1 3)
(defvar puyo-shape nil)
(defun puyo-get-shape-cell (x y)
(aref puyo-shape (+ (* y 3) x)))
(defun puyo-draw-shape ()
"ぷよを描画する"
(dotimes (y 3)
(dotimes (x 3)
(let ((c (puyo-get-shape-cell x y)))
(if (/= c puyo-blank)
(puyo-set-cell (+ puyo-pos-x x) (+ puyo-pos-y y) c))))))
(setq puyo-shape [0 1 0 0 1 0 0 0 0]) (puyo-draw-shape)
(defun puyo-erase-shape ()
"ぷよを消去する"
(dotimes (y 3)
(dotimes (x 3)
(let ((c (puyo-get-shape-cell x y)))
(if (/= c puyo-blank)
(puyo-set-cell (+ puyo-pos-x x) (+ puyo-pos-y y) puyo-blank))))))
(defun puyo-make-shape ()
"ぷよを作る"
(vector 0 (1+ (random 4)) 0
0 (1+ (random 4)) 0
0 0 0))
(progn (setq puyo-shape (puyo-make-shape)) (puyo-draw-shape))
(defun puyo-new-shape ()
"次のぷよを出す"
(setq puyo-pos-x (- (floor (/ puyo-width 2)) 2))
(if (/= (puyo-get-cell (+ puyo-pos-x 1)
puyo-top-left-y)
puyo-blank)
(puyo-end-game)
(setq puyo-pos-y 0)
(setq puyo-shape (puyo-make-shape))
(puyo-draw-shape)))
(defun puyo-end-game () "ゲームオーバー" (interactive) (puyo-cleanup) (message "ゲームオーバー"))
...
(puyo-erase-shape)
(setq puyo-pos-y (1+ puyo-pos-y))
(setq puyo-drop 0)
(puyo-draw-shape))))
(puyo-new-shape)
(defun puyo-test-shape()
"ぷよが衝突したか"
(let ((hit nil))
(dotimes (y 3)
(dotimes (x 3)
(unless hit
(setq hit
(let ((c (puyo-get-shape-cell x y))
(xx (+ puyo-pos-x x))
(yy (+ puyo-pos-y y)))
(and (/= c puyo-blank)
(if (< yy 0)
(or (< xx 0)
(>= xx puyo-width))
(or (>= xx puyo-width)
(>= yy puyo-height)
(/= (puyo-get-cell x y)
puyo-blank)))))))))
hit))
(puyo-erase-shape)
(setq puyo-pos-y (1+ puyo-pos-y))
(let ((hit (puyo-test-shape)))
(if hit
(setq puyo-pos-y (1- puyo-pos-y))
(setq puyo-drop 0))
(puyo-draw-shape)
(if (and hit (>= puyo-drop 2))
(puyo-new-shape))))))
(defvar puyo-mode-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap "x" 'puyo-move-down)
(define-key keymap "s" 'puyo-move-bottom)
(define-key keymap "z" 'puyo-move-left)
(define-key keymap "c" 'puyo-move-right)
(define-key keymap "v" 'puyo-rotate-left)
(define-key keymap "b" 'puyo-rotate-right)
keymap))
(use-local-map puyo-mode-map)
(defun puyo-rotate-left ()
"ぷよを反時計回りに回す"
(interactive)
(let ((old puyo-shape))
(puyo-erase-shape)
(setq puyo-shape
(vector (aref puyo-shape 2) (aref puyo-shape 5) (aref puyo-shape 8)
(aref puyo-shape 1) (aref puyo-shape 4) (aref puyo-shape 7)
(aref puyo-shape 0) (aref puyo-shape 3) (aref puyo-shape 6)))
(unless (puyo-try-rotate-move)
(setq puyo-shape old))
(puyo-draw-shape)))
(defun puyo-rotate-right ()
"ぷよを時計回りに回す"
(interactive)
(let ((old puyo-shape))
(puyo-erase-shape)
(setq puyo-shape
(vector (aref puyo-shape 6) (aref puyo-shape 3) (aref puyo-shape 0)
(aref puyo-shape 7) (aref puyo-shape 4) (aref puyo-shape 1)
(aref puyo-shape 8) (aref puyo-shape 5) (aref puyo-shape 2)))
(if (puyo-test-shape)
(setq puyo-shape old))
(puyo-draw-shape)))
(defun puyo-move-left ()
"ぷよを左に動かす"
(interactive)
(puyo-erase-shape)
(setq puyo-pos-x (1- puyo-pos-x))
(if (puyo-test-shape)
(setq puyo-pos-x (1+ puyo-pos-x)))
(puyo-draw-shape))
(defun puyo-move-left ()
"ぷよを右に動かす"
(interactive)
(puyo-erase-shape)
(setq puyo-pos-x (1+ puyo-pos-x))
(if (puyo-test-shape)
(setq puyo-pos-x (1- puyo-pos-x)))
(puyo-draw-shape))
(defun puyo-move-down ()
"ぷよを下に動かす"
(interactive)
(puyo-erase-shape)
(setq puyo-pos-y (1+ puyo-pos-y))
(if (puyo-test-shape)
(progn
(setq puyo-pos-y (1- puyo-pos-y))
(puyo-draw-shape)
(puyo-new-shape))
(puyo-draw-shape)))
(defvar puyo-task nil "実行中のタスク")
(defun puyo-update-game (puyo-buffer)
"ゲームをすすめる"
(let (hit)
(setq puyo-drop (+ puyo-drop (/ puyo-default-tick-period puyo-default-drop-speed)))
(when (>= puyo-drop 1)
(cond
((eq puyo-task 'drop)
(puyo-drop)
(setq puyo-drop 0))
(t
(puyo-erase-shape)
(setq puyo-pos-y (1+ puyo-pos-y))
(let ((hit (puyo-test-shape)))
(if hit
(setq puyo-pos-y (1- puyo-pos-y))
(setq puyo-drop 0))
(puyo-draw-shape)
(if (and hit (>= puyo-drop 2))
(puyo-new-shape))))))))
(defun puyo-drop ()
"浮いてるぷよを落とす"
(dotimes (y (1- puyo-height))
(dotimes (x puyo-width)
(let* ((yy (- puyo-height y))
(c (puyo-get-cell x yy)))
(when (/= c puyo-blank)
(while (= (puyo-get-cell x (1+ yy)) puyo-blank)
(setq yy (1+ yy)))
(puyo-set-cell x (- puyo-height y) puyo-blank)
(puyo-set-cell x yy c))))))
(defun puyo-chain-1 (x y c color footprint)
(if (or (< x 0)
(< y 0)
(>= x puyo-width)
(>= y puyo-height)
(= (aref footprint (+ x (* y puyo-width))) 1))
0
(let ((d (puyo-get-cell x y)))
(if (= c d)
(progn
(aset footprint (+ x (* y puyo-width)) 1)
(if color
(puyo-set-cell x y color))
(+ (puyo-chain-1 (1- x) y c color visit)
(puyo-chain-1 x (1- y) c color visit)
(puyo-chain-1 (1+ x) y c color visit)
(puyo-chain-1 x (1+ y) c color visit)
1))
0))))
(defun puyo-chain (x y color)
"連鎖する"
(let ((c (puyo-get-cell x y)))
(if (/= c puyo-blank)
(let ((footprint (make-vector (* puyo-width puyo-height) 0)))
(+ (puyo-chain-1 (1- x) y c color visit)
(puyo-chain-1 x (1- y) c color visit)
(puyo-chain-1 (1+ x) y c color visit)
(puyo-chain-1 x (1+ y) c color visit)
))
0)))
(defun puyo-update-game (puyo-buffer)
"ゲームをすすめる"
(let (hit)
(setq puyo-drop (+ puyo-drop (/ puyo-default-tick-period puyo-default-drop-speed)))
(when (>= puyo-drop 1)
(cond
((eq puyo-task 'drop)
(puyo-drop)
(setq puyo-task 'chain)
(setq puyo-drop 0))
((eq puyo-task 'chain)
(let ((count 0))
(dotimes (y puyo-height)
(dotimes (x puyo-width)
(let ((c (puyo-chain x y nil)))
(message "%s" c)
(when (>= c 4)
(puyo-chain x y puyo-blank)
(setq count (1+ count))))))
(if (> count 0)
(setq puyo-task 'drop)
(setq puyo-task nil)
(puyo-new-shape)))
(setq puyo-drop 0))
(t
(puyo-erase-shape)
(setq puyo-pos-y (1+ puyo-pos-y))
(let ((hit (puyo-test-shape)))
(if hit
(setq puyo-pos-y (1- puyo-pos-y))
(setq puyo-drop 0))
(puyo-draw-shape)
(if (and hit (>= puyo-drop 2))
(puyo-shape-done))))))))
Date: 2008/11/15 19時28分00秒
HTML generated by org-mode 6.05b in emacs 23