chessboard.scm 7.88 KB
Newer Older
1
; AisleRiot - chessboard.scm
2
; Copyright (C) 2001, 2003 Rosanna Yuen <zana@webwynk.net>
3
;
4
; This program is free software: you can redistribute it and/or modify
5
; it under the terms of the GNU General Public License as published by
6 7
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
8 9 10 11 12 13 14
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
15
; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

17 18
(use-modules (aisleriot interface) (aisleriot api))

19 20
(def-save-var BASE-VAL 0)
(def-save-var base-set? #f)
21 22 23 24 25 26 27 28 29

(define (new-game)
  (initialize-playing-area)
  (set-ace-low)
  (make-standard-deck)
  (shuffle-deck)

  (set! base-set? #f)

30
  (add-extended-slot '() right 'tableau)
31 32 33 34 35 36

  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
37
  (add-extended-slot '() right 'tableau)
38 39 40

  (add-carriage-return-slot)

41
  (add-extended-slot '() right 'tableau)
42 43 44
  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
45
  (add-normal-slot DECK 'foundation)
46
  (add-blank-slot)
47
  (add-extended-slot '() right 'tableau)
48 49 50

  (add-carriage-return-slot)

51
  (add-extended-slot '() right 'tableau)
52 53 54
  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
55
  (add-normal-slot '() 'foundation)
56
  (add-blank-slot)
57
  (add-extended-slot '() right 'tableau)
58 59 60

  (add-carriage-return-slot)

61
  (add-extended-slot '() right 'tableau)
62 63 64
  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
65
  (add-normal-slot '() 'foundation)
66
  (add-blank-slot)
67
  (add-extended-slot '() right 'tableau)
68 69
  (add-carriage-return-slot)

70
  (add-extended-slot '() right 'tableau)
71 72 73
  (add-blank-slot)
  (add-blank-slot)
  (add-blank-slot)
74
  (add-normal-slot '() 'foundation)
75
  (add-blank-slot)
76
  (add-extended-slot '() right 'tableau)
77 78 79 80 81 82 83 84 85

  (deal-cards-face-up 3 '(0 2 5 8 11 1 4 7 10 13 0 2 5 8 11 1 4 7 10 13 0 2 5 8 11 1 4 7 10 13 0 2 5 8 11 1 4 7 10 13 0 2 5 8 11 1 4 7 10 13 0 1))

  (give-status-message)

  (list 10 5))

(define (give-status-message)
  (if (not base-set?)
86
      (set-statusbar-message " ")
87 88 89 90 91
      (set-statusbar-message (get-base-string))))

(define (get-base-string)
  (cond ((and (> BASE-VAL 1)
	      (< BASE-VAL 11))
92
	 (string-append (_"Base Card: ") (number->string BASE-VAL)))
93
	((= BASE-VAL 1)
94
	 (_"Base Card: Ace"))
95
	((= BASE-VAL 11)
96
	 (_"Base Card: Jack"))
97
	((= BASE-VAL 12)
98
	 (_"Base Card: Queen"))
99
	((= BASE-VAL 13)
100
	 (_"Base Card: King"))
101
	(#t "")))
102 103 104 105 106 107 108 109 110

(define (button-pressed slot-id card-list)
  (and (not (empty-slot? slot-id))
       (not (= slot-id 3))
       (not (= slot-id 6))
       (not (= slot-id 9))
       (not (= slot-id 12))
       (= (length card-list) 1)))

111 112 113
(define (droppable? start-slot card-list end-slot)
  (cond ((= start-slot end-slot) #f)
        ((member end-slot '(3 6 9 12))
114
	 (and (or (and (empty-slot? end-slot)
115
		       (or (not base-set?)
116 117 118 119 120 121 122
			   (= (get-value (car card-list)) BASE-VAL)))
		  (and (not (empty-slot? end-slot))
		       (= (get-suit (get-top-card end-slot))
			  (get-suit (car card-list)))
		       (or (= (get-value (car card-list))
			      (+ 1 (get-value (get-top-card end-slot))))
			   (and (= (get-value (car card-list)) ace)
123 124
				(= (get-value (get-top-card end-slot)) king)))))))
	(#t (or (empty-slot? end-slot)
125 126 127 128 129 130 131 132 133
	     (and (= (get-suit (get-top-card end-slot))
		     (get-suit (car card-list)))
		  (or (= (get-value (get-top-card end-slot))
			 (+ 1 (get-value (car card-list))))
		      (and (= (get-value (get-top-card end-slot)) king)
			   (= (get-value (car card-list)) ace))
		      (and (= (get-value (get-top-card end-slot)) ace)
			   (= (get-value (car card-list)) king))
		      (= (+ 1 (get-value (get-top-card end-slot)))
134 135 136 137 138 139 140 141 142 143
			 (get-value (car card-list)))))))))

(define (button-released start-slot card-list end-slot)
  (and (droppable? start-slot card-list end-slot)
       (move-n-cards! start-slot end-slot card-list)
       (or (not (member end-slot '(3 6 9 12)))
           (and (add-to-score! 1)
                (or base-set?
                    (and (set! BASE-VAL (get-value (car card-list)))
                         (set! base-set? #t)))))))
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

(define (button-clicked slot-id)
  #f)

(define (move-to-foundation slot f-slot)
  (cond ((= f-slot 15)
	 #f)
	((not base-set?)
	 (and (set! base-set? #t)
	      (set! BASE-VAL (get-value (get-top-card slot)))
	      (deal-cards slot '(3))
	      (add-to-score! 1)))
	((and (empty-slot? f-slot)
	      (= (get-value (get-top-card slot)) BASE-VAL))
	 (and (deal-cards slot (list f-slot))
	      (add-to-score! 1)))
	((and (not (empty-slot? f-slot))
	      (= (get-suit (get-top-card f-slot))
		 (get-suit (get-top-card slot)))
	      (or (and (= (get-value (get-top-card f-slot)) king)
		       (= (get-value (get-top-card slot)) ace))
		  (= (+ 1 (get-value (get-top-card f-slot)))
		     (get-value (get-top-card slot)))))
	 (and (deal-cards slot (list f-slot))
	      (add-to-score! 1)))
	(#t (move-to-foundation slot (+ 3 f-slot)))))

(define (button-double-clicked slot-id)
  (and (not (empty-slot? slot-id))
       (or (= slot-id 0)
	   (not (= (modulo slot-id 3) 0)))
       (move-to-foundation slot-id 3)))

(define (game-continuable)
  (give-status-message)
  (and (not (game-won))
       (get-hint)))

(define (game-won)
  (and (empty-slot? 0)
       (empty-slot? 1)
       (empty-slot? 2)
       (empty-slot? 4)
       (empty-slot? 5)
       (empty-slot? 7)
       (empty-slot? 8)
       (empty-slot? 10)
       (empty-slot? 11)
       (empty-slot? 13)))

(define (to-foundations? slot f-slot)
  (cond ((= slot 14)
	 #f)
	((not base-set?)
198
	 (list 0 (_"Move a card to the Foundation")))
199 200 201 202 203 204 205 206 207 208
	((or (empty-slot? slot)
	     (= slot 3)
	     (= slot 6)
	     (= slot 9)
	     (= slot 12)
	     (= f-slot 15))
	 (to-foundations? (+ 1 slot) 3))
	((and (empty-slot? f-slot)
	      (= (get-value (get-top-card slot))
		 BASE-VAL))
209
	 (hint-move slot 1 f-slot))
210 211 212 213 214 215 216
	((and (not (empty-slot? f-slot))
	      (= (get-suit (get-top-card f-slot))
		 (get-suit (get-top-card slot)))
	      (or (and (= (get-value (get-top-card slot)) ace)
		       (= (get-value (get-top-card f-slot)) king))
		  (= (get-value (get-top-card slot))
		     (+ 1 (get-value (get-top-card f-slot))))))
217
	 (hint-move slot 1 f-slot))
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	(#t (to-foundations? slot (+ 3 f-slot)))))

(define (to-tableau? slot1 slot2)
  (cond ((= slot1 14)
	 #f)
	((or (empty-slot? slot1)
	     (= slot2 14)
	     (= slot1 3)
	     (= slot1 6)
	     (= slot1 9)
	     (= slot1 12))
	 (to-tableau? (+ 1 slot1) (+ 2 slot1)))
	((and (not (or (= slot2 3)
		       (= slot2 6)
		       (= slot2 9)
		       (= slot2 12)))
	      (not (empty-slot? slot2))
	      (= (get-suit (get-top-card slot1))
		 (get-suit (get-top-card slot2)))
	      (or (= (get-value (get-top-card slot1))
		     (+ 1 (get-value (get-top-card slot2))))
		  (and (= (get-value (get-top-card slot1)) king)
		       (= (get-value (get-top-card slot2)) ace))
		  (and (= (get-value (get-top-card slot1)) ace)
		       (= (get-value (get-top-card slot2)) king))
		  (= (get-value (get-top-card slot2))
		     (+ 1 (get-value (get-top-card slot1))))))
245
	 (hint-move slot1 1 slot2))
246 247 248 249 250 251 252 253 254 255 256 257 258 259
	(#t
	 (to-tableau? slot1 (+ 1 slot2)))))

(define (empties?)
  (and (or (empty-slot? 0)
	   (empty-slot? 1)
	   (empty-slot? 2)
	   (empty-slot? 4)
	   (empty-slot? 5)
	   (empty-slot? 7)
	   (empty-slot? 8)
	   (empty-slot? 10)
	   (empty-slot? 11)
	   (empty-slot? 13))
260
       (list 0 (_"Move something into the empty Tableau slot"))))
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

(define (get-hint)
  (or (to-foundations? 0 3)
      (to-tableau? 0 1)
      (empties?)))

(define (get-options) 
  #f)

(define (apply-options options) 
  #f)

(define (timeout) 
  #f)

276 277
(set-features droppable-feature)

278 279
(set-lambda new-game button-pressed button-released button-clicked
button-double-clicked game-continuable game-won get-hint get-options
280
apply-options timeout droppable?)