e28628d2ff1aeb369d26961484d542faa1e02e3f
[IRC.git] / Robust / src / Benchmarks / TileSearch / Java / TileGrid.java
1 public class TileGrid {
2     public TileGrid( int gridSize ) {
3         // make the grid size big enough
4         // such that starting with a tile
5         // in the middle and placing tiles
6         // in one direction, that the grid
7         // is big enough without requiring
8         // bound-checking
9         this.gridSize = gridSize;
10
11         grid = new int[gridSize][];
12         for( int i = 0; i < gridSize; ++i ) {
13             grid[i] = new int[gridSize];
14             for( int j = 0; j < gridSize; ++j ) {
15                 // use -1 to indicate no tile
16                 grid[i][j] = -1;
17             }
18         }
19     }
20
21     public int gridSize;
22
23     // each element of this grid is an integer
24     // index into a tilesFitted array -- not
25     // very good object-oriented style!
26     public int grid[][];
27
28
29     public TileGrid copy() {
30         TileGrid tg = new TileGrid( gridSize );
31         
32         for( int i = 0; i < gridSize; ++i ) {
33             for( int j = 0; j < gridSize; ++j ) {
34                 tg.grid[i][j] = grid[i][j];
35             }
36         }
37
38         return tg;
39     }
40
41     public boolean anyValidFit( Tile   tileToFit, 
42                                 Tile   tileFitted,
43                                 Tile[] tilesFitted ) {
44         //System.printString( "top fo anyValidFit\n" );
45         return validFitNorth( tileToFit, tileFitted, tilesFitted ) ||
46                validFitSouth( tileToFit, tileFitted, tilesFitted ) ||
47                validFitEast ( tileToFit, tileFitted, tilesFitted ) ||
48                validFitWest ( tileToFit, tileFitted, tilesFitted );
49     }
50
51     public boolean validFitNorth( Tile   tileToFit,
52                                   Tile   tileFitted,
53                                   Tile[] tilesFitted ) {
54         //System.printString( "top of validFitNorth\n" );
55         //System.printString( "tileToFit.s:" + tileToFit.s + "\n" );
56         //System.printString( "tileFitted.n:" + tileFitted.n + "\n" );
57
58         // when the tileToFit's S matches fitted N...
59         if( tileToFit.s == tileFitted.n ) {
60             tileToFit.x = tileFitted.x;
61             tileToFit.y = tileFitted.y - 1;
62         
63             /*
64             System.printString( "Check if can put it here\n" );
65             System.printString( "x: " + tileToFit.x + "; y: " + tileToFit.y + "\n" );
66             System.printInt( grid[tileToFit.x][tileToFit.y]  );
67             System.printString( "\n" );
68             System.printInt( grid[tileToFit.x][tileToFit.y-1] );
69             if(grid[tileToFit.x][tileToFit.y-1] != -1) {
70                 System.printString( " s:" + tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s );
71             }
72             System.printString( "\n" );
73             System.printInt( grid[tileToFit.x+1][tileToFit.y] );
74             if(grid[tileToFit.x+1][tileToFit.y] != -1) {
75                 System.printString( " w:" + tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w );
76             }
77             System.printString( "\n" );
78             System.printInt( grid[tileToFit.x-1][tileToFit.y] );
79             if(grid[tileToFit.x-1][tileToFit.y] != -1) {
80                 System.printString( " e:" + tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e );
81             }
82             System.printString( "\n" );
83             */
84             
85             //  check that the place to fit is empty  AND
86             // (place to fit + N is empty or matches) AND
87             // (place to fit + E is empty or matches) AND
88             // (place to fit + W is empty or matches)
89             if( grid[tileToFit.x][tileToFit.y]                   == -1           &&
90
91                 (grid[tileToFit.x][tileToFit.y-1]                == -1 ||
92                  tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
93
94                 (grid[tileToFit.x+1][tileToFit.y]                == -1 ||
95                  tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) &&
96
97                 (grid[tileToFit.x-1][tileToFit.y]                == -1 ||
98                  tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w)   ) {
99                 return true;
100             }
101         }
102
103         return false;
104     }
105
106     public boolean validFitSouth( Tile   tileToFit,
107                                   Tile   tileFitted,
108                                   Tile[] tilesFitted ) {
109         //System.printString( "top of validFitSouth\n" );
110
111         // when the tileToFit's N matches fitted S...
112         if( tileToFit.n == tileFitted.s ) {
113             tileToFit.x = tileFitted.x;
114             tileToFit.y = tileFitted.y + 1;
115            
116             //  check that the place to fit is empty  AND
117             // (place to fit + S is empty or matches) AND
118             // (place to fit + E is empty or matches) AND
119             // (place to fit + W is empty or matches)
120             if( grid[tileToFit.x][tileToFit.y]                   == -1           &&
121
122                 (grid[tileToFit.x][tileToFit.y+1]                == -1 ||
123                  tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
124
125                 (grid[tileToFit.x+1][tileToFit.y]                == -1 ||
126                  tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) &&
127
128                 (grid[tileToFit.x-1][tileToFit.y]                == -1 ||
129                  tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w)   ) {
130                 return true;
131             }
132         }
133
134         return false;
135     }
136
137     public boolean validFitEast( Tile   tileToFit,
138                                  Tile   tileFitted,
139                                  Tile[] tilesFitted ) {
140         //System.printString( "top of validFitEast\n" );
141
142         // when the tileToFit's W matches fitted E...
143         if( tileToFit.w == tileFitted.e ) {
144             tileToFit.x = tileFitted.x + 1;
145             tileToFit.y = tileFitted.y;
146
147             /*
148             System.printString( "raw grid:\n" );
149             printGridRaw();
150
151             System.printString( "x: " );
152             System.printInt( tileToFit.x );
153             System.printString( "\n" );
154
155             System.printString( "y: " );
156             System.printInt( tileToFit.y );
157             System.printString( "\n" );
158
159             System.printString( "tile index 1: " );
160             System.printInt( grid[tileToFit.x][tileToFit.y-1] );
161             System.printString( "\n" );
162
163             System.printString( "tile index 2: " );
164             System.printInt( grid[tileToFit.x][tileToFit.y+1] );
165             System.printString( "\n" );
166
167             System.printString( "tile index 3: " );
168             System.printInt( grid[tileToFit.x+1][tileToFit.y] );
169             System.printString( "\n" );
170             */
171
172             //  check that the place to fit is empty  AND
173             // (place to fit + N is empty or matches) AND
174             // (place to fit + S is empty or matches) AND
175             // (place to fit + E is empty or matches)
176             if( grid[tileToFit.x][tileToFit.y]                   == -1           &&
177
178                 (            grid[tileToFit.x][tileToFit.y-1]    == -1 ||
179                  tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
180
181                 (            grid[tileToFit.x][tileToFit.y+1]    == -1 ||
182                  tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
183
184                 (            grid[tileToFit.x+1][tileToFit.y]    == -1 ||
185                  tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e)   ) {
186                 return true;
187             }
188         }
189
190         return false;
191     }
192
193     public boolean validFitWest( Tile   tileToFit,
194                                  Tile   tileFitted,
195                                  Tile[] tilesFitted ) {
196         //System.printString( "top of validFitWest\n" );
197
198         // when the tileToFit's E matches fitted W...
199         if( tileToFit.e == tileFitted.w ) {
200             tileToFit.x = tileFitted.x - 1;
201             tileToFit.y = tileFitted.y;
202            
203             //  check that the place to fit is empty  AND
204             // (place to fit + N is empty or matches) AND
205             // (place to fit + S is empty or matches) AND
206             // (place to fit + W is empty or matches)
207             if( grid[tileToFit.x][tileToFit.y]                   == -1           &&
208
209                 (grid[tileToFit.x][tileToFit.y-1]                == -1 ||
210                  tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
211
212                 (grid[tileToFit.x][tileToFit.y+1]                == -1 ||
213                  tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
214
215                 (grid[tileToFit.x-1][tileToFit.y]                == -1 ||
216                  tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w)   ) {
217                 return true;
218             }
219         }
220
221         return false;
222     }
223
224
225     // indices to represent the bounding
226     // box of tiles placed in the grid
227     public int x0, y0, x1, y1;
228
229     public void printGridRaw() {
230         for( int j = 0; j < gridSize; ++j ) {
231             for( int i = 0; i < gridSize; ++i ) {
232                 System.printInt( grid[i][j] );
233
234                 if( grid[i][j] < 0 ) {
235                     System.printString( " " );
236                 } else {
237                     System.printString( "  " );
238                 }
239             }
240             System.printString("\n");
241         }
242     }
243
244     public void printGrid( Tile[] tilesFitted ) {
245 /*      
246         System.printString( "Printing a grid...\n" );
247         printGridRaw();
248
249         computeBoundingBox();
250
251         for( int j = y0; j <= y1; ++j )
252         {
253             for( int i = x0; i <= x1; ++i )
254             {
255                 System.printString( "i=" );
256                 System.printInt( i );
257                 System.printString( ", j=" );
258                 System.printInt( j );
259                 //System.printString( "\n" );
260
261                 if( grid[i][j] == -1 ) {
262                     printEmptyTileRow();
263                 } else {
264                     tilesFitted[grid[i][j]].printRow0();
265                 }
266             }
267             System.printString( "\n" );
268
269             for( int i = x0; i <= x1; ++i )
270             {
271                 System.printString( "i=" );
272                 System.printInt( i );
273                 System.printString( ", j=" );
274                 System.printInt( j );
275                 //System.printString( "\n" );
276
277                 if( grid[i][j] == -1 ) {
278                     printEmptyTileRow();
279                 } else {
280                     tilesFitted[grid[i][j]].printRow1();
281                 }
282             }
283             System.printString( "\n" );
284
285             for( int i = x0; i <= x1; ++i )
286             {
287                 System.printString( "i=" );
288                 System.printInt( i );
289                 System.printString( ", j=" );
290                 System.printInt( j );
291                 //System.printString( "\n" );
292
293                 iString( "\n" )f( grid[i][j] == -1 ) {
294                     printEmptyTileRow();
295                 } else {
296                     tilesFitted[grid[i][j]].printRow2();
297                 }
298             }
299             System.printString( "\n" );
300
301             for( int i = x0; i <= x1; ++i )
302             {
303                 System.printString( "i=" );
304                 System.printInt( i );
305                 System.printString( ", j=" );
306                 System.printInt( j );
307                 //System.printString( "\n" );
308
309                 if( grid[i][j] == -1 ) {
310                     printEmptyTileRow();
311                 } else {
312                     tilesFitted[grid[i][j]].printRow3();
313                 }
314             }
315             System.printString( "\n" );
316
317             for( int i = x0; i <= x1; ++i )
318             {
319                 System.printString( "i=" );
320                 System.printInt( i );
321                 System.printString( ", j=" );
322                 System.printInt( j );
323                 //System.printString( "\n" );
324
325                 if( grid[i][j] == -1 ) {
326                     printEmptyTileRow();
327                 } else {
328                     tilesFitted[grid[i][j]].printRow4();
329                 }
330             }
331             System.out.println();
332         }
333 */
334     }
335
336     public void printEmptyTileRow() {
337         System.printString( "         \n" );
338     }
339
340     public void computeBoundingBox() {
341         System.printString( "Starting computeBoundingBox\n" );
342
343         int i = 0;
344         while( i < gridSize*gridSize ) {
345             int a = i % gridSize;
346             int b = i / gridSize;
347
348             if( grid[b][a] != -1 ) {
349                 x0 = b;
350
351                 // this statement is like "break"
352                 i = gridSize*gridSize;
353             }
354
355             ++i;
356         }
357
358         i = 0;
359         while( i < gridSize*gridSize ) {
360             int a = i % gridSize;
361             int b = i / gridSize;
362
363             if( grid[a][b] != -1 ) {
364                 y0 = b;
365
366                 // this statement is like "break"
367                 i = gridSize*gridSize;
368             }
369             
370             ++i;
371         }
372
373         i = 0;
374         while( i < gridSize*gridSize ) {
375             int a = i % gridSize;
376             int b = i / gridSize;
377             int c = gridSize - 1 - b;
378             
379             if( grid[c][a] != -1 ) {
380                 x1 = c;
381
382                 // this statement is like "break"
383                 i = gridSize*gridSize;
384             }
385             
386             ++i;
387         }
388
389         i = 0;
390         while( i < gridSize*gridSize ) {
391             int a = i % gridSize;
392             int b = i / gridSize;
393             int c = gridSize - 1 - b;
394             
395             if( grid[a][c] != -1 ) {
396                 y1 = c;
397
398                 // this statement is like "break"
399                 i = gridSize*gridSize;
400             }
401
402             ++i;
403         }
404
405         System.printString( "Ending computeBoundingBox\n" );
406     }
407 }