-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardstack.ss
executable file
·171 lines (152 loc) · 5 KB
/
cardstack.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
#|
File : cardstack
Author : Wouter Van Rossem
|#
#!r6rs
(library
(cardstack)
(export cardstack%)
(import (rnrs base (6))
(rnrs io simple)
(rnrs control)
(scheme class)
(prefix (scheme base) s:)
(prefix (magicthegathering datastructures positional-list adt) pl:)
(prefix (magicthegathering datastructures stack linked) stack:)
(magicthegathering card))
; Class name: cardstack
; Superclass: object%
; Attributes: - public: searchable?
; - private: stack = stack
; Methods:
; - public:
; * empty?:
; arguments: none
; output : bool
; description: check if the cardstack is empty
; * size:
; arguments: none
; output : number
; description: return the size of the cardstack
; * add-card:
; arguments: card%
; output: none
; description: add a new card to the cardstack
; * add-cards:
; arguments: list of card% objects
; output: none
; description: add different cards (in a list) to the cardstack
; * remove-top:
; arguments: none
; output: card%
; description: remove the top card of the cardstack
; * look-top:
; arguments: none
; output: card%
; description: look at the top card of the cardstack
; * shuffle:
; arguments: none
; output: none
; description: shuffle the cardstack, i.e. rearrange all objects randomly
; * top-to-bottom:
; arguments: none, optionnaly n
; output: none
; description: put the top card of the cardstack on the bottom
; Repeat this n times
; * find:
; arguments: card%
; output: card%
; description: find a card in the cardstack
; * remove:
; arguments: card%
; output: card%
; description: delete a card from the cardstack
; * for-each:
; arguments: lambda
; output: none
; description: apply a procedure to each element of the cardstack
; * set-(un)searchable:
; arguments: none
; output: none
; description: set if this cardstack is searchable or not
; * reset:
; arguments: none
; output: none
; description: reset the cardstack, i.e. make a new positional list
; - private:
; * find-pos:
; arguments: card
; output: position
; description: find the position of a card in the cardstack
(define cardstack%
(class object%
(init-field (searchable? #f))
(super-new)
(define stack (stack:new))
; ( -> bool )
(define/public (empty?)
(stack:empty? stack))
; ( -> number )
(define/public (size)
(stack:size stack))
; ( card% -> )
(define/public (add-card card)
(if (is-a? card card%)
(stack:push! stack card)
(error "CARDSTACK -- trying to add a non-card to a cardstack" card)))
; ( pair -> )
(define/public (add-cards card-list)
(unless (null? card-list)
(add-card (car card-list))
(add-cards (cdr card-list))))
; ( -> card% )
(define/public (remove-top)
(if (empty?)
(error "CARDSTACK -- remove-top from empty cardstack" stack)
(stack:pop! stack)))
; ( -> card% )
(define/public (look-top)
(if (empty?)
(error "CARDSTACK -- look-top from empty cardstack" stack)
(stack:top stack)))
; ( -> )
(define/public (shuffle)
(if (empty?)
(error "CARDSTACK -- shuffle on empty cardstack" stack)
(stack:shuffle stack)))
; ( -> )
(define/public (top-to-bottom . n)
(cond
((null? n) (stack:top-to-bottom stack))
((= (s:car n) 0) )
(else (stack:top-to-bottom stack)
(top-to-bottom (- (s:car n) 1)))))
; ( card% -> pos )
(define/private (find-pos card)
(stack:find stack card))
; ( card% -> card% )
(define/public (find card)
(let ((pos (find-pos card)))
(if pos
(pl:peek (cdr stack) pos))))
; ( card% -> card% )
(define/public (remove card)
(let* ((found? (find-pos card))
(card (pl:peek stack found?)))
(if found?
(stack:delete! stack found?)
#f)))
; ( ( card% -> ) -> )
(define/public (for-each func)
(stack:for-each stack func))
; ( -> )
(define/public (set-searchable)
(unless searchable?
(set! searchable? #t)))
; ( -> )
(define/public (set-unsearchable)
(when searchable?
(set! searchable? #f)))
; ( -> )
(define/public (reset)
(set! stack (stack:new))))))