maze_python/guess copy.py
2025-07-03 18:38:13 +08:00

70 lines
2.3 KiB
Python

def generate_clue_file(output_path="clue.json"):
"""
生成包含密码线索和哈希值的 JSON 文件
包含 1~3 个合法线索条件(基于真实密码内容)
"""
password = generate_random_password()
lock = PasswordLock()
stored_hash = lock.hash_password(password)
digits = list(map(int, password))
conditions = []
# 判断是否满足 [-1, -1] 条件
satisfy_prime_unique = satisfies_prime_unique_condition(digits)
# 线索种类池
available_methods = [1, 2]
if satisfy_prime_unique:
available_methods.append(3) # 添加 [-1, -1] 的机会
# 随机生成 1~3 个线索
num_conditions = random.randint(1, 3)
used_positions = set() # 已使用的位置(避免重复添加相同位置的奇偶性)
for _ in range(num_conditions):
method = random.choice(available_methods)
if method == 1: # [a, t]: 奇偶性判断
while True:
pos = random.randint(1, 3)
if pos not in used_positions:
break
digit = digits[pos - 1]
parity = digit % 2 # 根据实际奇偶性生成
conditions.append([pos, parity])
used_positions.add(pos)
elif method == 2: # [x, y, z]: 固定某位数字
while True:
idx = random.randint(0, 2)
if idx not in used_positions:
break
cond = [-1, -1, -1]
cond[idx] = digits[idx]
conditions.append(cond)
used_positions.add(idx)
elif method == 3: # [-1, -1]: 每位为素数且不重复
if satisfy_prime_unique and [-1, -1] not in conditions:
conditions.append([-1, -1])
# 至少保留一个线索
if not conditions:
return generate_clue_file(output_path)
# 写入文件
clue_data = {
"C": conditions,
"L": stored_hash
}
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(clue_data, f, ensure_ascii=False, indent=4)
print(f"已生成线索文件: {output_path}")
print(f"原始密码: {password}")
print(f"线索内容: {conditions}")
print(f"哈希值: {stored_hash}")
return password, conditions, stored_hash