importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.KeyEvent;importorg.w3c.dom.Node;public class MyMethod implementsMyDraw{private int step = 0;private intchoose;int success = 0;public intisSuccess() {returnsuccess;
}private int row = 3;private int col = 3;private Dir dir =Dir.R;private int w =MyMaze.BORDER_SIZE;private int h =MyMaze.BORDER_SIZE;privateString name;
Color c;
MyMaze mm;
Stack moveStack= newStack();
Stack recordStack= newStack();public MyMethod(MyMaze mm,Color c,intchoose,String name){this.c =c;this.mm =mm;this.choose =choose;this.name =name;
keepMoveRecord();
}public voiddraw(Graphics g){
move();
Color color=g.getColor();
g.setColor(c);
g.fillOval(col*MyMaze.BORDER_SIZE, row*MyMaze.BORDER_SIZE, w, h);
g.setColor(color);
}voidmove(){if(this.row == 27 && this.col == 27){
success= 2;//成功
return;
}if(moveStack.isEmpty()){
success= 1;//无路可走
return;
}
step++;switch(choose) {case 0:return;case 1:if(RisOpen()){
goRight();return;
}if(DisOpen()){
goDown();return;
}if(UisOpen()){
goUp();return;
}if(LisOpen()){
goLeft();return;
}break;case 2:if(DisOpen()){
goDown();return;
}if(RisOpen()){
goRight();return;
}if(UisOpen()){
goUp();return;
}if(LisOpen()){
goLeft();return;
}break;
}
goBack();
}booleanLisOpen(){if(checkWall(this.row, this.col-1) || checkRecordRepeat(this.row, this.col-1)){return false;
}return true;
}booleanUisOpen(){if(checkWall(this.row-1, this.col) || checkRecordRepeat(this.row-1, this.col)){return false;
}return true;
}booleanRisOpen(){if(checkWall(this.row, this.col+1) || checkRecordRepeat(this.row, this.col+1)){return false;
}return true;
}booleanDisOpen(){if(checkWall(this.row+1, this.col) || checkRecordRepeat(this.row+1, this.col)){return false;
}return true;
}voidkeepMoveRecord(){
keepMove();
keepRecord();
}voidkeepMove(){
moveStack.push(row, col, dir);
}voidkeepRecord(){
recordStack.push(row, col, dir);
}boolean checkRecordRepeat(int r,intc){returnrecordStack.isInStack(r, c);
}boolean checkWall(int row,intcol){if(mm.w.wallIsSet(row, col)){return true;
}return false;
}voidgoRight(){
System.out.println(this.name + ": 向右走一步 + step:"+step);this.row =row;this.col = col+1;
keepMoveRecord();
}voidgoDown(){
System.out.println(this.name + ": 向下走一步 + step:"+step);this.row = row+1;this.col =col;
keepMoveRecord();
}voidgoLeft(){
System.out.println(this.name + ": 向左走一步 + step:"+step);this.row =row;this.col = col-1;
keepMoveRecord();
}voidgoUp(){
System.out.println(this.name + ": 向上走一步 + step:"+step);this.row = row-1;this.col =col;
keepMoveRecord();
}voidgoBack(){
moveStack.pop();
System.out.println(this.name + ": 向后退一步。。。");this.row =moveStack.getTopRow();this.col =moveStack.getTopCol();
}voidkeyPressed(KeyEvent e){int key =e.getKeyCode();switch(key){caseKeyEvent.VK_LEFT:if(LisOpen()){
goLeft();
}break;caseKeyEvent.VK_RIGHT:if(RisOpen()){
goRight();
}break;caseKeyEvent.VK_UP:if(UisOpen()){
goUp();
}break;caseKeyEvent.VK_DOWN:if(DisOpen()){
goDown();
}break;caseKeyEvent.VK_B:
goBack();
}
}
}