我正在用java创建Sokoban游戏。
地图/运动场是一个10×10的数组。数组中的一个字段可以包含5个不同的对象中的一个
Field
现在,我想将该地图存储在MySql数据库中,我不太确定如何处理这个问题。我不知道这张桌子会是什么样子。稍后,我应该能够拉出地图,这样玩家就可以立即发挥或修改球场。
我想使用一个由100个字符组成的字符串,每个对象都有一个特定的字符,所以我知道它的意义和位置。
发布于 2020-03-23 14:24:10
是的,所以一种方法是根据列/行有一个具有唯一键的表。然后,您可以存储相对于链接到目标字段、胸部、播放器、墙壁、空字段的列/行的键。
编辑:要回答你对这个答案的问题,你可以创建一个位置类,并且x和y代表网格中的一个点。然后重写等于/hashCode以使其惟一。然后,您可以使用地图存储位置和相对GameObject在该位置!
public class Location {
private final int x;
private final int y;
private final int hashCode;
public Location(int x, int y) {
this.x = x;
this.y = y;
this.hashCode = Objects.hash(x, y);
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other instanceof Location) {
Location otherLocation = (Location) other;
return otherLocation.x == x && otherLocation.y == y;
}
return false;
}
}
interface GameObject {
}
class TargetField implements GameObject {
}
class MyGame {
private final Map<Location, GameObject> map;
MyGame(Map<Location, GameObject> map) {
this.map = map;
}
public void set(Location location, GameObject object) {
map.put(location, object);
}
}https://stackoverflow.com/questions/60815346
复制相似问题