📄 maze.java
字号:
return false;
}
}
class DepthFirstSearch extends Thread //DFS class
{
public void run()
{
findExit();
}
public boolean findExit()
{
//till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class IterativeDeepSearch extends Thread //IDS class
{
public void run()
{
findExit();
}
public boolean findExit()
{
//till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initRandomGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//left
if (step(-1,0))
if (findExit()==true)
return true;
else mouse.x++;
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//up
if (step(0,-1))
if (findExit()==true)
return true;
else mouse.y++;
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class ASTAR extends Thread //A* search class
{
public void run()
{ findExit();
}
public boolean findExit()
{ //till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class NewState extends Thread //get a new state and implement a DFS
{
public void run()
{
findExit();
}
public boolean findExit()
{ //till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initRandomGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//left
if (step(-1,0))
if (findExit()==true)
return true;
else mouse.x++;
//up
if (step(0,-1))
if (findExit()==true)
return true;
else mouse.y++;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class jpMainPanel extends JPanel //the first Maze panel
{
int maze[][];
Point mouse;
///let the main draw gui program get the maze setting
public void setMaze(int arry[][])
{
this.maze=arry;
}
///let the main draw gui program get the mouse position
public void setMouse(Point arg)
{
mouse=arg;
}
///main draw gui program
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int stuff=100;
//draw the maze
for (int i=1;i<maze.length-1;i++)
for (int j=1;j<maze[i].length-1;j++){
if (maze[i][j]==1){
//wall
g.drawString("*",j*20+stuff,i*20+stuff);
}
else if (maze[i][j]==0){
//road
}
else if (maze[i][j]==10){
//starter
}
else if (maze[i][j]==11){
//end
g.setColor(new Color(0,255,0));
g.drawString("O",j*20+stuff,i*20+stuff);
g.setColor(new Color(0,0,0));
}
else{}
}
//draw the position of the mouse
g.setColor(new Color(0,0,255));
g.drawString("O",mouse.x*20+stuff,mouse.y*20+stuff);
g.setColor(new Color(0,0,0));
}
}
class jpRandomPanel extends JPanel //the second maze panel
{
int maze[][];
Point mouse;
///let the main draw gui program get the maze setting
public void setMaze(int arry[][])
{
this.maze=arry;
}
///let the main draw gui program get the mouse position
public void setMouse(Point arg)
{
mouse=arg;
}
///main draw gui program
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int stuff=100;
//draw the maze
for (int i=1;i<maze.length-1;i++)
for (int j=1;j<maze[i].length-1;j++){
if (maze[i][j]==1){
//wall
g.drawString("*",j*20+stuff,i*20+stuff);
}
else if (maze[i][j]==0){
//road
}
else if (maze[i][j]==10){
//starter
}
else if (maze[i][j]==11){
//end
g.setColor(new Color(0,255,0));
g.drawString("O",j*20+stuff,i*20+stuff);
g.setColor(new Color(0,0,0));
}
else{}
}
//draw the position of the mouse
g.setColor(new Color(0,0,255));
g.drawString("O",mouse.x*20+stuff,mouse.y*20+stuff);
g.setColor(new Color(0,0,0));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -