java - 从 LoZ Oracle 游戏中创建 secret 系统

标签 java python game-engine

最近我一直在重玩《塞尔达传说:季节神谕》和《时代神谕》游戏,密码引起了我的注意。我现在想用 Python 或 Java 重新创建它,看看它是如何工作的。

问题是,我找不到办法。你会如何制作一个编码系统

  1. 依赖于您输入的第一个“链接”代码(labrynna 或 Holodrum 的 secret ,正如游戏中所命名的那样),因此您无法窃取其他人的代码;
  2. 检测拼写错误。如果您输入 fobaz 而不是 foobar,它会检测到代码错误;
  3. 可以存储一堆(相当多的)信息。而且它也在 5-20 个字符之间,就像在游戏中一样(这不是必需的,但会很好)

注意:我并不是想为 Oracle 游戏创建一个生成器,只是一些代码来生成和读取类似于 Oracle 游戏中的 secret 的代码

本题的编程范围是如何使用模仿 Oracle 游戏中的密码的系统对信息进行编码和解码。

最佳答案

我期望这是一种序列化+编码的形式。

您基本上需要生成一个包含各种 secret 值的字节数组(这就是大多数binary protocols的写入方式),然后将每个字节编码为相应的字符(或更多),以便输出“字符串”可读。以下是如何在 Python 中将数据序列化为字节数组的示例:

玩家信息

player_name = "Robert"

player_stats = {
    "health": 200,
    "mana": 150,
    "has_sword": True,
    "has_arrows": False,
    "has_shovel": True,
    "dungeon1_complete": True
}

设置序列化信息

# If these can be greater than 255, they must be stored across multiple bytes - extra work
byte_attribs = ["health", "mana"]

bit_attribs = ["has_sword", "has_arrows", "has_shovel", "dungeon1_complete"]

player_name_max_length = 7
byte_attrib_offset = player_name_max_length
bit_attrib_offset = byte_attrib_offset + len(byte_attribs)

secret_storage = bytearray(bit_attrib_offset + len(bit_attribs))

assert(len(player_name) <= player_name_max_length)

执行序列化

# Serialize Player Name
secret_storage[:player_name_max_length] = player_name.rjust(player_name_max_length)

# Serialize attributes:
for idx, attrib in enumerate(byte_attribs):
    secret_storage[byte_attrib_offset + idx] = player_stats[attrib]

for idx, attrib in enumerate(bit_attribs):
    byte_offset = idx // 8  # attribs 0-7 go in byte 0, etc.
    bit_offset = idx % 8

    # Bit manipulation examples: http://wiki.python.org/moin/BitManipulation
    current_byte = bit_attrib_offset + byte_offset
    if player_stats[attrib]:
        mask = 1 << bit_offset
        secret_storage[current_byte] = secret_storage[current_byte] | mask
    else:
        mask = ~(1 << bit_offset)
        secret_storage[current_byte] = secret_storage[current_byte] & mask

检索单个值

print "Storage array encoded as ascii:", secret_storage

# Access name:
print "Name:", secret_storage[:player_name_max_length].lstrip()
# >>> Name: Robert

# Access byte values:
attrib_idx = byte_attribs.index("mana")
print "Mana level:", secret_storage[byte_attrib_offset + attrib_idx]
# >>> Mana level: 150

# Access bit flags:
attrib_idx = bit_attribs.index("dungeon1_complete")
print "Completed Dungeon 1:", bool(secret_storage[bit_attrib_offset + (attrib_idx // 8)] & (1 << attrib_idx % 8))
# >>> Completed Dungeon 1: True

关于java - 从 LoZ Oracle 游戏中创建 secret 系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18165994/

相关文章:

java - JPA : Store a list of integers in a single field

python - 如何在 Python 日志记录中设置 GMT/UTC 时间戳?

java - 在不使用 Box2D 的情况下在 LibGDX 中制作具有碰撞和重力的游戏

java - 当复合子节点本身发生事件时,如何从 VBox 中删除复合子节点?

java - android Horizo​​ntalScrollView 中左右滚动的按钮

java - Binder 线程上的 GPS 警告消息

c++ - SDL2 无法正确绘制矩形

python - Tkinter 将视频插入窗口

python - 如何将反斜杠转义序列放入 f-string

c# - XNA(C#) 中的 Vector2.Transform() 和 Vector2.TransformNormal() 有什么区别