我正在用 java 创建 Sokoban 游戏。
map /比赛 field 是一个 10x10 数组。 数组中的一个字段可以包含 5 个不同对象之一
- 目标字段
- 胸部
- 玩家
- 墙
- 空白区域(这只是玩家可以走过的空白区域)
现在我想将该 map 存储在 MySql 数据库中,我不太确定如何解决这个问题。 我不知道 table 会是什么样子。 稍后我应该能够拉出 map ,以便玩家可以立即玩或修改 field 。
我考虑过使用 100 个字符的字符串,每个对象都有一个特定的字符,所以我知道它的含义和位置。
最佳答案
是的,一种方法是拥有一个具有基于列/行的唯一键的表。然后,您可以存储与链接到目标字段、箱子、玩家、墙壁、空字段的列/行相关的 key 。
编辑: 要回答您对此答案的问题,您可以创建一个 Location 类,其中 x 和 y 代表网格中的一个点。然后重写 equals/hashCode 以使其唯一。然后您可以使用 map 来存储位置和该位置的相对游戏对象!
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);
}
}
关于java - 如何在 MySQL Workbench 中存储 10x10 map /游戏字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60815346/