「java迷宫程序」java迷宫小游戏

博主:adminadmin 2022-11-26 22:02:08 54

本篇文章给大家谈谈java迷宫程序,以及java迷宫小游戏对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

急需一java高手帮忙写一迷宫程序

给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。

你看一下吧。

----------------------------------------

package stackpackage;

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Maze {

// 上

private static Point directionTop = new Point(-1, 0);

// 下

private static Point directionBottom = new Point(1, 0);

// 左

private static Point directionLeft = new Point(0, -1);

// 右

private static Point directionRight = new Point(0, 1);

private static Point[] directions = { directionTop, directionRight,

directionBottom, directionLeft };

private static boolean isStop = false;

private static int row = 0;

private static int col = 0;

private static Point startPoint = new Point();

public static void main(String[] args) throws Exception {

FileReader fr = new FileReader("data.txt");

BufferedReader br = new BufferedReader(fr);

int rowIndex = 1;

int[][] maze = null;

while (br.ready()) {

String line = br.readLine();

Scanner sc = new Scanner(line);

if (rowIndex == 1) {

row = sc.nextInt();

col = sc.nextInt();

maze = new int[row][col];

} else {

if (rowIndex row + 2) {

for (int i = 0; i col; i++) {

maze[rowIndex - 2][i] = sc.nextInt();

}

} else {

startPoint.x = sc.nextInt();

startPoint.y = sc.nextInt();

}

}

rowIndex++;

}

ListPoint route = new ArrayListPoint();

route.add(startPoint);

findNext(startPoint);

puzzle(maze, startPoint, route);

System.out.println(route);

}

private static void puzzle(int[][] maze, Point p, ListPoint route) {

if (isStop) {

return;

}

Point[] nextDirections = p.nextDirections;

for (int i = 0; i nextDirections.length; i++) {

if (isStop) {

return;

}

Point direction = nextDirections[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (newP.isEffective() maze[newP.x][newP.y] == 0

!route.contains(newP)) {

newP.before = p;

findNext(newP);

route.add(newP);

if (isExit(newP)) {

isStop = true;

break;

}

puzzle(maze, newP, route);

}

}

if (isStop) {

return;

}

route.remove(route.size() - 1);

}

private static void findNext(Point p) {

int index = 0;

Point[] nextDirections = new Point[3];

for (int i = 0; i nextDirections.length; i++) {

nextDirections[i] = new Point(0, 0);

}

for (int i = 0; i directions.length; i++) {

Point direction = directions[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (newP.isEffective() !newP.equals(p.before) newP.x row

newP.y col) {

nextDirections[index++] = direction;

}

}

p.nextDirections = nextDirections;

}

private static boolean isExit(Point p) {

if (startPoint.equals(p)) {

return false;

}

for (int i = 0; i directions.length; i++) {

Point direction = directions[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (!newP.equals(p.before)

(newP.x = row || newP.y = col || newP.x 0 || newP.y 0)) {

return true;

}

}

return false;

}

}

class Point {

int x = 0;

int y = 0;

Point[] nextDirections = null;

Point before = null;

public Point() {

}

public Point(int x, int y) {

this.x = x;

this.y = y;

}

public String toString() {

return "" + x + "," + y + "";

}

public boolean isEffective() {

return x = 0 y = 0;

}

public boolean equals(Object obj) {

return equals((Point) obj);

}

public boolean equals(Point p) {

if (p == null) {

return false;

}

return this.x == p.x this.y == p.y;

}

}

java自动走迷宫线程问题

以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。

(1) 根据二维数组,输出迷宫的图形。

(2) 探索迷宫的四个方向:RIGHT为向右,DOWN向下,LEFT向左,UP向上,输出从入口到出口的行走路径。

求走迷宫问题的算法,要求用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.StackNode stack = new java.util.StackNode();

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迷宫算法问题(用栈实现)有算法简述

import java.io.File;

import java.io.FileNotFoundException;

import java.util.HashSet;

import java.util.LinkedList;

import java.util.List;

import java.util.Scanner;

public class Test {

public static void main(String[] args) throws FileNotFoundException {

Scanner input = new Scanner(new File("Maze2tong.txt"));

int row = 0;

char[][] Mazemap = new char[12][58];

while (input.hasNext()) {

String line = input.nextLine();

for (int column = 0; column = line.length() - 1; column++) {

char c = line.charAt(column);

Mazemap[row][column] = c;

}

row++;

}

for (int i = 0; i  12; i++) {

for (int j = 0; j  58; j++) {

System.out.print(Mazemap[i][j]);

}

System.out.print("\n");

}

LinkedListTwoTupleInteger, Integer trace = new LinkedListTwoTupleInteger, Integer();

System.out.println(maze(Mazemap, trace));

System.out.println(trace);

}

public static boolean maze(char[][] maze,

ListTwoTupleInteger, Integer trace) {

LinkedListTwoTupleInteger, Integer path = new LinkedListTwoTupleInteger, Integer();

HashSetTwoTupleInteger, Integer traverse = new HashSetTwoTupleInteger, Integer();

for (int i = 0; i  maze.length; i++) {

for (int j = 0; j  maze[i].length; j++) {

if (maze[i][j] == 'S') {

path.add(new TwoTupleInteger, Integer(i, j));

}

}

}

while (!path.isEmpty()) {

TwoTupleInteger, Integer temp = path.pop();

if (traverse.contains(temp)) {

continue;

} else if (maze[temp.first][temp.second] == 'F') {

trace.add(temp);

return true;

} else if (!traverse.contains(temp)) {

if (temp.second + 1  maze[temp.first].length

 maze[temp.first][temp.second + 1] != 'W')

path.add(new TwoTupleInteger, Integer(temp.first,

temp.second + 1));

if (temp.second - 1  0

 maze[temp.first][temp.second - 1] != 'W')

path.add(new TwoTupleInteger, Integer(temp.first,

temp.second - 1));

if (temp.first + 1  maze.length

 maze[temp.first + 1][temp.second] != 'W')

path.add(new TwoTupleInteger, Integer(temp.first + 1,

temp.second));

if (temp.first - 1  0

 maze[temp.first - 1][temp.second] != 'W')

path.add(new TwoTupleInteger, Integer(temp.first - 1,

temp.second));

traverse.add(temp);

trace.add(temp);

}

}

trace.clear();

return false;

}

}

class TwoTupleA, B {

public final A first;

public final B second;

public TwoTuple(A a, B b) {

first = a;

second = b;

}

@Override

public int hashCode() {

return first.hashCode() + second.hashCode();

}

@Override

public boolean equals(Object obj) {

if (!(obj instanceof TwoTuple)) {

}

return obj instanceof TwoTuple  first.equals(((TwoTuple) obj).first)

 second.equals(((TwoTuple) obj).second);

}

public String toString() {

return "(" + first + ", " + second + ")";

}

} // /:-

import java.io.File;

import java.io.FileNotFoundException;

import java.util.LinkedList;

import java.util.Scanner;

class MyPoint

{

    public boolean visited=false;

    public int parentRow=-1;

    public int parentColumn=-1;

    public final char content;

    public int x;

    public int y;

    public MyPoint(char c,int x,int y)

    {

     this.content = c;

     this.x = x;

     this.y = y;

    }

}

public class Maze

{

    public static MyPoint[][] getMazeArray(){

Scanner input = null;

MyPoint[][] mazemap = new MyPoint[12][58];

try {

input = new Scanner(new File("Maze2tong.txt"));

int row = 0;

while (input.hasNext()) {

String line = input.nextLine();

for (int column = 0; column = line.length() - 1; column++) {

char c = line.charAt(column);

MyPoint point = new MyPoint(c,row,column);

mazemap[row][column] = point;

}

row++;

}

input.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return mazemap;

    }

    public static boolean tomRun(MyPoint[][] maze,MyPoint end)

    {

     int x = maze.length;

     int y = maze[0].length;

     LinkedListMyPoint stack=new LinkedListMyPoint();

for (int i = 0; i  maze.length; i++) {

for (int j = 0; j  maze[i].length; j++) {

if (maze[i][j].content == 'S') {

stack.push(maze[i][j]);

maze[i][j].visited=true;

}

}

}

        boolean result=false;

        while(!stack.isEmpty())

        {

            MyPoint t=stack.pop();

           //System.out.println("pop point:"+t.x+" "+t.y+" value:"+maze[t.x][t.y]);

            if(t.content == 'F')

            {

                result=true;

                end.x = t.x;

                end.y = t.y;

                break;

            }

            if(t.x-1  0  maze[t.x-1][t.y].visited==false  maze[t.x-1][t.y].content != 'W')

            {

                stack.push(maze[t.x-1][t.y]);

                maze[t.x-1][t.y].parentRow=t.x;

                maze[t.x-1][t.y].parentColumn=t.y;

                maze[t.x-1][t.y].visited=true;

            }

            if(t.x + 1  x  maze[t.x+1][t.y].visited==false  maze[t.x+1][t.y].content != 'W')

            {

                stack.push(maze[t.x+1][t.y]);

                maze[t.x+1][t.y].parentRow=t.x;

                maze[t.x+1][t.y].parentColumn=t.y;

                maze[t.x+1][t.y].visited=true;

            }

            if(t.y - 1  0  maze[t.x][t.y - 1].visited==false  maze[t.x][t.y-1].content != 'W')

            {

                stack.push(maze[t.x][t.y-1]);

                maze[t.x][t.y-1].parentRow=t.x;

                maze[t.x][t.y-1].parentColumn=t.y;

                maze[t.x][t.y-1].visited=true;

            }

            if( t.y + 1  y  maze[t.x][t.y + 1].visited==false  maze[t.x][t.y+1].content != 'W')

            {

                stack.push(maze[t.x][t.y+1]);

                maze[t.x][t.y+1].parentRow=t.x;

                maze[t.x][t.y+1].parentColumn=t.y;

                maze[t.x][t.y+1].visited=true;

            }

 

        }

        return result;

    }

    public static void show(int x,int y,MyPoint[][] visited)

    {

        if(visited[x][y].parentRow==-1)

        {

            System.out.println("["+x+","+y+"]");

            return;

        }

        show(visited[x][y].parentRow,visited[x][y].parentColumn,visited);

        System.out.println("-"+"["+x+","+y+"]");

    }

    public static void main(String[] args)

    {

     MyPoint[][] maze = getMazeArray();

     MyPoint point = new MyPoint('c',1,1);

        if(tomRun(maze,point))

        {

            System.out.println("逃生路径如下:");

            show(point.x,point.y,maze);

        }

        else

            System.out.println("无法走出迷宫!");

    }

}

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW

WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOW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OWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW

WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW

WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

java迷宫程序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java迷宫小游戏、java迷宫程序的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。