重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、牟定网站维护、网站推广。
你看一下吧。
----------------------------------------
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;
}
}
import java.io.*;
import java.util.Stack;
class MazeCell {
int x, y;
MazeCell() {
}
MazeCell(int i, int j) {
x = i; y = j;
}
boolean equals(MazeCell cell) {
return x == cell.x y == cell.y;
}
}
class Maze {
int rows = 0, cols = 0;
char[][] store;
MazeCell currentCell, exitCell = new MazeCell(), entryCell = new MazeCell();
final char exitMarker = 'e', entryMarker = 'm', visited = '.';
final char passage = '0', wall = '1';
Stack mazeStack = new Stack();
Maze() {
int row = 0, col = 0;
Stack mazeRows = new Stack();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
String str;
System.out.println("Enter a rectangular maze using the following "
+ "characters:\nm - entry\ne - exit\n1 - wall\n0 - passage\n"
+ "Enter one line at at time; end with Ctrl-z (PC) or Ctrl-D (Unix):");
try {
str = buffer.readLine();
while (str != null) {
row++;
cols = str.length();
str = "1" + str + "1"; // put 1s in the borderline cells;
mazeRows.push(str);
if (str.indexOf(exitMarker) != -1) {
exitCell.x = row;
exitCell.y = str.indexOf(exitMarker);
}
if (str.indexOf(entryMarker) != -1) {
entryCell.x = row;
entryCell.y = str.indexOf(entryMarker);
}
str = buffer.readLine();
}
} catch(IOException eof) {
}
rows = row;
store = new char[rows+2][]; // create a 1D array of char arrays;
store[0] = new char[cols+2]; // a borderline row;
for ( ; !mazeRows.isEmpty(); row--)
store[row] = ((String) mazeRows.pop()).toCharArray();
store[rows+1] = new char[cols+2]; // another borderline row;
for (col = 0; col = cols+1; col++) {
store[0][col] = wall; // fill the borderline rows with 1s;
store[rows+1][col] = wall;
}
}
void display(PrintStream out) {
for (int row = 0; row = rows+1; row++)
out.println(store[row]);
out.println();
}
void pushUnvisited(int row, int col) {
if (store[row][col] == passage || store[row][col] == exitMarker)
mazeStack.push(new MazeCell(row,col));
}
void exitMaze(PrintStream out) {
int row = 0, col = 0;
currentCell = entryCell;
out.println();
while (!currentCell.equals(exitCell)) {
row = currentCell.x;
col = currentCell.y;
display(System.out); // print a snapshot;
if (!currentCell.equals(entryCell))
store[row][col] = visited;
pushUnvisited(row-1,col);
pushUnvisited(row+1,col);
pushUnvisited(row,col-1);
pushUnvisited(row,col+1);
if (mazeStack.isEmpty()) {
display(out);
out.println("Failure");
return;
}
else currentCell = (MazeCell) mazeStack.pop();
}
display(out);
out.println("Success");
}
static public void main (String args[]) {
(new Maze()).exitMaze(System.out);
}
}
试一下这个,是不是你想要的……
___________________________________________________________________________________________________________________________
html代码?你是说你要做一个java applet的网页迷宫小游戏??
我这是用c写的。你可以看看,希望能帮助到你。
#include"stdlib.h"
#include"stdio.h"
#define N 50
#define M 50
int X;
int maze[N+2][M+2];
struct point{
int row,col,predecessor;
}queue[512];
int head=0,tail=0;
void shoudong_maze(int m,int n){
int i,j;
printf("\n\n");
printf("请按行输入迷宫,0表示通路,1表示障碍:\n\n");
for(i=0;im;i++)
for(j=0;jn;j++)
scanf("%d",maze[i][j]);
}
void zidong_maze(int m,int n){
int i,j;
printf("\n迷宫生成中……\n\n");
system("pause");
for(i=0;im;i++)
for(j=0;jn;j++)
maze[i][j]=rand()%2;
//由于rand()产生的随机数是从0到RAND_MAX
//RAND_MAX是定义在stdlib.h中的,其值至少为32767)
//要产生从X到Y的数,只需要这样写:k=rand()%(Y-X+1)+X;
}
void print_maze(int m,int n){
int i,j;
printf("\n迷宫生成结果如下:\n\n");
printf("迷宫入口\n");
printf("↓");
for(i=0;im;i++)
{printf("\n");
for(j=0;jn;j++)
{if(maze[i][j]==0) printf("□");
if(maze[i][j]==1) printf("■");}
}
printf("→迷宫出口\n");
}
void result_maze(int m,int n)
{ int i,j;
printf("迷宫通路(用☆表示)如下所示:\n\t");
for(i=0;im;i++)
{ printf("\n");
for(j=0;jn;j++)
{if(maze[i][j]==0||maze[i][j]==2) printf("□");
if(maze[i][j]==1) printf("■");
if(maze[i][j]==3) printf("☆");
}
}
}
void enqueue(struct point p)
{ queue[tail]=p;
tail++;
}
struct point dequeue()
{ head++;
return queue[head-1];
}
int is_empty()
{ return head==tail;
}
void visit(int row,int col,int maze[52][52])
{ struct point visit_point={row,col,head-1};
maze[row][col]=2;
enqueue(visit_point);
}
int mgpath(int maze[52][52],int m,int n)
{ X=1;
struct point p={0,0,-1};
if(maze[p.row][p.col]==1)
{ printf("\n===============================================\n");
printf("此迷宫无解\n\n");X=0;return 0;}
maze[p.row][p.col]=2;
enqueue(p);
while(!is_empty())
{p=dequeue();
if((p.row==m-1)(p.col==n-1)) break;
if((p.col+1n)(maze[p.row][p.col+1]==0)) visit(p.row,p.col+1,maze);
if((p.row+1m)(maze[p.row+1][p.col]==0)) visit(p.row+1,p.col,maze);
if((p.col-1=0)(maze[p.row][p.col-1]==0)) visit(p.row,p.col-1,maze);
if((p.row-1=0)(maze[p.row-1][p.col]==0)) visit(p.row-1,p.col,maze);
}
if(p.row==m-1p.col==n-1)
{printf("\n==================================================================\n");
printf("迷宫路径为:\n");
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
while(p.predecessor!=-1)
{p=queue[p.predecessor];
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
}
}
else {printf("\n=============================================================\n");
printf("此迷宫无解!\n\n");X=0;}
return 0;
}
int main()
{int i,m,n,cycle=0;
while(cycle!=(-1))
{
printf("********************************************************************************\n");
printf(" ☆欢迎进入迷宫求解系统☆\n");
printf(" 设计者:尹旭 林静波(信息2班)\n");
printf("********************************************************************************\n");
printf(" 手动生成迷宫 请按:1\n");
printf(" 自动生成迷宫 请按:2\n");
printf(" 退出 请按:3\n\n");
printf("********************************************************************************\n");
printf("\n");
printf("请选择你的操作:\n");
scanf("%d",i);
switch(i)
{case 1:printf("\n请输入行数:");
scanf("%d",m);
printf("\n");
printf("请输入列数:");
scanf("%d",n);
while((m=0||m50)||(n=0||n50))
{ printf("\n抱歉,你输入的行列数超出预设范围(0-50,0-50),请重新输入:\n\n");
printf("请输入行数:");
scanf("%d",m);
printf("\n");
printf("请输入列数:");
scanf("%d",n);
}
shoudong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');
break;
case 2:printf("\n请输入行数:");
scanf("%d",m);
printf("\n");
printf("请输入列数:");
scanf("%d",n);
while((m=0||m50)||(n=0||n50))
{printf("\n抱歉,你输入的行列数超出预设范围(0-50,0-50),请重新输入:\n\n");
printf("请输入行数:");
scanf("%d",m);
printf("\n");
printf("请输入列数:");
scanf("%d",n);
}
zidong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");getchar();while(getchar()!='\n');break;
case 3:cycle=(-1);
break;
default:printf("\n");
printf("你的输入有误!\n");
printf("\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');break;
}
}
}