创建可以在Java中放置和移动对象的二维地图
我是Java新手,我正在尝试制作一个基于2D文本的游戏,该游戏内部包含一个对象可以交互和移动的世界地图。然而,一开始我就感到困惑。假设我有一张3x3的地图。因此,每个磁贴都是多维数组中的一个值(例如坐标[0][2]上的磁贴)。但是,每个磁贴必须是对象数组(例如,磁贴对象(森林、地面、沙漠)或实体(人、岩石、鸭子))。它应该是一个数组,因为每个图块必须至少包含一个图块对象(森林、地面、沙漠),但理论上可以包含无限多个实体 我在这里尝试并结合了一些答案。当前的想法是创建一个只有坐标的超类实体。创建实体时(给定一些坐标),对象将链接到该位置的世界地图(似乎不起作用)。世界地图是一个可以容纳实体的多维数组 实体没有链接到世界地图,我不知道为什么。在Entity.java和World.java文件中肯定会出错,但我仍然不确定如何将位置设置为世界地图请注意,稍后我需要设置或更改实体在地图上的位置,或将实体作为一个整体删除(包括其在地图上的位置)。创建可以在Java中放置和移动对象的二维地图,java,arrays,multidimensional-array,tile,Java,Arrays,Multidimensional Array,Tile,我是Java新手,我正在尝试制作一个基于2D文本的游戏,该游戏内部包含一个对象可以交互和移动的世界地图。然而,一开始我就感到困惑。假设我有一张3x3的地图。因此,每个磁贴都是多维数组中的一个值(例如坐标[0][2]上的磁贴)。但是,每个磁贴必须是对象数组(例如,磁贴对象(森林、地面、沙漠)或实体(人、岩石、鸭子))。它应该是一个数组,因为每个图块必须至少包含一个图块对象(森林、地面、沙漠),但理论上可以包含无限多个实体 我在这里尝试并结合了一些答案。当前的想法是创建一个只有坐标的超类实体。创建实
我不是100%确定你想要实现什么,但让我回答你的直接问题,然后可能给你一些提示。 因此,如果要将实体John分配到左上角,可以通过以下方式完成:
world.mapArray[0][0]=John代码>
无论如何,为javabean/pojo
提供访问器和变异器方法是一个很好的实践。说到这里,我建议你把你所有的领域都私有化,然后。在这种情况下,您的代码如下所示:
public class Map {
private Entity[][] mapArray;
// Creates an istance of a map with a user-defined size
public Map(int width, int height) {
mapArray = new Entity[width][height];
}
public void setEntityAt(Entity entity, int x, int y) {
if (x < 0 || x > mapArray.length || y < 0 || y > mapArray[x].length) {
throw new IllegalArgumentException("Incorrect X or Y parameters");
}
mapArray[x][y] = entity;
}
}
最后,但并非最不重要的一点是,如果您试图编译代码,您必须知道在一个Java文件中不能有超过1个公共类。
另外,您可以使用JUnit进行测试。我尽量不帮助别人,告诉他们按我的方式思考。去直觉引导你的地方往往更好。尽管如此,我仍然认为您应该重构Map类,因为您并不真正想要一个java Map,并给它一个不同的名称,在这里我建议World作为您的类来包含您的分幅。你的主要课程应该是游戏,有一个主要的方法。我之所以建议这些更改,只是因为Map和main在java中有自己的含义
您还需要分离类,以便每个类都位于自己的.java文件中。因此,您开始的工作,加上上面提到的重构,剩下三个文件:
Entity.java
package stackoverflow;
public class Entity {
String name;
int xPosition, yPosition;
// Creates an entity with a name, and an x/y-position on a maps
public Entity(String name, int xPosition, int yPosition) {
this.name = name;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
}
World.java
package stackoverflow;
public class World {
Entity[][] mapArray;
// Creates an istance of a map with a user-defined size
public World(int width, int height) {
mapArray = new Entity[width][height];
}
}
Game.java
package stackoverflow;
public class Game {
static int mapWidth = 8;
static int mapHeight = 8;
public static void main(String[] args) {
World world = new World(mapWidth, mapHeight);
Entity john = new Entity("John", 0, 0);
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
Entity tile = new Entity("Tile", x, y);
}
}
}
}
Entity.java
包装堆垛溢出;
公共类实体{
字符串名;
int xPosition,yPosition;
//在地图上创建具有名称和x/y位置的图元
公共实体(字符串名称、int-xPosition、int-yPosition){
this.name=名称;
this.xPosition=xPosition;
this.yPosition=yPosition;
}
}
java
包装堆垛溢出;
公共阶级世界{
实体[][]映射数组;
//创建具有用户定义大小的地图距离
公共世界(整数宽度、整数高度){
mapArray=新实体[宽度][高度];
}
}
Game.java
包装堆垛溢出;
公开课游戏{
静态int-mapWidth=8;
静态int MAPHEIGH=8;
公共静态void main(字符串[]args){
世界世界=新世界(地图宽度、地图高度);
实体约翰=新实体(“约翰”,0,0);
对于(int x=0;x
多维数组可以工作,但有一些缺点。你也可以把你的世界变成一个实体列表,你可以在其中添加、删除、遍历等等,可能比数组更容易。但是你可以用你现在所知道的来湿你的脚。对于测试,我建议您将JUnit添加到项目中。我也喜欢宣传测试驱动开发,但这可能是一个完全不同的问题
要使实体移动,请将移动(x,y)方法或移动到(x,y)方法添加到实体对象,并将移动距离(或新坐标)作为参数。您只是将平铺贴图误认为是世界。这些概念并不相同。平铺贴图描述了世界的基本外观,可能还描述了一些基本属性(如可行走、不可通行)
然而,这并不是对世界的完整描述。任何不是平铺的东西都需要另外建模。我可以想出三个可行的概念:
a、 )为占据平铺位置的对象添加一个附加数组。如果在任何时候只有一个“东西”可以占据一个特定的位置,这可能会很好地工作
b、 )而不是使用平铺数组,而是使用位置数组。不同之处在于,您可以对位置进行建模,以包含您需要的任何内容,例如,一个地砖加上该位置的居住者列表。这是a的OO变体
c、 )具有独立的数据结构,可跟踪世界上存在的事物,例如人员列表。您需要对某个位置进行建模,该位置是可以填充世界的项目状态的一部分,例如,一个人现在有一个额外的x,y坐标,指示其在世界中的位置
无论您选择哪种,它们都有各自的优点和缺点,您也可以使用混合型,例如,有位置图和项目列表。这结合了两种模型的优点(通过位置图快速简单地检查位置周围环境,以及通过列表简单地访问整个世界人口)。挑选适合你需要的东西。我认为你的瓷砖不应该是一个实体。我认为瓷砖应该封装实体
public class Tile {
final int xPos,yPos;
private String type;
List<Entity> entities = new ArrayList<>();
public Tile(int xPosition, int yPosition) {
xPos = xPosition;
yPos = yPosition;
}
public void setType(String type){
this.type = type;
}
public String getType() {
return type;
}
public void addEntity(Entity e){
entities.add(e);
}
public void removeEntity(Entity e){
entities.remove(e);
}
}
那么,我们怎样才能让约翰加入这个世界呢?我说,我们只是把他加入这个世界,让世界知道什么
public class Tile {
final int xPos,yPos;
private String type;
List<Entity> entities = new ArrayList<>();
public Tile(int xPosition, int yPosition) {
xPos = xPosition;
yPos = yPosition;
}
public void setType(String type){
this.type = type;
}
public String getType() {
return type;
}
public void addEntity(Entity e){
entities.add(e);
}
public void removeEntity(Entity e){
entities.remove(e);
}
}
public class World {
Tile[][] coordinates;
List<Tile> tiles = new ArrayList<>();
// Creates an instance of a map with a user-defined size
public World(int width, int height) {
coordinates = new Tile[width][height];
for(int i = 0; i<width; i++){
for(int j = 0; j<height; j++){
coordinates[i][j] = new Tile(i,j);
tiles.add(coordinates[i][j]);
}
}
}
public void addEntity(Entity e){
int x = e.getX();
int y = e.getY();
coordinates[x][y].addEntity(e);
}
public void setTileType(int x, int y, String type){
coordinates[x][y].setType(type);
}
public List<Tile> getTiles(){
return new List<>(tiles);
}
public List<Entity> getEntities(){
List<Entity> entities = new ArrayList<>();
for(Tile[] row: coordinates){
for(Tile tile: row){
entities.addAll(tile.entities);
}
}
}
}
public static void main(String[] args) {
// Creators
World worldMap = new World(mapWidth, mapHeight);
Character john = new Character("John", 0, 0);
Character mary = new Character("Mary", 1, 4);
worldMap.addEntity(john);
worldMap.addEntity(mary);
for (int x = 0; x < mapWidth; x++) {
worldMap.setTileType(x, 5, "forest");
}
// Printing out info about character(s)
//I think the world map should have a list of characters.
for (Character character : charList) {
System.out.print(character+": " + character.getName() + "\n");
}
System.out.print("\n"+charList.size() + " characters in play\n\n");
List<Tile> tileList = worldMap.getTiles();
// Printing out info about tile(s)
for (Tile tile : tileList) {
System.out.print(tile + " type: " + tile.getType() + "\n");
}
System.out.print("\n"+tileList.size() + " tiles in play");
}
Character john = new Character("john", x, y);
world.addEntity(john);
world.coordinates[john.getX()][john.getY()].addEntity(john);