bristol.scm 7.71 KB
Newer Older
1
; AisleRiot - bristol.scm
2
; Copyright (C) 1999, 2003 Rosanna Yuen <rwsy@mit.edu>
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 21 22 23 24
(define (new-game)
  (initialize-playing-area)
  (set-ace-low)
  (make-standard-deck)
  (shuffle-deck)

25
  (add-normal-slot DECK 'stock)
26

27
  (set! HORIZPOS (+ HORIZPOS 0.75))
28

29 30 31
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
  (add-normal-slot '() 'reserve)
32

33
  (set! HORIZPOS (+ HORIZPOS 0.75))
34 35 36 37
  (add-normal-slot '() 'foundation)
  (add-normal-slot '() 'foundation)
  (add-normal-slot '() 'foundation)
  (add-normal-slot '() 'foundation)
38 39 40

  (add-carriage-return-slot)

41
  (add-extended-slot '() right 'tableau)
42
  (add-blank-slot)
43
  (set! HORIZPOS (+ HORIZPOS 0.75))
44
  (add-extended-slot '() right 'tableau)
45
  (add-blank-slot)
46
  (set! HORIZPOS (+ HORIZPOS 0.75))
47
  (add-extended-slot '() right 'tableau)
48
  (add-blank-slot)
49
  (set! HORIZPOS (+ HORIZPOS 0.75))
50
  (add-extended-slot '() right 'tableau)
51 52 53

  (add-carriage-return-slot)

54
  (add-extended-slot '() right 'tableau)
55
  (add-blank-slot)
56
  (set! HORIZPOS (+ HORIZPOS 0.75))
57
  (add-extended-slot '() right 'tableau)
58
  (add-blank-slot)
59
  (set! HORIZPOS (+ HORIZPOS 0.75))
60
  (add-extended-slot '() right 'tableau)
61
  (add-blank-slot)
62
  (set! HORIZPOS (+ HORIZPOS 0.75))
63
  (add-extended-slot '() right 'tableau)
64 65 66 67 68

  (deal-cards-face-up 0 '(8 9 10 11 12 13 14 15 
			    8 9 10 11 12 13 14 15 
			    8 9 10 11 12 13 14 15))
  (check-for-kings 8)
69

70
  (give-status-message)
71

72
  (list 11 3))
73

74 75 76
(define (give-status-message)
  (set-statusbar-message (get-stock-no-string)))

77
(define (get-stock-no-string)
78
  (string-append (_"Stock left:") " "
79 80
		 (number->string (length (get-cards 0)))))

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
(define (check-for-kings slot-id)
  (if (> slot-id 15)
      #f
      (begin
	(if (= (get-value (get-top-card slot-id)) king)
	    (begin
	      (let ((new-deck (get-cards slot-id)))
		(set-cards! slot-id (list (cadr new-deck)
					  (caddr new-deck)
					  (car new-deck))))
	      (if (= (get-value (get-top-card slot-id)) king)
		  (begin
		    (let ((new-deck (get-cards slot-id)))
		      (set-cards! slot-id (list (cadr new-deck)
						(caddr new-deck)
						(car new-deck)))))))
	    (if (= (get-value (cadr (get-cards slot-id))) king)
		(let ((new-deck (get-cards slot-id)))
		  (set-cards! slot-id (list (car new-deck)
					    (caddr new-deck)
					    (cadr new-deck))))))
	(check-for-kings (+ 1 slot-id)))))

(define (button-pressed slot-id card-list)
  (and (not (empty-slot? slot-id))
       (not (= slot-id 0))
       (not (and (> slot-id 3)
		 (< slot-id 8)))
       (= (length card-list) 1)))

111
(define (droppable? start-slot card-list end-slot)
112 113
  (cond ((= start-slot end-slot) #f)
	((and (> end-slot 3)
114
	      (< end-slot 8))
115 116 117 118 119 120
	 (cond ((empty-slot? end-slot)
		(= (get-value (car card-list)) ace))
	       (#t
	        (= (+ 1 (get-value (get-top-card end-slot)))
		   (get-value (car card-list))))))
	(#t (and (> end-slot 7)
121 122
	      (not (empty-slot? end-slot))
	      (= (get-value (get-top-card end-slot))
123 124 125 126 127 128 129 130
		 (+ 1 (get-value (car card-list))))))))

(define (button-released start-slot card-list end-slot)
  (and (droppable? start-slot card-list end-slot)
       (or (< end-slot 4)
	   (> end-slot 7)
	   (add-to-score! 1))
       (move-n-cards! start-slot end-slot card-list)))
131

132 133 134 135 136 137 138 139
(define (dealable?)
  (not (empty-slot? 0)))

