View Javadoc

1   package erland.game.tetris;
2   /*
3    * Copyright (C) 2003 Erland Isaksson (erland_i@hotmail.com)
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *
19   */
20  
21  import java.awt.*;
22  
23  /***
24   * Represents the block:
25   * <br>0x00
26   * <br>xxx0
27   * <br>0000
28   * @author Erland Isaksson
29   */
30  class Block1 extends Block
31  {
32  	/*** The color of the block */
33  	protected static Color color = Color.red;
34  	public Color getColor()
35  	{
36  		return color;
37  	}
38  	protected boolean check(BlockMatrix m, int x, int y, int rotation)
39  	{
40  		if(rotation>=360) {
41  			rotation-=360;
42  		}else if(rotation<0) {
43  			rotation+=360;
44  		}
45  
46  		if(rotation == 0) {
47  			if(!m.isUsed(x,y) && !m.isUsed(x+1,y) && !m.isUsed(x-1,y) && !m.isUsed(x,y-1)) {
48  				return true;
49  			}
50  		}else if(rotation == 90) {
51  			if(!m.isUsed(x,y) && !m.isUsed(x,y+1) && !m.isUsed(x,y-1) && !m.isUsed(x+1,y)) {
52  				return true;
53  			}
54  		}else if(rotation == 180) {
55  			if(!m.isUsed(x,y) && !m.isUsed(x+1,y) && !m.isUsed(x-1,y) && !m.isUsed(x,y+1)) {
56  				return true;
57  			}
58  		}else if(rotation == 270) {
59  			if(!m.isUsed(x,y) && !m.isUsed(x,y+1) && !m.isUsed(x,y-1) && !m.isUsed(x-1,y)) {
60  				return true;
61  			}
62  		}
63  		return false;
64  	}
65  	
66  	protected void unset(BlockMatrix m, int x, int y, int rotation)
67  	{
68  		if(rotation>=360) {
69  			rotation-=360;
70  		}else if(rotation<0) {
71  			rotation+=360;
72  		}
73  		
74  		if(rotation == 0) {
75  			m.setUnused(x,y);
76  			m.setUnused(x+1,y);
77  			m.setUnused(x-1,y);
78  			m.setUnused(x,y-1);
79  		}else if(rotation == 90) {
80  			m.setUnused(x,y);
81  			m.setUnused(x,y+1);
82  			m.setUnused(x,y-1);
83  			m.setUnused(x+1,y);
84  		}else if(rotation == 180) {
85  			m.setUnused(x,y);
86  			m.setUnused(x+1,y);
87  			m.setUnused(x-1,y);
88  			m.setUnused(x,y+1);
89  		}else if(rotation == 270) {
90  			m.setUnused(x,y);
91  			m.setUnused(x,y+1);
92  			m.setUnused(x,y-1);
93  			m.setUnused(x-1,y);
94  		}
95  	}
96  	
97  	protected void set(BlockMatrix m, int x, int y, int rotation)
98  	{
99  		if(rotation>=360) {
100 			rotation-=360;
101 		}else if(rotation<0) {
102 			rotation+=360;
103 		}
104 		
105 		this.x = x;
106 		this.y = y;
107 		this.rotation = rotation;
108 		
109 		if(rotation == 0) {
110 			m.setUsed(x,y,color);
111 			m.setUsed(x+1,y,color);
112 			m.setUsed(x-1,y,color);
113 			m.setUsed(x,y-1,color);
114 		}else if(rotation == 90) {
115 			m.setUsed(x,y,color);
116 			m.setUsed(x,y+1,color);
117 			m.setUsed(x,y-1,color);
118 			m.setUsed(x+1,y,color);
119 		}else if(rotation == 180) {
120 			m.setUsed(x,y,color);
121 			m.setUsed(x+1,y,color);
122 			m.setUsed(x-1,y,color);
123 			m.setUsed(x,y+1,color);
124 		}else if(rotation == 270) {
125 			m.setUsed(x,y,color);
126 			m.setUsed(x,y+1,color);
127 			m.setUsed(x,y-1,color);
128 			m.setUsed(x-1,y,color);
129 		}
130 	}
131 	
132 }