1+ package Games .Tetris ;
2+
3+ import java .awt .Color ;
4+ import java .util .Vector ;
5+
6+ public class TetrisBlock
7+ {
8+ public static final TetrisBlock LONG = new TetrisBlock (
9+ new boolean [][]{{true , true , true , true }}, Color .RED );
10+
11+ public static final TetrisBlock LEFT_L = new TetrisBlock (new boolean [][]{
12+ {true , true , true }, {true , false , false }}, Color .GREEN );
13+
14+ public static final TetrisBlock TEE = new TetrisBlock (
15+ new boolean [][]{{true , true , true }, {false , true , false }}, Color .BLUE );
16+
17+ public static final TetrisBlock RIGHT_L = new TetrisBlock (new boolean [][]{
18+ {true , true , true }, {false , false , true }}, Color .CYAN );
19+
20+ public static final TetrisBlock SQUARE = new TetrisBlock (new boolean [][]{
21+ {true , true }, {true , true }}, Color .BLACK );
22+
23+ private boolean [][] blocks ;
24+ private Color color ;
25+
26+ // location of the bottum left hand corner.
27+ private int x , y ;
28+
29+ private TetrisBlock (boolean [][] blocks , Color color )
30+ {
31+ this .blocks = blocks ;
32+ this .color = color ;
33+ }
34+
35+ private TetrisBlock (boolean [][] blocks , Color color , int x , int y )
36+ {
37+ this (blocks , color );
38+ this .x = x ;
39+ this .y = y ;
40+ }
41+
42+ public static TetrisBlock getRandomBlock ()
43+ {
44+ switch ((int )(Math .random ()*5 ))
45+ {
46+ case 0 : return LONG ;
47+ case 1 : return LEFT_L ;
48+ case 2 : return TEE ;
49+ case 3 : return RIGHT_L ;
50+ case 4 : return SQUARE ;
51+ }
52+ return null ;
53+ }
54+
55+ public int getX ()
56+ {
57+ return x ;
58+ }
59+
60+ public int getY ()
61+ {
62+ return y ;
63+ }
64+
65+ public int getWidth ()
66+ {
67+ return blocks .length ;
68+ }
69+
70+ public int getHeight ()
71+ {
72+ return blocks [0 ].length ;
73+ }
74+
75+ public TetrisBlock setLocation (int x , int y )
76+ {
77+ return new TetrisBlock (blocks , color , x , y );
78+ }
79+
80+ public TetrisBlock moveDown ()
81+ {
82+ return new TetrisBlock (blocks , color , x , y +1 );
83+ }
84+
85+ public TetrisBlock moveLeft ()
86+ {
87+ return new TetrisBlock (blocks , color , x -1 , y );
88+ }
89+
90+ public TetrisBlock moveRight ()
91+ {
92+ return new TetrisBlock (blocks , color , x +1 , y );
93+ }
94+
95+ public boolean overlaps (int x , int y )
96+ {
97+ for (int i = 0 ; i < blocks .length ; i ++)
98+ {
99+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
100+ {
101+ if (blocks [i ][j ])
102+ {
103+ if ((this .x +i == x ) && (this .y -blocks [0 ].length +1 +j == y ))
104+ return true ;
105+ }
106+ }
107+ }
108+ return false ;
109+ }
110+
111+ public boolean overlaps (TetrisBlock other )
112+ {
113+ for (int i = 0 ; i < blocks .length ; i ++)
114+ {
115+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
116+ {
117+ if (blocks [i ][j ])
118+ {
119+ if (other .overlaps (x +i , y -blocks [0 ].length +1 +j ))
120+ return true ;
121+ }
122+ }
123+ }
124+ return false ;
125+ }
126+
127+ public TetrisBlock turnRight ()
128+ {
129+ boolean [][] newBlocks = new boolean [blocks [0 ].length ][blocks .length ];
130+ for (int i = 0 ; i < blocks .length ; i ++)
131+ {
132+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
133+ {
134+ newBlocks [blocks [0 ].length -1 -j ][i ] = blocks [i ][j ];
135+ }
136+ }
137+ return new TetrisBlock (newBlocks , color , x , y );
138+ }
139+
140+ public TetrisBlock turnLeft ()
141+ {
142+ boolean [][] newBlocks = new boolean [blocks [0 ].length ][blocks .length ];
143+ for (int i = 0 ; i < blocks .length ; i ++)
144+ {
145+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
146+ {
147+ newBlocks [j ][blocks .length -1 -i ] = blocks [i ][j ];
148+ }
149+ }
150+ return new TetrisBlock (newBlocks , color , x , y );
151+ }
152+
153+ public String toString ()
154+ {
155+ String ret = "" ;
156+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
157+ {
158+ ret +=(j !=0 )?"\n " :"" ;
159+ for (int i = 0 ; i < blocks .length ; i ++)
160+ {
161+ if (blocks [i ][j ])
162+ ret +="#" ;
163+ else
164+ ret +=" " ;
165+ }
166+ }
167+ return ret ;
168+ }
169+
170+ public static TetrisBlock getSingleBlock (int x , int y , Color color )
171+ {
172+ return new TetrisBlock (new boolean [][]{{true }}, color , x , y );
173+ }
174+
175+ public Vector <TetrisBlock > getSingleBlocks ()
176+ {
177+ Vector <TetrisBlock > parts = new Vector ();
178+ for (int i = 0 ; i < blocks .length ; i ++)
179+ {
180+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
181+ {
182+ if (blocks [i ][j ])
183+ parts .add (getSingleBlock (x +i ,y -blocks [0 ].length +1 +j , color ));
184+ }
185+ }
186+ return parts ;
187+ }
188+
189+ public void draw (BlockGrid grid )
190+ {
191+ grid .setColor (color );
192+ for (int i = 0 ; i < blocks .length ; i ++)
193+ {
194+ for (int j = 0 ; j < blocks [0 ].length ; j ++)
195+ {
196+ if (blocks [i ][j ])
197+ grid .drawSquare (x +i ,y -blocks [0 ].length +1 +j );
198+ }
199+ }
200+ }
201+
202+ public static void main (String [] args )
203+ {
204+ TetrisBlock a = TetrisBlock .SQUARE .setLocation (2 ,2 );
205+ TetrisBlock b = TetrisBlock .SQUARE .setLocation (5 ,2 );
206+ System .out .println (a .overlaps (b ));
207+ }
208+ }
0 commit comments