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