eight-off.scm 7.91 KB
Newer Older
1
; AisleRiot - bakers_game.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
(def-save-var free-reserves 0)
20

21 22 23 24
(define foundation '(0 1 2 3))
(define reserve '(4 5 6 7 8 9 10 11))
(define tableau '(12 13 14 15 16 17 18 19))

25 26 27 28 29 30
(define (new-game)
  (initialize-playing-area)
  (set-ace-low)
  (make-standard-deck)
  (shuffle-deck)

31
  (add-normal-slot DECK 'foundation)
32
  (add-carriage-return-slot)
33
  (add-normal-slot '() 'foundation)
34
  (add-carriage-return-slot)
35
  (add-normal-slot '() 'foundation)
36
  (add-carriage-return-slot)
37
  (add-normal-slot '() 'foundation)
38 39


40
  (set! VERTPOS 0)
41 42

  (add-blank-slot)
43 44 45 46 47 48 49 50
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
51 52 53 54 55 56

  (add-carriage-return-slot)

  (add-blank-slot)
  (add-blank-slot)

57 58 59 60 61 62 63 64
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
  (add-extended-slot '() down 'tableau)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  (deal-cards-face-up 0 '(12 13 14 15 16 17 18 19 12 13 14 15 16 17 18
			     19 12 13 14 15 16 17 18 19 12 13 14 15 16
			     17 18 19 12 13 14 15 16 17 18 19 12 13 14 
			     15 16 17 18 19 4 5 6 7))

  (set! free-reserves 4)

  (list 10 4))

(define (button-pressed slot-id card-list)
  (and (not (empty-slot? slot-id))
       (or (= (length card-list) 1)
	   (and (> slot-id 11)
		(< (length card-list) (+ 2 free-reserves))
		(check-same-suit-list card-list)
		(check-straight-descending-list card-list)))))

83
(define (droppable? start-slot card-list end-slot)
84 85 86 87 88
  (cond ((= start-slot end-slot)
	 #f)
	((> end-slot 11)
	 (and (or (and (empty-slot? end-slot)
		       (= (get-value (car (reverse card-list)))
89
			  king))
90 91 92 93
		  (and (not (empty-slot? end-slot))
		       (= (get-suit (get-top-card end-slot))
			  (get-suit (car card-list)))
		       (= (get-value (get-top-card end-slot))
94
			  (+ 1 (get-value (car (reverse card-list)))))))))
95 96 97 98
	((and (= (length card-list) 1)
	      (empty-slot? end-slot)
	      (> end-slot 3)
	      (< end-slot 12))
99
	 #t)
100 101 102 103 104 105 106 107
	((and (= (length card-list) 1)
	      (< end-slot 4))
	 (and (or (and (empty-slot? end-slot)
		       (= (get-value (car card-list)) ace))
		  (and (not (empty-slot? end-slot))
		       (= (get-suit (get-top-card end-slot))
			  (get-suit (car card-list)))
		       (= (+ 1 (get-value (get-top-card end-slot)))
108
			  (get-value (car card-list)))))))
109 110
	(#t #f)))

111 112 113 114 115 116 117 118 119 120 121 122 123 124
(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 (< start-slot 4)
           (> start-slot 11)
           (set! free-reserves (+ free-reserves 1)))
       (or (< end-slot 4)
           (> end-slot 11)
           (set! free-reserves (- free-reserves 1)))
       (or (> start-slot 3)
           (add-to-score! -1))
       (or (> end-slot 3)
           (add-to-score! 1))))

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
(define (button-clicked slot-id)
  #f)

(define (move-to-empty-foundation slot f-slot)
  (cond ((= f-slot 4)
	 #f)
	((empty-slot? f-slot)
	 (deal-cards slot (list f-slot)))
	(#t (move-to-empty-foundation slot (+ 1 f-slot)))))

(define (move-to-foundation slot f-slot)
  (cond ((= f-slot 4)
	 #f)
	((and (not (empty-slot? f-slot))
	      (= (get-suit (get-top-card slot))
		 (get-suit (get-top-card f-slot))))
	 (and (= (get-value (get-top-card slot))
		 (+ 1 (get-value (get-top-card f-slot))))
	      (deal-cards slot (list f-slot))))
	(#t (move-to-foundation slot (+ 1 f-slot)))))

146 147 148 149 150 151 152 153 154
(define (autoplay-foundations)
  (define (autoplay-foundations-tail)
    (if (or-map button-double-clicked '(4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19))
        (delayed-call autoplay-foundations-tail)
        #t))
  (if (or-map button-double-clicked '(4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19))
      (autoplay-foundations-tail)
      #f))

155
(define (button-double-clicked slot-id)
156 157 158 159 160 161 162 163 164 165
  (if (> slot-id 3)
      (and (not (empty-slot? slot-id))
           (or (and (= (get-value (get-top-card slot-id))
		       ace)
		    (move-to-empty-foundation slot-id 0))
	       (move-to-foundation slot-id 0))
           (add-to-score! 1)
           (or (> slot-id 11)
	       (set! free-reserves (+ 1 free-reserves))))
       (autoplay-foundations)))
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

(define (game-continuable)
  (and (not (game-won))
       (get-hint)))

(define (game-won)
  (and (= (length (get-cards 0)) 13)
       (= (length (get-cards 1)) 13)
       (= (length (get-cards 2)) 13)
       (= (length (get-cards 3)) 13)))

(define (check-to-foundations? slot f-slot)
  (cond ((= slot 20)
	 #f)
	((or (empty-slot? slot)
	     (= f-slot 4))
	 (check-to-foundations? (+ 1 slot) 0))
	((= (get-value (get-top-card slot)) ace)
184
	 (hint-move slot 1 (find-empty-slot foundation)))
185 186 187 188 189
	((and (not (empty-slot? f-slot))
	      (= (get-suit (get-top-card slot))
		 (get-suit (get-top-card f-slot)))
	      (= (get-value (get-top-card slot))
		 (+ 1 (get-value (get-top-card f-slot)))))
190
	 (hint-move slot 1 f-slot))
191 192 193 194 195 196 197 198 199
	(#t (check-to-foundations? slot (+ 1 f-slot)))))

(define (check-for-king card-list iter slot)
  (cond ((= (length card-list) 0)
	 #f)
	((and (= (length card-list) 1)
	      (> slot 11))
	 #f)
	((= (get-value (car card-list)) king)
200
	 #t)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	((= iter 0)
	 #f)
	((and (> (length card-list)1)
	      (= (get-suit (car card-list))
		 (get-suit (cadr card-list)))
	      (= (+ 1 (get-value (car card-list)))
		 (get-value (cadr card-list))))
	 (check-for-king (cdr card-list) (- iter 1) slot))
	(#t #f)))

(define (check-for-spec-card card-list iter value)
  (cond ((= (length card-list) 0)
	 #f)
	((= (get-value (car card-list)) value)
	 #t)
	((= iter 0)
	 #f)
	((and (> (length card-list) 1)
	      (= (get-suit (car card-list))
		 (get-suit (cadr card-list)))
	      (= (+ 1 (get-value (car card-list)))
		 (get-value (cadr card-list))))
	 (check-for-spec-card (cdr card-list) (- iter 1) value))
	(#t #f)))

(define (check-to-tableau? slot t-slot)
  (cond ((= slot 20)
	 #f)
	((or (empty-slot? slot)
	     (= t-slot 20))
	 (check-to-tableau? (+ 1 slot) 12))
	((and (not (= slot t-slot))
	      (empty-slot? t-slot)
	      (check-for-king (get-cards slot) free-reserves slot))
235
	 (hint-move slot (- 14 (get-value (get-top-card slot))) t-slot))
236 237 238 239 240 241 242
	((and (not (= slot t-slot))
	      (not (empty-slot? t-slot))
	      (= (get-suit (get-top-card slot))
		 (get-suit (get-top-card t-slot)))
	      (check-for-spec-card (get-cards slot) 
				   free-reserves 
				   (- (get-value (get-top-card t-slot)) 1)))
243
	 (hint-move slot (- (get-value (get-top-card t-slot)) (get-value (get-top-card slot))) t-slot))
244 245 246 247
	(#t (check-to-tableau? slot (+ 1 t-slot)))))

(define (check-for-empty-reserve)
  (and (> free-reserves 0)
248
       (list 0 (_"Move something on to an empty reserve"))))
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

(define (get-hint)
  (or (check-to-foundations? 4 0)
      (check-to-tableau? 4 12)
      (check-for-empty-reserve)))

(define (get-options) 
  #f)

(define (apply-options options) 
  #f)

(define (timeout) 
  #f)

264 265
(set-features droppable-feature)

266 267
(set-lambda new-game button-pressed button-released button-clicked
button-double-clicked game-continuable game-won get-hint get-options
268
apply-options timeout droppable?)