├── CHANGELOG ├── README ├── README.es ├── cl-chess.asd ├── moves ├── queen.lisp ├── knight.lisp ├── bishop.lisp ├── rook.lisp ├── king.lisp ├── pawn.lisp └── moves.lisp ├── io.lisp ├── miguedrez.lisp ├── ai.lisp ├── data.lisp └── COPYING /CHANGELOG: -------------------------------------------------------------------------------- 1 | 2 | 0.9 2009-02-21 First published release. 3 | 0.91 2009-02-25 Corrected encoding of source files. 4 | 0.92 2009-02-26 Improved movement notation: from "2 3 2 4" ugly style 5 | to "b2 b3" style. 6 | 0.95 2009-03-07 Code completely translated into English. 7 | 8 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | Developed with SLIME. 3 | 4 | Fire up your Lisp interpreter (I use SBCL) and load the main file: 5 | (load "miguedrez.lisp") 6 | 7 | Now you can start the game: 8 | (miguedrez:ajz) 9 | 10 | Enjoy! 11 | 12 | Suggestions (complains and things like that :-) to: 13 | manuf_81 at y a h o o . c o m 14 | 15 | -------------------------------------------------------------------------------- /README.es: -------------------------------------------------------------------------------- 1 | 2 | Desarrollado con slime. 3 | 4 | Lanza tu intérprete de lisp (clisp, sbcl...) y carga el fichero principal: 5 | 6 | (load "miguedrez.lisp") 7 | 8 | Una vez termine, lanza la función principal de juego: 9 | 10 | (miguedrez:ajz) 11 | 12 | ¡Diviértete! 13 | 14 | Sujerencias (quejas y demás :-) a 15 | manuf_81 at y a h o o . c o m 16 | 17 | -------------------------------------------------------------------------------- /cl-chess.asd: -------------------------------------------------------------------------------- 1 | (defpackage #:cl-chess-asd 2 | (:use :cl :asdf)) 3 | 4 | (in-package :cl-chess-asd) 5 | 6 | (defsystem cl-chess 7 | :name "miguedrez" 8 | :version "0.9.5" 9 | :maintainer "Olexiy Zamkoviy" 10 | :author "Manuel Felipe Gamallo Rivero" 11 | :licence "GPL v3" 12 | :description "Chess library" 13 | :components ( 14 | (:file "miguedrez") 15 | (:file "data" :depends-on ("miguedrez")) 16 | (:file "io" :depends-on ("miguedrez")) 17 | (:file "ai" :depends-on ("miguedrez")) 18 | (:module "moves" 19 | :components ( 20 | (:file "moves") 21 | (:file "bishop" :depends-on ("moves")) 22 | (:file "king" :depends-on ("moves")) 23 | (:file "knight" :depends-on ("moves")) 24 | (:file "pawn" :depends-on ("moves")) 25 | (:file "queen" :depends-on ("moves")) 26 | (:file "rook")) 27 | :depends-on ("miguedrez")))) 28 | -------------------------------------------------------------------------------- /moves/queen.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; queen.lisp 4 | ;;; Movements of the queens 5 | ;;; gamallo, July 9, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | (defun possible-queen (board pos) 29 | (let* ((rook-functions '((white possible-white-rook) 30 | (black possible-back-rook))) 31 | (bishop-functions '((white possible-white-bishop) 32 | (black possible-black-bishop))) 33 | (color (if (eq (aref (board-board board) 34 | (pos-row pos) 35 | (pos-col pos)) 36 | 'DB) 37 | 'white 38 | 'black)) 39 | (rook-function (second (assoc color rook-functions))) 40 | (bishop-function (second (assoc color bishop-functions))) 41 | (positions '())) 42 | 43 | (setf positions (funcall rook-function board pos)) 44 | (setf positions (append positions 45 | (funcall bishop-function board pos))))) 46 | -------------------------------------------------------------------------------- /io.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; io.lisp 4 | ;;; Input/Output 5 | ;;; gamallo, February 11, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | ;;; Hash table containing the descriptions of the pieces 26 | (in-package :miguedrez) 27 | (defvar *pieces* (make-hash-table)) 28 | (setf (gethash 'TN *pieces*) "BR") 29 | (setf (gethash 'TB *pieces*) "WR") 30 | (setf (gethash 'CN *pieces*) "BN") 31 | (setf (gethash 'CB *pieces*) "WN") 32 | (setf (gethash 'AN *pieces*) "BB") 33 | (setf (gethash 'AB *pieces*) "WB") 34 | (setf (gethash 'RN *pieces*) "BK") 35 | (setf (gethash 'RB *pieces*) "WK") 36 | (setf (gethash 'DN *pieces*) "BQ") 37 | (setf (gethash 'DB *pieces*) "WQ") 38 | (setf (gethash 'PN *pieces*) "BP") 39 | (setf (gethash 'PB *pieces*) "WP") 40 | (setf (gethash 'VV *pieces*) "..") 41 | 42 | 43 | ;;; Show board to the user 44 | (defun show-board (board) 45 | (format t "~% A B C D E F G H~%~%") 46 | (dotimes (row 8) 47 | (format t " ~A " (- 8 row)) 48 | (dotimes (column 8) 49 | (format t "~A " (gethash (aref (board-board board) row column) 50 | *pieces*))) 51 | (format t "~%~%"))) 52 | 53 | 54 | ;;; Ask the user for a move 55 | (defun read-move () 56 | (let (row-from column-from row-to column-to) 57 | (loop 58 | (when (and (not (null row-from)) 59 | (not (null column-from)) 60 | (not (null row-to)) 61 | (not (null column-to))) 62 | (return)) 63 | (format t "Movimiento: ") 64 | (let ((mov1 (read)) 65 | (mov2 (read))) 66 | (ignore-errors 67 | (when (= 2 (length (string mov1))) 68 | (setf row-from (digit-char-p (aref (string mov1) 1))) 69 | (setf column-from (position (aref (string mov1) 0) "ABCDEFGH" 70 | :test (function string-equal)))) 71 | (when (= 2 (length (string mov2))) 72 | (setf row-to (digit-char-p (aref (string mov2) 1))) 73 | (setf column-to (position (aref (string mov2) 0) "ABCDEFGH" 74 | :test (function string-equal))))))) 75 | ;; (format t "~A ~A ~A ~A" row-from column-from row-to column-to) 76 | (make-move :from (make-pos :row (- 8 row-from) 77 | :col column-from) 78 | :to (make-pos :row (- 8 row-to) 79 | :col column-to)))) 80 | -------------------------------------------------------------------------------- /miguedrez.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; miguedrez.lisp 4 | ;;; Main 5 | ;;; gamallo, February 11, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | (defpackage :miguedrez 26 | (:use :cl :cl-user) 27 | (:export :ajz :make-move :make-pos :*pieces* :make-player :create-initial-board-white-bottom :board-board :valid :execute-move :checkmatep :player-color :black :white)) 28 | 29 | (in-package :miguedrez) 30 | 31 | (defvar *calls* 0) 32 | 33 | (defmacro define-constant (name value &optional doc) 34 | `(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value) 35 | ,@(when doc (list doc)))) 36 | 37 | (define-constant +knight-bishop-pos-weigh+ 38 | (make-array 8 :initial-contents '(10 10 30 60 60 30 10 10))) 39 | 40 | (define-constant +rook-pos-weigh+ 41 | (make-array 8 :initial-contents '(60 30 30 10 10 30 30 60))) 42 | 43 | (define-constant +pawn-pos-v-weigh+ 44 | (make-array 8 :initial-contents '(0 0 5 10 30 100 250 900))) 45 | 46 | (define-constant +pawn-pos-h-weigh+ 47 | (make-array 8 :initial-contents '(2 5 10 20 20 10 5 2))) 48 | 49 | (defun ajz () 50 | 51 | ;; initialize 52 | (let* ((player1 (make-player :type 'manual :color 'white)) 53 | (player2 (make-player :type 'auto :color 'black)) 54 | ;; (table (create-initial-board-tests)) 55 | (table (create-initial-board-white-bottom)) 56 | (current-player) 57 | (current-move)) 58 | 59 | (setf current-player player1) 60 | 61 | (loop 62 | 63 | (show-board table) 64 | (setf *calls* 0) 65 | ;; checkmate? 66 | (if (checkmatep table current-player) 67 | ;; Yes -> end 68 | (progn 69 | (format t "Pierde el color ~A.~%" 70 | (player-color current-player)) 71 | (return)) 72 | ;; No -> load move 73 | (setf current-move (load-move table current-player))) 74 | 75 | ;; check move - including castlings 76 | (if (valid table current-move (player-color current-player)) 77 | 78 | ;; valid -> do movement 79 | (progn 80 | (execute-move table current-move) 81 | (if (eq current-player player1) 82 | (setf current-player player2) 83 | (setf current-player player1))) 84 | 85 | ;; invalid -> automatic? 86 | (if (eq (player-type current-player) 'auto) 87 | ;; yes -> error 88 | (progn 89 | (print "Esto es una mierda...") 90 | (return)) 91 | ;; not -> ask again 92 | (print "Movimiento de mierda. Otro, anda...")))))) 93 | 94 | (defun load-move (table player) 95 | (if (eq (player-type player) 'manual) 96 | (read-move) 97 | (choose-move table (player-color player)))) 98 | -------------------------------------------------------------------------------- /moves/knight.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; knight.lisp 4 | ;;; Movements of the knights 5 | ;;; gamallo, April 1, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | ;;; Places threatened by a knight 29 | (defun pos-knight (board pos) 30 | (let ((positions '())) 31 | ;; One o'clock 32 | (if (and (> (pos-row pos) 1) 33 | (< (pos-col pos) 7)) 34 | (setf positions (cons (make-pos 35 | :row (- (pos-row pos) 2) 36 | :col (1+ (pos-col pos))) 37 | positions))) 38 | ;; Two o'clock 39 | (if (and (> (pos-row pos) 0) 40 | (< (pos-col pos) 6)) 41 | (setf positions (cons (make-pos 42 | :row (1- (pos-row pos)) 43 | :col (+ (pos-col pos) 2)) 44 | positions))) 45 | ;; Four o'clock 46 | (if (and (< (pos-row pos) 7) 47 | (< (pos-col pos) 6)) 48 | (setf positions (cons (make-pos 49 | :row (1+ (pos-row pos)) 50 | :col (+ (pos-col pos) 2)) 51 | positions))) 52 | ;; Five o'clock 53 | (if (and (< (pos-row pos) 6) 54 | (< (pos-col pos) 7)) 55 | (setf positions (cons (make-pos 56 | :row (+ (pos-row pos) 2) 57 | :col (1+ (pos-col pos))) 58 | positions))) 59 | ;; Seven o'clock 60 | (if (and (< (pos-row pos) 6) 61 | (> (pos-col pos) 0)) 62 | (setf positions (cons (make-pos 63 | :row (+ (pos-row pos) 2) 64 | :col (1- (pos-col pos))) 65 | positions))) 66 | ;; Eight o'clock 67 | (if (and (< (pos-row pos) 7) 68 | (> (pos-col pos) 1)) 69 | (setf positions (cons (make-pos 70 | :row (1+ (pos-row pos)) 71 | :col (- (pos-col pos) 2)) 72 | positions))) 73 | ;; Ten o'clock 74 | (if (and (> (pos-row pos) 0) 75 | (> (pos-col pos) 1)) 76 | (setf positions (cons (make-pos 77 | :row (1- (pos-row pos)) 78 | :col (- (pos-col pos) 2)) 79 | positions))) 80 | ;; Eleven o'clock 81 | (if (and (> (pos-row pos) 1) 82 | (> (pos-col pos) 0)) 83 | (setf positions (cons (make-pos 84 | :row (- (pos-row pos) 2) 85 | :col (1- (pos-col pos))) 86 | positions))) 87 | positions)) 88 | 89 | 90 | 91 | 92 | 93 | (defun possible-knight (board pos) 94 | (let ((positions '())) 95 | (dolist (a-pos (pos-knight board pos)) 96 | (if (eq (aref (board-board board) 97 | (pos-row pos) 98 | (pos-col pos)) 99 | 'CB) 100 | ;; Knight is white 101 | (if (eq (aref (board-whites board) 102 | (pos-row a-pos) 103 | (pos-col a-pos)) 104 | '0) 105 | (setf positions (cons a-pos positions))) 106 | ;; Knight is black 107 | (if (eq (aref (board-blacks board) 108 | (pos-row a-pos) 109 | (pos-col a-pos)) 110 | '0) 111 | (setf positions (cons a-pos positions))))) 112 | positions)) 113 | -------------------------------------------------------------------------------- /ai.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; ai.lisp 4 | ;;; Artificial Intelligence 5 | ;;; gamallo, February 11, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | ;; Pieces weight more or less depending on their proximity to the 28 | ;; center of the board. 29 | 30 | (defun choose-move (board color) 31 | (second (multiple-value-list 32 | (alpha-beta board 3 -1000000 1000000 color)))) 33 | 34 | 35 | 36 | 37 | 38 | (defun alpha-beta (board depth alpha beta color) 39 | (if (eq depth 0) 40 | (fev board) 41 | (let ((move) 42 | (children (children board color))) 43 | (dolist (a-move children) 44 | (when (king-valid board a-move color) 45 | (when (null move) (setf move a-move)) 46 | (let* ((new-board (execute-move (copy-board board) 47 | a-move)) 48 | (val (alpha-beta new-board (- depth 1) alpha beta (invert-color color)))) 49 | (if (eq color 'white) 50 | (progn 51 | (when (> val alpha) 52 | (setf alpha val) 53 | (setf move a-move)) 54 | (when (> alpha beta) 55 | (values alpha move))) 56 | (progn 57 | (when (< val beta) 58 | (setf beta val) 59 | (setf move a-move)) 60 | (when (> alpha beta) 61 | (values beta move))))))) 62 | (if (eq color 'white) 63 | (values alpha move) 64 | (values beta move))))) 65 | 66 | 67 | 68 | 69 | (defun fev (board) 70 | (let ((fev 0)) 71 | (dotimes (i 8) 72 | (dotimes (j 8) 73 | (let ((piece (aref (board-board board) i j))) 74 | (cond ((eq piece 'PB) 75 | (setf fev (+ fev 76 | 100 77 | (if (eq 78 | (board-whites-initial-pos board) 79 | 'bottom) 80 | (aref +pawn-pos-v-weigh+ (- 7 i)) 81 | (aref +pawn-pos-v-weigh+ i)) 82 | (aref +pawn-pos-h-weigh+ j)))) 83 | ((eq piece 'PN) 84 | (setf fev (- fev 85 | 100 86 | (if (eq 87 | (board-whites-initial-pos board) 88 | 'bottom) 89 | (aref +pawn-pos-v-weigh+ i) 90 | (aref +pawn-pos-v-weigh+ (- 7 i))) 91 | (aref +pawn-pos-h-weigh+ j)))) 92 | ((eq piece 'TB) 93 | (setf fev (+ fev 94 | 500 95 | (aref +rook-pos-weigh+ i) 96 | (aref +rook-pos-weigh+ j)))) 97 | ((eq piece 'TN) 98 | (setf fev (- fev 99 | 500 100 | (aref +rook-pos-weigh+ i) 101 | (aref +rook-pos-weigh+ j)))) 102 | ((eq piece 'AB) 103 | (setf fev (+ fev 104 | 300 105 | (aref +knight-bishop-pos-weigh+ i) 106 | (aref +knight-bishop-pos-weigh+ j)))) 107 | ((eq piece 'AN) 108 | (setf fev (- fev 109 | 300 110 | (aref +knight-bishop-pos-weigh+ i) 111 | (aref +knight-bishop-pos-weigh+ j)))) 112 | ((eq piece 'CB) 113 | (setf fev (+ fev 114 | 300 115 | (aref +knight-bishop-pos-weigh+ i) 116 | (aref +knight-bishop-pos-weigh+ j)))) 117 | ((eq piece 'CN) 118 | (setf fev (- fev 119 | 300 120 | (aref +knight-bishop-pos-weigh+ i) 121 | (aref +knight-bishop-pos-weigh+ j)))) 122 | ((eq piece 'RB) 123 | (setf fev (+ fev 1000))) 124 | ((eq piece 'RN) 125 | (setf fev (- fev 1000))) 126 | ((eq piece 'DB) 127 | (setf fev (+ fev 900))) 128 | ((eq piece 'DN) 129 | (setf fev (- fev 900))))))) 130 | fev)) 131 | -------------------------------------------------------------------------------- /moves/bishop.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; bishop.lisp 4 | ;;; Movements of the bishops 5 | ;;; gamallo, April 2, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | (defun possible-white-move (board pos) 29 | (eq (aref (board-board board) 30 | (pos-row pos) 31 | (pos-col pos)) 'VV)) 32 | 33 | (defun possible-white-capture (board pos) 34 | (eq (aref (board-blacks board) 35 | (pos-row pos) 36 | (pos-col pos)) '1)) 37 | 38 | (defun possible-black-move (board pos) 39 | (eq (aref (board-board board) 40 | (pos-row pos) 41 | (pos-col pos)) 'VV)) 42 | 43 | (defun possible-black-capture (board pos) 44 | (eq (aref (board-whites board) 45 | (pos-row pos) 46 | (pos-col pos)) '1)) 47 | 48 | (defun diagonal-move (pos f c) 49 | (make-pos :row (+ (pos-row pos) f) 50 | :col (+ (pos-col pos) c))) 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | (defun possible-white-bishop (board pos) 59 | (let ((positions '())) 60 | ;; NE 61 | (dotimes (c (min (pos-row pos) 62 | (- 7 (pos-col pos)))) 63 | (let ((new-pos 64 | (diagonal-move pos (- (1+ c)) (1+ c)))) 65 | (cond ((possible-white-move board new-pos) 66 | (setf positions (cons new-pos positions))) 67 | ((possible-white-capture board new-pos) 68 | (setf positions (cons new-pos positions)) 69 | (return nil)) 70 | (t (return nil))))) 71 | 72 | ;; SE 73 | (dotimes (c (min (- 7 (pos-row pos)) 74 | (- 7 (pos-col pos)))) 75 | (let ((new-pos 76 | (diagonal-move pos (1+ c) (1+ c)))) 77 | (cond ((possible-white-move board new-pos) 78 | (setf positions (cons new-pos positions))) 79 | ((possible-white-capture board new-pos) 80 | (setf positions (cons new-pos positions)) 81 | (return nil)) 82 | (t (return nil))))) 83 | 84 | ;; SW 85 | (dotimes (c (min (- 7 (pos-row pos)) 86 | (pos-col pos))) 87 | (let ((new-pos 88 | (diagonal-move pos (1+ c) (- (1+ c))))) 89 | (cond ((possible-white-move board new-pos) 90 | (setf positions (cons new-pos positions))) 91 | ((possible-white-capture board new-pos) 92 | (setf positions (cons new-pos positions)) 93 | (return nil)) 94 | (t (return nil))))) 95 | 96 | ;; NW 97 | (dotimes (c (min (pos-row pos) 98 | (pos-col pos))) 99 | (let ((new-pos 100 | (diagonal-move pos (- (1+ c)) (- (1+ c))))) 101 | (cond ((possible-white-move board new-pos) 102 | (setf positions (cons new-pos positions))) 103 | ((possible-white-capture board new-pos) 104 | (setf positions (cons new-pos positions)) 105 | (return nil)) 106 | (t (return nil))))) 107 | positions)) 108 | 109 | 110 | 111 | 112 | 113 | (defun possible-black-bishop (board pos) 114 | (let ((positions '())) 115 | ;; NE 116 | (dotimes (c (min (pos-row pos) 117 | (- 7 (pos-col pos)))) 118 | (let ((new-pos 119 | (diagonal-move pos (- (1+ c)) (1+ c)))) 120 | (cond ((possible-black-move board new-pos) 121 | (setf positions (cons new-pos positions))) 122 | ((possible-black-capture board new-pos) 123 | (setf positions (cons new-pos positions)) 124 | (return nil)) 125 | (t (return nil))))) 126 | 127 | ;; SE 128 | (dotimes (c (min (- 7 (pos-row pos)) 129 | (- 7 (pos-col pos)))) 130 | (let ((new-pos 131 | (diagonal-move pos (1+ c) (1+ c)))) 132 | (cond ((possible-black-move board new-pos) 133 | (setf positions (cons new-pos positions))) 134 | ((possible-black-capture board new-pos) 135 | (setf positions (cons new-pos positions)) 136 | (return nil)) 137 | (t (return nil))))) 138 | 139 | ;; SW 140 | (dotimes (c (min (- 7 (pos-row pos)) 141 | (pos-col pos))) 142 | (let ((new-pos 143 | (diagonal-move pos (1+ c) (- (1+ c))))) 144 | (cond ((possible-black-move board new-pos) 145 | (setf positions (cons new-pos positions))) 146 | ((possible-black-capture board new-pos) 147 | (setf positions (cons new-pos positions)) 148 | (return nil)) 149 | (t (return nil))))) 150 | 151 | ;; NW 152 | (dotimes (c (min (pos-row pos) 153 | (pos-col pos))) 154 | (let ((new-pos 155 | (diagonal-move pos (- (1+ c)) (- (1+ c))))) 156 | (cond ((possible-black-move board new-pos) 157 | (setf positions (cons new-pos positions))) 158 | ((possible-black-capture board new-pos) 159 | (setf positions (cons new-pos positions)) 160 | (return nil)) 161 | (t (return nil))))) 162 | positions)) 163 | 164 | 165 | 166 | 167 | 168 | (defun possible-bishop (board pos) 169 | (if (eq (aref (board-board board) 170 | (pos-row pos) 171 | (pos-col pos)) 172 | 'AB) 173 | ;; Bishop is white 174 | (possible-white-bishop board pos) 175 | ;; Bishop is black 176 | (possible-black-bishop board pos))) 177 | -------------------------------------------------------------------------------- /moves/rook.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; rook.lisp 4 | ;;; Movements of the rooks 5 | ;;; gamallo, April 1, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Foobar. 11 | 12 | ;; Foobar 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. 16 | 17 | ;; Foobar 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Foobar. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | 29 | (defun possible-white-rook (board pos) 30 | (let ((positions '())) 31 | ;; Twelve o'clock 32 | (dotimes (row (pos-row pos)) 33 | (cond ((eq (aref (board-board board) 34 | (- (pos-row pos) (1+ row)) 35 | (pos-col pos)) 36 | 'VV) 37 | (setf positions 38 | (cons (make-pos 39 | :row (- (pos-row pos) (1+ row)) 40 | :col (pos-col pos)) 41 | positions))) 42 | ((eq (aref (board-blacks board) 43 | (- (pos-row pos) (1+ row)) 44 | (pos-col pos)) 45 | '1) 46 | (setf positions 47 | (cons (make-pos 48 | :row (- (pos-row pos) (1+ row)) 49 | :col (pos-col pos)) 50 | positions)) 51 | (return nil)) 52 | (t (return nil)))) 53 | ;; Three o'clock 54 | (dotimes (col (- 7 (pos-col pos))) 55 | (cond ((eq (aref (board-board board) 56 | (pos-row pos) 57 | (+ (pos-col pos) col 1)) 58 | 'VV) 59 | (setf positions 60 | (cons (make-pos 61 | :row (pos-row pos) 62 | :col (+ (pos-col pos) col 1)) 63 | positions))) 64 | ((eq (aref (board-blacks board) 65 | (pos-row pos) 66 | (+ (pos-col pos) col 1)) 67 | '1) 68 | (setf positions 69 | (cons (make-pos 70 | :row (pos-row pos) 71 | :col (+ (pos-col pos) col 1)) 72 | positions)) 73 | (return nil)) 74 | (t (return nil)))) 75 | ;; Six o'clock 76 | (dotimes (row (- 7 (pos-row pos))) 77 | (cond ((eq (aref (board-board board) 78 | (+ (pos-row pos) row 1) 79 | (pos-col pos)) 80 | 'VV) 81 | (setf positions 82 | (cons (make-pos 83 | :row (+ (pos-row pos) row 1) 84 | :col (pos-col pos)) 85 | positions))) 86 | ((eq (aref (board-blacks board) 87 | (+ (pos-row pos) row 1) 88 | (pos-col pos)) 89 | '1) 90 | (setf positions 91 | (cons (make-pos 92 | :row (+ (pos-row pos) row 1) 93 | :col (pos-col pos)) 94 | positions)) 95 | (return nil)) 96 | (t (return nil)))) 97 | ;; Nine o'clock 98 | (dotimes (col (pos-col pos)) 99 | (cond ((eq (aref (board-board board) 100 | (pos-row pos) 101 | (- (pos-col pos) (1+ col))) 102 | 'VV) 103 | (setf positions 104 | (cons (make-pos 105 | :row (pos-row pos) 106 | :col (- (pos-col pos) 107 | (1+ col))) 108 | positions))) 109 | ((eq (aref (board-blacks board) 110 | (pos-row pos) 111 | (- (pos-col pos) (1+ col))) 112 | '1) 113 | (setf positions 114 | (cons (make-pos 115 | :row (pos-row pos) 116 | :col (- (pos-col pos) 117 | (1+ col))) 118 | positions)) 119 | (return nil)) 120 | (t (return nil)))) 121 | positions)) 122 | 123 | 124 | (defun possible-back-rook (board pos) 125 | (let ((positions '())) 126 | ;; Twelve o'clock 127 | (dotimes (row (pos-row pos)) 128 | (cond ((eq (aref (board-board board) 129 | (- (pos-row pos) (1+ row)) 130 | (pos-col pos)) 131 | 'VV) 132 | (setf positions 133 | (cons (make-pos 134 | :row (- (pos-row pos) (1+ row)) 135 | :col (pos-col pos)) 136 | positions))) 137 | ((eq (aref (board-whites board) 138 | (- (pos-row pos) (1+ row)) 139 | (pos-col pos)) 140 | '1) 141 | (setf positions 142 | (cons (make-pos 143 | :row (- (pos-row pos) (1+ row)) 144 | :col (pos-col pos)) 145 | positions)) 146 | (return nil)) 147 | (t (return nil)))) 148 | ;; Three o'clock 149 | (dotimes (col (- 7 (pos-col pos))) 150 | (cond ((eq (aref (board-board board) 151 | (pos-row pos) 152 | (+ (pos-col pos) col 1)) 153 | 'VV) 154 | (setf positions 155 | (cons (make-pos 156 | :row (pos-row pos) 157 | :col (+ (pos-col pos) col 1)) 158 | positions))) 159 | ((eq (aref (board-whites board) 160 | (pos-row pos) 161 | (+ (pos-col pos) col 1)) 162 | '1) 163 | (setf positions 164 | (cons (make-pos 165 | :row (pos-row pos) 166 | :col (+ (pos-col pos) col 1)) 167 | positions)) 168 | (return nil)) 169 | (t (return nil)))) 170 | ;; Six o'clock 171 | (dotimes (row (- 7 (pos-row pos))) 172 | (cond ((eq (aref (board-board board) 173 | (+ (pos-row pos) row 1) 174 | (pos-col pos)) 175 | 'VV) 176 | (setf positions 177 | (cons (make-pos 178 | :row (+ (pos-row pos) row 1) 179 | :col (pos-col pos)) 180 | positions))) 181 | ((eq (aref (board-whites board) 182 | (+ (pos-row pos) row 1) 183 | (pos-col pos)) 184 | '1) 185 | (setf positions 186 | (cons (make-pos 187 | :row (+ (pos-row pos) row 1) 188 | :col (pos-col pos)) 189 | positions)) 190 | (return nil)) 191 | (t (return nil)))) 192 | ;; Nine o'clock 193 | (dotimes (col (pos-col pos)) 194 | (cond ((eq (aref (board-board board) 195 | (pos-row pos) 196 | (- (pos-col pos) (1+ col))) 197 | 'VV) 198 | (setf positions 199 | (cons (make-pos 200 | :row (pos-row pos) 201 | :col (- (pos-col pos) 202 | (1+ col))) 203 | positions))) 204 | ((eq (aref (board-whites board) 205 | (pos-row pos) 206 | (- (pos-col pos) (1+ col))) 207 | '1) 208 | (setf positions 209 | (cons (make-pos 210 | :row (pos-row pos) 211 | :col (- (pos-col pos) 212 | (1+ col))) 213 | positions)) 214 | (return nil)) 215 | (t (return nil)))) 216 | positions)) 217 | 218 | 219 | 220 | 221 | 222 | (defun possible-rook (board pos) 223 | (if (eq (aref (board-board board) 224 | (pos-row pos) 225 | (pos-col pos)) 226 | 'TB) 227 | ;; The rook is white 228 | (possible-white-rook board pos) 229 | ;; The rook is black 230 | (possible-back-rook board pos))) 231 | -------------------------------------------------------------------------------- /moves/king.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; king.lisp 4 | ;;; Movements of kings 5 | ;;; gamallo, July 25, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | (defun maybe-pos-king (pos) 29 | (let ((row (pos-row pos)) 30 | (col (pos-col pos)) 31 | (maybe-positions '())) 32 | (when (> row 0) ; Twelve o'clock 33 | (add-pos maybe-positions (- row 1) col)) 34 | (when (and (> row 0) (< col 7)) ; Half-past one 35 | (add-pos maybe-positions (- row 1) (1+ col))) 36 | (when (< col 7) ; Three o'clock 37 | (add-pos maybe-positions row (1+ col))) 38 | (when (and (< row 7) (< col 7)) ; Half-past four 39 | (add-pos maybe-positions (1+ row) (1+ col))) 40 | (when (< row 7) ; Six o'clock 41 | (add-pos maybe-positions (1+ row) col)) 42 | (when (and (< row 7) (> col 0)) ; Half-past seven 43 | (add-pos maybe-positions (1+ row) (- col 1))) 44 | (when (> col 0) ; nine o'clock 45 | (add-pos maybe-positions row (- col 1))) 46 | (when (and (> row 0) (> col 0)) ; Half-past ten 47 | (add-pos maybe-positions (- row 1) (- col 1))) 48 | maybe-positions)) 49 | 50 | 51 | 52 | 53 | 54 | (defun empty-pos (board pos) 55 | (eq (aref (board-board board) 56 | (pos-row pos) 57 | (pos-col pos)) 58 | 'VV)) 59 | 60 | (defun occupied-pos (board pos funcion) 61 | (eq (aref (funcall funcion board) 62 | (pos-row pos) 63 | (pos-col pos)) 64 | '1)) 65 | 66 | 67 | 68 | (defun possible-king-without-castling (board pos) 69 | (let ((opponent-function (if (eq (aref (board-board board) 70 | (pos-row pos) 71 | (pos-col pos)) 72 | 'RB) 73 | #'board-blacks 74 | #'board-whites)) 75 | (color (if (eq (aref (board-board board) 76 | (pos-row pos) 77 | (pos-col pos)) 78 | 'RB) 79 | 'white 80 | 'black)) 81 | (maybe-positions 82 | (maybe-pos-king pos)) 83 | (positions '())) 84 | (dolist (p maybe-positions) 85 | (if (or (empty-pos board p) 86 | (occupied-pos board p opponent-function)) 87 | (setf positions (cons p positions)))) 88 | positions)) 89 | 90 | 91 | (defun possible-king (board pos) 92 | (let ((color (if (eq (aref (board-board board) 93 | (pos-row pos) 94 | (pos-col pos)) 95 | 'RB) 96 | 'white 97 | 'black)) 98 | (positions (possible-king-without-castling board pos))) 99 | (setf positions (append positions (short-castle-if-possible board positions color))) 100 | (setf positions (append positions (long-castle-if-possible board positions color))) 101 | positions)) 102 | 103 | 104 | 105 | (defun short-castle-if-possible (board a-list color) 106 | (cond ((and (eq color 'white) 107 | (eq (board-whites-initial-pos board) 'bottom)) 108 | (when (and (not (board-white-king-moved board)) 109 | (empty-pos board 110 | (make-pos :row 7 :col 5)) 111 | (empty-pos board 112 | (make-pos :row 7 :col 6)) 113 | (eq (board-pos (board-board board) 114 | (make-pos :row 7 115 | :col 7)) 116 | 'TB) 117 | (not (king-threatened board color))) 118 | (add-pos a-list 7 6))) 119 | ((and (eq color 'white) 120 | (eq (board-whites-initial-pos board) 'top)) 121 | (when (and (not (board-white-king-moved board)) 122 | (empty-pos board 123 | (make-pos :row 0 :col 2)) 124 | (empty-pos board 125 | (make-pos :row 0 :col 1)) 126 | (eq (board-pos (board-board board) 127 | (make-pos :row 0 128 | :col 0)) 129 | 'TB) 130 | (not (king-threatened board color))) 131 | (add-pos a-list 0 1))) 132 | ((and (eq color 'black) 133 | (eq (board-whites-initial-pos board) 'bottom)) 134 | (when (and (not (board-black-king-moved board)) 135 | (empty-pos board 136 | (make-pos :row 0 :col 5)) 137 | (empty-pos board 138 | (make-pos :row 0 :col 6)) 139 | (eq (board-pos (board-board board) 140 | (make-pos :row 0 141 | :col 7)) 142 | 'TN) 143 | (not (king-threatened board color))) 144 | (add-pos a-list 0 6))) 145 | ((and (eq color 'black) 146 | (eq (board-whites-initial-pos board) 'top)) 147 | (when (and (not (board-black-king-moved board)) 148 | (empty-pos board 149 | (make-pos :row 7 :col 2)) 150 | (empty-pos board 151 | (make-pos :row 7 :col 1)) 152 | (eq (board-pos (board-board board) 153 | (make-pos :row 7 154 | :col 0)) 155 | 'TN) 156 | (not (king-threatened board color))) 157 | (add-pos a-list 7 1))))) 158 | 159 | 160 | (defun long-castle-if-possible (board a-list color) 161 | (cond ((and (eq color 'white) 162 | (eq (board-whites-initial-pos board) 'bottom)) 163 | (when (and (not (board-white-king-moved board)) 164 | (empty-pos board 165 | (make-pos :row 7 :col 2)) 166 | (empty-pos board 167 | (make-pos :row 7 :col 3)) 168 | (eq (board-pos (board-board board) 169 | (make-pos :row 7 170 | :col 0)) 171 | 'TB) 172 | (not (king-threatened board color))) 173 | (add-pos a-list 7 2))) 174 | ((and (eq color 'white) 175 | (eq (board-whites-initial-pos board) 'top)) 176 | (when (and (not (board-white-king-moved board)) 177 | (empty-pos board 178 | (make-pos :row 0 :col 5)) 179 | (empty-pos board 180 | (make-pos :row 0 :col 4)) 181 | (eq (board-pos (board-board board) 182 | (make-pos :row 0 183 | :col 7)) 184 | 'TB) 185 | (not (king-threatened board color))) 186 | (add-pos a-list 0 5))) 187 | ((and (eq color 'black) 188 | (eq (board-whites-initial-pos board) 'bottom)) 189 | (when (and (not (board-black-king-moved board)) 190 | (empty-pos board 191 | (make-pos :row 0 :col 2)) 192 | (empty-pos board 193 | (make-pos :row 0 :col 3)) 194 | (eq (board-pos (board-board board) 195 | (make-pos :row 0 196 | :col 0)) 197 | 'TN) 198 | (not (king-threatened board color))) 199 | (add-pos a-list 0 2))) 200 | ((and (eq color 'black) 201 | (eq (board-whites-initial-pos board) 'top)) 202 | (when (and (not (board-black-king-moved board)) 203 | (empty-pos board 204 | (make-pos :row 7 :col 5)) 205 | (empty-pos board 206 | (make-pos :row 7 :col 4)) 207 | (eq (board-pos (board-board board) 208 | (make-pos :row 7 209 | :col 7)) 210 | 'TN) 211 | (not (king-threatened board color))) 212 | (add-pos a-list 7 5))))) 213 | -------------------------------------------------------------------------------- /data.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; data.lisp 4 | ;;; Data definitions and manipulation functions 5 | ;;; gamallo, February 11, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | ;;; Main data type 26 | (in-package :miguedrez) 27 | (defstruct board 28 | (board) 29 | (whites) 30 | (blacks) 31 | (whites-enpass) 32 | (blacks-enpass) 33 | (whites-initial-pos) 34 | (white-king-pos) 35 | (black-king-pos) 36 | (white-king-moved) 37 | (black-king-moved)) 38 | 39 | 40 | ;;; Player 41 | (defstruct player 42 | (type) ;; 'auto, 'manual 43 | (color)) ;; 'white, 'black 44 | 45 | 46 | (defun copy-board (board) 47 | (make-board :board (copy-board-array (board-board board)) 48 | :whites (copy-board-array (board-whites board)) 49 | :blacks (copy-board-array (board-blacks board)) 50 | :whites-enpass (copy-board-row 51 | (board-whites-enpass board)) 52 | :blacks-enpass (copy-board-row 53 | (board-blacks-enpass board)) 54 | :whites-initial-pos 55 | (board-whites-initial-pos board) 56 | :white-king-pos 57 | (copy-pos (board-white-king-pos board)) 58 | :black-king-pos 59 | (copy-pos (board-black-king-pos board)) 60 | :white-king-moved (board-white-king-moved board) 61 | :black-king-moved (board-black-king-moved board))) 62 | 63 | (defun copy-board-array (board-array) 64 | (let ((new-board-array (make-array '(8 8)))) 65 | (dotimes (row 8) 66 | (dotimes (col 8) 67 | (setf (aref new-board-array row col) 68 | (aref board-array row col)))) 69 | new-board-array)) 70 | 71 | (defun copy-board-row (row-array) 72 | (let ((new-row-array (make-array 8))) 73 | (dotimes (i 8 new-row-array) 74 | (setf (aref new-row-array i) 75 | (aref row-array i))))) 76 | 77 | 78 | (defun invert-color (color) 79 | (if (eq color 'white) 'black 'white)) 80 | 81 | (defun whitep (color) 82 | (eq color 'white)) 83 | 84 | ;;; Is a given position occupied by a color? 85 | (defun color-occupied-p (board-color row col) 86 | (eq (aref board-color row col) '1)) 87 | 88 | ;;; Position 89 | (defstruct pos 90 | (row) 91 | (col)) 92 | 93 | (defun equal-pos (pos1 pos2) 94 | (and (eq (pos-row pos1) (pos-row pos2)) 95 | (eq (pos-col pos1) (pos-col pos2)))) 96 | 97 | 98 | ;;; Creates a new pos given row and column and adds to the list 99 | (defmacro add-pos (pos-list row col) 100 | `(setf ,pos-list (cons (make-pos :row ,row :col ,col) ,pos-list))) 101 | 102 | 103 | ;;; A movement 104 | (defstruct move 105 | (from) 106 | (to)) 107 | 108 | (defun equal-moves (mov1 mov2) 109 | (and (equal-pos (move-from mov1) (move-from mov2)) 110 | (equal-pos (move-to mov1) (move-to mov2)))) 111 | 112 | 113 | ;;; A very used construction: a cell from a 8x8 vector 114 | (defmacro board-pos (board pos) 115 | `(aref ,board (pos-row ,pos) (pos-col ,pos))) 116 | 117 | 118 | 119 | ;;; Creates an initial board with the white pieces at the bottom 120 | (defun create-initial-board-white-bottom () 121 | (make-board :board (make-array '(8 8) 122 | :initial-contents 123 | '((TN CN AN DN RN AN CN TN) 124 | (PN PN PN PN PN PN PN PN) 125 | (VV VV VV VV VV VV VV VV) 126 | (VV VV VV VV VV VV VV VV) 127 | (VV VV VV VV VV VV VV VV) 128 | (VV VV VV VV VV VV VV VV) 129 | (PB PB PB PB PB PB PB PB) 130 | (TB CB AB DB RB AB CB TB))) 131 | :whites (make-array '(8 8) 132 | :initial-contents 133 | '((0 0 0 0 0 0 0 0) 134 | (0 0 0 0 0 0 0 0) 135 | (0 0 0 0 0 0 0 0) 136 | (0 0 0 0 0 0 0 0) 137 | (0 0 0 0 0 0 0 0) 138 | (0 0 0 0 0 0 0 0) 139 | (1 1 1 1 1 1 1 1) 140 | (1 1 1 1 1 1 1 1))) 141 | :blacks (make-array '(8 8) 142 | :initial-contents 143 | '((1 1 1 1 1 1 1 1) 144 | (1 1 1 1 1 1 1 1) 145 | (0 0 0 0 0 0 0 0) 146 | (0 0 0 0 0 0 0 0) 147 | (0 0 0 0 0 0 0 0) 148 | (0 0 0 0 0 0 0 0) 149 | (0 0 0 0 0 0 0 0) 150 | (0 0 0 0 0 0 0 0))) 151 | :whites-enpass (make-array 152 | 8 153 | :initial-contents '(0 0 0 0 0 0 0 0)) 154 | :blacks-enpass (make-array 155 | 8 156 | :initial-contents '(0 0 0 0 0 0 0 0)) 157 | :whites-initial-pos 'bottom 158 | :white-king-pos (make-pos :row 7 :col 4) 159 | :black-king-pos (make-pos :row 0 :col 4) 160 | :white-king-moved nil 161 | :black-king-moved nil)) 162 | 163 | 164 | ;;; Creates an initial board with the white pieces at the top 165 | (defun create-initial-board-white-top () 166 | (make-board :board (make-array '(8 8) 167 | :initial-contents 168 | '((TB CB AB RB DB AB CB TB) 169 | (PB PB PB PB PB PB PB PB) 170 | (VV VV VV VV VV VV VV VV) 171 | (VV VV VV VV VV VV VV VV) 172 | (VV VV VV VV VV VV VV VV) 173 | (VV VV VV VV VV VV VV VV) 174 | (PN PN PN PN PN PN PN PN) 175 | (TN CN AN RN DN AN CN TN))) 176 | :whites (make-array '(8 8) 177 | :initial-contents 178 | '((1 1 1 1 1 1 1 1) 179 | (1 1 1 1 1 1 1 1) 180 | (0 0 0 0 0 0 0 0) 181 | (0 0 0 0 0 0 0 0) 182 | (0 0 0 0 0 0 0 0) 183 | (0 0 0 0 0 0 0 0) 184 | (0 0 0 0 0 0 0 0) 185 | (0 0 0 0 0 0 0 0))) 186 | :blacks (make-array '(8 8) 187 | :initial-contents 188 | '((0 0 0 0 0 0 0 0) 189 | (0 0 0 0 0 0 0 0) 190 | (0 0 0 0 0 0 0 0) 191 | (0 0 0 0 0 0 0 0) 192 | (0 0 0 0 0 0 0 0) 193 | (0 0 0 0 0 0 0 0) 194 | (1 1 1 1 1 1 1 1) 195 | (1 1 1 1 1 1 1 1))) 196 | :whites-enpass (make-array 197 | 8 198 | :initial-contents '(0 0 0 0 0 0 0 0)) 199 | :blacks-enpass (make-array 200 | 8 201 | :initial-contents '(0 0 0 0 0 0 0 0)) 202 | :whites-initial-pos 'top 203 | :white-king-pos (make-pos :row 0 :col 3) 204 | :black-king-pos (make-pos :row 7 :col 3) 205 | :white-king-moved nil 206 | :black-king-moved nil)) 207 | 208 | 209 | ;;; Creates an initial test board 210 | (defun create-initial-board-tests () 211 | (make-board :board (make-array '(8 8) 212 | :initial-contents 213 | '((VV VV VV VV VV VV RN VV) 214 | (VV VV DB VV VV VV VV VV) 215 | (VV VV VV VV RB VV VV VV) 216 | (VV VV VV VV VV VV VV PN) 217 | (VV VV VV VV VV VV VV PB) 218 | (VV VV VV VV VV VV VV VV) 219 | (VV VV VV VV VV PB VV VV) 220 | (VV VV VV VV VV VV VV VV))) 221 | :whites (make-array '(8 8) 222 | :initial-contents 223 | '((0 0 0 0 0 0 0 0) 224 | (0 0 1 0 0 0 0 0) 225 | (0 0 0 0 1 0 0 0) 226 | (0 0 0 0 0 0 0 0) 227 | (0 0 0 0 0 0 0 1) 228 | (0 0 0 0 0 0 0 0) 229 | (0 0 0 0 0 1 0 0) 230 | (0 0 0 0 0 0 0 0))) 231 | :blacks (make-array '(8 8) 232 | :initial-contents 233 | '((0 0 0 0 0 0 1 0) 234 | (0 0 0 0 0 0 0 0) 235 | (0 0 0 0 0 0 0 0) 236 | (0 0 0 0 0 0 0 1) 237 | (0 0 0 0 0 0 0 0) 238 | (0 0 0 0 0 0 0 0) 239 | (0 0 0 0 0 0 0 0) 240 | (0 0 0 0 0 0 0 0))) 241 | :whites-enpass (make-array 242 | 8 243 | :initial-contents '(0 0 0 0 0 0 0 0)) 244 | :blacks-enpass (make-array 245 | 8 246 | :initial-contents '(0 0 0 0 0 0 0 0)) 247 | :whites-initial-pos 'bottom 248 | :white-king-pos (make-pos :row 2 :col 4) 249 | :black-king-pos (make-pos :row 0 :col 6) 250 | :white-king-moved t 251 | :black-king-moved t)) 252 | -------------------------------------------------------------------------------- /moves/pawn.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; pawn.lisp 4 | ;;; Pawn movements 5 | ;;; gamallo, March 22, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | ;;; Places to which a pawn can move whitout atacking 29 | (defun free-pos-pawn (board pos) 30 | (let ((positions '())) 31 | ;; White pieces at the bottom 32 | (if (eq (board-whites-initial-pos board) 'bottom) 33 | ;; The pawn is white 34 | (if (eq (board-pos (board-board board) pos) 'PB) 35 | (progn 36 | ;; We are NOT at the upper border 37 | (when (not (= (pos-row pos) 0)) 38 | (add-pos positions 39 | (1- (pos-row pos)) 40 | (pos-col pos))) 41 | ;; We are in the initial pos: we can advance two 42 | ;; cells if way is clear 43 | (when (and (= (pos-row pos) 6) 44 | (eq (aref (board-board board) 45 | 5 46 | (pos-col pos)) 47 | 'VV)) 48 | (add-pos positions 4 (pos-col pos)))) 49 | ;; The pawn is black 50 | (progn 51 | ;; We are NOT at the lower border 52 | (when (not (= (pos-row pos) 7)) 53 | (add-pos positions 54 | (1+ (pos-row pos)) 55 | (pos-col pos))) 56 | ;; We are int the initial pos: we can advance two 57 | ;; cells if the way is clear 58 | (when (and (= (pos-row pos) 1) 59 | (eq (aref (board-board board) 60 | 2 61 | (pos-col pos)) 62 | 'VV)) 63 | (add-pos positions 3 (pos-col pos))))) 64 | ;; White pieces at the top 65 | ;; Pawn is white 66 | (if (eq (board-pos (board-board board) pos) 'PB) 67 | (progn 68 | ;; We are not at the lower border 69 | (when (not (= (pos-row pos) 7)) 70 | (add-pos positions 71 | (1+ (pos-row pos)) 72 | (pos-col pos))) 73 | ;; We are in the initial pos: we can advance two 74 | ;; cells if way is clear 75 | (when (and (= (pos-row pos) 1) 76 | (eq (aref (board-board board) 77 | 2 78 | (pos-col pos)) 79 | 'VV)) 80 | (add-pos positions 3 (pos-col pos)))) 81 | ;; Pawn is black 82 | (progn 83 | ;; We are not at the upper border 84 | (when (not (= (pos-row pos) 0)) 85 | (add-pos positions 86 | (1- (pos-row pos)) 87 | (pos-col pos))) 88 | ;; We are in the initial pos: we can advance two 89 | ;; cells if way is clear 90 | (when (and (= (pos-row pos) 6) 91 | (eq (aref (board-board board) 92 | 5 93 | (pos-col pos)) 94 | 'VV)) 95 | (add-pos positions 4 (pos-col pos)))))) 96 | positions)) 97 | 98 | 99 | 100 | 101 | ;;; Places an ataccking pawn can move to 102 | (defun attack-pos-pawn (board pos) 103 | (let ((positions '())) 104 | ;; White pieces at the bottom 105 | (if (eq (board-whites-initial-pos board) 'bottom) 106 | ;; Pawn is white 107 | (if (eq (board-pos (board-board board) pos) 'PB) 108 | ;; We are not at the upper border 109 | (when (not (= (pos-row pos) 0)) 110 | ;; We are not at the left border 111 | (when (not (= (pos-col pos) 0)) 112 | (add-pos positions 113 | (1- (pos-row pos)) 114 | (1- (pos-col pos)))) 115 | ;; We are not at the right border 116 | (when (not (= (pos-col pos) 7)) 117 | (add-pos positions 118 | (1- (pos-row pos)) 119 | (1+ (pos-col pos))))) 120 | ;; The pawn is black 121 | ;; We are not at the lower border 122 | (when (not (= (pos-row pos) 7)) 123 | ;; We are not at the left border 124 | (when (not (= (pos-col pos) 0)) 125 | (add-pos positions 126 | (1+ (pos-row pos)) 127 | (1- (pos-col pos)))) 128 | ;; We are not at the right border 129 | (when (not (= (pos-col pos) 7)) 130 | (add-pos positions 131 | (1+ (pos-row pos)) 132 | (1+ (pos-col pos)))))) 133 | ;; White pieces at the top 134 | ;; Pawn is white 135 | (if (eq (board-pos (board-board board) pos) 'PB) 136 | ;; We are not at the lower border 137 | (when (not (= (pos-row pos) 7)) 138 | ;; We are not at the left border 139 | (when (not (= (pos-col pos) 0)) 140 | (add-pos positions 141 | (1+ (pos-row pos)) 142 | (1- (pos-col pos)))) 143 | ;; We are not at the right border 144 | (when (not (= (pos-col pos) 7)) 145 | (add-pos positions 146 | (1+ (pos-row pos)) 147 | (1+ (pos-col pos))))) 148 | ;; Pawn is black 149 | ;; We are not at the upper border 150 | (when (not (= (pos-row pos) 0)) 151 | ;; We are not at the left border 152 | (when (not (= (pos-col pos) 0)) 153 | (add-pos positions 154 | (1- (pos-row pos)) 155 | (1- (pos-col pos)))) 156 | ;; We are not at the right border 157 | (when (not (= (pos-col pos) 7)) 158 | (add-pos positions 159 | (1- (pos-row pos)) 160 | (1+ (pos-col pos))))))) 161 | positions)) 162 | 163 | 164 | 165 | 166 | ;;; Places to which a pawn can move. 167 | (defun possible-free-pawn (board pos positions) 168 | (dolist (a-pos (free-pos-pawn board pos)) 169 | (when (eq (board-pos (board-board board) a-pos) 'VV) 170 | (setf positions (cons a-pos positions)))) 171 | positions) 172 | 173 | 174 | 175 | 176 | ;;; Places to which an attacking pawn can move. 177 | ;;; NOTE: We take into account enpases 178 | (defun possible-attack-pawn (board pos positions) 179 | (if (eq (board-pos (board-board board) pos) 'PB) 180 | ;; Pawn is white 181 | (dolist (a-pos (attack-pos-pawn board pos)) 182 | (cond ((color-occupied-p (board-blacks board) 183 | (pos-row a-pos) 184 | (pos-col a-pos)) 185 | ;; Dest pos is occupied by a black piece 186 | (setf positions (cons a-pos positions))) 187 | ((and (eq (pos-row a-pos) 2) 188 | (eq (board-whites-initial-pos board) 'bottom) 189 | (eq (aref (board-blacks-enpass board) 190 | (pos-col a-pos)) 191 | '1)) 192 | ;; White pieces are at the bottom, the pawn moves to the row 193 | ;; of enpass captures and, yes, there is a pawn that can be 194 | ;; enpass-captured 195 | (setf positions (cons a-pos positions))) 196 | ((and (eq (pos-row a-pos) 5) 197 | (eq (board-whites-initial-pos board) 'top) 198 | (eq (aref (board-blacks-enpass board) 199 | (pos-col a-pos)) 200 | '1)) 201 | ;; White pieces are at the top, the pawn moves to the row 202 | ;; of enpass captures and, yes, there is a pawn that can be 203 | ;; enpass-captured 204 | (setf positions (cons a-pos positions))))) 205 | 206 | ;; Pawn is black 207 | (dolist (a-pos (attack-pos-pawn board pos)) 208 | (cond ((color-occupied-p (board-whites board) 209 | (pos-row a-pos) 210 | (pos-col a-pos)) 211 | ;; Dest pos is occupied by a white piece 212 | (setf positions (cons a-pos positions))) 213 | ((and (eq (pos-row a-pos) 5) 214 | (eq (board-whites-initial-pos board) 'bottom) 215 | (eq (aref (board-whites-enpass board) 216 | (pos-col a-pos)) 217 | '1)) 218 | ;; White pieces are at the bottom, the pawn moves to the row 219 | ;; of enpass captures and, yes, there is a pawn that can be 220 | ;; enpass-captured 221 | (setf positions (cons a-pos positions))) 222 | ((and (eq (pos-row a-pos) 2) 223 | (eq (board-whites-initial-pos board) 'top) 224 | (eq (aref (board-whites-enpass board) 225 | (pos-col a-pos)) 226 | '1)) 227 | ;; White pieces are at the top, the pawn moves to the row 228 | ;; of enpass captures and, yes, there is a pawn that can be 229 | ;; enpass-captured 230 | (setf positions (cons a-pos positions)))))) 231 | positions) 232 | 233 | 234 | 235 | 236 | 237 | (defun possible-pawn (board pos) 238 | (incf *calls*) 239 | (let ((positions '())) 240 | (setf positions (possible-attack-pawn board pos positions)) 241 | (setf positions (possible-free-pawn board pos positions)) 242 | positions)) 243 | -------------------------------------------------------------------------------- /moves/moves.lisp: -------------------------------------------------------------------------------- 1 | ;;; -*- encoding: utf-8 -*- 2 | ;;; 3 | ;;; moves.lisp 4 | ;;; Movements of the pieces 5 | ;;; gamallo, March 8, 2007 6 | ;;; 7 | 8 | ;; Copyright 2007, 2008, 2009 Manuel Felipe Gamallo Rivero. 9 | 10 | ;; This file is part of Miguedrez. 11 | 12 | ;; Miguedrez 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. 16 | 17 | ;; Miguedrez 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. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with Miguedrez. If not, see . 24 | 25 | 26 | (in-package :miguedrez) 27 | 28 | ;;; This function executes a move on a board 29 | ;;; It DOES NOT CHECK that the dest pos is void, that is job for 30 | ;;; check-move. 31 | (defun execute-move (board move) 32 | (execute-move-enpass board move) 33 | (execute-move-board board move) 34 | (if (eq (board-pos (board-whites board) 35 | (move-from move)) 36 | '1) 37 | (progn 38 | (execute-move-white board move) 39 | (when (equal-pos (board-white-king-pos board) 40 | (move-from move)) 41 | (setf (board-white-king-pos board) 42 | (move-to move)) 43 | (when (not (board-white-king-moved board)) 44 | (setf (board-white-king-moved board) t) 45 | ;; We check if it's a castling and, in that case, 46 | ;; we move the corresponding rook 47 | (if (eq (board-whites-initial-pos board) 'bottom) 48 | (cond ((equal-pos 49 | (move-to move) 50 | (make-pos :row 7 :col 6)) 51 | (execute-move 52 | board 53 | (make-move 54 | :from (make-pos :row 7 :col 7) 55 | :to (make-pos :row 7 :col 5)))) 56 | ((equal-pos 57 | (move-to move) 58 | (make-pos :row 7 :col 2)) 59 | (execute-move 60 | board 61 | (make-move 62 | :from (make-pos :row 7 :col 0) 63 | :to (make-pos :row 7 :col 3))))) 64 | (cond ((equal-pos 65 | (move-to move) 66 | (make-pos :row 0 :col 5)) 67 | (execute-move 68 | board 69 | (make-move 70 | :from (make-pos :row 0 :col 7) 71 | :to (make-pos :row 0 :col 4)))) 72 | ((equal-pos 73 | (move-to move) 74 | (make-pos :row 0 :col 1)) 75 | (execute-move 76 | board 77 | (make-move 78 | :from (make-pos :row 0 :col 0) 79 | :to (make-pos :row 0 :col 2)))))))) 80 | (queen-if-promotion board move)) 81 | (progn 82 | (execute-move-black board move) 83 | (when (equal-pos (board-black-king-pos board) 84 | (move-from move)) 85 | (setf (board-black-king-pos board) 86 | (move-to move)) 87 | (when (not (board-black-king-moved board)) 88 | (setf (board-black-king-moved board) t) 89 | ;; We check if it's a castling and, in that case, 90 | ;; move the corresponding rook 91 | (if (eq (board-whites-initial-pos board) 'top) 92 | (cond ((equal-pos 93 | (move-to move) 94 | (make-pos :row 7 :col 5)) 95 | (execute-move 96 | board 97 | (make-move 98 | :from (make-pos :row 7 :col 7) 99 | :to (make-pos :row 7 :col 4)))) 100 | ((equal-pos 101 | (move-to move) 102 | (make-pos :row 7 :col 1)) 103 | (execute-move 104 | board 105 | (make-move 106 | :from (make-pos :row 7 :col 0) 107 | :to (make-pos :row 7 :col 2))))) 108 | (cond ((equal-pos 109 | (move-to move) 110 | (make-pos :row 0 :col 6)) 111 | (execute-move 112 | board 113 | (make-move 114 | :from (make-pos :row 0 :col 7) 115 | :to (make-pos :row 0 :col 5)))) 116 | ((equal-pos 117 | (move-to move) 118 | (make-pos :row 0 :col 2)) 119 | (execute-move 120 | board 121 | (make-move 122 | :from (make-pos :row 0 :col 0) 123 | :to (make-pos :row 0 :col 1)))))))) 124 | (queen-if-promotion board move))) 125 | board) 126 | 127 | 128 | 129 | 130 | 131 | ;;; Function to move a piece on the main board 132 | (defun execute-move-board (board move) 133 | (setf (board-pos (board-board board) (move-to move)) 134 | (board-pos (board-board board) (move-from move))) 135 | (setf (board-pos (board-board board) (move-from move)) 136 | 'VV)) 137 | 138 | 139 | 140 | 141 | 142 | ;;; Function to move a piece in the white pieces board 143 | (defun execute-move-white (board move) 144 | (setf (board-pos (board-whites board) (move-to move)) '1) 145 | (setf (board-pos (board-whites board) (move-from move)) '0) 146 | (when (eq (board-pos (board-blacks board) (move-to move)) '1) 147 | (setf (board-pos (board-blacks board) (move-to move)) '0))) 148 | 149 | 150 | 151 | 152 | 153 | ;;; Function to move a piece in the black pieces board 154 | (defun execute-move-black (board move) 155 | (setf (board-pos (board-blacks board) 156 | (move-to move)) 157 | '1) 158 | (setf (board-pos (board-blacks board) 159 | (move-from move)) 160 | '0) 161 | (when (eq (board-pos (board-whites board) 162 | (move-to move)) 163 | '1) 164 | (setf (board-pos (board-whites board) 165 | (move-to move)) 166 | '0))) 167 | 168 | 169 | 170 | 171 | (defun queen-if-promotion (board move) 172 | (cond ((and (or (eq (pos-row (move-to move)) '7) 173 | (eq (pos-row (move-to move)) '0)) 174 | (eq (board-pos (board-board board) (move-to move)) 'PB)) 175 | (setf (board-pos (board-board board) (move-to move)) 'DB)) 176 | ((and (or (eq (pos-row (move-to move)) '7) 177 | (eq (pos-row (move-to move)) '0)) 178 | (eq (board-pos (board-board board) (move-to move)) 'PN)) 179 | (setf (board-pos (board-board board) (move-to move)) 'DN)))) 180 | 181 | 182 | 183 | 184 | 185 | ;;; Function that takes care of the enpass arrays when moving 186 | (defun execute-move-enpass (board move) 187 | (if (eq (board-whites-initial-pos board) 'bottom) 188 | ;; White pieces at the bottom 189 | 190 | (let ((a-board (board-board board)) 191 | (from (move-from move)) 192 | (to (move-to move))) 193 | (cond ((and (eq (board-pos a-board from) 'PN) 194 | (eq (pos-row to) 5) 195 | (eq (aref (board-whites-enpass board) (pos-col to)) '1)) 196 | ;; We are capturing a white pawn in the enpass position 197 | (setf (aref a-board 4 (pos-col to)) 'VV) 198 | (setf (aref (board-whites board) 4 (pos-col to)) '0)) 199 | ((and (eq (board-pos a-board from) 'PB) 200 | (eq (pos-row to) 2) 201 | (eq (aref (board-blacks-enpass board) (pos-col to)) '1)) 202 | ;; We are capturing a black pawn in the enpass position 203 | (setf (aref a-board 3 (pos-col to)) 'VV) 204 | (setf (aref (board-blacks board) 3 (pos-col to)) '0))) 205 | 206 | (setf (board-whites-enpass board) 207 | (make-array 8 :initial-contents '(0 0 0 0 0 0 0 0))) 208 | (setf (board-blacks-enpass board) 209 | (make-array 8 :initial-contents '(0 0 0 0 0 0 0 0))) 210 | 211 | (cond ((eq (board-pos a-board from) 'PB) 212 | ;; The piece we are moving is a white pawn 213 | (if (and (eq (pos-row from) '6) 214 | (eq (pos-row to) '4)) 215 | ;; The pawn is in its initial position and we want to 216 | ;; advance two cells 217 | (setf (aref (board-whites-enpass board) (pos-col to)) '1))) 218 | 219 | ((eq (board-pos a-board from) 'PN) 220 | ;; The piece we are moving is a black pawn 221 | (if (and (eq (pos-row from) '1) 222 | (eq (pos-row to) '3)) 223 | ;; The pawn is in its initial position and we want to 224 | ;; advance two cells 225 | (setf (aref (board-blacks-enpass board) (pos-col to)) '1))))) 226 | 227 | 228 | ;; White pieces at the top 229 | (let ((a-board (board-board board)) 230 | (from (move-from move)) 231 | (to (move-to move))) 232 | 233 | (cond ((and (eq (board-pos a-board from) 'PN) 234 | (eq (pos-row to) 2) 235 | (eq (aref (board-whites-enpass board) (pos-col to)) '1)) 236 | ;; We are capturing a white pawn in the enpass position 237 | (setf (aref a-board 3 (pos-col to)) 'VV) 238 | (setf (aref (board-whites board) 3 (pos-col to)) '0)) 239 | ((and (eq (board-pos a-board from) 'PB) 240 | (eq (pos-row to) 5) 241 | (eq (aref (board-blacks-enpass board) (pos-col to)) '1)) 242 | ;; We are capturing a black pawn in the enpass position 243 | (setf (aref a-board 4 (pos-col to)) 'VV) 244 | (setf (aref (board-blacks board) 4 (pos-col to)) '0))) 245 | 246 | (setf (board-whites-enpass board) 247 | (make-array 8 :initial-contents '(0 0 0 0 0 0 0 0))) 248 | (setf (board-blacks-enpass board) 249 | (make-array 8 :initial-contents '(0 0 0 0 0 0 0 0))) 250 | 251 | 252 | (cond ((eq (board-pos a-board from) 'PB) 253 | ;; We are about to move a white pawn 254 | (if (and (eq (pos-row from) '1) 255 | (eq (pos-row to) '3)) 256 | ;; The pawn is in its initial position, and we want to 257 | ;; advance two cells 258 | (setf (aref (board-whites-enpass board) (pos-col to)) '1))) 259 | 260 | ((eq (aref a-board (pos-row from) (pos-col from)) 'PN) 261 | ;; We are about to move a black pawn 262 | (if (and (eq (pos-row from) '6) 263 | (eq (pos-row to) '4)) 264 | ;; The pawn is in its initial position and we want to 265 | ;; advance two cells 266 | (setf (aref (board-blacks-enpass board) (pos-col to)) '1))))))) 267 | 268 | 269 | 270 | 271 | (defun children-piece (board pos &key castle) 272 | (let ((children '()) 273 | (piece (board-pos (board-board board) pos))) 274 | (cond ((or (eq piece 'PB) (eq piece 'PN)) 275 | (setf children (possible-pawn board pos))) 276 | ((or (eq piece 'CB) (eq piece 'CN)) 277 | (setf children (possible-knight board pos))) 278 | ((or (eq piece 'TB) (eq piece 'TN)) 279 | (setf children (possible-rook board pos))) 280 | ((or (eq piece 'AB) (eq piece 'AN)) 281 | (setf children (possible-bishop board pos))) 282 | ((or (eq piece 'DB) (eq piece 'DN)) 283 | (setf children (possible-queen board pos))) 284 | ((or (eq piece 'RB) (eq piece 'RN)) 285 | (setf children 286 | (if (eq castle 'without-castling) 287 | (possible-king-without-castling board pos) 288 | (possible-king board pos))))) 289 | (mapcar #'(lambda (to) 290 | (make-move :from pos :to to)) children))) 291 | 292 | 293 | 294 | (defun children (board color &key castle) 295 | (let ((board-color (if (whitep color) 296 | (board-whites board) 297 | (board-blacks board))) 298 | (children '())) 299 | (dotimes (row 8 children) 300 | (dotimes (col 8) 301 | (when (color-occupied-p board-color row col) 302 | (let ((current-pos (make-pos :row row 303 | :col col))) 304 | (setf children (nconc children 305 | (children-piece 306 | board 307 | current-pos 308 | :castle castle))))))))) 309 | 310 | 311 | 312 | 313 | ;;; Is this position threatened by this color? 314 | (defun pos-threatened (a-board a-pos a-color) 315 | (member a-pos 316 | (mapcar #'move-to 317 | (children a-board 318 | (invert-color a-color) 319 | :castle 'without-castling)) 320 | :test #'equal-pos)) 321 | 322 | (defun valid (board move color) 323 | (let ((piece (if (eq color 'white) 'RB 'RN))) 324 | (and (member move (children board color) 325 | :test #'equal-moves) 326 | (king-valid board move color)))) 327 | 328 | (defun king-valid (board move color) 329 | (let ((new-board (copy-board board))) 330 | (execute-move new-board move) 331 | (null (king-threatened new-board color)))) 332 | 333 | (defun king-threatened (board color) 334 | (pos-threatened board 335 | (if (eq color 'white) 336 | (board-white-king-pos board) 337 | (board-black-king-pos board)) 338 | color)) 339 | 340 | (defun checkmatep (a-board a-player) 341 | (and 342 | ;; The king is threatened 343 | (king-threatened a-board (player-color a-player)) 344 | ;; For all of the children for the king, he stays threatened 345 | (let ((a-pos (if (eq (player-color a-player) 'white) 346 | (board-white-king-pos a-board) 347 | (board-black-king-pos a-board))) 348 | (some-move-allowed)) 349 | (dolist (a-move 350 | (children a-board (player-color a-player) :castle 'without-castling) 351 | (not some-move-allowed)) 352 | ;; If some move is possible, we set some-move-allowed to T 353 | (let* ((new-board (execute-move (copy-board a-board) 354 | a-move)) 355 | (new-king-pos (if (eq (player-color a-player) 'white) 356 | (board-white-king-pos new-board) 357 | (board-black-king-pos new-board)))) 358 | (when (not (pos-threatened new-board 359 | new-king-pos 360 | (player-color a-player))) 361 | (setf some-move-allowed t))))))) 362 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------