]> gitweb.factorcode.org Git - factor.git/blob - extra/tetris/piece/piece.factor
Fix comments to be ! not #!.
[factor.git] / extra / tetris / piece / piece.factor
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 ;
4 IN: tetris.piece
5
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.
9
10 TUPLE: piece
11     { tetromino tetromino }
12     { rotation integer initial: 0 }
13     { location array initial: { 0 0 } } ;
14
15 : <piece> ( tetromino -- piece )
16     piece new swap >>tetromino ;
17
18 : (piece-blocks) ( piece -- blocks )
19     ! rotates the piece
20     [ rotation>> ] [ tetromino>> states>> ] bi nth ;
21
22 : piece-blocks ( piece -- blocks )
23     ! rotates and positions the piece
24     [ (piece-blocks) ] [ location>> ] bi [ v+ ] curry map ;
25
26 : piece-width ( piece -- width )
27     piece-blocks blocks-width ;
28
29 : set-start-location ( piece board-width -- piece )
30     over piece-width [ 2 /i ] bi@ - 0 2array >>location ;
31
32 : <random-piece> ( board-width -- piece )
33     random-tetromino <piece> swap set-start-location ;
34
35 : <piece-llist> ( board-width -- llist )
36     [ [ <random-piece> ] curry ] keep [ <piece-llist> ] curry lazy-cons ;
37
38 : modulo ( n m -- n )
39   ! -2 7 mod => -2, -2 7 modulo =>  5
40   [ mod ] [ + ] [ mod ] tri ;
41
42 : (rotate-piece) ( rotation inc n-states -- rotation' )
43     [ + ] dip modulo ;
44
45 : rotate-piece ( piece inc -- piece )
46     over tetromino>> states>> length
47     [ (rotate-piece) ] 2curry change-rotation ;
48
49 : move-piece ( piece move -- piece )
50     [ v+ ] curry change-location ;