1 public class SubProblem {
11 public Tile[] tilesToFit;
12 public Tile[] tilesFitted;
13 public TileGrid workingGrid;
15 // these indices are into the respective
17 public int indexToFit;
18 public int indexFitted;
20 // this score represents the evaluation
21 // of every arrangement of this sub-problem's
22 // bestArrangements list
25 public boolean partial;
27 public void incrementIndices() {
29 if( indexFitted == tilesFitted.length ) {
35 public void initializeSubProblem( SubProblem nsp, int checkingFits ) {
36 nsp.tilesToFit = new Tile[tilesToFit.length - 1];
40 for( int i = 0; i < tilesToFit.length; ++i ) {
41 // copy everything but the tile that
42 // is being moved to the fitted list
43 if( i != indexToFit ) {
44 nsp.tilesToFit[j] = tilesToFit[i].copy();
49 nsp.tilesFitted = new Tile[tilesFitted.length + 1];
50 nsp.tilesFitted[nsp.tilesFitted.length - 1] = tilesToFit[indexToFit].copy();
52 for( int i = 0; i < tilesFitted.length; ++i ) {
53 nsp.tilesFitted[i] = tilesFitted[i].copy();
54 // if((checkingFits == 1) ||
55 // (checkingFits == 3)) {
56 nsp.tilesFitted[i].x = tilesFitted[i].x;
57 nsp.tilesFitted[i].y = tilesFitted[i].y;
61 // set fitted tiles position according to fit type
62 if( checkingFits == 1 ) {
63 nsp.tilesFitted[nsp.tilesFitted.length - 1].x =
64 tilesFitted[indexFitted].x;
65 nsp.tilesFitted[nsp.tilesFitted.length - 1].y =
66 tilesFitted[indexFitted].y - 1;
67 } else if( checkingFits == 2 ) {
68 nsp.tilesFitted[nsp.tilesFitted.length - 1].x =
69 tilesFitted[indexFitted].x;
70 nsp.tilesFitted[nsp.tilesFitted.length - 1].y =
71 tilesFitted[indexFitted].y + 1;
72 } else if( checkingFits == 3 ) {
73 nsp.tilesFitted[nsp.tilesFitted.length - 1].x =
74 tilesFitted[indexFitted].x + 1;
75 nsp.tilesFitted[nsp.tilesFitted.length - 1].y =
76 tilesFitted[indexFitted].y;
77 } else { // ( checkingFits == 4 )
78 nsp.tilesFitted[nsp.tilesFitted.length - 1].x =
79 tilesFitted[indexFitted].x - 1;
80 nsp.tilesFitted[nsp.tilesFitted.length - 1].y =
81 tilesFitted[indexFitted].y;
84 // copy grid and place newly fitted tile in sub-problem's
85 // version of the grid
86 nsp.workingGrid = workingGrid.copy();
87 nsp.workingGrid.grid[nsp.tilesFitted[nsp.tilesFitted.length - 1].x]
88 [nsp.tilesFitted[nsp.tilesFitted.length - 1].y] =
89 nsp.tilesFitted.length - 1;
91 nsp.highScore = highScore;
94 System.printString( "-----------new sub-problem------------\n" );
95 //System.printString( "raw grid\n" );
96 //nsp.workingGrid.printGridRaw();
97 System.printString( "tiles fitted:\n" );
98 nsp.printTileArray( nsp.tilesFitted );
99 System.printString( "tiles to fit:\n" );
100 nsp.printTileArray( nsp.tilesToFit );
101 //System.printString( "the nice grid:\n" );
102 //nsp.workingGrid.printGrid( nsp.tilesFitted );
103 System.printString( "nsp.indexToFit: " );
104 System.printInt( nsp.indexToFit );
105 System.printString( "\n" );
106 System.printString( "nsp.indexFitted: " );
107 System.printInt( nsp.indexFitted );
108 System.printString( "\n" );
109 System.printString( "-----------end sub-problem------------\n" );
113 public void scoreWorkingGrid() {
115 for( int i = 0; i < tilesFitted.length; ++i ) {
116 Tile tileToScore = tilesFitted[i];
117 // add those face values that are not adjacent to other face
119 if(this.workingGrid.grid[tileToScore.x][tileToScore.y-1] == -1) {
120 highScore += tileToScore.n;
123 if(this.workingGrid.grid[tileToScore.x][tileToScore.y+1] == -1) {
124 highScore += tileToScore.s;
127 if(this.workingGrid.grid[tileToScore.x+1][tileToScore.y] == -1) {
128 highScore += tileToScore.e;
131 if(this.workingGrid.grid[tileToScore.x-1][tileToScore.y] == -1) {
132 highScore += tileToScore.w;
137 public printTileArray( Tile tiles[] ) {
138 for( int i = 0; i < tiles.length; ++i ) {
139 tiles[i].printRow0();
140 System.printString( " " );
142 System.printString( "\n" );
144 for( int i = 0; i < tiles.length; ++i ) {
145 tiles[i].printRow1();
146 System.printString( " " );
148 System.printString( "\n" );
150 for( int i = 0; i < tiles.length; ++i ) {
151 tiles[i].printRow2();
152 System.printString( " " );
154 System.printString( "\n" );
156 for( int i = 0; i < tiles.length; ++i ) {
157 tiles[i].printRow3();
158 System.printString( " " );
160 System.printString( "\n" );
162 for( int i = 0; i < tiles.length; ++i ) {
163 tiles[i].printRow4();
164 System.printString( ", " );
166 System.printString( "\n" );