📄 boxworld.java
字号:
//****************GAME CONTROL WITH
// MOUSE**********************************************
//*************************************************************************************
private void moveTo(int xpos, int ypos) {
repeat = true;
int x1 = ypos / CELL_SIZE;
int y1 = xpos / CELL_SIZE;
int d;
boolean valid = true;
int st, bc = 0;
if (x1 == x && y1 != y) {
if (y1 > y) {
for (int i = y; i <= y1; i++) {
st = state[ x][ i];
if (st == WALL_ST || st == NULL_ST) {
valid = false;
break;
} else if (st == BOX_ST || st == FIT_ST) {
bc++;
if (bc > 1) {
valid = false;
break;
}
}
}
} else {
for (int i = y1; i <= y; i++) {
st = state[ x][ i];
if (st == WALL_ST || st == NULL_ST) {
valid = false;
break;
} else if (st == BOX_ST || st == FIT_ST) {
bc++;
if (bc > 1) {
valid = false;
break;
}
}
}
}
} else if (x1 != x && y1 == y) {
if (x1 > x) {
for (int i = x; i <= x1; i++) {
st = state[ i][ y];
if (st == WALL_ST || st == NULL_ST) {
valid = false;
break;
} else if (st == BOX_ST || st == FIT_ST) {
bc++;
if (bc > 1) {
valid = false;
break;
}
}
}
} else {
for (int i = x1; i <= x; i++) {
st = state[ i][ y];
if (st == WALL_ST || st == NULL_ST) {
valid = false;
break;
} else if (st == BOX_ST || st == FIT_ST) {
bc++;
if (bc > 1) {
valid = false;
break;
}
}
}
}
} else {
valid = false;
}
if (valid) {
if (x1 == x && y1 != y) {
if (y1 > y) {
d = y1 - y;
for (int i = 0; i < d && repeat; i++) {
changeState(RIGHT_MOVE);
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e1) {
}
}
} else {
d = y - y1;
for (int i = 0; i < d && repeat; i++) {
changeState(LEFT_MOVE);
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e1) {
}
}
}
} else if (x1 != x && y1 == y) {
if (x1 > x) {
d = x1 - x;
for (int i = 0; i < d && repeat; i++) {
changeState(DOWN_MOVE);
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e1) {
}
}
} else {
d = x - x1;
for (int i = 0; i < d && repeat; i++) {
changeState(UP_MOVE);
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e1) {
}
}
}
}
} else {
Vector path = findPath(new Point(y, x), new Point(y1, x1));
if (path.size() > 1) {
Point cpnt, opnt;
opnt = (Point) path.elementAt(0);
int psize = path.size();
//System.out.println();
for (int i = 1; i < psize && repeat; i++) {
cpnt = (Point) path.elementAt(i);
//System.out.println("("+opnt.x+","+opnt.y+")->("+cpnt.x+","+
// cpnt.y+")");
moveOne(cpnt.y, cpnt.x, opnt.y, opnt.x);
opnt = cpnt;
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e1) {
}
}
}
}
}
private void moveOne(int x1, int y1, int x, int y) {
int dx = x - x1, dy = y - y1;
if (Math.abs(dx) + Math.abs(dy) != 1) { return; }
if (dx == -1) {
//System.out.println("DOWN");
changeState(DOWN_MOVE);
} else if (dx == 1) {
//System.out.println("UP");
changeState(UP_MOVE);
} else if (dy == -1) {
//System.out.println("RIGHT");
changeState(RIGHT_MOVE);
} else if (dy == 1) {
//System.out.println("LEFT");
changeState(LEFT_MOVE);
}
}
private Vector findPath(Point p1, Point p2) {
Vector nlist = new Vector();
Vector path = new Vector();
Hashtable vmap = new Hashtable();
nlist.addElement(p1);
Point pp = p1, pp2 = null;
int xx, yy, vi = 0;
while (!(pp.equals(p2) || nlist.size() == vi)) {
pp = (Point) nlist.elementAt(vi++);
xx = pp.x - 1;
yy = pp.y;
if (xx >= 0
&& (state[ yy][ xx] == BACK_ST
|| state[ yy][ xx] == GOAL_ST || state[ yy][ xx] == GOAL_ST)
&& !vmap.containsKey(pp2 = new Point(xx, yy))
&& !nlist.contains(pp2)) {
nlist.addElement(pp2);
vmap.put(pp2, pp);
}
xx = pp.x + 1;
if (xx < X_SIZE
&& (state[ yy][ xx] == BACK_ST
|| state[ yy][ xx] == GOAL_ST || state[ yy][ xx] == GOAL_ST)
&& !vmap.containsKey(pp2 = new Point(xx, yy))
&& !nlist.contains(pp2)) {
nlist.addElement(pp2);
vmap.put(pp2, pp);
}
xx = pp.x;
yy = pp.y - 1;
if (yy >= 0
&& (state[ yy][ xx] == BACK_ST
|| state[ yy][ xx] == GOAL_ST || state[ yy][ xx] == GOAL_ST)
&& !vmap.containsKey(pp2 = new Point(xx, yy))
&& !nlist.contains(pp2)) {
nlist.addElement(pp2);
vmap.put(pp2, pp);
}
yy = pp.y + 1;
if (yy < Y_SIZE
&& (state[ yy][ xx] == BACK_ST
|| state[ yy][ xx] == GOAL_ST || state[ yy][ xx] == GOAL_ST)
&& !vmap.containsKey(pp2 = new Point(xx, yy))
&& !nlist.contains(pp2)) {
nlist.addElement(pp2);
vmap.put(pp2, pp);
}
}
if (pp.equals(p2)) {
do {
path.insertElementAt(pp, 0);
pp2 = pp;
pp = (Point) vmap.get(pp);
} while (pp2 != pp && pp != null);
}
return path;
}
public void moveTo2(int xpos, int ypos) {
repeat = true;
int x1 = ypos / CELL_SIZE;
int y1 = xpos / CELL_SIZE;
int d;
if (state[ x1][ y1] == NULL_ST || state[ x1][ y1] == WALL_ST) return;
if (x1 == x && y1 != y) {
if (y1 > y) {
d = y1 - y;
for (int i = 0; i < d && repeat; i++) {
changeState(RIGHT_MOVE);
}
} else {
d = y - y1;
for (int i = 0; i < d && repeat; i++) {
changeState(LEFT_MOVE);
}
}
} else if (x1 != x && y1 == y) {
if (x1 > x) {
d = x1 - x;
for (int i = 0; i < d && repeat; i++) {
changeState(DOWN_MOVE);
}
} else {
d = x - x1;
for (int i = 0; i < d && repeat; i++) {
changeState(UP_MOVE);
}
}
}
}
private void changeMode() {
switch (gameMode) {
case START_MODE:
{
gameMode = PLAY_MODE;
break;
}
case PLAY_MODE:
{
break;
}
case HELP_MODE:
{
break;
}
case END_MODE:
{
nextWorld();
gameMode = PLAY_MODE;
break;
}
}
update(getGraphics());
}
private void controlWorld(int key) {
switch (key) {
case KeyEvent.VK_N:
//next world
nextWorld();
break;
case KeyEvent.VK_P:
//previous world
previousWorld();
break;
case KeyEvent.VK_S:
//restart
startWorld();
break;
case KeyEvent.VK_U:
//undo
if (gameMode == PLAY_MODE) undo();
break;
case KeyEvent.VK_R:
//redo
if (gameMode == PLAY_MODE) redo();
break;
case KeyEvent.VK_B:
//toggle beep
toggleBeep();
break;
case KeyEvent.VK_H:
//show help
help();
break;
case KeyEvent.VK_C:
//continue the game
continueGame();
break;
}
}
private void nextWorld() {
worldId++;
if (worldId == 5) {
//int x = worldId * 100;
}
if (worldId == vecs.length) worldId = 0;
vec = vecs[ worldId];
controlWorld(KeyEvent.VK_S);
}
private void previousWorld() {
worldId--;
if (worldId < 0) worldId = vecs.length - 1;
vec = vecs[ worldId];
controlWorld(KeyEvent.VK_S);
}
private void startWorld() {
ereaseWorld();
loadWorld();
repaint();
undoStack.removeAllElements();
redoStack.removeAllElements();
gameMode = PLAY_MODE;
}
private void toggleBeep() {
beepOn = !beepOn;
beep.setState(beepOn);
}
private void help() {
gameMode = HELP_MODE;
changeMode();
}
private void continueGame() {
gameMode = PLAY_MODE;
update(getGraphics());
}
private void ereaseWorld() {
for (int i = 0; i < X_SIZE; i++) {
for (int j = 0; j < Y_SIZE; j++) {
state[ i][ j] = 0;
}
}
}
//******************* UNDO ********************
private Stack undoStack = new Stack();
private Stack redoStack = new Stack();
private boolean isUndo = false;
private boolean isRedo = false;
private void undo() {
isUndo = true;
if (undoStack.empty()) {
beep();
} else {
changeState(((Integer) undoStack.pop()).intValue());
}
isUndo = false;
}
private void redo() {
isRedo = true;
if (redoStack.empty()) {
beep();
} else {
changeState(((Integer) redoStack.pop()).intValue());
}
isRedo = false;
}
private boolean checkEnd() {
int v, x, y;
boolean ret = true;
for (int i = 0; i < goals.length; i++) {
v = goals[ i];
x = 0xFFFF & v;
y = 0xFFFF & (v >> 16);
if (state[ x][ y] != FIT_ST) {
ret = false;
break;
}
}
return ret;
}
//****************** GRAPHICS *********************************
//private Image[] imgVec = new Image[8];
//private int ISIZE = CELL_SIZE;
// STATE PAINTING
/**
* Paints the NULL state
*/
private void paintNull(Graphics g, int j, int i) {
// Image img=imgVec[NULL_ST];
// if(img==null){
// img=createImage(ISIZE,ISIZE);
// imgVec[NULL_ST]=img;
//Graphics ig=img.getGraphics();
g.setColor(Color.black);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
// }
// g.drawImage(img,j * DIM, i * DIM,this);
}
private void paintWall(Graphics g, int j, int i) {
// Image img=imgVec[WALL_ST];
// if(img==null){
// img=createImage(ISIZE,ISIZE);
// imgVec[WALL_ST]=img;
// Graphics ig=img.getGraphics();
g.setColor(Color.gray);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
g.setColor(Color.lightGray);
g.drawRect(j * CELL_SIZE + 1, i * CELL_SIZE + 1, CELL_SIZE - 2,
CELL_SIZE - 2);
// }
// g.drawImage(img,j * CELL_SIZE, i * CELL_SIZE,this);
}
private void paintBack(Graphics g, int j, int i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -