Advertisement
Guest User

draw-chessboard.lisp

a guest
Feb 2nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.51 KB | None | 0 0
  1. ;;;; draw-chessboard.lisp --- Drawing a chessboard using McCLIM
  2. ;;;; Screenshot: https://i.imgur.com/6Dne4jz.png
  3.  
  4. (ql:quickload "mcclim")
  5. ;; (asdf:load-system :mcclim)
  6. ;; (require :mcclim)
  7.  
  8. (defpackage "ODAI-PT12-840-CHESSBOARD"
  9.   (:use "CLIM" "CLIM-LISP"))
  10. (in-package "ODAI-PT12-840-CHESSBOARD")
  11.  
  12. (defconstant square-size 50)
  13.  
  14. (defconstant max-file 8)
  15. (defconstant max-rank 8)
  16. (defconstant canvas-width
  17.   (+ (* square-size max-file) (* square-size 2)))
  18. (defconstant canvas-height
  19.   (+ (* square-size max-rank) (* square-size 2)))
  20.  
  21. (defconstant light-square-color clim:+blanched-almond+)
  22. (defconstant dark-square-color
  23.   (make-rgb-color (/ 91 255) (/ 58 255) (/ 49 255)))
  24. (defconstant canvas-background-color clim:+saddle-brown+)
  25. (defconstant coordinates-text-color clim:+gray10+)
  26.  
  27. (define-application-frame chessboard ()
  28.   ()
  29.   (:menu-bar nil)
  30.   (:panes (canvas :application-pane
  31.                   :width canvas-width
  32.                   :height canvas-height
  33.                   :display-function 'draw-canvas))
  34.   (:layouts (default
  35.              (vertically ()
  36.                canvas))))
  37.  
  38. (defun draw-square* (sheet x y &key (background-color clim:+light-grey+)
  39.                                     (text-color clim:+black+)
  40.                                     character)
  41.   (draw-rectangle* sheet
  42.                    #1=(* x square-size)
  43.                    #2=(* y square-size)
  44.                    (+ #1# square-size)
  45.                    (+ #2# square-size)
  46.                    :filled t
  47.                    :ink background-color)
  48.   (when character
  49.     (draw-text* sheet
  50.                 (string character)
  51.                 (+ (* x square-size) (/ square-size 2))
  52.                 (+ (* y square-size) (/ square-size 2))
  53.                 :align-x :center
  54.                 :align-y :center
  55.                 :text-size (/ square-size 2)
  56.                 :ink text-color)))
  57.  
  58. (defun draw-canvas (frame sheet)
  59.   (declare (ignore frame))
  60.   (draw-rectangle* sheet
  61.                    0 0 canvas-width canvas-height
  62.                    :filled t
  63.                    :ink canvas-background-color)
  64.   (loop for index from 0
  65.         for x from 0 upto max-file
  66.         do (loop for y from 0 upto (1+ max-rank)
  67.                  do (cond ((and (= x 0)
  68.                                 (> y 0))
  69.                            (draw-square* sheet x y
  70.                                          :background-color canvas-background-color
  71.                                          :character (aref " 123456789"
  72.                                                           (- (1+ max-rank) y))
  73.                                          :text-color coordinates-text-color))
  74.                           ((and (> x 0)
  75.                                 (= y (1+ max-rank)))
  76.                            (draw-square* sheet x y
  77.                                          :background-color canvas-background-color
  78.                                          :character (aref " abcdefghi" x)
  79.                                          :text-color coordinates-text-color))
  80.                           ((and (< 0 x (1+ max-file))
  81.                                 (< 0 y (1+ max-rank)))
  82.                            (draw-square* sheet x y
  83.                                          :background-color
  84.                                          (if (oddp (+ index y))
  85.                                              dark-square-color
  86.                                              light-square-color)))))))
  87.  
  88. (run-frame-top-level (make-application-frame 'chessboard :pretty-name "Chessboard"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement