1 ! Copyright (C) 2006, 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel math math.vectors sequences tetris.tetromino lists.lazy ;
6 #! The rotation is an index into the tetromino's states array, and the
7 #! position is added to the tetromino's blocks to give them their location on the
8 #! tetris board. If the location is f then the piece is not yet on the board.
11 { tetromino tetromino }
12 { rotation integer initial: 0 }
13 { location array initial: { 0 0 } } ;
15 : <piece> ( tetromino -- piece )
16 piece new swap >>tetromino ;
18 : (piece-blocks) ( piece -- blocks )
20 [ rotation>> ] [ tetromino>> states>> ] bi nth ;
22 : piece-blocks ( piece -- blocks )
23 #! rotates and positions the piece
24 [ (piece-blocks) ] [ location>> ] bi [ v+ ] curry map ;
26 : piece-width ( piece -- width )
27 piece-blocks blocks-width ;
29 : set-start-location ( piece board-width -- piece )
30 over piece-width [ 2 /i ] bi@ - 0 2array >>location ;
32 : <random-piece> ( board-width -- piece )
33 random-tetromino <piece> swap set-start-location ;
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) ( rotation inc n-states -- rotation' )
45 : rotate-piece ( piece inc -- piece )
46 over tetromino>> states>> length
47 [ (rotate-piece) ] 2curry change-rotation ;
49 : move-piece ( piece move -- piece )
50 [ v+ ] curry change-location ;