-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-L-shape.scm
232 lines (217 loc) · 11.2 KB
/
make-L-shape.scm
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
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
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
(load "make-stone.scm")
(define (make-L-shape% color)
(new
(class object%
(init init-rotation)
(init init-color)
(field (stones (make-vector 4)))
(super-new)
;Defines the current rotation of a stone
(define current-rotation init-rotation)
;Defines the new-color for the shape
(define new-color init-color)
;Defines a new variable
(define current-color null)
;Findes the color corresponding to the init-color number
(send this find-new-color)
;Defines stones in the shape and puts them on the board
(vector-set! stones 0 (make-stone% 1 6 current-color))
(vector-set! stones 1 (make-stone% 2 6 current-color))
(vector-set! stones 2 (make-stone% 3 6 current-color))
(vector-set! stones 3 (make-stone% 3 7 current-color))
;Gives the stones in the shape aliases for easy access from inside shape
(define stone1 (vector-ref stones 0))
(define stone2 (vector-ref stones 1))
(define stone3 (vector-ref stones 2))
(define stone4 (vector-ref stones 3))
;sets the color of the shape
(define/public (find-new-color)
(cond
((= new-color 0) (set! current-color "green"))
((= new-color 1) (set! current-color "red"))
((= new-color 2) (set! current-color "yellow"))
((= new-color 3) (set! current-color "blue"))
((= new-color 4) (set! current-color "purple"))
((= new-color 5) (set! current-color "orange"))
))
;-------------------------------MOVES THE SHAPE VERTICALLY-----------------------------------
;help-function to drop-down
(define (drop-help)
(send stone4 drop-down)
(send stone3 drop-down)
(send stone2 drop-down)
(send stone1 drop-down))
;Moves all stones in this shape one step down
(define/public (drop-down)
(cond ;checks witch rotation-state it's in
((and (eq? current-rotation 0)
(send stone3 check-down)
(send stone4 check-down))
(drop-help))
((and (eq? current-rotation 1)
(send stone4 check-down)
(send stone2 check-down)
(send stone1 check-down))
(drop-help))
((and (eq? current-rotation 2)
(send stone1 check-down)
(send stone4 check-down))
(drop-help))
((and (eq? current-rotation 3)
(send stone1 check-down)
(send stone2 check-down)
(send stone3 check-down))
(drop-help))
(else
(send *player* add-10-score!)
(send *gameboard* check-top-3))))
;Makes the shape fall down untill a stone gets in the way
(define/public (fall)
(cond ;checks witch rotation-state it's in
((and (eq? current-rotation 0)
(send stone3 check-down)
(send stone4 check-down))
(drop-help)
(send *player* add-10-score!);this gives the player more points for falling from longer distance
(send this fall))
((and (eq? current-rotation 1)
(send stone4 check-down)
(send stone2 check-down)
(send stone1 check-down))
(drop-help)
(send *player* add-10-score!) ;this gives the player more points for falling from longer distance
(send this fall))
((and (eq? current-rotation 2)
(send stone1 check-down)
(send stone4 check-down))
(drop-help)
(send *player* add-10-score!) ;this gives the player more points for falling from longer distance
(send this fall))
((and (eq? current-rotation 3)
(send stone1 check-down)
(send stone2 check-down)
(send stone3 check-down))
(drop-help)
(send *player* add-10-score!);this gives the player more points for falling from longer distance
(send this fall))
(else
(send *player* add-10-score!)
(send *gameboard* check-top-3))))
;-----------------------------------MOVES THE SHAPE HORISONTALLY-------------------------------------------
;Help to move-left, substracting the position of each stone with one column
(define (left-helper)
(send stone1 move! (send stone1 get-current-row) (- (send stone1 get-current-col) 1))
(send stone2 move! (send stone2 get-current-row) (- (send stone2 get-current-col) 1))
(send stone3 move! (send stone3 get-current-row) (- (send stone3 get-current-col) 1))
(send stone4 move! (send stone4 get-current-row) (- (send stone4 get-current-col) 1)))
;Move left
(define/public (move-left)
(cond ;checks witch rotation-state it's in
((and (= current-rotation 0)
(send stone1 check-left)
(send stone2 check-left)
(send stone3 check-left))
(left-helper))
((and (= current-rotation 1)
(send stone3 check-left)
(send stone4 check-left))
(left-helper))
((and (= current-rotation 2)
(send stone4 check-left)
(send stone2 check-left)
(send stone1 check-left))
(left-helper))
((and (= current-rotation 3)
(send stone4 check-left)
(send stone1 check-left))
(left-helper))))
;Help-function to move right, adding the position of each stone with one column
(define (right-helper)
(send stone1 move! (send stone1 get-current-row) (+ (send stone1 get-current-col) 1))
(send stone2 move! (send stone2 get-current-row) (+ (send stone2 get-current-col) 1))
(send stone3 move! (send stone3 get-current-row) (+ (send stone3 get-current-col) 1))
(send stone4 move! (send stone4 get-current-row) (+ (send stone4 get-current-col) 1)))
;Move right
(define/public (move-right)
(cond ;checks witch rotation-state it's in
((and (= current-rotation 0)
(send stone4 check-right))
(right-helper))
((and (= current-rotation 1)
(send stone1 check-right)
(send stone4 check-right))
(right-helper))
((and (= current-rotation 2)
(send stone3 check-right)
(send stone2 check-right)
(send stone1 check-right))
(right-helper))
((and (= current-rotation 3)
(send stone4 check-right)
(send stone3 check-right))
(right-helper))))
;-----------------------------------ROTATION OF THE SHAPE-------------------------------------------
;Helper for rotate
;checks witch rotation-state it's in, and if the new positions are free from other stones,
;in that case move the stones of the shape.
(define (rotate-helper)
(cond
;If the shape is an L
((and (eq? current-rotation 0)
(send *gameboard* check-position stones (send stone1 get-current-row) (+ (send stone1 get-current-col) 2))
(send *gameboard* check-position stones (- (send stone2 get-current-row) 1) (+ (send stone2 get-current-col) 1))
(send *gameboard* check-position stones (- (send stone3 get-current-row) 2) (send stone3 get-current-col))
(send *gameboard* check-position stones (- (send stone4 get-current-row) 1) (- (send stone4 get-current-col) 1)))
(send stone1 move! (send stone1 get-current-row) (+ (send stone1 get-current-col) 2))
(send stone2 move! (- (send stone2 get-current-row) 1) (+ (send stone2 get-current-col) 1))
(send stone3 move! (- (send stone3 get-current-row) 2) (send stone3 get-current-col))
(send stone4 move! (- (send stone4 get-current-row) 1) (- (send stone4 get-current-col) 1))
(set! current-rotation 1))
;If the shape has rotated 90 degrees
((and (eq? current-rotation 1)
(send *gameboard* check-position stones (+ (send stone1 get-current-row) 2) (- (send stone1 get-current-col) 1))
(send *gameboard* check-position stones (+ (send stone2 get-current-row) 1) (send stone2 get-current-col))
(send *gameboard* check-position stones (send stone3 get-current-row) (+ (send stone3 get-current-col) 1))
(send *gameboard* check-position stones (- (send stone4 get-current-row) 1) (send stone4 get-current-col)))
(send stone1 move! (+ (send stone1 get-current-row) 2) (- (send stone1 get-current-col) 1))
(send stone2 move! (+ (send stone2 get-current-row) 1) (send stone2 get-current-col))
(send stone3 move! (send stone3 get-current-row) (+ (send stone3 get-current-col) 1))
(send stone4 move! (- (send stone4 get-current-row) 1) (send stone4 get-current-col))
(set! current-rotation 2))
;If the shape has rotated 180 degrees
((and (eq? current-rotation 2)
(send *gameboard* check-position stones (- (send stone1 get-current-row) 1) (- (send stone1 get-current-col) 1))
(send *gameboard* check-position stones (send stone2 get-current-row) (send stone2 get-current-col))
(send *gameboard* check-position stones (+ (send stone3 get-current-row) 1) (+ (send stone3 get-current-col) 1))
(send *gameboard* check-position stones (send stone4 get-current-row) (+ (send stone4 get-current-col) 2)))
(send stone1 move! (- (send stone1 get-current-row) 1) (- (send stone1 get-current-col) 1))
(send stone2 move! (send stone2 get-current-row) (send stone2 get-current-col))
(send stone3 move! (+ (send stone3 get-current-row) 1) (+ (send stone3 get-current-col) 1))
(send stone4 move! (send stone4 get-current-row) (+ (send stone4 get-current-col) 2))
(set! current-rotation 3))
;If the shape has rotated 270 degrees
((and (send *gameboard* check-position stones (- (send stone1 get-current-row) 1) (send stone1 get-current-col))
(send *gameboard* check-position stones (send stone2 get-current-row) (- (send stone2 get-current-col) 1))
(send *gameboard* check-position stones (+ (send stone3 get-current-row) 1) (- (send stone3 get-current-col) 2))
(send *gameboard* check-position stones (+ (send stone4 get-current-row) 2) (- (send stone4 get-current-col) 1)))
(send stone1 move! (- (send stone1 get-current-row) 1) (send stone1 get-current-col))
(send stone2 move! (send stone2 get-current-row) (- (send stone2 get-current-col) 1))
(send stone3 move! (+ (send stone3 get-current-row) 1) (- (send stone3 get-current-col) 2))
(send stone4 move! (+ (send stone4 get-current-row) 2) (- (send stone4 get-current-col) 1))
(set! current-rotation 0))))
;Rotates the shape 90 degrees clockwise
(define/public (rotate)
(cond
((and (eq? current-rotation 0)
(eq? (send stone4 get-current-col) 11))
(send this move-left)
(rotate-helper))
((and (eq? current-rotation 2)
(eq? (send stone3 get-current-col) 11))
(send this move-left)
(rotate-helper))
(else (rotate-helper))
))
)
(init-rotation 0)
(init-color color)))