www.pudn.com > j2mewireless_examples.zip > Sokoban.java


/*
 * @(#)Sokoban.java	1.4 01/04/04
 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
 */

package examples.sokoban;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;

/**
 * Sokoban implements the command handling and initialization
 * needed to be a MIDlet. It creates and initializes the commands,
 * scores, and Sokoban canvas that displays the game board.
 */
public class Sokoban extends MIDlet implements CommandListener {
	Display display;
	SokoCanvas canvas;
	Score score;
	Screen scoreScreen;
	
	Command undoCommand = new Command("Undo", Command.BACK, 1);
	Command restartCommand = new Command("Start Over", Command.SCREEN, 21);
	Command exitCommand = new Command("Exit", Command.EXIT, 60);
	Command scoresCommand = new Command("Show Scores", Command.SCREEN, 25);
	Command okCommand = new Command("OK", Command.OK, 30);
	Command nextCommand = new Command("Next Level", Command.SCREEN, 22);
	Command prevCommand = new Command("Previous Level", Command.SCREEN, 23);
	
	/** 
	 * Creates a new Sokoban instance and caches the Display,
	 * Score, and SokoCanvas objects.
	 */
	public Sokoban() {
		display = Display.getDisplay(this);
		score = new Score();
		canvas = new SokoCanvas(this, score);
	}

	/**
	 * The startApp method opens the score file and retrieves
	 * the last level.  The method initializes the canvas
	 * and adds the commands to it and make itself the listener
	 * for the commands.
	 * It should return immediately to keep the dispatcher
	 * from hanging.
	 */
	public void startApp() {
		score.open();
		canvas.addCommand(undoCommand);
		canvas.addCommand(scoresCommand);
		canvas.addCommand(restartCommand);
		canvas.addCommand(exitCommand);
		canvas.addCommand(nextCommand);
		canvas.addCommand(prevCommand);
		canvas.setCommandListener(this);
		display.setCurrent(canvas);
		canvas.init();
	}
	
	/**
	 * The pauseApp method is used to close the store of Scores.
	 */
	public void pauseApp() {
		score.close();
	}
	
	/**
	 * Destroy must cleanup.
	 * Close the store of scores.
	 */
	public void destroyApp(boolean unconditional) {
		score.close();
	}

	/*
	 * Respond to a commands issued on any Screen
	 */
	public void commandAction(Command c, Displayable s) {
		if (c == undoCommand) {
			canvas.undoMove();
		} else if (c == restartCommand) {
			canvas.restartLevel();
		} else if (c == scoresCommand) {
			scoreScreen = canvas.getScoreScreen();
			scoreScreen.addCommand(okCommand);
			scoreScreen.setCommandListener(this);
			display.setCurrent(scoreScreen);
		} else if (c == okCommand && s == scoreScreen) {
			display.setCurrent(canvas);
		} else if (c == exitCommand) {
			destroyApp(false);
			notifyDestroyed();
		} else if (c == List.SELECT_COMMAND && s == canvas) {
			// Solved the level
			scoreScreen = canvas.getScoreScreen();
			scoreScreen.addCommand(okCommand);
			scoreScreen.setCommandListener(this);
			display.setCurrent(scoreScreen);
			canvas.nextLevel(1);
		} else if (c == nextCommand) {
			canvas.nextLevel(1);
			display.setCurrent(canvas);
		} else if (c == prevCommand) {
			canvas.nextLevel(-1);
			display.setCurrent(canvas);
		}
	}
}