package homeWork;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
/*1。在你的节点上,若右手不为墙壁,就原地右转在直走,尔后重复侦测右手情况。
2。若右手为墙壁,就原地左转,重复侦测右手情况。
但由于第2步的做法,会造成前进时变成先左转,右转再直走,所以有以下变化:
2。1。若右手为墙壁,前面不为墙壁,则直走后侦测右手情况。
2。2。若右手为墙壁,前面也为墙壁,就原地左转两次,再重复侦测右手情况。
如此重复摸墙壁,就可走出迷宫了。*/
/*东南西北四个方向,用一个变量标记,向东走时向右转即是向南转,西--北,北--东,南--西
* 左转 东--北,西--南,北--西,南--东*/
public class Maze extends JApplet implements ActionListener{//走迷宫,右手试探法
private static final long serialVersionUID=1L;
//JTextArea outputArea;
JButton actionButton;
//String outputString="";
char maze[][]={{'#','#','#','#','#','#','#','#','#','#','#','#',},
{'#','.','.','.','#','.','.','.','.','.','.','#',},
{'.','.','#','.','#','.','#','#','#','#','.','#',},
{'#','#','#','.','#','.','.','.','.','#','.','#',},
{'#','.','.','.','.','#','#','#','.','#','.','.',},
{'#','#','#','#','.','#','.','#','.','#','.','#',},
{'#','.','.','#','.','#','.','#','.','#','.','#',},
{'#','#','.','#','.','#','.','#','.','#','.','#',},
{'#','.','.','.','.','.','.','.','.','#','.','#',},
{'#','#','#','#','#','#','.','#','#','#','.','#',},
{'#','.','.','.','.','.','.','#','.','.','.','#',},
{'#','#','#','#','#','#','#','#','#','#','#','#',}};
int direction=1;//表示方向,1--东,2--南,3--西,4--北,默认东
int col,row;//当前行和列
public void init() {
Container container=getContentPane();
container.setLayout(new FlowLayout());
//outputArea=new JTextArea();
//container.add(outputArea);
row=2;
col=0;
actionButton=new JButton("GO!");
actionButton.addActionListener(this);
container.add(actionButton);
}
public void actionPerformed(ActionEvent event) {
mazeTraverse(maze,row,col);
repaint();
}
public void mazeTraverse(char maze[][],int startRow,int startCol) {
if(row!=4 || col!=12) {
if(righthand(direction,row,col)) {//右边为墙壁
if(beforeHand(direction,row,col)) {//前面为墙壁
TurnLeft(direction);//左转两次
TurnLeft(direction);
mazeTraverse(maze, row, col);
}
else{//前面不是墙壁
forthRight(direction);
mazeTraverse(maze, row, col);
}
}
else{//右边不是墙壁
TurnRight(direction);
forthRight(direction);
mazeTraverse(maze, row, col);
}
}
}
public void East() {
direction=1;
col++;//行不变,列加一
}
public void South() {
direction=2;
row++;//列不变,行加一
}
public void West() {
direction=3;
col--;
}
public void North() {
direction=4;
row--;
}
public boolean righthand(int d,int r,int c) {//判断右手的情况,是否为墙壁,是则返回真
switch (d) {
case 1:
if(maze[++r][c]=='#')
return true;
break;
case 2:
if(maze[r][--c]=='#')
return true;
break;
case 3:
if(maze[--r][c]=='#')
return true;
break;
case 4:
if(maze[r][++c]=='#')
return true;
break;
}
return false;
}
public boolean beforeHand(int d,int r,int c) {//若前面为墙壁,则返回真
switch (d) {
case 1:
if(maze[r][++c]=='#')
return true;
break;
case 2:
if(maze[++r][c]=='#')
return true;
break;
case 3:
if(maze[r][--c]=='#')
return true;
break;
case 4:
if(maze[--r][c]=='#')
return true;
break;
}
return false;
}
public void forthRight(int d) {
switch (d) {
case 1:
col++;
maze[row][col]='*';
break;
case 2:
row++;
maze[row][col]='*';
break;
case 3:
col--;
maze[row][col]='*';
break;
case 4:
row--;
maze[row][col]='*';
break;
default:
break;
}
}
public void TurnRight(int d) {
switch(d) {
case 1:
direction=2;
break;
case 2:
direction=3;
break;
case 3:
direction=4;
break;
case 4:
direction=1;
break;
default:
break;
}
}
public void TurnLeft(int d) {
switch (d) {
case 1:
direction=4;
break;
case 2:
direction=1;
break;
case 3:
direction=2;
break;
case 4:
direction=3;
break;
default:
break;
}
}
public void paint(Graphics g) {
//DecimalFormat twoDights = new DecimalFormat("");
super.paint(g);
int x=5,y=30;
for (int r = 0; r < maze.length; r++){
for(int c=0;c<maze[r].length;c++)
g.drawString(""+maze[r][c],x+=20,y);
y+=15;
x=5;
//outputString+=twoDights.format(array[r][c])+" ";
//outputString+="/n";
}
//outputArea.setText(outputString);
}
}