📄 puzzle.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class Puzzle extends MIDlet implements CommandListener {
public Display mDisplay;
public DrawBoard mDrawBoard;
public Form mFormHelp, mFormAbout;
public Command backCommand, exitCommand, helpCommand, aboutCommand;
public StringItem mStringHelp;
public ImageItem mImageAbout;
public Image xImgAbout;
public Puzzle() {
backCommand = new Command( "Back", Command.SCREEN, 0);
exitCommand = new Command( "Exit", Command.SCREEN, 1);
helpCommand = new Command( "Help", Command.SCREEN, 2);
aboutCommand = new Command( "About", Command.SCREEN, 3);
mDrawBoard = new DrawBoard();
mDrawBoard.addCommand( exitCommand);
mDrawBoard.setCommandListener( this);
mDrawBoard.addCommand( helpCommand);
mDrawBoard.setCommandListener( this);
mDrawBoard.addCommand( aboutCommand);
mDrawBoard.setCommandListener( this);
}
public void startApp() {
mDisplay = Display.getDisplay( this);
mDisplay.setCurrent( mDrawBoard);
}
public void pauseApp() {}
public void destroyApp( boolean unconditional) {}
public void commandAction( Command c, Displayable d) {
if ( c == exitCommand) {
destroyApp( false);
notifyDestroyed();
}
if ( c == helpCommand)
DisplayHelp();
if ( c == aboutCommand)
DisplayAbout();
if ( c == backCommand)
mDisplay.setCurrent( mDrawBoard);
}
public void DisplayHelp() {
mFormHelp = new Form( "How to play");
mStringHelp = new StringItem( null, " [*] = Random\n [0] = Pattern\n [#] = Mode\n [2] = Up\n [5] = Down\n [4] = Left\n [6] = Right");
backCommand = new Command( "Back", Command.SCREEN, 1);
mFormHelp.append( mStringHelp);
mFormHelp.addCommand( backCommand);
mFormHelp.setCommandListener( this);
mDisplay.setCurrent( mFormHelp);
}
public void DisplayAbout() {
mFormAbout = new Form( "About");
backCommand = new Command( "Back", Command.SCREEN, 1);
try {
xImgAbout = Image.createImage( "/puzzle.png");
} catch ( java.io.IOException e) {
System.out.println( "* Error loading image *");
}
mImageAbout = new ImageItem( "", xImgAbout, ImageItem.LAYOUT_CENTER, "");
mFormAbout.append( mImageAbout);
mFormAbout.addCommand( backCommand);
mFormAbout.setCommandListener( this);
mDisplay.setCurrent( mFormAbout);
}
}
class DrawBoard extends Canvas {
public int board[][] = new int[4][4];
public Font mFontPlain, mFontBold, mFontBoldLarge;
public int rb, cb, width, height;
public static int NUMERIC_MODE = 0;
public static int ALPHABETIC_MODE = 1;
public int modeBoard = NUMERIC_MODE;
public static int BY_RANDOM = 0;
public static int BY_PATTERN = 1;
public int makeBoard = BY_RANDOM;
public int pattern = 0;
public int counter = 0;
public DrawBoard() {
InitGame();
NewGame();
}
public void InitGame() {
int r, c;
rb = 3;
cb = 3;
width = getWidth();
height = getHeight();
mFontPlain = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
mFontBold = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
mFontBoldLarge = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
for( r = 0; r < 4; r++)
for( c = 0; c < 4; c++)
board[r][c] = (r * 4) + c + 1;
counter = 0;
repaint();
}
public void paint( Graphics g) {
int r, c, n;
int size = 15;
int offset = 5;
String p;
g.setColor( 255, 255, 255);
g.fillRect( 0, 0, width, height);
g.setColor( 0, 0, 0);
g.setFont( mFontBold);
for( r = 0; r < 4; r++) {
for( c = 0; c < 4; c++) {
if ( board[r][c] != 16) {
g.drawRoundRect( (c * size) + offset, (r * size) + offset, size, size, 2, 2);
if (modeBoard == 0) {
g.drawString( "" + board[r][c], (c * size) + size - 3, (r * size) + offset + 4, g.TOP|g.HCENTER);
} else {
String aChar = new Character((char)(board[r][c]+64)).toString();
g.drawString( aChar, (c * size) + size - 3, (r * size) + offset + 4, g.TOP|g.HCENTER);
}
}
}
}
g.drawString( "Move", (size * 5) + 6, offset + 4, g.TOP|g.HCENTER);
g.setFont( mFontPlain);
g.drawString( "# " + counter, (size * 5) + 6, size + offset + 4, g.TOP|g.HCENTER);
p = ( makeBoard == BY_RANDOM) ? "RND" : Integer.toString( pattern);
g.setFont( mFontBold);
g.drawString( "(" + p + ")", (size * 5) + 6, (size * 3), g.TOP|g.HCENTER);
n = 0;
g.setColor( 0, 0, 0);
for( r = 0; r < 4; r++) {
for( c = 0; c < 4; c++) {
if ( board[r][c] == (r * 4) + c + 1) {
n++;
g.fillRect( ( n * 5), (size * 5), 4, 4);
}
}
}
if ( MoveSuccess()) {
g.setColor( 255, 255, 255);
g.fillRect( 0, 0, width, height);
g.setColor( 0, 0, 0);
g.setFont( mFontBoldLarge);
g.drawString( "Success!", width / 2, 10, g.TOP|g.HCENTER);
g.setFont( mFontBold);
g.drawString( "Moves = " + counter, width / 2, 40, g.TOP|g.HCENTER);
g.setFont( mFontPlain);
g.drawString( "Press any key...", width / 2, 60, g.TOP|g.HCENTER);
}
}
public boolean MoveSuccess() {
int r, c;
for( r = 0; r < 4; r++)
for( c = 0; c < 4; c++)
if ( board[r][c] != (r * 4) + c + 1) return false;
return true;
}
public void keyPressed( int keyCode) {
int button = getGameAction( keyCode);
if ( MoveSuccess()) {
NewGame();
return;
}
System.out.println( "keycode = " + keyCode + " getGameAction = " + button);
if ( button == UP || keyCode == KEY_NUM2)
if (rb < 3) SwapBlankTo( rb + 1, cb);
if ( button == DOWN || keyCode == KEY_NUM5)
if (rb > 0) SwapBlankTo( rb - 1, cb);
if ( button == LEFT || keyCode == KEY_NUM4)
if (cb < 3) SwapBlankTo( rb, cb + 1);
if ( button == RIGHT || keyCode == KEY_NUM6)
if (cb > 0) SwapBlankTo( rb, cb - 1);
if ( keyCode == KEY_STAR) {
makeBoard = BY_RANDOM;
pattern = 0;
NewGame();
}
if ( keyCode == KEY_NUM0) {
makeBoard = BY_PATTERN;
pattern = (pattern == 10) ? 1 : ++pattern;
NewGame();
}
if ( keyCode == KEY_POUND)
ChangeMode();
repaint();
}
public void ChangeMode() {
modeBoard = ( modeBoard == NUMERIC_MODE) ? ALPHABETIC_MODE : NUMERIC_MODE;
}
public void SwapBlankTo(int r2, int c2) {
int tmp;
tmp = board[rb][cb];
board[rb][cb] = board[r2][c2];
board[r2][c2] = tmp;
rb = r2;
cb = c2;
counter++;
}
public void NewGame() {
int i, d;
Random randNum;
if ( makeBoard == BY_RANDOM)
randNum = new Random( System.currentTimeMillis());
else
randNum = new Random( pattern);
InitGame();
for( i = 0; i < 200; i++) {
d = Math.abs(randNum.nextInt() % 4);
switch ( d) {
case 0 : if (rb < 3) SwapBlankTo( rb + 1, cb); break;
case 1 : if (rb > 0) SwapBlankTo( rb - 1, cb); break;
case 2 : if (cb < 3) SwapBlankTo( rb, cb + 1); break;
case 3 : if (cb > 0) SwapBlankTo( rb, cb - 1); break;
}
}
counter = 0;
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -