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);
}
}