所以目的是在java控制台中创建一个pacman游戏 我有一个名为Labyrinth的类,用于初始化基于int数组的2d Object数组。由于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;
}
}
}
“tab”是int数组,“tab2”是对象数组。 “value”参数是基于用于创建2d对象数组的int数组的int 是否有可能实例化Pacman,鬼魂和各种食物:
public class Space{
Ghost g;
Pacman pac;
Food f;
public Space(int value, Position p) {
super(p);
switch (value) {
case 2:
pac = new Pacman(p);
break;
case 3:
g = new Ghost(p);
break;
case 4:
f = new Gum(p);
break;
case 5:
f = new Fruit(p);
break;
case 6:
f = new Shroom(p);
break;
default:
break;
}
}
这意味着我在同一个阵列“坐标”上有一个空间物体和食物,pacman或幽灵对象。
编辑:这里的目的是将Pacman从1个“太空”物体移动到另一个物体 Edit2:没意识到我把相同的代码放了两次
答案 0 :(得分:0)
编辑:这里的目的是将Pacman从1个“空间”对象移动到另一个
那么,你为什么不想那样建模呢?
让您的Space
班级接受一些“内容”:
enum SpaceContent{
EMPTY,FOOD,GHOST,PAC_MAN
}
class Space {
private SpaceContent content = SpaceContent.EMPTY;
public void setContent(SpaceContent content){
this.content = content;
}
}
这样做时,数组元素的类型Space
这可能看起来比直接使用2dim-array类型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;
}
}
以及移动数据的方法:
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];
}
}