问题描述
我目前正在用Java编写一个2D迷宫,其中的NPC会根据时间步长移动!但是,我不完全确定如何根据每个时间步骤更新其位置。我将它放在第一个时间步中将敌人正确地添加到其位置的位置。但是,我不太确定如何在下一步进行更新。这是创建我在说什么所需的代码:
这是主要课程:
import java.util.*;
import java.io.*;
public class astar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String mapFile;
String barberFile;
createMap mazeInput = new createMap();
System.out.print("Please enter the map file name: ");
mapFile = input.nextLine().trim();
System.out.println();
System.out.print("Please enter the barber file name: ");
barberFile = input.nextLine().trim();
mazeInput.createMaze(mapFile);
mazeInput.placeEnemies(barberFile);
//mazeInput.displayMap();
}
}
这是createMap.java,这是从用户提供的文本文件中读取地图的方式:
import java.util.*;
import java.io.*;
public class Map {
char [][] mapFinal;
int time = 0;
public void createArray(int x,int y){
mapFinal = new char[x][y];
for (int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
mapFinal[i][j] = ' '; // filling the array with blank spaces.
}
}
}
public void addStart(int startX,int startY){
mapFinal[startX][startY] = 'S';
}
public void addGoal (int goalX,int goalY){
mapFinal[goalX][goalY] = 'G';
}
public void addWall(int wallX,int wallY){
mapFinal[wallX][wallY] = 'W';
}
public void addBarber(int timeStep,int xCords,int yCords){
if (timeStep == time){
mapFinal[xCords][yCords] = 'B';
}
}
public void displayMap(){
System.out.println("Timestep: " + time);
for (int i = 0; i < mapFinal.length; i++){
for (int j = 0; j < mapFinal[i].length; j++){
System.out.print(mapFinal[i][j] + " ");
}
System.out.println();
}
System.out.print("--------------------------");
System.out.println();
}
}
这是Map.java,这实际上是在地图上添加内容的地方:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class createMap {
int x;
int y;
Map mazeForMap = new Map();
ArrayList<String> map = new ArrayList<String>();
public void createMaze(String mazeFile){
try{
Scanner mapRead = new Scanner(new File(mazeFile));
while(mapRead.hasNext()){
map.add(mapRead.nextLine().trim());
}
mapRead.close();
for(int i = 0; i < map.size(); i++){
String value = map.get(i);
String[] tempArray = value.split(" ");
switch(tempArray[0]){
case "M":
x = Integer.parseInt(tempArray[1]);
y = Integer.parseInt(tempArray[2]);
mazeForMap.createArray(x,y);
break;
case "S":
x = Integer.parseInt(tempArray[1]);
y = Integer.parseInt(tempArray[2]);
mazeForMap.addStart(x,y);
break;
case "G":
x = Integer.parseInt(tempArray[1]);
y = Integer.parseInt(tempArray[2]);
mazeForMap.addGoal(x,y);
break;
case "W":
x = Integer.parseInt(tempArray[1]);
y = Integer.parseInt(tempArray[2]);
mazeForMap.addWall(x,y);
break;
default:
System.out.println("Maze map loaded successfully.");
break;
}
}
} catch (Exception o){
System.out.println("There was an error reading the map file. Please ensure you have entered the correct file name.");
}
}
public void placeEnemies(String barberFile){
try{
int timeStep,xCords,yCords;
Scanner barberRead = new Scanner(new File(barberFile));
ArrayList<String> map = new ArrayList<String>();
while(barberRead.hasNext()){
map.add(barberRead.nextLine().trim());
}
//mazeForMap.getBarbers(barberRead);
barberRead.close();
for(int i = 0; i < map.size() - 1; i++){
String value = map.get(i);
String[] tempBArray = value.split(" ");
switch(tempBArray[0]){
case "-1":
break;
default:
timeStep = Integer.parseInt(tempBArray[0]);
xCords = Integer.parseInt(tempBArray[1]);
yCords = Integer.parseInt(tempBArray[2]);
mazeForMap.addBarber(timeStep,yCords);
break;
}
}
mazeForMap.displayMap();
} catch (Exception o){
System.out.println("There was an error reading the barber file. Please ensure you have entered the correct file name.");
}
}
public void displayMap(){
mazeForMap.displayMap();
}
}
您需要在包裹中添加地图文件和理发文件,这就是我一直在使用的文件:
mapcup.txt
M 10 8
S 0 2
G 6 5
W 4 3
W 4 4
W 4 5
W 4 6
W 5 6
W 6 6
W 7 6
W 8 3
W 8 4
W 8 5
W 8 6
E
这是一个地图文件,M设置2d数组的尺寸,S是起点,G是目标。 W是墙,否则是无法到达的区域。
barbercup.txt
0 5 3
0 5 4
0 6 3
0 6 2
1 5 3
1 5 4
1 6 3
1 6 2
2 5 3
2 5 4
2 6 3
2 6 2
3 5 3
3 5 4
3 6 3
3 6 2
4 5 3
4 5 4
4 6 3
4 6 2
5 5 3
5 5 4
5 6 3
5 6 2
6 5 3
6 5 4
6 6 3
6 6 2
7 5 3
7 5 4
7 6 3
7 6 2
8 5 3
8 5 4
8 6 3
8 6 2
9 5 3
9 5 4
9 6 3
9 6 2
10 5 3
10 5 4
10 6 3
10 6 2
11 5 3
11 5 4
11 6 3
11 6 2
12 5 3
12 5 4
12 6 3
12 6 2
13 5 3
13 5 4
13 6 3
13 6 2
14 5 3
14 5 4
14 6 3
14 6 2
15 5 3
15 5 4
15 6 3
15 6 2
16 5 3
16 5 4
16 6 3
16 6 2
17 5 3
17 5 4
17 6 3
17 6 2
-1
此文件左侧的每个数字都是一个时间步。它将读取该文件并将所有敌人放置在第一步的位置(0),但是我无法弄清楚如何实时增加他们并更改他们的位置。
谢谢您的输入!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)