如何在 MySQL workbench 中存储 10x10 地图/游戏区域?
How can I store 10x10 map / game field in MySQL workbench?
我正在 java 中制作推箱子游戏。
地图/运动场是一个 10x10 阵列。
数组中的一个字段可以包含 5 个不同对象之一
- 目标字段
- 宝箱
- 玩家
- 墙
- Empty Field(这只是一个玩家可以走过的空白区域)
现在我想将该地图存储在 MySql 数据库中 我不太确定如何处理这个问题。
我不知道 table 会是什么样子。
稍后我应该能够拉出地图,以便玩家可以立即玩或修改场地。
我想过使用 100 个字符的字符串,每个对象都有一个特定的字符,所以我知道它的含义和位置。
是的,所以一种方法是让 table 具有基于 column/row 的唯一键。然后,您可以将 column/row 和 link 相关的密钥存储到目标字段、箱子、玩家、墙壁、空字段。
编辑:
要回答关于此答案的问题,您可以创建一个 Location class,其中 x 和 y 代表网格中的一个点。然后覆盖 equals/hashCode 使其唯一。然后你可以使用一个地图来存储位置和该位置的相关游戏对象!
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 中制作推箱子游戏。
地图/运动场是一个 10x10 阵列。 数组中的一个字段可以包含 5 个不同对象之一
- 目标字段
- 宝箱
- 玩家
- 墙
- Empty Field(这只是一个玩家可以走过的空白区域)
现在我想将该地图存储在 MySql 数据库中 我不太确定如何处理这个问题。 我不知道 table 会是什么样子。 稍后我应该能够拉出地图,以便玩家可以立即玩或修改场地。
我想过使用 100 个字符的字符串,每个对象都有一个特定的字符,所以我知道它的含义和位置。
是的,所以一种方法是让 table 具有基于 column/row 的唯一键。然后,您可以将 column/row 和 link 相关的密钥存储到目标字段、箱子、玩家、墙壁、空字段。
编辑: 要回答关于此答案的问题,您可以创建一个 Location class,其中 x 和 y 代表网格中的一个点。然后覆盖 equals/hashCode 使其唯一。然后你可以使用一个地图来存储位置和该位置的相关游戏对象!
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);
}
}