java迷宫设总结_java版迷宫

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值