Java 我可以在同一阵列空间中有两个对象吗

Java 我可以在同一阵列空间中有两个对象吗,java,arrays,Java,Arrays,因此,我们的目标是在java控制台中创建一个pacman游戏。 我有一个名为迷宫的类,用于基于int数组初始化2d对象数组。因为Pacman中的正方形既可以是可移动的空间,也可以是墙,所以我将其实例化为空间或墙 public Labyrinth() { tab2 = new Object[25][23]; // On part du principe que la taille du labyrinth ne change pas for (int ligne = 0; li

因此,我们的目标是在java控制台中创建一个pacman游戏。
我有一个名为迷宫的类,用于基于int数组初始化2d对象数组。因为Pacman中的正方形既可以是可移动的空间,也可以是墙,所以我将其实例化为空间或墙

public Labyrinth() {

    tab2 = new Object[25][23]; // On part du principe que la taille du labyrinth ne change pas

    for (int ligne = 0; ligne < tab.length; ++ligne) {
        for (int colonne = 0; colonne < tab[ligne].length; ++colonne) {
            int value = tab[ligne][colonne];
            Position p = new Position(ligne, colonne);
            switch (value) {
                case 0:
                    s = new Space(value, p);
                    tab2[ligne][colonne] = s;
                    break;
                case 1:
                    w = new Wall(p);
                    tab2[ligne][colonne] = w;
                    break;
                case 2:
                    s = new Space(value, p);
                    tab2[ligne][colonne] = s;
                    break;
                case 3:
                    s = new Space(value, p);
                    tab2[ligne][colonne] = s;
                    break;
                case 4:
                    s = new Space(value, p);
                    tab2[ligne][colonne] = s;
                    break;
                case 5:
                    s = new Space(value, p);
                    tab2[ligne][colonne] = s;
                    break;
                case 6:
                    s = new Space(value, p);
                    tab2[ligne][colonne] = s;
                    break;
            }
        }
    }
这意味着我在同一个数组“坐标”上有一个空间对象和一个食物、吃豆人或鬼对象

编辑:这里的目标是将Pacman从一个“空间”对象移动到另一个
Edit2:我没意识到我放了两次同样的代码

编辑:这里的目标是将Pacman从一个“空间”对象移动到另一个

那么,你为什么不想这样建模呢

让你的
空间
类接受一些“内容”:

执行此操作时,数组元素都是相同类型的
Space

与直接将2dim数组设置为
SpaceContent
类型相比,这似乎只是一个小小的优势,但请看以下内容:

enum Direction {
 NORTH,EAST,SOUTH,WEST
}

class Space {
  private SpaceContent content = SpaceContent.EMPTY;
  private Map<Direction,Space> neigbours = new HasMap<>();
  // ...
  public void setNeigbour(Direction direction, Space space){
    neigbours.put(direction,space);
  }

  boolean isSpace(){
     return true;
  } 

  public boolean moveTo(Direction direction){
    if(!neigbours.containsKey(direction))
       return false; // cannot move out of the field. 

    Space target = neigbours.get(direction);
    if(!target.isSpace())
        return false; // cannot move into a wall

    target.setContent(content);
    content = SpaceContent.EMPTY;
  }
}

class Wall extends Space{
   @override 
   boolean isSpace(){
     return false;
  }        
}
枚举方向{
北、东、南、西
}
类空间{
私有SpaceContent=SpaceContent.EMPTY;
私有映射neigbours=new HasMap();
// ...
公共空间设置边界(方向、空间){
neigbours.put(方向、空间);
}
布尔isSpace(){
返回true;
} 
公共布尔值移动到(方向){
如果(!neigbours.containsKey(方向))
return false;//无法移出该字段。
空间目标=neigbours.get(方向);
如果(!target.isSpace())
return false;//不能移动到墙中
target.setContent(content);
content=SpaceContent.EMPTY;
}
}
类墙扩展了空间{
@凌驾
布尔isSpace(){
返回false;
}        
}
在移动图形的方法上:

  class Ghost{
    private Direction lastDirection;
    private int x,y;
    // getters and setters
    public void movedTo(Direction newDirection){
       lastDirection = newDirection;
       x= newDirection.nextX(x); // we have to enhance the enum for this
       y= newDirection.nextY(y); 
    }
  }
  List<Ghost> ghosts = new ArrayList<>(); // filled with ghosts elsewhere.
  // tab2 is an array of `Space`
  void moveGhosts(){
    for(Ghost ghost : ghosts){
       Direction direction = ghost.getLastDirection();
       Space currentField = 
      while(!tab2[ghost.getX()][ghost.getY()].moveTo(direction)){
         direction = turnRight(direction);
      }
      ghost.moveTo(direction);
    }

    private void turnRight(Direction direction){
       return  Direction.values()[direction.ordinal()+1% Direction.values().length];
    }
}
类重影{
私人指导;
私有整数x,y;
//接球手和接球手
公共无效移动到(新方向){
lastDirection=新方向;
x=newDirection.nextX(x);//我们必须为此增强枚举
y=新方向。nextY(y);
}
}
列表重影=新建ArrayList();//到处都是鬼。
//tab2是`空格'的数组`
void moveGhosts(){
用于(鬼:鬼){
方向=ghost.getLastDirection();
空间当前字段=
而(!tab2[ghost.getX()][ghost.getY()].moveTo(方向)){
方向=右转(方向);
}
鬼。移动到(方向);
}
私人空地右转(方向){
返回方向.values()[Direction.ordinal()+1%方向.values().length];
}
}

你是说
游戏对象[]gos
?不知道那是什么。我不清楚你的主要描述与你的“编辑”有什么关系。我可能解释得很糟糕。我有一个“墙”或“可移动”空间的二维数组。在一个空间里,可以有一个吃豆人。如何将Pacman从一个空间移动到另一个空间。您的意思是每个数组元素当前都是一个空间或墙。现在,您需要在单个阵列空间中有多个游戏对象,并且该空间中可能有数量未知的对象;是这样吗?移动pacman很简单,只需将其放置在2d数组中它所属的位置。
enum Direction {
 NORTH,EAST,SOUTH,WEST
}

class Space {
  private SpaceContent content = SpaceContent.EMPTY;
  private Map<Direction,Space> neigbours = new HasMap<>();
  // ...
  public void setNeigbour(Direction direction, Space space){
    neigbours.put(direction,space);
  }

  boolean isSpace(){
     return true;
  } 

  public boolean moveTo(Direction direction){
    if(!neigbours.containsKey(direction))
       return false; // cannot move out of the field. 

    Space target = neigbours.get(direction);
    if(!target.isSpace())
        return false; // cannot move into a wall

    target.setContent(content);
    content = SpaceContent.EMPTY;
  }
}

class Wall extends Space{
   @override 
   boolean isSpace(){
     return false;
  }        
}
  class Ghost{
    private Direction lastDirection;
    private int x,y;
    // getters and setters
    public void movedTo(Direction newDirection){
       lastDirection = newDirection;
       x= newDirection.nextX(x); // we have to enhance the enum for this
       y= newDirection.nextY(y); 
    }
  }
  List<Ghost> ghosts = new ArrayList<>(); // filled with ghosts elsewhere.
  // tab2 is an array of `Space`
  void moveGhosts(){
    for(Ghost ghost : ghosts){
       Direction direction = ghost.getLastDirection();
       Space currentField = 
      while(!tab2[ghost.getX()][ghost.getY()].moveTo(direction)){
         direction = turnRight(direction);
      }
      ghost.moveTo(direction);
    }

    private void turnRight(Direction direction){
       return  Direction.values()[direction.ordinal()+1% Direction.values().length];
    }
}