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
9 this.gridSize = gridSize;
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
23 // each element of this grid is an integer
24 // index into a tilesFitted array -- not
25 // very good object-oriented style!
29 public TileGrid copy() {
30 TileGrid tg = new TileGrid( gridSize );
32 for( int i = 0; i < gridSize; ++i ) {
33 for( int j = 0; j < gridSize; ++j ) {
34 tg.grid[i][j] = grid[i][j];
41 public boolean anyValidFit( Tile tileToFit,
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 );
51 public boolean validFitNorth( Tile tileToFit,
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" );
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;
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 );
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 );
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 );
82 System.printString( "\n" );
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 &&
91 (grid[tileToFit.x][tileToFit.y-1] == -1 ||
92 tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
94 (grid[tileToFit.x+1][tileToFit.y] == -1 ||
95 tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) &&
97 (grid[tileToFit.x-1][tileToFit.y] == -1 ||
98 tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w) ) {
106 public boolean validFitSouth( Tile tileToFit,
108 Tile[] tilesFitted ) {
109 //System.printString( "top of validFitSouth\n" );
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;
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 &&
122 (grid[tileToFit.x][tileToFit.y+1] == -1 ||
123 tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
125 (grid[tileToFit.x+1][tileToFit.y] == -1 ||
126 tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) &&
128 (grid[tileToFit.x-1][tileToFit.y] == -1 ||
129 tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w) ) {
137 public boolean validFitEast( Tile tileToFit,
139 Tile[] tilesFitted ) {
140 //System.printString( "top of validFitEast\n" );
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;
148 System.printString( "raw grid:\n" );
151 System.printString( "x: " );
152 System.printInt( tileToFit.x );
153 System.printString( "\n" );
155 System.printString( "y: " );
156 System.printInt( tileToFit.y );
157 System.printString( "\n" );
159 System.printString( "tile index 1: " );
160 System.printInt( grid[tileToFit.x][tileToFit.y-1] );
161 System.printString( "\n" );
163 System.printString( "tile index 2: " );
164 System.printInt( grid[tileToFit.x][tileToFit.y+1] );
165 System.printString( "\n" );
167 System.printString( "tile index 3: " );
168 System.printInt( grid[tileToFit.x+1][tileToFit.y] );
169 System.printString( "\n" );
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 &&
178 ( grid[tileToFit.x][tileToFit.y-1] == -1 ||
179 tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
181 ( grid[tileToFit.x][tileToFit.y+1] == -1 ||
182 tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
184 ( grid[tileToFit.x+1][tileToFit.y] == -1 ||
185 tilesFitted[grid[tileToFit.x+1][tileToFit.y]].w == tileToFit.e) ) {
193 public boolean validFitWest( Tile tileToFit,
195 Tile[] tilesFitted ) {
196 //System.printString( "top of validFitWest\n" );
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;
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 &&
209 (grid[tileToFit.x][tileToFit.y-1] == -1 ||
210 tilesFitted[grid[tileToFit.x][tileToFit.y-1]].s == tileToFit.n) &&
212 (grid[tileToFit.x][tileToFit.y+1] == -1 ||
213 tilesFitted[grid[tileToFit.x][tileToFit.y+1]].n == tileToFit.s) &&
215 (grid[tileToFit.x-1][tileToFit.y] == -1 ||
216 tilesFitted[grid[tileToFit.x-1][tileToFit.y]].e == tileToFit.w) ) {
225 // indices to represent the bounding
226 // box of tiles placed in the grid
227 public int x0, y0, x1, y1;
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] );
234 if( grid[i][j] < 0 ) {
235 System.printString( " " );
237 System.printString( " " );
240 System.printString("\n");
244 public void printGrid( Tile[] tilesFitted ) {
246 System.printString( "Printing a grid...\n" );
249 computeBoundingBox();
251 for( int j = y0; j <= y1; ++j )
253 for( int i = x0; i <= x1; ++i )
255 System.printString( "i=" );
256 System.printInt( i );
257 System.printString( ", j=" );
258 System.printInt( j );
259 //System.printString( "\n" );
261 if( grid[i][j] == -1 ) {
264 tilesFitted[grid[i][j]].printRow0();
267 System.printString( "\n" );
269 for( int i = x0; i <= x1; ++i )
271 System.printString( "i=" );
272 System.printInt( i );
273 System.printString( ", j=" );
274 System.printInt( j );
275 //System.printString( "\n" );
277 if( grid[i][j] == -1 ) {
280 tilesFitted[grid[i][j]].printRow1();
283 System.printString( "\n" );
285 for( int i = x0; i <= x1; ++i )
287 System.printString( "i=" );
288 System.printInt( i );
289 System.printString( ", j=" );
290 System.printInt( j );
291 //System.printString( "\n" );
293 iString( "\n" )f( grid[i][j] == -1 ) {
296 tilesFitted[grid[i][j]].printRow2();
299 System.printString( "\n" );
301 for( int i = x0; i <= x1; ++i )
303 System.printString( "i=" );
304 System.printInt( i );
305 System.printString( ", j=" );
306 System.printInt( j );
307 //System.printString( "\n" );
309 if( grid[i][j] == -1 ) {
312 tilesFitted[grid[i][j]].printRow3();
315 System.printString( "\n" );
317 for( int i = x0; i <= x1; ++i )
319 System.printString( "i=" );
320 System.printInt( i );
321 System.printString( ", j=" );
322 System.printInt( j );
323 //System.printString( "\n" );
325 if( grid[i][j] == -1 ) {
328 tilesFitted[grid[i][j]].printRow4();
331 System.out.println();
336 public void printEmptyTileRow() {
337 System.printString( " \n" );
340 public void computeBoundingBox() {
341 System.printString( "Starting computeBoundingBox\n" );
344 while( i < gridSize*gridSize ) {
345 int a = i % gridSize;
346 int b = i / gridSize;
348 if( grid[b][a] != -1 ) {
351 // this statement is like "break"
352 i = gridSize*gridSize;
359 while( i < gridSize*gridSize ) {
360 int a = i % gridSize;
361 int b = i / gridSize;
363 if( grid[a][b] != -1 ) {
366 // this statement is like "break"
367 i = gridSize*gridSize;
374 while( i < gridSize*gridSize ) {
375 int a = i % gridSize;
376 int b = i / gridSize;
377 int c = gridSize - 1 - b;
379 if( grid[c][a] != -1 ) {
382 // this statement is like "break"
383 i = gridSize*gridSize;
390 while( i < gridSize*gridSize ) {
391 int a = i % gridSize;
392 int b = i / gridSize;
393 int c = gridSize - 1 - b;
395 if( grid[a][c] != -1 ) {
398 // this statement is like "break"
399 i = gridSize*gridSize;
405 System.printString( "Ending computeBoundingBox\n" );