FileDocCategorySizeDatePackage
Board.javaAPI DocJ2ME MIDP 2.010352Thu Nov 07 12:02:20 GMT 2002example.tilepuzzle

Board

public class Board extends Canvas implements CommandListener

Fields Summary
javax.microedition.midlet.MIDlet
midlet
Display
dpy
Options
options
String
letters
Font
font
Piece
blankp
Piece[]
all
Piece[]
grid
Random
rand
int
gridx
int
gridy
int
gridw
int
gridh
int
cellw
int
cellh
int
cellyoff
int
cellxoff
static final int
CMD_ABOUT
static final int
CMD_EXIT
static final int
CMD_OPTIONS
static final int
CMD_RESET
static final int
CMD_START
static final int
CMD_UNLOCK
static final int
CMD_ZLAST
Command[]
cmd
static final int
INITIALIZED
static final int
PLAYING
static final int
WON
int
gameState
boolean
cheated
Constructors Summary
public Board(javax.microedition.midlet.MIDlet midlet_)


	int i;

	// "global" variables

	midlet = midlet_;
	dpy = Display.getDisplay(midlet);
	gridw = 4;
	gridh = 4;

	font = Font.getFont(Font.FACE_SYSTEM,
	    Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

	// REMIND update when font metrics info gets implemented
	cellw = font.charWidth('M") + 7;
	cellh = font.getHeight() + 1;
	cellxoff = 3;
	cellyoff = 0;

	gridx = (getWidth() - (gridw*cellw) + 1) / 2;
	gridy = 10;

	cheated = false;
	rand = new Random();

	// create the grid arrays

	grid = new Piece[gridw][];
	for (i = 0; i < gridw; i++) {
		grid[i] = new Piece[gridh];
	}

	all = new Piece[gridw*gridh];
	for (i = 0; i < (gridw*gridh)-1; i++) {
		int x = i % gridw;
		int y = i / gridw;
		String s = letters.substring(i, i+1);
		grid[x][y] =
		    all[i] =
			new Piece(s, i, x, y, i < (gridw*gridh/2));
	}

	// make the special blank piece
	
	blankp = new Piece(null, gridw*gridh-1, gridw-1, gridh-1, false);
	grid[gridw-1][gridh-1] = blankp;
	all[gridw*gridh-1] = blankp;

	// set up commands

	cmd = new Command[CMD_ZLAST];

	cmd[CMD_ABOUT] =
	    new BoardCommand("About", Command.HELP, 5, CMD_ABOUT);

	cmd[CMD_EXIT] =
	    new BoardCommand("Exit", Command.EXIT, 6, CMD_EXIT);

	cmd[CMD_OPTIONS] =
	    new BoardCommand("Options", Command.SCREEN, 3, CMD_OPTIONS);
	
	cmd[CMD_RESET] = 
	    new BoardCommand("Reset", Command.SCREEN, 1, CMD_RESET);

	cmd[CMD_START] =
	    new BoardCommand("Start", Command.SCREEN, 1, CMD_START);

	cmd[CMD_UNLOCK] =
	    new BoardCommand("Unlock", Command.SCREEN, 4, CMD_UNLOCK);

	// set up the listener

	setCommandListener(this);

	// set up options screen

	options = new Options(dpy, this);

	// set up initial state

	setState(INITIALIZED);
Methods Summary
voidD(java.lang.String s)



    System.out.println(s); 
public voidcommandAction(Command c, Displayable d)

	switch (((BoardCommand) c).tag) {
	    case CMD_ABOUT:
		About.showAbout(Display.getDisplay(midlet));
		break;
	    case CMD_EXIT:
		midlet.notifyDestroyed();
		break;
	    case CMD_OPTIONS:
		dpy.setCurrent(options);
		break;
	    case CMD_RESET:
		cheated = false;
		resetGrid();
		setState(INITIALIZED);
		repaint();
		break;
	    case CMD_START:
		cheated = false;

		if (options.funny) {
			rearrangeFunnily(options.hard);
		} else {
			randomize(options.hard);
		}

		setState(PLAYING);
		repaint();
		break;
	    case CMD_UNLOCK:
		cheated = true;
		setState(PLAYING);
		repaint();
		break;
	}
intdisplacement()

	boolean temp[] = new boolean[gridw*gridh-1];	// all false
	int n = 0;

	for (int i = 0; i < gridh; i++) {
		for (int j = 0; j < gridw; j++) {
			Piece p = grid[j][i];
			if (p == blankp)
			    continue;
			temp[p.serial] = true;
			for (int k = 0; k < p.serial; k++) {
				if (!temp[k])
				    n++;
			}
		}
	}
	return n;
public voidhideNotify()

	// System.out.println("Board: hideNotify");
booleanisSolved()

	for (int i = 0; i < gridh; i++) {
		for (int j = 0; j < gridw; j++) {
			if (! grid[j][i].isHome()) {
				return false;
			}
		}
	}
	return true;
public voidkeyPressed(int code)

	if (gameState != PLAYING)
	    return;

	int game = getGameAction(code);

	int swapx = blankp.x;
	int swapy = blankp.y;

	int direction = (options.reversed ? -1 : 1);

	switch (game) {
	    case Canvas.UP:
		swapy += direction;
		break;
	    case Canvas.DOWN:
		swapy -= direction;
		break;
	    case Canvas.LEFT:
		swapx += direction;
		break;
	    case Canvas.RIGHT:
		swapx -= direction;
		break;
	    default:
		return;
	}

	if (swapx < 0 || swapx >= gridw ||
	    swapy < 0 || swapy >= gridh) {
		return;
	}

	moveBlank(swapx, swapy);
	repaint();

	if (isSolved()) {
		setState(WON);
	}
voidmoveBlank(int swapx, int swapy)

	setGrid(grid[swapx][swapy], blankp.x, blankp.y);
	setGrid(blankp, swapx, swapy);
public voidpaint(Graphics g)

	g.setColor(0xFFFFFF);
	g.fillRect(0, 0, getWidth(), getHeight());
	    
	g.translate(gridx, gridy);
	g.setColor(0);
	g.drawRect(-2, -2, gridw*cellw + 2, gridh*cellh + 2);

	for (int j = 0; j < gridw; j++) {
		for (int k = 0; k < gridh; k++) {
			grid[j][k].paint(g);
		}
	}

	if (gameState == WON) {
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setColor(0);
		g.setFont(Font.getDefaultFont());
		g.drawString(
		    (cheated ? "CHEATER!" : "YOU WIN!"),
		    getWidth() / 2,
		    getHeight() - 1,
		    Graphics.BOTTOM|Graphics.HCENTER);
	}
intrandRange(int n)

	int r = rand.nextInt() % n;
	if (r < 0)
	    r += n;
	return r;
voidrandomize(boolean hard)

	shuffle();
	int ra, rb;
	int x, y;

	if (hard) {
		ra = 7;
		rb = 0;
	} else {
		ra = 0;
		rb = 7;
	}

	x = rand.nextInt() & 1;
	y = rand.nextInt() & 1;

	if (x == 1 && y == 1) {
		x = 2;
		y = 0;
	}

	swap(x, y, all[ra].x, all[ra].y);
	swap((rand.nextInt()&1)+1, 3, all[rb].x, all[rb].y);

	if ((displacement() & 1) == 1)
	    swap(1, 3, 2, 3);
voidrandomize_by_moving()

	int dx, dy, v;

	for (int i = 0; i < 100; i++) {
		dx = dy = 0;
		v = (rand.nextInt() & 2) - 1;	    // 1 or -1
		if ((rand.nextInt() & 1) == 0)
		    dx = v;
		else
		    dy = v;

		if (blankp.x + dx < 0)
		    dx = 1;
		if (blankp.x + dx == gridw)
		    dx = -1;

		if (blankp.y + dy < 0)
		    dy = 1;
		if (blankp.y + dy == gridh)
		    dy = -1;

		moveBlank(blankp.x + dx, blankp.y + dy);
	}

	// now move the blank tile to the lower right corner

	while (blankp.x != gridw-1)
	    moveBlank(blankp.x + 1, blankp.y);

	while (blankp.y != gridh-1)
	    moveBlank(blankp.x, blankp.y + 1);
voidrearrangeFunnily(boolean hard)

	resetGrid();

	if (hard) {
		// RATE YOUR MIDP LAN
		swap(0, 0, 3, 1);
		swap(2, 2, 3, 2);
		swap(3, 2, 0, 3);
		swap(0, 3, 2, 3);
	} else {
		// RATE YOUR MIDP NAL
		swap(2, 2, 3, 2);
		swap(3, 2, 0, 3);
	}
voidresetGrid()

	Piece temp[] = new Piece[gridw*gridh];
	int k = 0;

	for (int i = 0; i < gridw; i++) {
		for (int j = 0; j < gridh; j++) {
			temp[k++] = grid[i][j];
		}
	}

	for (k = 0; k < temp.length; k++) {
		temp[k].goHome();
	}
voidsetGrid(example.tilepuzzle.Board$Piece p, int x, int y)

	grid[x][y] = p;
	p.setLocation(x, y);
voidsetState(int ns)

	gameState = ns;
	switch (gameState) {
	    case INITIALIZED:
		addCommand(cmd[CMD_ABOUT]);
		removeCommand(cmd[CMD_RESET]);
		addCommand(cmd[CMD_START]);
		addCommand(cmd[CMD_UNLOCK]);
		addCommand(cmd[CMD_EXIT]);
		addCommand(cmd[CMD_OPTIONS]);
		break;
	    case PLAYING:
		addCommand(cmd[CMD_ABOUT]);
		addCommand(cmd[CMD_RESET]);
		removeCommand(cmd[CMD_START]);
		removeCommand(cmd[CMD_UNLOCK]);
		addCommand(cmd[CMD_EXIT]);
		addCommand(cmd[CMD_OPTIONS]);
		break;
	    case WON:
		addCommand(cmd[CMD_ABOUT]);
		removeCommand(cmd[CMD_RESET]);
		addCommand(cmd[CMD_START]);
		addCommand(cmd[CMD_UNLOCK]);
		addCommand(cmd[CMD_EXIT]);
		addCommand(cmd[CMD_OPTIONS]);
		break;
	}
public voidshowNotify()

	// System.out.println("Board: showNotify");
voidshuffle()

	int limit = gridw*gridh-1;
	Piece ta[] = new Piece[limit];
	Piece temp;

	System.arraycopy(all, 0, ta, 0, limit);
	for (int i = 0; i < limit; i++) {
		int j = randRange(limit);
		temp = ta[j];
		ta[j] = ta[i];
		ta[i] = temp;
	}

	for (int i = 0; i < limit; i++) {
		setGrid(ta[i], i/gridw, i%gridw);
	}

	setGrid(blankp, gridw-1, gridh-1);
voidswap(int x1, int y1, int x2, int y2)

	Piece t = grid[x1][y1];
	setGrid(grid[x2][y2], x1, y1);
	setGrid(t, x2, y2);