迷宮java代碼
⑴ java怎麼生成迷宮地圖
//作者:zhongZw
packagecn.zhongZw.model;
importjava.util.ArrayList;
importjava.util.Random;
publicclassMazeModel{
privateintwidth=0;
privateintheight=0;
privateRandomrnd=newRandom();
publicMazeModel(){
this.width=50;//迷宮寬度
this.height=50;//迷宮高度
}
publicintgetWidth(){
returnwidth;
}
publicvoidsetWidth(intwidth){
this.width=width;
}
publicintgetHeight(){
returnheight;
}
publicvoidsetHeight(intheight){
this.height=height;
}
publicMazeModel(intwidth,intheight){
super();
this.width=width;
this.height=height;
}
publicArrayList<MazePoint>getMaze(){
ArrayList<MazePoint>maze=newArrayList<MazePoint>();
for(inth=0;h<height;h++){
for(intw=0;w<width;w++){
MazePointpoint=newMazePoint(w,h);
maze.add(point);
}
}
returnCreateMaze(maze);
}
privateArrayList<MazePoint>CreateMaze(ArrayList<MazePoint>maze){
inttop=0;
intx=0;
inty=0;
ArrayList<MazePoint>team=newArrayList<MazePoint>();
team.add(maze.get(x+y*width));
while(top>=0){
int[]val=newint[]{
-1,-1,-1,-1
};
inttimes=0;
booleanflag=false;
MazePointpt=(MazePoint)team.get(top);
x=pt.getX();
y=pt.getY();
pt.visted=true;
ro1:while(times<4){
intdir=rnd.nextInt(4);
if(val[dir]==dir)
continue;
else
val[dir]=dir;
switch(dir){
case0://左邊
if((x-1)>=0&&maze.get(x-1+y*width).visted==false){
maze.get(x+y*width).setLeft();
maze.get(x-1+y*width).setRight();
team.add(maze.get(x-1+y*width));
top++;
flag=true;
breakro1;
}
break;
case1://右邊
if((x+1)<width&&maze.get(x+1+y*width).visted==false){
maze.get(x+y*width).setRight();
maze.get(x+1+y*width).setLeft();
team.add(maze.get(x+1+y*width));
top++;
flag=true;
breakro1;
}
break;
case2://上邊
if((y-1)>=0&&maze.get(x+(y-1)*width).visted==false){
maze.get(x+y*width).setUp();
maze.get(x+(y-1)*width).setDown();
team.add(maze.get(x+(y-1)*width));
top++;
flag=true;
breakro1;
}
break;
case3://下邊
if((y+1)<height&&maze.get(x+(y+1)*width).visted==false){
maze.get(x+y*width).setDown();
maze.get(x+(y+1)*width).setUp();
team.add(maze.get(x+(y+1)*width));
top++;
flag=true;
breakro1;
}
break;
}
times+=1;
}
if(!flag){
team.remove(top);
top-=1;
}
}
returnmaze;
}
}
- 迷宮
//作者:zhongZw
//郵箱:[email protected]
packagecn.zhongZw.model;
importjava.util.*;
importjava.lang.*;
publicclassMazePoint{
privateintleft=0;
privateintright=0;
privateintup=0;
privateintdown=0;
privateintx;
privateinty;
publicbooleanvisted;
publicMazePoint(intx,inty){
this.x=x;
this.y=y;
}
publicintgetLeft(){
returnleft;
}
publicvoidsetLeft(){
this.left=1;
}
publicintgetRight(){
returnright;
}
publicvoidsetRight(){
this.right=1;
}
publicintgetUp(){
returnup;
}
publicvoidsetUp(){
this.up=1;
}
publicintgetDown(){
returndown;
}
publicvoidsetDown(){
this.down=1;
}
publicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returny;
}
publicvoidsetY(inty){
this.y=y;
}
}
[java]view plain
⑵ 急求java課程設計,內容可以是小游戲的,如(迷宮,計算器,停車場之戀的),要能運行,謝謝
連連看java源代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分數標簽
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ連連看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再來一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike<=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //這里一定要將按鈕點擊信息歸為初始
init();
for(int i = 0;i < 6;i++){
for(int j = 0;j < 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg && secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情況下能不能消去。仔細分析,不一條條注釋
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判斷是否相鄰
remove();
}
else{
for (j=0;j<7;j++ ) {
if (grid[x0][j]==0){ //判斷第一個按鈕同行哪個按鈕為空
if (y>j) { //如果第二個按鈕的Y坐標大於空按鈕的Y坐標說明第一按鈕在第二按鈕左邊
for (i=y-1;i>=j;i-- ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1說明通過了第一次驗證
}
if (k==1) {
linePassOne();
}
}
if (y<j){ //如果第二個按鈕的Y坐標小於空按鈕的Y坐標說明第一按鈕在第二按鈕右邊
for (i=y+1;i<=j ;i++ ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0<x) {
for (n=x0;n<=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x-1) {
remove();
}
}
}
if (x0>x) {
for (n=x0;n>=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x+1) {
remove();
}
}
}
}
}
for (i=0;i<8;i++ ) { //列
if (grid[i][y0]==0) {
if (x>i) {
for (j=x-1;j>=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x<i) {
for (j=x+1;j<=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0<y) {
for (n=y0;n<=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y-1) {
remove();
}
}
}
if (y0>y) {
for (n=y0;n>=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0>j){ //第一按鈕同行空按鈕在左邊
for (i=y0-1;i>=j ;i-- ){ //判斷第一按鈕同左側空按鈕之間有沒按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2說明通過了第二次驗證
}
}
if (y0<j){ //第一按鈕同行空按鈕在與第二按鈕之間
for (i=y0+1;i<=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0>i) {
for (j=x0-1;j>=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0<i) {
for (j=x0+1;j<=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
⑶ java老鼠迷宮代碼難嗎
非常難。思路:
1、設老鼠的行進路線都是優先選擇下->右->上->左。
2、設老鼠很聰明,走過的路線走撒泡尿,表示鼠大爺到此一游,我們可以把數組的值改為3,表示走過,但走不通。
3、這是一個int[8][8]的二位數組,那麼開始位置下標是1,1,結束位置是6,6。行和列分別用、j表示。
4、實際路線我們可以設置2表示,我們可以使用遞歸,讓老鼠不斷測試路線。
5、最後列印數組,看老鼠的實際路線。
⑷ 求走迷宮問題的演算法,要求用Java寫的
public class Maze { private int[][] maze = null;
private int[] xx = { 1, 0, -1, 0 };
private int[] yy = { 0, 1, 0, -1 };
private Queue queue = null; public Maze(int[][] maze) {
this.maze = maze;
queue = new Queue(maze.length * maze.length);
} public void go() {
Point outPt = new Point(maze.length - 1, maze[0].length - 1);
Point curPt = new Point(0, 0);
Node curNode = new Node(curPt, null);
maze[curPt.x][curPt.y] = 2;
queue.entryQ(curNode); while (!queue.isEmpty()) {
curNode = queue.outQ();
for (int i = 0; i < xx.length; ++i) {
Point nextPt = new Point();
nextPt.x = (curNode.point).x + xx[i];
nextPt.y = (curNode.point).y + yy[i];
if (check(nextPt)) {
Node nextNode = new Node(nextPt, curNode);
queue.entryQ(nextNode);
maze[nextPt.x][nextPt.y] = 2;
if (nextPt.equals(outPt)) {
java.util.Stack<Node> stack = new java.util.Stack<Node>();
stack.push(nextNode);
while ((curNode = nextNode.previous) != null) {
nextNode = curNode;
stack.push(curNode);
}
System.out.println("A Path is:");
while (!stack.isEmpty()) {
curNode = stack.pop();
System.out.println(curNode.point);
}
return;
}
}
}
}
System.out.println("Non solution!");
} private boolean check(Point p) {
if (p.x < 0 || p.x >= maze.length || p.y < 0 || p.y >= maze[0].length) {
return false;
}
if (maze[p.x][p.y] != 0) {
return false;
}
return true;
} public static void main(String[] args) {
int[][] maze = {
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
};
new Maze(maze).go();
} private class Queue { Node[] array = null;
int size = 0;
int len = 0;
int head = 0;
int tail = 0; public Queue(int n) {
array = new Node[n + 1];
size = n + 1;
} public boolean entryQ(Node node) {
if (isFull()) {
return false;
}
tail = (tail + 1) % size;
array[tail] = node;
len++;
return true;
} public Node outQ() {
if (isEmpty()) {
return null;
}
head = (head + 1) % size;
len--;
return array[head];
} public boolean isEmpty() {
return (len == 0 || head == tail) ? true : false;
} public boolean isFull() {
return ((tail + 1) % size == head) ? true : false;
}
} private class Node { Point point = null;
Node previous = null; public Node() {
this(null,null);
} public Node(Point point, Node node) {
this.point = point;
this.previous = node;
}
} private class Point { int x = 0;
int y = 0; public Point() {
this(0, 0);
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public boolean equals(Point p) {
return (x == p.x) && (y == p.y);
} @Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}
⑸ 求Java關於迷宮的演算法(用棧實現)
packagecom.Albert.LabyringhStack;
publicclassPoint{
intx;
inty;
intdirection;//direction指向此點附近的一個點應該有四個編號為1234
publicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returny;
}
publicvoidsetY(inty){
this.y=y;
}
publicintgetDirection(){
returndirection;
}
publicvoidsetDirection(intdirection){
this.direction=direction;
}
publicvoidaddDirection(){
this.direction++;
}
publicPoint(){
}
publicPoint(intx,inty){
super();
this.x=x;
this.y=y;
this.direction=1;
}
publicPoint(intx,inty,intdirection){
super();
this.x=x;
this.y=y;
this.direction=direction;
}
}
packagecom.Albert.LabyringhStack;
importjava.util.*;
publicclassLabyringhStack{
publicPointS;
publicPointF;
char[][]mazemap;
Stack<Point>path;
publicLabyringhStack(){
}
publicLabyringhStack(char[][]ma){//初始化存入數組
this.mazemap=newchar[ma.length][ma[0].length];
for(inti=0;i<ma.length;i++){
for(intj=0;j<ma[0].length;j++){//mazemap[0]必須有元素不可為空
this.mazemap[i][j]=ma[i][j];
}
}
S=returnPlace('S');
F=returnPlace('F');
}
publicPointreturnPlace(chars){//返回數組中字元的位置
Pointpoint=newPoint();
for(inti=0;i<this.mazemap.length;i++){
for(intj=0;j<this.mazemap[0].length;j++){//mazemap[0]必須有元素不可為空
if(this.mazemap[i][j]==s)
{point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
returnpoint;
}
publiccharreturnChar(Pointpoint){
if(point.getX()>=0&&point.getY()>=0)
returnthis.mazemap[point.getX()][point.getY()];
else
return'#';
}
publicvoidreplacePlace(Pointpoint,chars){//更改特定位置處的字元
mazemap[point.getX()][point.getY()]=s;
}
publicvoidprintPath(){
Stack<Point>tempPath=newStack<Point>();
while(!path.empty()){//對棧進行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print("("+tempPath.peek().getX()+","+tempPath.pop().getY()+")");
}
}
publicbooleangetPath(){//取得路徑的演算法如果有路徑就返回真
path=newStack<Point>();
S.setDirection(1);
path.push(S);
replacePlace(S,'X');
while(!path.empty()){
PointnowPoint=path.peek();//取得當前位置
if(nowPoint.getX()==F.getX()&&nowPoint.getY()==F.getY()){
//printPath();
returntrue;
}
Pointtemp=newPoint();//存放下一個可走的位置
intfind=0;//標志是否可向下走
while(nowPoint.getDirection()<5&&find==0){
switch(nowPoint.getDirection()){
case1:temp=newPoint(nowPoint.getX(),nowPoint.getY()-1,1);break;//取得當前位置左邊的位置
case2:temp=newPoint(nowPoint.getX()+1,nowPoint.getY(),1);break;//取得當前位置下邊的位置
case3:temp=newPoint(nowPoint.getX(),nowPoint.getY()+1,1);break;//取得當前位置右邊的位置
case4:temp=newPoint(nowPoint.getX()-1,nowPoint.getY(),1);break;//取得當前位置上邊的位置
}
nowPoint.addDirection();//指向下一個需要驗證的點
if(returnChar(temp)=='O'||returnChar(temp)=='F')find=1;//如果能向下走則置為1
}
if(find==1){//如果可走就進棧
replacePlace(temp,'X');//設置成X防止回走
// printArr();
path.push(temp);
}else{//如果不可走就退棧
replacePlace(nowPoint,'O');
path.pop();
}
}
returnfalse;
}
publicvoidprintArr(){
for(inti=0;i<mazemap.length;i++){
for(intj=0;j<mazemap[0].length;j++){//mazemap[0]必須有元素不可為空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}
}
packagecom.Albert.LabyringhStack;
publicclassMain{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
char[][]mazemap={
{'M','M','M','M','M','M','M','M'},
{'M','S','O','O','M','M','M','M'},
{'M','M','M','O','M','M','M','M'},
{'M','M','O','O','O','O','M','M'},
{'M','M','M','M','M','F','M','M'},
{'M','M','M','M','M','M','M','M'},
{'M','M','M','M','M','M','M','M'}
};
LabyringhStacksolution=newLabyringhStack(mazemap);
if(solution.getPath()){
System.out.print("迷宮路徑如下:");
solution.printPath();
}
else{
System.out.println("沒有可走的路");
}
}
}