迷宫
1.玩家类---player
package com.test.recursion;
import java.util.HashMap;
import java.util.Map;
public class Player {
private String playerName; //玩家姓名
private String playerPassword; //玩家密码
private int playerStep; //玩家步长
Map<Integer,String> map = new HashMap<Integer, String>(); //玩家走的路线的行号表示步数,列号由(行,列)组成的字符串
public Player(String playerName,String playerPassword){
this.playerName = playerName;
this.playerPassword = playerPassword;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getPlayerPassword() {
return playerPassword;
}
public void setPlayerPassword(String playerPassword) {
this.playerPassword = playerPassword;
}
public int getPlayerStep() {
return playerStep;
}
public void setPlayerStep(int playerStep) {
this.playerStep = playerStep;
}
@Override
public String toString() {
return "玩家" + this.getPlayerName() + "通过迷宫走过的步数为:" + this.getPlayerStep();
}
}
2.核心类---MazeBacktracking
package com.test.recursion;
import java.util.Scanner;
import java.util.Set;
/**
* 迷宫问题
* 0---表示可以走的
* 1---表示墙
* 2---表示走过的
*
*/
public class MazeBacktracking {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int rows = 0; //行数
int cows = 0; //列数
System.out.println("请输入迷宫的行数(>=4):");
rows = SCANNER.nextInt();
System.out.println("请输入迷宫的列数(>=4):");
cows = SCANNER.nextInt();
while (rows < 4 ) {
System.out.println("输入的行数不正确,请重新输入:");
rows = SCANNER.nextInt();
}
while (cows < 4) {
System.out.println("输入的列数不正确,请重新输入:");
cows = SCANNER.nextInt();
}
int[][] maze = new int[rows][cows];
//将四边设置为1,表示为围墙
for (int i = 0; i < maze[0].length; i++) {
maze[0][i] = 1; //上边
maze[maze.length - 1][i] = 1; //下边
}
for (int i = 0; i < maze.length; i++) {
maze[i][0] = 1; //左边
maze[i][maze[0].length - 1] = 1; //右边
}
//maze[maze.length - 2][maze[0].length - 1]设置为出口
maze[maze.length - 2][maze[0].length - 1] = 0;
//将maze[maze.length - 1][0]设置为入口
maze[(maze.length - 1) / 2][0] = 0;
//设置障碍
//Math类中的random---[0,1),设置障碍个数为(maze.length * maze[0].length) / 5
int obstaclesNum = (maze.length * maze[0].length ) / 5; //设置障碍个数
for (int i = 0; i < obstaclesNum; i++) {
maze[(int)((Math.random() * (maze.length - 2)) + 1)][(int)((Math.random() * (maze[0].length - 2)) + 1)] = 1;
}
//设置入口的右边不设置障碍---这是因为怕随机产生的障碍刚好这里有一个,那还没有开始就结束了
maze[((maze.length - 1) / 2)][1] = 0;
//玩家
System.out.println("请输入玩家号:");
String playerName = SCANNER.next();
System.out.println("请输入密码:");
String playerPassword = SCANNER.next();
Player player = new Player(playerName, playerPassword);
//玩家从入口开始玩迷宫----设置走路路线从上右下左
int playRows = (maze.length -1) /2; //得到入口的行位置
int playcows = 0; //得到入口的列位置
while (true && playRows < maze.length - 1 && playcows < maze[0].length - 1) {
//上---如果上为0,则表示可以走
if(maze[playRows -1][playcows] == 0){
maze[playRows - 1][playcows] = 2; //表示走这一格,走过后设置为2
playRows--; //行向上移动一格
}else if(maze[playRows][playcows + 1] == 0){
//右---如果右为0,则表示可以走
maze[playRows][playcows + 1] = 2;
playcows++; //列向右移动一格
}else if(maze[playRows + 1][playcows] == 0){
//下---如果下为0,则表示可以走
maze[playRows + 1][playcows] = 2;
playRows++;
}else if(maze[playRows][playcows - 1] == 0){
//左---如果左为0,则表示可以走
maze[playRows][playcows - 1] = 2;
playcows--;
}else{
break;
}
player.setPlayerStep(player.getPlayerStep() + 1); //表示玩家走了一步
player.map.put(Integer.valueOf(player.getPlayerStep()), "(" + playRows + "," + playcows + ")");
}
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
System.out.printf("%d\t",maze[i][j]);
}
System.out.println();
}
//玩家玩迷宫的信息,行号不为出口行号,列号不为出口列号,则为失败
if(playRows != maze.length - 2 && playcows != maze[0].length - 1){
System.out.println("玩家玩迷宫失败,没有找到出口!!!");
}
System.out.println("玩家走的步数为:" + player.getPlayerStep() +"\n玩家走的路线为:");
Set<Integer> keys = player.map.keySet(); //步数的信息
for (Integer key : keys) {
System.out.printf("%s\t",player.map.get(key));
}
}
}
结果为: