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   * Class that keeps track of the color and state on
25   * all squares in a game area
26   */
27  class BlockContainerData implements BlockMatrix {
28      /*** Matrix that keeps track of the state of all squares */
29      protected boolean matrix[][];
30      /*** Matrix that keeps track of the colour of all squares */
31      protected Color colorMatrix[][];
32  
33      /***
34       * Create a new instance
35       * @param sizeX Number of horizontal squares
36       * @param sizeY Number of vertical squares
37       */
38      BlockContainerData(int sizeX, int sizeY)
39      {
40          matrix = new boolean[sizeX][sizeY];
41          colorMatrix = new Color[sizeX][sizeY];
42      }
43  
44      /***
45       * Reset the states on all squares
46       */
47      public void clear()
48      {
49          for(int y=0;y<getHeight();y++) {
50              clearRow(y);
51          }
52      }
53  
54      /***
55       * Reset the states on all squares in a row
56       * @param y Vertical row that should be cleared, 0 is the top row
57       */
58      public void clearRow(int y)
59      {
60          for(int x=0;x<getWidth();x++) {
61              matrix[x][y]=false;
62          }
63      }
64  
65      public Color getColor(int x, int y)
66      {
67          if(x>=0 && x<matrix.length && y<matrix[0].length && y>=0) {
68              return colorMatrix[x][y];
69          }else {
70              return Color.black;
71          }
72      }
73  
74      public int getWidth()
75      {
76          return matrix.length;
77      }
78      public int getHeight()
79      {
80          return matrix[0].length;
81      }
82  
83      public boolean isUsed(int x, int y)
84      {
85          if(x>=0 && x<matrix.length && y<matrix[0].length) {
86              if(y>=0) {
87                  return matrix[x][y];
88              }else {
89                  return false;
90              }
91          }else {
92              return true;
93          }
94      }
95  
96      public void setUsed(int x, int y, Color c)
97      {
98          if(x>=0 && x<matrix.length && y>=0 && y<matrix[0].length) {
99              matrix[x][y]=true;
100             colorMatrix[x][y]=c;
101         }
102     }
103     public void setUnused(int x, int y)
104     {
105         if(x>=0 && x<matrix.length && y>=0 && y<matrix[0].length) {
106             matrix[x][y] = false;
107         }
108     }
109 
110     public void fromString(String data) {
111         if(data.length()>=matrix.length*matrix[0].length) {
112             for(int i=0;i<matrix.length*matrix[0].length;i++) {
113                 char c=data.charAt(i);
114                 Color col=null;
115                 if(c=='0') {
116                     col = null;
117                 }else if(c=='1') {
118                     col = Color.red;
119                 }else if(c=='2') {
120                     col = Color.green;
121                 }else if(c=='3') {
122                     col = Color.yellow;
123                 }else if(c=='4') {
124                     col = Color.cyan;
125                 }else if(c=='5') {
126                     col = Color.blue;
127                 }else if(c=='6') {
128                     col = Color.pink;
129                 }else if(c=='7') {
130                     col = Color.magenta;
131                 }
132                 if(col==null) {
133                     matrix[i/matrix[0].length][i%matrix[0].length]=false;
134                     colorMatrix[i/matrix[0].length][i%matrix[0].length]=null;
135                 }else {
136                     matrix[i/matrix[0].length][i%matrix[0].length]=true;
137                     colorMatrix[i/matrix[0].length][i%matrix[0].length]=col;
138                 }
139             }
140         }
141     }
142     public String toString() {
143         StringBuffer sb = new StringBuffer(matrix.length*matrix[0].length);
144         for (int x = 0; x < matrix.length; x++) {
145             for (int y = 0; y < matrix[x].length; y++) {
146                 boolean bUsed = matrix[x][y];
147                 if(bUsed) {
148                     if(colorMatrix[x][y]==Color.red) {
149                         sb.append('1');
150                     }else if(colorMatrix[x][y]==Color.green) {
151                         sb.append('2');
152                     }else if(colorMatrix[x][y]==Color.yellow) {
153                         sb.append('3');
154                     }else if(colorMatrix[x][y]==Color.cyan) {
155                         sb.append('4');
156                     }else if(colorMatrix[x][y]==Color.blue) {
157                         sb.append('5');
158                     }else if(colorMatrix[x][y]==Color.pink) {
159                         sb.append('6');
160                     }else if(colorMatrix[x][y]==Color.magenta) {
161                         sb.append('7');
162                     }
163                 }else {
164                     sb.append('0');
165                 }
166             }
167         }
168         return sb.toString();
169     }
170 }