2 public Tile( int n, int s, int e, int w ) {
13 Tile t = new Tile( n, s, e, w );
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" );
25 public void printRow0() {
26 System.printString ( "+-------+" );
31 System.printString( "| " );
33 System.printString( "| " );
36 System.printString( " |" );
39 public void printRow2() {
41 System.printString( "|" );
43 System.printString( "| " );
45 System.printInt ( w );
47 System.printString( " " );
49 System.printString( " " );
51 System.printInt ( e );
52 System.printString ( " |" );
55 public void printRow3() {
57 System.printString( "| " );
59 System.printString( "| " );
61 System.printInt ( s );
62 System.printString ( " |" );
65 public void printRow4() {
66 System.printString ( "+-------+" );
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
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
84 this.gridSize = gridSize;
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
98 // each element of this grid is an integer
99 // index into a tilesFitted array -- not
100 // very good object-oriented style!
103 public TileGrid copy() {
104 TileGrid tg = new TileGrid( gridSize );
106 for( int i = 0; i < gridSize; ++i ) {
107 for( int j = 0; j < gridSize; ++j ) {
108 tg.grid[i][j] = grid[i][j];
115 public boolean anyValidFit( Tile tileToFit,
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 );
125 public boolean validFitNorth( Tile tileToFit,
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" );
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;
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 );
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 );
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 );
156 System.printString( "\n" );
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 &&
164 (grid[tileToFit.x][tileToFit.y-1] == -1 ||
165 tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
167 (grid[tileToFit.x+1][tileToFit.y] == -1 ||
168 tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) &&
170 (grid[tileToFit.x-1][tileToFit.y] == -1 ||
171 tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w) ) {
179 public boolean validFitSouth( Tile tileToFit,
181 Tile[] tilesFitted ) {
182 //System.printString( "top of validFitSouth\n" );
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;
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 &&
195 (grid[tileToFit.x][tileToFit.y+1] == -1 ||
196 tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
198 (grid[tileToFit.x+1][tileToFit.y] == -1 ||
199 tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) &&
201 (grid[tileToFit.x-1][tileToFit.y] == -1 ||
202 tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w) ) {
210 public boolean validFitEast( Tile tileToFit,
212 Tile[] tilesFitted ) {
213 //System.printString( "top of validFitEast\n" );
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;
221 System.printString( "raw grid:\n" );
224 System.printString( "x: " );
225 System.printInt( tileToFit.x );
226 System.printString( "\n" );
228 System.printString( "y: " );
229 System.printInt( tileToFit.y );
230 System.printString( "\n" );
232 System.printString( "tile index 1: " );
233 System.printInt( grid[tileToFit.x][tileToFit.y-1] );
234 System.printString( "\n" );
236 System.printString( "tile index 2: " );
237 System.printInt( grid[tileToFit.x][tileToFit.y+1] );
238 System.printString( "\n" );
240 System.printString( "tile index 3: " );
241 System.printInt( grid[tileToFit.x+1][tileToFit.y] );
242 System.printString( "\n" );
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 &&
251 ( grid[tileToFit.x][tileToFit.y-1] == -1 ||
252 tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
254 ( grid[tileToFit.x][tileToFit.y+1] == -1 ||
255 tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
257 ( grid[tileToFit.x+1][tileToFit.y] == -1 ||
258 tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) ) {
266 public boolean validFitWest( Tile tileToFit,
268 Tile[] tilesFitted ) {
269 //System.printString( "top of validFitWest\n" );
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;
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 &&
282 (grid[tileToFit.x][tileToFit.y-1] == -1 ||
283 tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
285 (grid[tileToFit.x][tileToFit.y+1] == -1 ||
286 tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
288 (grid[tileToFit.x-1][tileToFit.y] == -1 ||
289 tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w) ) {
298 // indices to represent the bounding
299 // box of tiles placed in the grid
300 public int x0, y0, x1, y1;
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] );
307 if( grid[i][j] < 0 ) {
308 System.printString( " " );
311 System.printString( " " );
314 System.printString( "\n" );
318 public void printGrid( Tile[] tilesFitted ) {
320 System.printString( "Printing a grid...\n" );
323 computeBoundingBox();
325 for( int j = y0; j <= y1; ++j )
327 for( int i = x0; i <= x1; ++i )
329 System.printString( "i=" );
330 System.printInt( i );
331 System.printString( ", j=" );
332 System.printInt( j );
333 //System.printString( "\n" );
335 if( grid[i][j] == -1 ) {
338 tilesFitted[grid[i][j]].printRow0();
341 System.printString( "\n" );
343 for( int i = x0; i <= x1; ++i )
345 System.printString( "i=" );
346 System.printInt( i );
347 System.printString( ", j=" );
348 System.printInt( j );
349 //System.printString( "\n" );
351 if( grid[i][j] == -1 ) {
354 tilesFitted[grid[i][j]].printRow1();
357 System.printString( "\n" );
359 for( int i = x0; i <= x1; ++i )
361 System.printString( "i=" );
362 System.printInt( i );
363 System.printString( ", j=" );
364 System.printInt( j );
365 //System.printString( "\n" );
367 if( grid[i][j] == -1 ) {
370 tilesFitted[grid[i][j]].printRow2();
373 System.printString( "\n" );
375 for( int i = x0; i <= x1; ++i )
377 System.printString( "i=" );
378 System.printInt( i );
379 System.printString( ", j=" );
380 System.printInt( j );
381 //System.printString( "\n" );
383 if( grid[i][j] == -1 ) {
386 tilesFitted[grid[i][j]].printRow3();
389 System.printString( "\n" );
391 for( int i = x0; i <= x1; ++i )
393 System.printString( "i=" );
394 System.printInt( i );
395 System.printString( ", j=" );
396 System.printInt( j );
397 //System.printString( "\n" );
399 if( grid[i][j] == -1 ) {
402 tilesFitted[grid[i][j]].printRow4();
405 System.printString( "\n" );
410 public void printEmptyTileRow() {
411 System.printString( " " );
414 public void computeBoundingBox() {
415 System.printString( "Starting computeBoundingBox\n" );
418 while( i < gridSize*gridSize ) {
419 int a = i % gridSize;
420 int b = i / gridSize;
422 if( grid[b][a] != -1 ) {
425 // this statement is like "break"
426 i = gridSize*gridSize;
433 while( i < gridSize*gridSize ) {
434 int a = i % gridSize;
435 int b = i / gridSize;
437 if( grid[a][b] != -1 ) {
440 // this statement is like "break"
441 i = gridSize*gridSize;
448 while( i < gridSize*gridSize ) {
449 int a = i % gridSize;
450 int b = i / gridSize;
451 int c = gridSize - 1 - b;
453 if( grid[c][a] != -1 ) {
456 // this statement is like "break"
457 i = gridSize*gridSize;
464 while( i < gridSize*gridSize ) {
465 int a = i % gridSize;
466 int b = i / gridSize;
467 int c = gridSize - 1 - b;
469 if( grid[a][c] != -1 ) {
472 // this statement is like "break"
473 i = gridSize*gridSize;
479 System.printString( "Ending computeBoundingBox\n" );