]> code.delx.au - gnu-emacs-elpa/blob - chess-tutorial.el
Release 2.0.4
[gnu-emacs-elpa] / chess-tutorial.el
1 ;;; chess-tutorial.el --- A simple chess training display
2
3 ;; Copyright (C) 2002, 2004, 2014 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
7 ;; Keywords: games
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; `M-x chess-tutorial' implements a simple knight movement exercise.
25 ;; The objective is to take all pawns on the chessboard without moving to a
26 ;; square which is attacked by a queen.
27
28 ;;; Code:
29
30 (require 'chess)
31 (require 'chess-display)
32 (require 'chess-game)
33 (require 'chess-message)
34
35 (chess-message-catalog 'english
36 '((queen-would-take . "The queen would take your knight!")
37 (congratulations . "Congratulations!")
38 (knight-1-done . "Goal: take all the pawns, without letting the queen take your knight")
39 (cannot-take-queen . "You cannot take the queen")))
40
41 (defun chess-tutorial-knight-1 (game _ignore event &rest _args)
42 (if (eq event 'move)
43 (let ((position (chess-game-pos game)))
44 (if (null (chess-pos-search position ?p))
45 (chess-message 'congratulations)
46 (cond
47 ((chess-search-position position
48 (car (chess-pos-search position ?N)) ?q)
49 (let ((chess-display-handling-event nil))
50 (chess-game-undo game 1))
51 (chess-error 'queen-would-take))
52 ((not (chess-pos-search position ?q))
53 (let ((chess-display-handling-event nil))
54 (chess-game-undo game 1))
55 (chess-error 'cannot-take-queen)))))))
56
57 ;;;###autoload
58 (defun chess-tutorial ()
59 "A simple chess training display."
60 (interactive)
61 (with-current-buffer (chess-create-display t)
62 (chess-module-set-leader nil)
63 (chess-display-set-from-fen "8/3p1p/2p3p/4q/2p3p/3p1p/8/N w - -")
64 (chess-game-add-hook (chess-display-game nil) 'chess-tutorial-knight-1)
65 (setq chess-pos-always-white t)
66 (chess-display-popup nil)
67 (chess-message 'knight-1-done)))
68
69 (provide 'chess-tutorial)
70
71 ;;; chess-tutorial.el ends here