📄 board.java
字号:
package ch09.section09;
import example.About;
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class Board
extends Canvas
implements CommandListener {
MIDlet midlet;
Display dpy;
Options options;
//初始化一个长度为15的字符串
String letters = "RATEYOURMINDPAL";
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 = 0;
static final int CMD_EXIT = 1;
static final int CMD_OPTIONS = 2;
static final int CMD_RESET = 3;
static final int CMD_START = 4;
static final int CMD_UNLOCK = 5;
static final int CMD_ZLAST = 6;
Command cmd[];
//声明变量
static final int INITIALIZED = 0;
static final int PLAYING = 1;
static final int WON = 2;
int gameState;
boolean cheated;
void D(String s) {
System.out.println(s);
}
class Piece {
String label;
boolean inv;
int serial;
//单元格初始化坐标
int ix, iy;
//单元格当前坐标
int x, y;
Piece(String str, int ser, int nx, int ny, boolean v) {
label = str;
serial = ser;
x = ix = nx;
y = iy = ny;
inv = v;
}
void setLocation(int nx, int ny) {
x = nx;
y = ny;
}
boolean isHome() {
return (x == ix) && (y == iy);
}
void goHome() {
setGrid(this, ix, iy);
}
//设置背景色为白色
void paint(Graphics g) {
int px = x * cellw;
int py = y * cellh;
if (label != null) {
if (inv) {
//边框为黑色
g.setColor(0);
g.setFont(font);
g.drawRect(px, py, cellw - 2, cellh - 2);
g.drawString(label, px + cellxoff, py + cellyoff,
Graphics.TOP | Graphics.LEFT);
}
else {
//空白框
g.setColor(0);
g.fillRect(px, py, cellw - 1, cellh - 1);
g.setColor(0xFFFFFF);
g.setFont(font);
g.drawString(label, px + cellxoff, py + cellyoff,
Graphics.TOP | Graphics.LEFT);
}
}
}
}
class BoardCommand
extends Command {
int tag;
BoardCommand(String label, int type, int pri, int tag_) {
super(label, type, pri);
tag = tag_;
}
}
void setGrid(Piece p, int x, int y) {
grid[x][y] = p;
p.setLocation(x, y);
}
//移动单元格
void moveBlank(int swapx, int swapy) {
setGrid(grid[swapx][swapy], blankp.x, blankp.y);
setGrid(blankp, swapx, swapy);
}
void swap(int x1, int y1, int x2, int y2) {
Piece t = grid[x1][y1];
setGrid(grid[x2][y2], x1, y1);
setGrid(t, x2, y2);
}
boolean isSolved() {
for (int i = 0; i < gridh; i++) {
for (int j = 0; j < gridw; j++) {
if (!grid[j][i].isHome()) {
return false;
}
}
}
return true;
}
//获取一个取值范围在[0..n]之间的一个整数
int randRange(int n) {
int r = rand.nextInt() % n;
if (r < 0) {
r += n;
}
return r;
}
void randomize_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);
}
//移动至右下角
while (blankp.x != gridw - 1) {
moveBlank(blankp.x + 1, blankp.y);
}
while (blankp.y != gridh - 1) {
moveBlank(blankp.x, blankp.y + 1);
}
}
void shuffle() {
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);
}
void randomize(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);
}
}
//计算匹配数,空白格必须在右下角
int displacement() {
boolean temp[] = new boolean[gridw * gridh - 1];
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;
}
void resetGrid() {
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();
}
}
void rearrangeFunnily(boolean hard) {
resetGrid();
if (hard) {
//难级别
swap(0, 0, 3, 1);
swap(2, 2, 3, 2);
swap(3, 2, 0, 3);
swap(0, 3, 2, 3);
}
else {
//易级别
swap(2, 2, 3, 2);
swap(3, 2, 0, 3);
}
}
void setState(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 Board(MIDlet midlet_) {
int i;
midlet = midlet_;
dpy = Display.getDisplay(midlet);
gridw = 4;
gridh = 4;
font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
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();
//创建单元格数组
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));
}
//生成空白格
blankp = new Piece(null, gridw * gridh - 1, gridw - 1, gridh - 1, false);
grid[gridw - 1][gridh - 1] = blankp;
all[gridw * gridh - 1] = blankp;
//设置命令按钮
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] = ew 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);
//设置监听器
setCommandListener(this);
//设置选项窗体
options = new Options(dpy, this);
//设置初始状态
setState(INITIALIZED);
}
public void commandAction(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;
}
}
public void showNotify() {
}
public void hideNotify() {
}
public void paint(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);
}
}
public void keyPressed(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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -