private void restartGame() newGame();

if (key == Canvas.UP) movePlayer(0, -1); else if (key == Canvas.DOWN) movePlayer(0, 1); else if (key == Canvas.LEFT) movePlayer(-1, 0); else if (key == Canvas.RIGHT) movePlayer(1, 0); else if (key == Canvas.FIRE) // Manually pick diamond if adjacent (simple logic) for (int dy=-1; dy<=1; dy++) for (int dx=-1; dx<=1; dx++) if (Math.abs(dx)+Math.abs(dy)==1) int x = playerX+dx, y = playerY+dy; if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT && map[y][x]==TILE_DIAMOND) movePlayer(dx, dy); return;

private void drawGameOver(Graphics g) g.setColor(0, 0, 0); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(255, 0, 0); g.drawString("GAME OVER", getWidth()/2, 80, Graphics.HCENTER); g.setColor(255, 255, 255); g.drawString("Press * to restart", getWidth()/2, 150, Graphics.HCENTER);

public void handleInput() int key = getGameAction(getKeyStates());

switch (map[y][x]) Graphics.LEFT); break; case TILE_PLAYER: g.setColor(0, 200, 0); g.fillRect(px, py, TILE_SIZE-1, TILE_SIZE-1); g.setColor(255, 255, 255); g.drawString("@", px+5, py+2, Graphics.TOP

public class DiamondRush extends MIDlet implements CommandListener, Runnable { private Display display; private GameCanvas canvas; private Command exitCommand; private boolean running; private Thread gameThread;

/* * Diamond Rush for Nokia X2-01 (320x240) * Controls: 2=Up, 8=Down, 4=Left, 6=Right, 5=Pick Diamond, * = Restart * Goal: Collect all diamonds to unlock the exit door. */ import javax.microedition.lcdui. ; import javax.microedition.midlet. ; import java.util.Random;

// Map dimensions private static final int WIDTH = 15; // 15 tiles wide private static final int HEIGHT = 12; // 12 tiles high private static final int TILE_SIZE = 20; // 20x20 pixels (20*15=300, 20*12=240)

public DiamondRush() display = Display.getDisplay(this); canvas = new GameCanvas(); exitCommand = new Command("Exit", Command.EXIT, 1); canvas.addCommand(exitCommand); canvas.setCommandListener(this);

public void pauseApp() {} public void destroyApp(boolean unconditional) running = false;

// Place exit door at bottom-right area int exitX = WIDTH-2, exitY = HEIGHT-2; while (map[exitY][exitX] != TILE_EMPTY && exitX > 1 && exitY > 1) exitX--; exitY--; map[exitY][exitX] = TILE_EXIT;