osmosis.scm 6.93 KB
Newer Older
1
; AisleRiot - osmosis.scm
2
; Copyright (C) 1998, 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
; This program is distributed in the hope that it will be useful,
10
; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 12
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
13
;
14
; 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) (ice-9 format))

19
(define deal-three #f)
20 21 22 23 24 25 26

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

27
  (add-extended-slot '() right 'reserve)      ;Slot 0
28
  (add-blank-slot)
29
  (add-extended-slot '() right 'foundation)      ;Slot 1
30
  (add-carriage-return-slot)
31
  (add-extended-slot '() right 'reserve)      ;Slot 2
32
  (add-blank-slot)
33
  (add-extended-slot '() right 'foundation)      ;Slot 3
34
  (add-carriage-return-slot)
35
  (add-extended-slot '() right 'reserve)      ;Slot 4
36
  (add-blank-slot)
37
  (add-extended-slot '() right 'foundation)      ;Slot 5
38
  (add-carriage-return-slot)
39
  (add-extended-slot '() right 'reserve)      ;Slot 6
40
  (add-blank-slot)
41
  (add-extended-slot '() right 'foundation)      ;Slot 7
42
  (add-carriage-return-slot)
43
  (add-normal-slot DECK 'stock)             ;Slot 8
44

45
  (if deal-three
46 47
    (add-partially-extended-slot '() right 3 'waste)
    (add-normal-slot '() 'waste)             
48 49 50
  )                                  ;Slot 9

  (initial-deal)
51

52
  (give-status-message)
53

54
  (add-to-score! 1)
55 56
  (list 6 5))

57 58 59 60 61
(define (initial-deal)
  (deal-cards 8 '(0 2 4 6 0 2 4 6 0 2 4 6))
  (deal-cards-face-up 8 '(0 2 4 6 1))
)

62
(define (give-status-message)
63 64 65
  (set-statusbar-message (string-append (get-stock-no-string)
					"   "
					(get-redeals-string))))
66 67

(define (get-stock-no-string)
68
  (format #f (_"Stock left: ~a") (number->string (length (get-cards 8))))
69
)
70 71

(define (get-redeals-string)
72 73
  (if deal-three
    ""
74
    (format #f (_"Redeals left: ~a") (number->string (- 2 FLIP-COUNTER)))
75 76
  )
)
77

78
(define (button-pressed slot-id card-list)
79
  (and (not (empty-slot? slot-id))
80
       (= (length card-list) 1)
81 82 83 84 85
       (or (= slot-id 0) 
	   (= slot-id 2)
	   (= slot-id 4) 
	   (= slot-id 6)
	   (= slot-id 9))))
86 87 88
		
(define (complete-transaction start-slot card-list end-slot)
  (move-n-cards! start-slot end-slot card-list)
89
  (add-to-score! 1)
90
  (if (not (empty-slot? start-slot))
91
      (make-visible-top-card start-slot))
92 93
  #t)

94 95 96 97
(define (find-card-val-in-list? cards value)
  (and (not (null? cards))
       (or (= value (get-value (car cards)))
	   (find-card-val-in-list? (cdr cards) value))))
98

99
(define (droppable? start-slot card-list end-slot)
100 101 102 103 104 105 106 107 108 109 110 111 112 113
  (and (not (= start-slot end-slot))
       (or (= end-slot 1)
	   (= end-slot 3)
	   (= end-slot 5)
	   (= end-slot 7))
       (if (empty-slot? end-slot)
	   (and (= (get-value (car (reverse (get-cards 1))))
		   (get-value (car card-list)))
		(while (empty-slot? (- end-slot 2)) 
		       (set! end-slot (- end-slot 2))))
	   (and (= (get-suit (get-top-card end-slot))
		   (get-suit (car card-list)))
		(or (= end-slot 1)
		    (find-card-val-in-list? (get-cards (- end-slot 2))
114 115 116 117
					    (get-value (car card-list))) )))))
  
(define (button-released start-slot card-list end-slot)
  (and (droppable? start-slot card-list end-slot)
118
       (complete-transaction start-slot card-list end-slot)))
119 120
  
(define (button-clicked slot-id)
121
  (and (= slot-id 8)
122
       (flip-stock 8 9 (if deal-three -1 2) (if deal-three 3 1))))
Rosanna Yuen's avatar
Rosanna Yuen committed
123 124 125 126 127 128 129 130 131 132 133 134

(define (check-to-move orig-slot end-slot above-list top-card)
  (if (not (null? above-list))
      (if (eq? (get-value top-card)
	       (get-value (car above-list)))
	  (begin
	    (remove-card orig-slot)
	    (complete-transaction orig-slot (list top-card) end-slot))
	  (check-to-move orig-slot end-slot (cdr above-list) top-card))
      #f))
  

135
(define (button-double-clicked slot)
Rosanna Yuen's avatar
Rosanna Yuen committed
136 137 138 139 140 141 142 143 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
  (if (and (or (= slot 0)
	       (= slot 2)
	       (= slot 4)
	       (= slot 6)
	       (= slot 9))
	   (not (empty-slot? slot)))
      (begin
	(let ((top-card (get-top-card slot)))
	  (if (eq? (get-suit top-card)
		   (get-suit (car (get-cards 1))))
	      (begin
		(remove-card slot)
		(complete-transaction slot (list top-card) 1))
	      (if (eq? (get-value top-card)
		       (get-value (car (reverse (get-cards 1)))))
		  (cond  ((empty-slot? 3)
			  (begin
			    (remove-card slot)
			    (complete-transaction slot (list top-card) 3)))
			 ((empty-slot? 5)
			  (begin
			    (remove-card slot)
			    (complete-transaction slot (list top-card) 5)))
			 (#t
			  (begin
			    (remove-card slot)
			    (complete-transaction slot (list top-card) 7))))
		  (cond ((and (not (empty-slot? 3))
			      (eq? (get-suit top-card)
				   (get-suit (car (get-cards 3)))))
			 (check-to-move slot 3 (get-cards 1) top-card))
			((and (not (empty-slot? 5))
			      (eq? (get-suit top-card)
				   (get-suit (car (get-cards 5)))))
			 (check-to-move slot 5 (get-cards 3) top-card))
			((and (not (empty-slot? 7))
			      (eq? (get-suit top-card)
				   (get-suit (car (get-cards 7)))))
			 (check-to-move slot 7 (get-cards 5) top-card))
			(#t #f))))))	 
      #f))
177

178
(define (placeable? from-slot card slot-id)
179 180 181 182
  (and (< slot-id 9)
       (or (if (empty-slot? slot-id)
	       (and (= (get-value card) 
		       (get-value (car (reverse (get-cards 1)))))
183
		    (hint-move from-slot 1 slot-id))
184 185 186 187
	       (and (= (get-suit card) (get-suit (get-top-card slot-id)))
		    (or (= slot-id 1)
			(find-card-val-in-list? (get-cards (- slot-id 2)) 
						(get-value card)))
188 189
		    (hint-move from-slot 1 slot-id)))
	   (placeable? from-slot card (+ slot-id 2)))))
190 191 192 193

(define (get-valid-move id-list)
  (and (not (null? id-list))
       (or (and (not (empty-slot? (car id-list)))
194
		(placeable? (car id-list) (get-top-card (car id-list)) 1))
195
	   (get-valid-move (cdr id-list)))))
196

197
(define (game-continuable)
198
  (give-status-message)
199 200
  (or (and (or deal-three
               (< FLIP-COUNTER 2))
201 202 203
	   (not (empty-slot? 9)))
      (not (empty-slot? 8))
      (get-valid-move '(0 2 4 6 9))))
204

205
(define (game-won)
206 207 208 209
  (and (= 13 (length (get-cards 1)))
       (= 13 (length (get-cards 3)))
       (= 13 (length (get-cards 5)))
       (= 13 (length (get-cards 7)))))
210

211
(define (get-hint)
212
  (or (get-valid-move '(0 2 4 6 9))
213 214 215 216 217 218
      (if deal-three
        (list 0 (_"Deal new cards from the deck"))
        (list 0 (_"Deal a new card from the deck"))
      )
  )
)
219

220 221
(define (get-options)
  (list (list (_"Three card deals") deal-three)))
222

223 224 225
(define (apply-options options)
  (set! deal-three (cadar options))
)
226 227 228

(define (timeout) #f)

229 230
(set-features droppable-feature)

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