(define (do-deal-next-cards)
  (if (> (length (get-cards 0)) 3)
      (deal-cards-face-up 0 '(1 2 3))
      (deal-cards-face-up 0 '(1))))

140
(define (button-clicked slot-id)
141 142 143
  (and (= slot-id 0)
       (dealable?)
       (do-deal-next-cards)))
144

Rosanna Yuen's avatar
Rosanna Yuen committed
145 146 147 148 149 150 151 152 153 154 155 156
(define (move-to-foundations? slot-id f-slot)
  (cond ((= f-slot 8)
	 #f)
	((and (not (empty-slot? f-slot))
	      (= (get-value (get-top-card slot-id))
		 (+ 1 (get-value (get-top-card f-slot)))))
	 (begin
	   (add-to-score! 1)
	   (deal-cards slot-id (list f-slot))))
	(#t
	 (move-to-foundations? slot-id (+ 1 f-slot)))))

157
(define (button-double-clicked slot-id)
Rosanna Yuen's avatar
Rosanna Yuen committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  (if (and (> slot-id 0)
	   (or (< slot-id 4)
	       (> slot-id 7))
	   (not (empty-slot? slot-id)))
      (if (= ace  (get-value (get-top-card slot-id)))
	  (begin
	    (add-to-score! 1)
	    (cond ((empty-slot? 4)
		   (deal-cards slot-id '(4)))
		  ((empty-slot? 5)
		   (deal-cards slot-id '(5)))
		  ((empty-slot? 6)
		   (deal-cards slot-id '(6)))
		  (#t
		   (deal-cards slot-id '(7)))))
	  (move-to-foundations? slot-id 4))
      #f))
175 176

(define (game-continuable)
177
  (give-status-message)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  (and (not (game-won))
       (get-hint)))

(define (game-won)
  (and (= 13 (length (get-cards 4)))
       (= 13 (length (get-cards 5)))
       (= 13 (length (get-cards 6)))
       (= 13 (length (get-cards 7)))))

(define (check-a-foundation slot-id foundation-id)
  (if (or (> foundation-id 7)
	  (empty-slot? slot-id))
      #f
      (cond ((and (empty-slot? foundation-id)
		  (= (get-value (get-top-card slot-id)) ace))
193
	     (hint-move slot-id 1 foundation-id))
194 195 196
	    ((and (not (empty-slot? foundation-id))
		  (= (+ 1 (get-value (get-top-card foundation-id)))
		     (get-value (get-top-card slot-id))))
197
	     (hint-move slot-id 1 foundation-id))
198 199 200 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 235 236
	    (#t (check-a-foundation slot-id (+ 1 foundation-id))))))

(define (check-to-foundations slot-id)
  (cond ((> slot-id 15)
	 #f)
	((= slot-id 4)
	 (check-to-foundations 8))
	((check-a-foundation slot-id 4)
	 (check-a-foundation slot-id 4))
	(#t
	 (check-to-foundations (+ 1 slot-id)))))

(define (check-reserve slot-id)
  (if (> slot-id 3)
      #f
      (or (and (not (empty-slot? slot-id))
	       (check-a-tslot slot-id 
			      (get-cards slot-id)
			      1
			      8))
	  (check-reserve (+ 1 slot-id)))))

(define (deepness card-list temp-deepness)
  (if (and (> (length card-list) 1)
	   (= (+ 1 (get-value (car card-list)))
	      (get-value (cadr card-list))))
      (deepness (cdr card-list) (+ 1 temp-deepness))
      temp-deepness))

(define (check-a-tslot slot1 card-list depth slot2)
  (if (or (> slot2 15)
	  (and (= (length card-list) depth)
	       (> slot1 7)))
      #f
      (if (and (not (empty-slot? slot2))
	       (not (= slot1 slot2))
	       (= (+ 1 (get-value (car card-list)))
		  (get-value (get-top-card slot2))))
	  (if (= depth 1)
237
	      (hint-move slot1 1 slot2)
238 239 240 241 242

	      (and (check-a-tslot slot1 
				  (cdr card-list)
				  (- depth 1)
				  8)
243
		   (hint-move slot1 1 slot2)))
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	  (check-a-tslot slot1 card-list depth (+ 1 slot2)))))

(define (check-tableau slot-id)
  (if (> slot-id 15)
      #f
      (if (and (not (empty-slot? slot-id))
	       (check-a-tslot slot-id 
			      (get-cards slot-id) 
			      (deepness (get-cards slot-id) 1) 
			      8))
	  (check-a-tslot slot-id 
			      (get-cards slot-id) 
			      (deepness (get-cards slot-id) 1) 
			      8)
	  (check-tableau (+ 1 slot-id)))))

260 261
(define (check-deal)
  (and (dealable?)
262
       (list 0 (_"Deal another round"))))
263 264 265 266 267

(define (get-hint)
  (or (check-to-foundations 1)
      (check-reserve 1)
      (check-tableau 8)
268
      (check-deal)))
269 270 271 272 273 274 275 276 277 278

(define (get-options) 
  #f)

(define (apply-options options) 
  #f)

(define (timeout) 
  #f)

279
(set-features droppable-feature dealable-feature)
280

281 282
(set-lambda new-game button-pressed button-released button-clicked
button-double-clicked game-continuable game-won get-hint get-options
283
apply-options timeout droppable? dealable?)