Java Reference
In-Depth Information
File: its/BlockPuzzle/BoardModel.java
package its.BlockPuzzle;
1.
2.
3.
4.
public class BoardModel{
5.
private int noOfRows, noOfCols;
6.
private int [][] board;
// The board as an array
7.
private int [] rowOfBlock; // The number of the row of every block
8.
private int [] colOfBlock; // The number of the column of every block
9.
10.
public BoardModel( int nr, int nc){
11.
noOfRows = nr;
12.
noOfCols = nc;
13.
board = new int [noOfRows][noOfCols];
14.
rowOfBlock = new int [noOfRows * noOfCols];
15.
colOfBlock = new int [noOfRows * noOfCols];
16.
// initialize the board. The blocks are
17.
// numbered row-wise 1,2,...
18.
int kk=1;
19.
20.
for ( int r=0; r < noOfRows; r++){
for ( int c=0; c < noOfCols; c++){
21.
if (kk < noOfRows * noOfCols){
22.
board[r][c] = kk;
23.
rowOfBlock[kk] = r;
24.
colOfBlock[kk] = c;
25.
kk++;
26.
27.
}
28.
}// for c
29.
}// for r
30.
31.
// ... and the missing block is at the lower right.
32.
board [noOfRows-1][noOfCols-1] = 0;
33.
rowOfBlock[0] = noOfRows-1;
34.
colOfBlock[0] = noOfCols-1;
35.
}
36.
37.
public boolean moveIt(MoveModel mm){
38.
int dir = mm.getDirection();
39.
int block = mm.getBlockNumber();
40.
int row
= rowOfBlock[block];
int col
= colOfBlock[block];
41.
boolean ok = true ;
42.
// an UP move is possible if the missing block
43.
Search WWH ::




Custom Search