1 ! Copyright (C) 2006, 2007 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel arrays tetris.tetromino math math.vectors
4 sequences quotations lists lists.lazy ;
7 #! A piece adds state to the tetromino that is the piece's delegate. The
8 #! rotation is an index into the tetromino's states array, and the position is
9 #! added to the tetromino's blocks to give them their location on the tetris
10 #! board. If the location is f then the piece is not yet on the board.
11 TUPLE: piece rotation location ;
13 : <piece> ( tetromino -- piece )
14 piece construct-delegate
15 0 over set-piece-rotation
16 { 0 0 } over set-piece-location ;
18 : (piece-blocks) ( piece -- blocks )
19 #! rotates the tetromino
20 dup piece-rotation swap tetromino-states nth ;
22 : piece-blocks ( piece -- blocks )
23 #! rotates and positions the tetromino
24 dup (piece-blocks) swap piece-location [ v+ ] curry map ;
26 : piece-width ( piece -- width )
27 piece-blocks blocks-width ;
29 : set-start-location ( piece board-width -- )
30 2 /i over piece-width 2 /i - 0 2array swap set-piece-location ;
32 : <random-piece> ( board-width -- piece )
33 random-tetromino <piece> [ swap set-start-location ] keep ;
35 : <piece-llist> ( board-width -- llist )
36 [ [ <random-piece> ] curry ] keep [ <piece-llist> ] curry lazy-cons ;
39 #! -2 7 mod => -2, -2 7 modulo => 5
40 tuck mod over + swap mod ;
42 : rotate-piece ( piece inc -- )
43 over piece-rotation + over tetromino-states length modulo swap set-piece-rotation ;
45 : move-piece ( piece move -- )
46 over piece-location v+ swap set-piece-location ;