实现路径生成
This commit is contained in:
parent
c7f28e7f88
commit
198b6bd506
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,4 +1,4 @@
|
||||
__pycache__/
|
||||
.venv/
|
||||
.idea/
|
||||
__pycache__/
|
||||
.venv/
|
||||
.idea/
|
||||
*.csv
|
@ -1,142 +1,198 @@
|
||||
import csv
|
||||
|
||||
class TreeNode:
|
||||
def __init__(self):
|
||||
self.fa = None
|
||||
self.children = []
|
||||
self.pos = None
|
||||
self.val = 0
|
||||
self.id = 0
|
||||
self.dp = 0
|
||||
self.path = []
|
||||
self.dp_path = []
|
||||
|
||||
class SourceCollector:
|
||||
def __init__(self, filename, maze=None):
|
||||
self.filename = filename
|
||||
self.maze = maze
|
||||
self.start_pos = None
|
||||
self.end_pos = None
|
||||
self.path = []
|
||||
|
||||
if self.filename:
|
||||
self.maze = []
|
||||
with open(f"{self.filename}",'r') as f:
|
||||
reader = csv.reader(f)
|
||||
for row in reader:
|
||||
t = []
|
||||
for i in row:
|
||||
if i.startswith('b') or i.startswith('l'):
|
||||
t.append('0')
|
||||
else:
|
||||
t.append(i)
|
||||
self.maze.append(t)
|
||||
self.rowNums = len(self.maze)
|
||||
self.colNums = len(self.maze[0])
|
||||
|
||||
|
||||
for i in range(self.rowNums):
|
||||
for j in range(self.colNums):
|
||||
if self.maze[i][j] =='s':
|
||||
self.start_pos = (i,j)
|
||||
if self.maze[i][j] =='e':
|
||||
self.end_pos = (i,j)
|
||||
|
||||
def dfs_show(self,u):
|
||||
if u.id != 0:
|
||||
print(f"id: {u.id} , fa:{u.fa.id} , val:{u.val}")
|
||||
else:
|
||||
print(f"id: {u.id} , val:{u.val}")
|
||||
print(u.path)
|
||||
for child in u.children:
|
||||
|
||||
self.dfs_show(child)
|
||||
|
||||
def outofmap(self,x,y):
|
||||
return x < 0 or y < 0 or x > self.rowNums or y > self.colNums
|
||||
def build_a_tree(self):
|
||||
sn = TreeNode()
|
||||
sn.pos = self.start_pos
|
||||
id = 0
|
||||
sn.id = id
|
||||
sn.val = 0
|
||||
sn.path = [sn.pos]
|
||||
|
||||
st = [[False] * self.colNums for _ in range(self.rowNums)]
|
||||
qsk = []
|
||||
sx, sy = self.start_pos
|
||||
st[sx][sy] = True
|
||||
qsk.append((self.start_pos[0],self.start_pos[1], sn,[]))
|
||||
dx = [-1,0,1,0]
|
||||
dy = [0,-1,0,1]
|
||||
|
||||
while qsk:
|
||||
x, y, u,path = qsk.pop()
|
||||
for _x, _y in zip(dx,dy):
|
||||
nx, ny = x + _x, y + _y
|
||||
if self.outofmap(nx,ny):
|
||||
continue
|
||||
if self.maze[nx][ny] == '1' or st[nx][ny]:
|
||||
continue
|
||||
|
||||
st[nx][ny] = True
|
||||
to = self.maze[nx][ny]
|
||||
new_path = path + [(nx,ny)]
|
||||
if to.startswith('g') or to.startswith('t'):
|
||||
new_node = TreeNode()
|
||||
id+=1
|
||||
new_node.id = id
|
||||
new_node.pos = (nx, ny)
|
||||
new_node.fa = u
|
||||
num_str = to[1:]
|
||||
new_node.path = new_path
|
||||
try:
|
||||
if to.startswith('g'):
|
||||
new_node.val = int(num_str)
|
||||
else:
|
||||
new_node.val = -int(num_str)
|
||||
except ValueError:
|
||||
print("wa ! ")
|
||||
u.children.append(new_node)
|
||||
qsk.append((nx, ny, new_node,[]))
|
||||
else:
|
||||
qsk.append((nx, ny, u,new_path))
|
||||
|
||||
return sn
|
||||
def dfs(self,sn):
|
||||
sn.dp = sn.val
|
||||
for child in sn.children:
|
||||
# print(f"cur : {child.pos} , fa : {child.fa.pos} , childrens : {child.path}")
|
||||
self.dfs(child)
|
||||
if child.dp > 0:
|
||||
sn.dp += child.dp
|
||||
sn.dp_path += child.path + child.dp_path + child.path[::-1]
|
||||
|
||||
|
||||
def find_path(self):
|
||||
self.path = []
|
||||
sn = self.build_a_tree()
|
||||
self.dfs(sn)
|
||||
return self.path
|
||||
|
||||
if __name__ == '__main__':
|
||||
obj = SourceCollector(filename="maze.csv")
|
||||
sn = obj.build_a_tree()
|
||||
# obj.dfs_show(sn)
|
||||
obj.dfs(sn)
|
||||
print(len(sn.dp_path))
|
||||
print(sn.pos)
|
||||
pre = sn.pos
|
||||
for _ in sn.dp_path:
|
||||
dx,dy = _[0] - pre[0],_[1]-pre[1]
|
||||
if dx > 0:
|
||||
print("down")
|
||||
elif dx < 0:
|
||||
print("up")
|
||||
elif dy > 0:
|
||||
print("right")
|
||||
elif dy < 0:
|
||||
print("left")
|
||||
pre = _
|
||||
|
||||
|
||||
import csv
|
||||
from collections import deque
|
||||
|
||||
class TreeNode:
|
||||
def __init__(self):
|
||||
self.fa = None
|
||||
self.children = []
|
||||
self.pos = None
|
||||
self.final_pos = None
|
||||
self.val = 0
|
||||
self.id = 0
|
||||
self.dp = 0
|
||||
|
||||
class SourceCollector:
|
||||
def __init__(self, filename=None, maze=None):
|
||||
self.filename = filename
|
||||
self.maze = maze
|
||||
self.start_pos = None
|
||||
self.end_pos = None
|
||||
self.path = []
|
||||
self.node_path = []
|
||||
if self.filename:
|
||||
self.maze = []
|
||||
with open(f"{self.filename}",'r') as f:
|
||||
reader = csv.reader(f)
|
||||
for row in reader:
|
||||
t = []
|
||||
for i in row:
|
||||
if i.startswith('b') or i.startswith('l'):
|
||||
t.append('0')
|
||||
else:
|
||||
t.append(i)
|
||||
self.maze.append(t)
|
||||
else:
|
||||
self.maze = maze
|
||||
self.rowNums = len(self.maze)
|
||||
self.colNums = len(self.maze[0])
|
||||
|
||||
|
||||
for i in range(self.rowNums):
|
||||
for j in range(self.colNums):
|
||||
if self.maze[i][j] =='s':
|
||||
self.start_pos = (i,j)
|
||||
if self.maze[i][j] =='e':
|
||||
self.end_pos = (i,j)
|
||||
|
||||
def dfs_show(self,u):
|
||||
if u.id != 0:
|
||||
print(f"id: {u.id} , fa:{u.fa.id} , val:{u.val} , pos:{u.pos}")
|
||||
else:
|
||||
print(f"id: {u.id} , val:{u.val} , pos:{u.pos}")
|
||||
for child in u.children:
|
||||
self.dfs_show(child)
|
||||
def build_a_tree(self):
|
||||
cnt = 0
|
||||
root = TreeNode()
|
||||
root.pos = self.start_pos
|
||||
root.id = 0
|
||||
root.val = 0
|
||||
root.fa = None
|
||||
|
||||
queue = deque([(self.start_pos[0], self.start_pos[1], root)])
|
||||
st = [[False] * self.colNums for _ in range(self.rowNums)]
|
||||
st[self.start_pos[0]][self.start_pos[1]] = True
|
||||
|
||||
dx = [-1, 0, 1, 0]
|
||||
dy = [0, -1, 0, 1]
|
||||
|
||||
while queue:
|
||||
x, y, parent = queue.popleft()
|
||||
for i in range(4):
|
||||
nx, ny = x + dx[i], y + dy[i]
|
||||
if self.outofmap(nx, ny) or st[nx][ny]:
|
||||
continue
|
||||
|
||||
if self.maze[nx][ny] != '1':
|
||||
st[nx][ny] = True
|
||||
|
||||
new_node = TreeNode()
|
||||
new_node.pos = (nx, ny)
|
||||
new_node.fa = parent
|
||||
cnt+=1
|
||||
new_node.id = cnt
|
||||
if self.maze[nx][ny].startswith('g'):
|
||||
new_node.val = int(self.maze[nx][ny][1:])
|
||||
elif self.maze[nx][ny].startswith('t'):
|
||||
new_node.val =-1 *int(self.maze[nx][ny][1:])
|
||||
parent.children.append(new_node)
|
||||
queue.append((nx, ny, new_node))
|
||||
return root
|
||||
|
||||
|
||||
|
||||
|
||||
def outofmap(self,x,y):
|
||||
return x < 0 or y < 0 or x > self.rowNums or y > self.colNums
|
||||
def getlca(self,u, v):
|
||||
def get_path_to_root(node):
|
||||
path = []
|
||||
while node:
|
||||
path.append(node)
|
||||
node = node.fa
|
||||
return path
|
||||
path_u = get_path_to_root(u)
|
||||
path_v = get_path_to_root(v)
|
||||
|
||||
path_u.reverse()
|
||||
path_v.reverse()
|
||||
|
||||
lca = None
|
||||
for i in range(min(len(path_u),len(path_v))):
|
||||
if path_u[i] == path_v[i]:
|
||||
lca = path_u[i]
|
||||
else:
|
||||
break
|
||||
|
||||
if lca is None:
|
||||
return []
|
||||
u_to_lca = []
|
||||
node = u
|
||||
while node != lca:
|
||||
u_to_lca.append(node.pos)
|
||||
node = node.fa
|
||||
|
||||
lca_to_v = []
|
||||
node_list = []
|
||||
node = v
|
||||
while node != lca:
|
||||
node_list.append(node)
|
||||
node = node.fa
|
||||
node_list.append(lca)
|
||||
node_list.reverse()
|
||||
for node in node_list:
|
||||
lca_to_v.append(node.pos)
|
||||
|
||||
full_path = u_to_lca + lca_to_v
|
||||
return full_path
|
||||
def dfs(self,sn):
|
||||
|
||||
sn.dp = sn.val
|
||||
sn.final_pos = sn.pos
|
||||
sn.path= [sn.pos]
|
||||
cur = None
|
||||
for idx,child in enumerate(sn.children):
|
||||
self.dfs(child)
|
||||
if child.dp > 0:
|
||||
sn.dp += child.dp
|
||||
if cur != None:
|
||||
sn.path.extend(self.getlca(cur,child))
|
||||
sn.path.extend(child.path)
|
||||
cur = child
|
||||
if idx == len(sn.children)-1:
|
||||
sn.final_pos = cur.final_pos
|
||||
|
||||
|
||||
|
||||
def get_path(self):
|
||||
return self.path
|
||||
|
||||
def run(self):
|
||||
sn = self.build_a_tree()
|
||||
# self.dfs_show(sn)
|
||||
self.dfs(sn)
|
||||
self.path = sn.path
|
||||
|
||||
|
||||
def output_list(self):
|
||||
copy_maze = self.maze
|
||||
for idx, (y, x) in enumerate(self.path):
|
||||
if copy_maze[y][x].startswith('s') | copy_maze[y][x].startswith('e'):
|
||||
continue
|
||||
if copy_maze[y][x].startswith('g') | copy_maze[y][x].startswith('t'):
|
||||
copy_maze[y][x] = f"{copy_maze[y][x]}p{idx}"
|
||||
continue
|
||||
copy_maze[y][x] = f"p{idx}"
|
||||
|
||||
return copy_maze
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
obj = SourceCollector(filename="maze.csv")
|
||||
obj.run()
|
||||
path = obj.get_path()
|
||||
for i in path:
|
||||
print(i)
|
||||
# print(sn.pos)
|
||||
# pre = sn.pos
|
||||
# for _ in sn.dp_path:
|
||||
# dx,dy = _[0] - pre[0],_[1]-pre[1]
|
||||
# if dx > 0:
|
||||
# print("down")
|
||||
# elif dx < 0:
|
||||
# print("up")
|
||||
# elif dy > 0:
|
||||
# print("right")
|
||||
# elif dy < 0:
|
||||
# print("left")
|
||||
# pre = _
|
272
boss_fight.py
272
boss_fight.py
@ -1,136 +1,136 @@
|
||||
# -*-coding: GBK -*-
|
||||
import heapq # 导入堆模块,用于实现优先队列
|
||||
from typing import List, Optional, Tuple # 导入类型提示相关模块
|
||||
|
||||
|
||||
def boss_strategy(coin: int, blood: int) -> Optional[Tuple[List[str], int]]:
|
||||
# 使用分支限界法寻找最小回合数击败BOSS的技能序列
|
||||
|
||||
# 参数:
|
||||
# coin: 玩家初始金币数
|
||||
# blood: BOSS初始血量
|
||||
|
||||
# 返回:
|
||||
# 最小回合数的技能序列列表,如果无解则返回None
|
||||
|
||||
start_coin = coin # 记录初始金币数,用于计算消耗金币
|
||||
|
||||
# 初始状态: (回合数, 剩余金币, BOSS血量, 充能, 连续E计数, 技能序列元组)
|
||||
start_state = (0, coin, blood, 0, 0, ())
|
||||
|
||||
# 创建优先队列: 元素为 (优先级, 状态)
|
||||
# 优先级元组: (回合数, 消耗金币, BOSS血量) - 值越小优先级越高
|
||||
heap = []
|
||||
heapq.heappush(heap, ((0, 0, blood), start_state)) # 将初始状态加入优先队列
|
||||
|
||||
# 创建访问集合: 用于避免重复状态
|
||||
# 状态键: (回合数, 剩余金币, BOSS血量, 充能, min(连续E计数,3))
|
||||
visited = set()
|
||||
start_ce_compressed = min(0, 3) # 压缩连续E计数状态
|
||||
visited.add((0, coin, blood, 0, start_ce_compressed)) # 将初始状态加入已访问集合
|
||||
|
||||
# 设置最大回合数限制,避免无限循环
|
||||
max_round = blood * 2 # 每次至少造成1点伤害,最多回合数为血量两倍
|
||||
|
||||
# 主循环: 处理优先队列中的状态
|
||||
while heap:
|
||||
# 从优先队列中取出优先级最高的状态
|
||||
priority, state = heapq.heappop(heap)
|
||||
# 解包状态元组
|
||||
round_cnt, cur_coin, cur_blood, energy, ce, seq_tuple = state
|
||||
|
||||
# 检查是否已击败BOSS
|
||||
if cur_blood <= 0:
|
||||
return (list(seq_tuple), cur_coin) # 返回技能序列和剩余金币
|
||||
|
||||
# 超过最大回合数则跳过当前状态
|
||||
if round_cnt >= max_round:
|
||||
continue
|
||||
|
||||
# 尝试使用技能A
|
||||
if cur_coin >= 2: # 检查金币是否足够
|
||||
new_coin = cur_coin - 2 # 扣除金币
|
||||
new_blood = cur_blood - 1 # 减少BOSS血量
|
||||
new_energy = energy + 1 # 增加充能
|
||||
new_ce = 0 # 使用非E技能,重置连续E计数
|
||||
new_round = round_cnt + 1 # 回合数+1
|
||||
new_seq = seq_tuple + ('A',) # 添加技能到序列
|
||||
|
||||
# 压缩连续E计数状态(>=3视为3)
|
||||
ce_compressed = min(new_ce, 3)
|
||||
# 创建状态键用于去重
|
||||
state_key = (new_round, new_coin, new_blood, new_energy, ce_compressed)
|
||||
|
||||
# 检查是否为新状态
|
||||
if state_key not in visited:
|
||||
visited.add(state_key) # 标记为已访问
|
||||
cost = start_coin - new_coin # 计算已消耗金币
|
||||
# 创建新状态
|
||||
new_state = (new_round, new_coin, new_blood, new_energy, new_ce, new_seq)
|
||||
# 将新状态加入优先队列
|
||||
heapq.heappush(heap, ((new_round, cost, new_blood), new_state))
|
||||
|
||||
# 尝试使用技能E
|
||||
cost_e = 3 # 默认消耗金币
|
||||
if ce >= 2: # 检查是否为连续第三次使用E
|
||||
cost_e = 2 # 第三次消耗2金币
|
||||
|
||||
if cur_coin >= cost_e: # 检查金币是否足够
|
||||
new_coin = cur_coin - cost_e # 扣除金币
|
||||
new_blood = cur_blood - 3 # 减少BOSS血量
|
||||
new_energy = energy + 1 # 增加充能
|
||||
new_ce = ce + 1 # 增加连续E计数
|
||||
new_round = round_cnt + 1 # 回合数+1
|
||||
new_seq = seq_tuple + ('E',) # 添加技能到序列
|
||||
|
||||
# 压缩连续E计数状态
|
||||
ce_compressed = min(new_ce, 3)
|
||||
# 创建状态键用于去重
|
||||
state_key = (new_round, new_coin, new_blood, new_energy, ce_compressed)
|
||||
|
||||
# 检查是否为新状态
|
||||
if state_key not in visited:
|
||||
visited.add(state_key) # 标记为已访问
|
||||
cost = start_coin - new_coin # 计算已消耗金币
|
||||
# 创建新状态
|
||||
new_state = (new_round, new_coin, new_blood, new_energy, new_ce, new_seq)
|
||||
# 将新状态加入优先队列
|
||||
heapq.heappush(heap, ((new_round, cost, new_blood), new_state))
|
||||
|
||||
# 尝试使用技能Q
|
||||
if energy >= 3 and cur_coin >= 3: # 检查充能和金币是否足够
|
||||
new_coin = cur_coin - 3 # 扣除金币
|
||||
new_blood = cur_blood - 8 # 减少BOSS血量
|
||||
new_energy = energy - 3 # 消耗充能
|
||||
new_ce = 0 # 使用非E技能,重置连续E计数
|
||||
new_round = round_cnt + 1 # 回合数+1
|
||||
new_seq = seq_tuple + ('Q',) # 添加技能到序列
|
||||
|
||||
# 压缩连续E计数状态
|
||||
ce_compressed = min(new_ce, 3)
|
||||
# 创建状态键用于去重
|
||||
state_key = (new_round, new_coin, new_blood, new_energy, ce_compressed)
|
||||
|
||||
# 检查是否为新状态
|
||||
if state_key not in visited:
|
||||
visited.add(state_key) # 标记为已访问
|
||||
cost = start_coin - new_coin # 计算已消耗金币
|
||||
# 创建新状态
|
||||
new_state = (new_round, new_coin, new_blood, new_energy, new_ce, new_seq)
|
||||
# 将新状态加入优先队列
|
||||
heapq.heappush(heap, ((new_round, cost, new_blood), new_state))
|
||||
|
||||
return None # 队列为空,无解
|
||||
|
||||
|
||||
# 主程序入口
|
||||
if __name__ == "__main__":
|
||||
# 测试示例: 金币=10, BOSS血量=10
|
||||
result = boss_strategy(20, 20)
|
||||
|
||||
if result is None:
|
||||
print("无法击败BOSS")
|
||||
else:
|
||||
skill_sequence, remaining_coins = result
|
||||
print("最小回合数技能序列:", skill_sequence) # 预期输出: ['A', 'A', 'A', 'Q']
|
||||
print("剩余金币:", remaining_coins) # 预期输出: 1
|
||||
# -*-coding: GBK -*-
|
||||
import heapq # 导入堆模块,用于实现优先队列
|
||||
from typing import List, Optional, Tuple # 导入类型提示相关模块
|
||||
|
||||
|
||||
def boss_strategy(coin: int, blood: int) -> Optional[Tuple[List[str], int]]:
|
||||
# 使用分支限界法寻找最小回合数击败BOSS的技能序列
|
||||
|
||||
# 参数:
|
||||
# coin: 玩家初始金币数
|
||||
# blood: BOSS初始血量
|
||||
|
||||
# 返回:
|
||||
# 最小回合数的技能序列列表,如果无解则返回None
|
||||
|
||||
start_coin = coin # 记录初始金币数,用于计算消耗金币
|
||||
|
||||
# 初始状态: (回合数, 剩余金币, BOSS血量, 充能, 连续E计数, 技能序列元组)
|
||||
start_state = (0, coin, blood, 0, 0, ())
|
||||
|
||||
# 创建优先队列: 元素为 (优先级, 状态)
|
||||
# 优先级元组: (回合数, 消耗金币, BOSS血量) - 值越小优先级越高
|
||||
heap = []
|
||||
heapq.heappush(heap, ((0, 0, blood), start_state)) # 将初始状态加入优先队列
|
||||
|
||||
# 创建访问集合: 用于避免重复状态
|
||||
# 状态键: (回合数, 剩余金币, BOSS血量, 充能, min(连续E计数,3))
|
||||
visited = set()
|
||||
start_ce_compressed = min(0, 3) # 压缩连续E计数状态
|
||||
visited.add((0, coin, blood, 0, start_ce_compressed)) # 将初始状态加入已访问集合
|
||||
|
||||
# 设置最大回合数限制,避免无限循环
|
||||
max_round = blood * 2 # 每次至少造成1点伤害,最多回合数为血量两倍
|
||||
|
||||
# 主循环: 处理优先队列中的状态
|
||||
while heap:
|
||||
# 从优先队列中取出优先级最高的状态
|
||||
priority, state = heapq.heappop(heap)
|
||||
# 解包状态元组
|
||||
round_cnt, cur_coin, cur_blood, energy, ce, seq_tuple = state
|
||||
|
||||
# 检查是否已击败BOSS
|
||||
if cur_blood <= 0:
|
||||
return (list(seq_tuple), cur_coin) # 返回技能序列和剩余金币
|
||||
|
||||
# 超过最大回合数则跳过当前状态
|
||||
if round_cnt >= max_round:
|
||||
continue
|
||||
|
||||
# 尝试使用技能A
|
||||
if cur_coin >= 2: # 检查金币是否足够
|
||||
new_coin = cur_coin - 2 # 扣除金币
|
||||
new_blood = cur_blood - 1 # 减少BOSS血量
|
||||
new_energy = energy + 1 # 增加充能
|
||||
new_ce = 0 # 使用非E技能,重置连续E计数
|
||||
new_round = round_cnt + 1 # 回合数+1
|
||||
new_seq = seq_tuple + ('A',) # 添加技能到序列
|
||||
|
||||
# 压缩连续E计数状态(>=3视为3)
|
||||
ce_compressed = min(new_ce, 3)
|
||||
# 创建状态键用于去重
|
||||
state_key = (new_round, new_coin, new_blood, new_energy, ce_compressed)
|
||||
|
||||
# 检查是否为新状态
|
||||
if state_key not in visited:
|
||||
visited.add(state_key) # 标记为已访问
|
||||
cost = start_coin - new_coin # 计算已消耗金币
|
||||
# 创建新状态
|
||||
new_state = (new_round, new_coin, new_blood, new_energy, new_ce, new_seq)
|
||||
# 将新状态加入优先队列
|
||||
heapq.heappush(heap, ((new_round, cost, new_blood), new_state))
|
||||
|
||||
# 尝试使用技能E
|
||||
cost_e = 3 # 默认消耗金币
|
||||
if ce >= 2: # 检查是否为连续第三次使用E
|
||||
cost_e = 2 # 第三次消耗2金币
|
||||
|
||||
if cur_coin >= cost_e: # 检查金币是否足够
|
||||
new_coin = cur_coin - cost_e # 扣除金币
|
||||
new_blood = cur_blood - 3 # 减少BOSS血量
|
||||
new_energy = energy + 1 # 增加充能
|
||||
new_ce = ce + 1 # 增加连续E计数
|
||||
new_round = round_cnt + 1 # 回合数+1
|
||||
new_seq = seq_tuple + ('E',) # 添加技能到序列
|
||||
|
||||
# 压缩连续E计数状态
|
||||
ce_compressed = min(new_ce, 3)
|
||||
# 创建状态键用于去重
|
||||
state_key = (new_round, new_coin, new_blood, new_energy, ce_compressed)
|
||||
|
||||
# 检查是否为新状态
|
||||
if state_key not in visited:
|
||||
visited.add(state_key) # 标记为已访问
|
||||
cost = start_coin - new_coin # 计算已消耗金币
|
||||
# 创建新状态
|
||||
new_state = (new_round, new_coin, new_blood, new_energy, new_ce, new_seq)
|
||||
# 将新状态加入优先队列
|
||||
heapq.heappush(heap, ((new_round, cost, new_blood), new_state))
|
||||
|
||||
# 尝试使用技能Q
|
||||
if energy >= 3 and cur_coin >= 3: # 检查充能和金币是否足够
|
||||
new_coin = cur_coin - 3 # 扣除金币
|
||||
new_blood = cur_blood - 8 # 减少BOSS血量
|
||||
new_energy = energy - 3 # 消耗充能
|
||||
new_ce = 0 # 使用非E技能,重置连续E计数
|
||||
new_round = round_cnt + 1 # 回合数+1
|
||||
new_seq = seq_tuple + ('Q',) # 添加技能到序列
|
||||
|
||||
# 压缩连续E计数状态
|
||||
ce_compressed = min(new_ce, 3)
|
||||
# 创建状态键用于去重
|
||||
state_key = (new_round, new_coin, new_blood, new_energy, ce_compressed)
|
||||
|
||||
# 检查是否为新状态
|
||||
if state_key not in visited:
|
||||
visited.add(state_key) # 标记为已访问
|
||||
cost = start_coin - new_coin # 计算已消耗金币
|
||||
# 创建新状态
|
||||
new_state = (new_round, new_coin, new_blood, new_energy, new_ce, new_seq)
|
||||
# 将新状态加入优先队列
|
||||
heapq.heappush(heap, ((new_round, cost, new_blood), new_state))
|
||||
|
||||
return None # 队列为空,无解
|
||||
|
||||
|
||||
# 主程序入口
|
||||
if __name__ == "__main__":
|
||||
# 测试示例: 金币=10, BOSS血量=10
|
||||
result = boss_strategy(20, 20)
|
||||
|
||||
if result is None:
|
||||
print("无法击败BOSS")
|
||||
else:
|
||||
skill_sequence, remaining_coins = result
|
||||
print("最小回合数技能序列:", skill_sequence) # 预期输出: ['A', 'A', 'A', 'Q']
|
||||
print("剩余金币:", remaining_coins) # 预期输出: 1
|
||||
|
6
main.py
6
main.py
@ -7,10 +7,10 @@ from draw import Button, Toast
|
||||
import sys
|
||||
import os
|
||||
|
||||
UI_HEIGHT = 800
|
||||
UI_WIDTH = 1100
|
||||
UI_HEIGHT = 1000
|
||||
UI_WIDTH = 1500
|
||||
|
||||
MAZE_SIZE = 800
|
||||
MAZE_SIZE = 150
|
||||
WALL_SIZE = 50
|
||||
FPS = 120
|
||||
|
||||
|
28
maze.csv
28
maze.csv
@ -1,16 +1,16 @@
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
||||
1,0,1,0,0,0,1,e,0,0,1,t11,1,0,0,1
|
||||
1,0,1,1,1,t17,1,0,1,0,1,0,0,0,t6,1
|
||||
1,0,1,0,1,0,0,0,1,0,1,0,1,0,g25,1
|
||||
1,0,1,0,0,0,1,0,1,0,1,0,1,1,1,1
|
||||
1,0,1,b89,1,0,1,0,1,0,1,0,1,g30,0,1
|
||||
1,0,0,0,1,0,1,0,1,0,1,0,1,l11,0,1
|
||||
1,g30,1,0,1,0,1,0,1,0,1,0,0,g21,0,1
|
||||
1,0,1,0,1,s,1,t11,1,1,1,0,1,0,0,1
|
||||
1,0,1,0,1,t14,1,0,0,0,1,0,1,0,0,1
|
||||
1,1,1,1,1,1,1,0,1,1,1,0,1,0,0,1
|
||||
1,0,0,g21,0,0,0,0,0,0,1,0,1,0,0,1
|
||||
1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1
|
||||
1,0,l23,0,0,0,g17,0,0,g22,1,0,0,l26,0,1
|
||||
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
|
||||
1,0,0,1,0,0,0,0,0,l25,0,t10,0,1,0,1
|
||||
1,1,0,1,1,1,1,1,1,1,1,1,l15,1,t7,1
|
||||
1,0,0,g26,0,e,t20,s,0,0,0,0,0,0,l25,1
|
||||
1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1
|
||||
1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1
|
||||
1,0,1,1,1,1,0,1,1,1,1,1,g24,1,0,1
|
||||
1,0,0,1,0,t16,0,0,1,0,0,0,0,1,l16,1
|
||||
1,1,1,1,1,1,1,1,1,1,1,0,1,1,g15,1
|
||||
1,0,t8,1,0,1,0,0,g12,0,1,0,0,1,0,1
|
||||
1,0,0,1,0,1,1,0,1,0,1,0,1,1,0,1
|
||||
1,0,0,0,0,0,0,0,1,0,1,0,t15,1,l16,1
|
||||
1,0,0,1,0,1,0,t16,1,0,1,0,0,1,0,1
|
||||
1,1,0,1,1,1,1,1,1,0,1,0,0,1,0,1
|
||||
1,0,0,0,0,0,0,0,1,0,t5,b89,0,1,0,1
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
||||
|
|
48
maze.py
48
maze.py
@ -1,5 +1,6 @@
|
||||
import pygame
|
||||
from maze_generator import MazeGenerator
|
||||
from SourceCollector import SourceCollector
|
||||
from tanxin import *
|
||||
import time
|
||||
|
||||
@ -18,9 +19,10 @@ class Maze:
|
||||
def generate(self):
|
||||
seed = int(time.time() * 1000) % (2**32)
|
||||
self.generater.generate(seed=seed)
|
||||
# player = GreedyPlayer(generater.maze)
|
||||
# player.find_path()
|
||||
self.grid = self.generater.maze
|
||||
obj = SourceCollector(maze=self.generater.maze)
|
||||
obj.run()
|
||||
self.grid = obj.output_list()
|
||||
print(self.grid)
|
||||
|
||||
def export_to_csv(self, filename):
|
||||
self.generater.export_to_csv(filename=filename)
|
||||
@ -39,8 +41,38 @@ class Maze:
|
||||
screen.blit(wall_texture, (x * tile_size, y * tile_size))
|
||||
if self.grid[y][x].startswith('g'):
|
||||
screen.blit(coin_texture, (x * tile_size, y * tile_size))
|
||||
|
||||
font = pygame.font.SysFont(None, tile_size // 2)
|
||||
path = self.grid[y][x].rfind('p')
|
||||
if path == -1:
|
||||
continue
|
||||
path = self.grid[y][x][path+1:]
|
||||
|
||||
center = (x * tile_size + tile_size // 2, y * tile_size + tile_size // 2)
|
||||
radius = tile_size // 3
|
||||
|
||||
text = font.render(path, True, (255, 255, 255))
|
||||
text_rect = text.get_rect(center=center)
|
||||
screen.blit(text, text_rect)
|
||||
|
||||
|
||||
|
||||
if self.grid[y][x].startswith('t'):
|
||||
screen.blit(trap_texture, (x * tile_size, y * tile_size))
|
||||
|
||||
font = pygame.font.SysFont(None, tile_size // 2)
|
||||
path = self.grid[y][x].rfind('p')
|
||||
if path == -1:
|
||||
continue
|
||||
path = self.grid[y][x][path+1:]
|
||||
|
||||
center = (x * tile_size + tile_size // 2, y * tile_size + tile_size // 2)
|
||||
radius = tile_size // 3
|
||||
|
||||
text = font.render(path, True, (255, 255, 255))
|
||||
text_rect = text.get_rect(center=center)
|
||||
screen.blit(text, text_rect)
|
||||
|
||||
if self.grid[y][x].startswith('|') or self.grid[y][x].startswith('-'):
|
||||
font = pygame.font.SysFont(None, tile_size // 2)
|
||||
num = 12
|
||||
@ -73,6 +105,16 @@ class Maze:
|
||||
text = font.render(text, True, (0, 0, 0))
|
||||
text_rect = text.get_rect(center=center)
|
||||
screen.blit(text, text_rect)
|
||||
if self.grid[y][x].startswith('p'):
|
||||
font = pygame.font.SysFont(None, tile_size // 2)
|
||||
text = self.grid[y][x][1:]
|
||||
center = (x * tile_size + tile_size // 2, y * tile_size + tile_size // 2)
|
||||
radius = tile_size // 3
|
||||
pygame.draw.circle(screen, (255, 215, 0), center, radius)
|
||||
if text:
|
||||
text = font.render(text, True, (0, 0, 0))
|
||||
text_rect = text.get_rect(center=center)
|
||||
screen.blit(text, text_rect)
|
||||
|
||||
|
||||
pygame.draw.line(screen, (0, 0, 0), (self.maze_size, 0), (self.maze_size, self.maze_size), 5)
|
||||
|
@ -22,82 +22,82 @@ class MazeGenerator:
|
||||
self.name = name # 迷宫名称
|
||||
self.special_elements = [] # 存储特殊元素的位置和值
|
||||
self.history_mazes = []
|
||||
|
||||
def initialize_maze(self):
|
||||
"""初始化迷宫,四周设置为墙"""
|
||||
self.maze = [[self.ROUTE for _ in range(self.size)] for _ in range(self.size)]
|
||||
for i in range(self.size):
|
||||
self.maze[0][i] = self.WALL
|
||||
self.maze[i][0] = self.WALL
|
||||
self.maze[self.size - 1][i] = self.WALL
|
||||
self.maze[i][self.size - 1] = self.WALL
|
||||
"""初始化迷宫,全部填充为墙"""
|
||||
self.maze = [[self.WALL for _ in range(self.size)] for _ in range(self.size)]
|
||||
|
||||
def create_maze(self, x1, y1, x2, y2):
|
||||
"""递归分割法生成迷宫"""
|
||||
if x2 - x1 < 2 or y2 - y1 < 2:
|
||||
return
|
||||
"""递归分割法生成迷宫,墙壁始终在偶数坐标"""
|
||||
def getWallIndex(start, length):
|
||||
assert length >= 3
|
||||
wall_index = random.randint(start + 1, start + length - 2)
|
||||
if wall_index % 2 == 1:
|
||||
wall_index -= 1
|
||||
return wall_index
|
||||
|
||||
x = x1 + 1 + random.randint(0, (x2 - x1 - 2))
|
||||
y = y1 + 1 + random.randint(0, (y2 - y1 - 2))
|
||||
def isValid(x, y):
|
||||
return 0 <= x < self.size and 0 <= y < self.size
|
||||
|
||||
# 画墙
|
||||
for i in range(x1, x2 + 1):
|
||||
self.maze[i][y] = self.WALL
|
||||
for i in range(y1, y2 + 1):
|
||||
self.maze[x][i] = self.WALL
|
||||
def isMovable(x, y):
|
||||
return self.maze[y][x] != self.WALL
|
||||
|
||||
# 递归分割四个区域
|
||||
self.create_maze(x1, y1, x - 1, y - 1)
|
||||
self.create_maze(x + 1, y + 1, x2, y2)
|
||||
self.create_maze(x + 1, y1, x2, y - 1)
|
||||
self.create_maze(x1, y + 1, x - 1, y2)
|
||||
def generateHoles(x, y, width, height, wall_x, wall_y):
|
||||
holes = []
|
||||
hole_entrys = [
|
||||
(random.randint(x, wall_x - 1), wall_y),
|
||||
(random.randint(wall_x + 1, x + width - 1), wall_y),
|
||||
(wall_x, random.randint(y, wall_y - 1)),
|
||||
(wall_x, random.randint(wall_y + 1, y + height - 1))
|
||||
]
|
||||
margin_entrys = [
|
||||
(x, wall_y), (x + width - 1, wall_y),
|
||||
(wall_x, y), (wall_x, y + height - 1)
|
||||
]
|
||||
adjacent_entrys = [
|
||||
(x - 1, wall_y), (x + width, wall_y),
|
||||
(wall_x, y - 1), (wall_x, y + height)
|
||||
]
|
||||
for i in range(4):
|
||||
adj_x, adj_y = adjacent_entrys[i]
|
||||
if isValid(adj_x, adj_y) and isMovable(adj_x, adj_y):
|
||||
mx, my = margin_entrys[i]
|
||||
self.maze[my][mx] = self.ROUTE
|
||||
else:
|
||||
holes.append(hole_entrys[i])
|
||||
ignore_hole = random.randint(0, len(holes) - 1)
|
||||
for i in range(len(holes)):
|
||||
if i != ignore_hole:
|
||||
hx, hy = holes[i]
|
||||
self.maze[hy][hx] = self.ROUTE
|
||||
|
||||
# 随机打通三面墙
|
||||
r = [0, 0, 0, 0]
|
||||
r[random.randint(0, 3)] = 1
|
||||
for i in range(4):
|
||||
if r[i] == 0:
|
||||
rx, ry = x, y
|
||||
if i == 0: # 上方
|
||||
while True:
|
||||
rx = x1 + random.randint(0, (x - x1 - 1))
|
||||
wall_count = sum([
|
||||
(int)(self.maze[rx - 1][ry]), (int)(self.maze[rx + 1][ry]),
|
||||
(int)(self.maze[rx][ry - 1]), (int)(self.maze[rx][ry + 1])
|
||||
])
|
||||
if wall_count <= 2 * (int)(self.WALL):
|
||||
break
|
||||
elif i == 1: # 右侧
|
||||
while True:
|
||||
ry = y + 1 + random.randint(0, (y2 - y - 1))
|
||||
wall_count = sum([
|
||||
(int)(self.maze[rx - 1][ry]), (int)(self.maze[rx + 1][ry]),
|
||||
(int)(self.maze[rx][ry - 1]), (int)(self.maze[rx][ry + 1])
|
||||
])
|
||||
if wall_count <= 2 * (int)(self.WALL):
|
||||
break
|
||||
elif i == 2: # 下方
|
||||
while True:
|
||||
rx = x + 1 + random.randint(0, (x2 - x - 1))
|
||||
wall_count = sum([
|
||||
(int)(self.maze[rx - 1][ry]), (int)(self.maze[rx + 1][ry]),
|
||||
(int)(self.maze[rx][ry - 1]), (int)(self.maze[rx][ry + 1])
|
||||
])
|
||||
if wall_count <= 2 * (int)(self.WALL):
|
||||
break
|
||||
elif i == 3: # 左侧
|
||||
while True:
|
||||
ry = y1 + random.randint(0, (y - y1 - 1))
|
||||
wall_count = sum([
|
||||
(int)(self.maze[rx - 1][ry]), (int)(self.maze[rx + 1][ry]),
|
||||
(int)(self.maze[rx][ry - 1]), (int)(self.maze[rx][ry + 1])
|
||||
])
|
||||
if wall_count <= 2 * (int)(self.WALL):
|
||||
break
|
||||
self.maze[rx][ry] = self.ROUTE
|
||||
self.history_mazes.append(self.maze)
|
||||
|
||||
# self.history_mazes.append(self.maze)
|
||||
def recursiveDivision(x, y, width, height):
|
||||
if width < 3 or height < 3:
|
||||
return
|
||||
wall_x = getWallIndex(x, width)
|
||||
wall_y = getWallIndex(y, height)
|
||||
for i in range(x, x + width):
|
||||
self.maze[wall_y][i] = self.WALL
|
||||
for i in range(y, y + height):
|
||||
self.maze[i][wall_x] = self.WALL
|
||||
generateHoles(x, y, width, height, wall_x, wall_y)
|
||||
recursiveDivision(x, y, wall_x - x, wall_y - y)
|
||||
recursiveDivision(x, wall_y + 1, wall_x - x, y + height - wall_y - 1)
|
||||
recursiveDivision(wall_x + 1, y, x + width - wall_x - 1, wall_y - y)
|
||||
recursiveDivision(wall_x + 1, wall_y + 1, x + width - wall_x - 1, y + height - wall_y - 1)
|
||||
# 先全部通路
|
||||
self.maze = [[self.ROUTE for _ in range(self.size)] for _ in range(self.size)]
|
||||
# 四周加墙
|
||||
for x in range(self.size):
|
||||
self.maze[0][x] = self.WALL
|
||||
self.maze[self.size - 1][x] = self.WALL
|
||||
for y in range(self.size):
|
||||
self.maze[y][0] = self.WALL
|
||||
self.maze[y][self.size - 1] = self.WALL
|
||||
# 递归分割
|
||||
try:
|
||||
recursiveDivision(1, 1, self.size - 2, self.size - 2)
|
||||
except:
|
||||
self.create_maze(x1, y1, x2, y2) # 如果递归失败,重新尝试
|
||||
def set_random_exits(self):
|
||||
"""随机设置迷宫入口和出口"""
|
||||
available = self.get_available_cells()
|
||||
@ -171,6 +171,7 @@ class MazeGenerator:
|
||||
random.seed(seed or random.randint(0, 1000))
|
||||
self.initialize_maze()
|
||||
self.create_maze(1, 1, self.size - 2, self.size - 2)
|
||||
self.patch_maze_edges() # 自动修正边界
|
||||
self.place_special_elements(boss_count, traps_range, mechanisms_range, skill_traps)
|
||||
print(f"成功生成迷宫: {self.name}")
|
||||
|
||||
@ -229,27 +230,77 @@ class MazeGenerator:
|
||||
for row in reader:
|
||||
l.append(row)
|
||||
return l
|
||||
def patch_maze_edges(self):
|
||||
|
||||
"""只在不破坏联通性的前提下,修正右侧和下侧边界的多余通路(加墙)"""
|
||||
n = self.size
|
||||
if n %2==1:
|
||||
return
|
||||
candidates = []
|
||||
# 倒数第二列(右侧)
|
||||
x = n - 2
|
||||
for y in range(1, n-1):
|
||||
if self.maze[y][x] == self.ROUTE:
|
||||
right = self.maze[y][x+1] if x+1 < n else self.WALL
|
||||
down = self.maze[y+1][x] if y+1 < n else self.WALL
|
||||
if right == self.ROUTE or down == self.ROUTE:
|
||||
candidates.append((y, x))
|
||||
# 倒数第
|
||||
# 二行(下侧)
|
||||
y = n - 2
|
||||
for x in range(1, n-1):
|
||||
if self.maze[y][x] == self.ROUTE:
|
||||
right = self.maze[y][x+1] if x+1 < n else self.WALL
|
||||
down = self.maze[y+1][x] if y+1 < n else self.WALL
|
||||
if right == self.ROUTE or down == self.ROUTE:
|
||||
candidates.append((y, x))
|
||||
# 逐个尝试加墙,确保联通
|
||||
for y, x in candidates:
|
||||
old = self.maze[y][x]
|
||||
self.maze[y][x] = self.WALL
|
||||
if not self.is_maze_connected():
|
||||
self.maze[y][x] = old # 恢复
|
||||
|
||||
def is_maze_connected(self):
|
||||
"""检查迷宫是否连通(深度优先搜索)"""
|
||||
visited = [[False] * self.size for _ in range(self.size)]
|
||||
|
||||
def dfs(x, y):
|
||||
if x < 0 or x >= self.size or y < 0 or y >= self.size:
|
||||
return
|
||||
if visited[y][x] or self.maze[y][x] == self.WALL:
|
||||
return
|
||||
visited[y][x] = True
|
||||
dfs(x + 1, y)
|
||||
dfs(x - 1, y)
|
||||
dfs(x, y + 1)
|
||||
dfs(x, y - 1)
|
||||
|
||||
# 从左上角开始搜索
|
||||
dfs(1, 1)
|
||||
|
||||
# 检查是否所有通路都被访问
|
||||
for y in range(1, self.size - 1):
|
||||
for x in range(1, self.size - 1):
|
||||
if self.maze[y][x] == self.ROUTE and not visited[y][x]:
|
||||
return False
|
||||
return True
|
||||
|
||||
def main():
|
||||
# 示例1: 生成带技能陷阱的迷宫
|
||||
generator = MazeGenerator(
|
||||
size=20,
|
||||
filename="dungeon_maze.csv",
|
||||
name="龙脊峡谷迷宫"
|
||||
filename="dungeon_maze.csv"
|
||||
)
|
||||
generator.generate(
|
||||
seed=666,
|
||||
boss_count=2,
|
||||
traps_range=(5, 10),
|
||||
mechanisms_range=(3, 7),
|
||||
skill_traps=8
|
||||
seed=666
|
||||
)
|
||||
generator.print_maze()
|
||||
generator.export_to_csv()
|
||||
generator.export_to_csv("d.csv")
|
||||
for i in generator.history_mazes:
|
||||
print(i[3:])
|
||||
|
||||
|
||||
reader = MazeGenerator(size=1, filename="dungeon_maze.csv")
|
||||
reader = MazeGenerator(size=1, filename="d.csv")
|
||||
if reader.read_from_csv():
|
||||
print("\n读取的迷宫:")
|
||||
reader.print_maze()
|
||||
|
614
mylock.py
614
mylock.py
@ -1,308 +1,308 @@
|
||||
import os
|
||||
import json
|
||||
from Lock import PasswordLock
|
||||
|
||||
|
||||
def is_prime(n):
|
||||
"""判断一个数字是否是素数"""
|
||||
if n < 2:
|
||||
return False
|
||||
for i in range(2, int(n ** 0.5) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def satisfies_prime_unique_condition(digits):
|
||||
"""检查是否满足[-1, -1]条件:每位密码为素数且不重复"""
|
||||
return all(is_prime(d) for d in digits) and len(set(digits)) == 3
|
||||
|
||||
|
||||
def crack_method1(conditions):
|
||||
"""从高位到低位回溯(第一位→第二位→第三位)"""
|
||||
possible_passwords = []
|
||||
tries = 0
|
||||
|
||||
def backtrack(index, current_digits):
|
||||
nonlocal tries
|
||||
tries += 1
|
||||
if index == 3:
|
||||
for condition in conditions:
|
||||
if condition == [-1, -1]:
|
||||
if not satisfies_prime_unique_condition(current_digits):
|
||||
return
|
||||
elif len(condition) == 2:
|
||||
a, t = condition
|
||||
a = abs(a)
|
||||
if 1 <= a <= 3:
|
||||
if t == 0 and current_digits[a - 1] % 2 != 0:
|
||||
return
|
||||
elif t == 1 and current_digits[a - 1] % 2 != 1:
|
||||
return
|
||||
else:
|
||||
return
|
||||
elif len(condition) == 3:
|
||||
b1, b2, b3 = condition
|
||||
if b1 != -1 and current_digits[0] != b1:
|
||||
return
|
||||
if b2 != -1 and current_digits[1] != b2:
|
||||
return
|
||||
if b3 != -1 and current_digits[2] != b3:
|
||||
return
|
||||
possible_passwords.append("".join(map(str, current_digits)))
|
||||
return
|
||||
|
||||
for digit in range(10):
|
||||
if index == 0:
|
||||
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 1:
|
||||
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 2:
|
||||
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
current_digits.append(digit)
|
||||
backtrack(index + 1, current_digits)
|
||||
current_digits.pop()
|
||||
|
||||
backtrack(0, [])
|
||||
return possible_passwords, tries
|
||||
|
||||
|
||||
def crack_method2(conditions):
|
||||
"""从第二位开始回溯(第二位→第三位→第一位)"""
|
||||
possible_passwords = []
|
||||
tries = 0
|
||||
|
||||
def backtrack(index, current_digits):
|
||||
nonlocal tries
|
||||
tries += 1
|
||||
if index == 3:
|
||||
reordered = [current_digits[2], current_digits[0], current_digits[1]]
|
||||
for condition in conditions:
|
||||
if condition == [-1, -1]:
|
||||
if not satisfies_prime_unique_condition(reordered):
|
||||
return
|
||||
elif len(condition) == 2:
|
||||
a, t = condition
|
||||
a = abs(a)
|
||||
if 1 <= a <= 3:
|
||||
if t == 0 and reordered[a - 1] % 2 != 0:
|
||||
return
|
||||
elif t == 1 and reordered[a - 1] % 2 != 1:
|
||||
return
|
||||
else:
|
||||
return
|
||||
elif len(condition) == 3:
|
||||
b1, b2, b3 = condition
|
||||
if b1 != -1 and reordered[0] != b1:
|
||||
return
|
||||
if b2 != -1 and reordered[1] != b2:
|
||||
return
|
||||
if b3 != -1 and reordered[2] != b3:
|
||||
return
|
||||
possible_passwords.append("".join(map(str, reordered)))
|
||||
return
|
||||
|
||||
for digit in range(10):
|
||||
if index == 0: # 第二位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 1: # 第三位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 2: # 第一位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
current_digits.append(digit)
|
||||
backtrack(index + 1, current_digits)
|
||||
current_digits.pop()
|
||||
|
||||
backtrack(0, [])
|
||||
return possible_passwords, tries
|
||||
|
||||
|
||||
def crack_method3(conditions):
|
||||
"""从第三位开始回溯(第三位→第一位→第二位)"""
|
||||
possible_passwords = []
|
||||
tries = 0
|
||||
|
||||
def backtrack(index, current_digits):
|
||||
nonlocal tries
|
||||
tries += 1
|
||||
if index == 3:
|
||||
reordered = [current_digits[1], current_digits[2], current_digits[0]]
|
||||
for condition in conditions:
|
||||
if condition == [-1, -1]:
|
||||
if not satisfies_prime_unique_condition(reordered):
|
||||
return
|
||||
elif len(condition) == 2:
|
||||
a, t = condition
|
||||
a = abs(a)
|
||||
if 1 <= a <= 3:
|
||||
if t == 0 and reordered[a - 1] % 2 != 0:
|
||||
return
|
||||
elif t == 1 and reordered[a - 1] % 2 != 1:
|
||||
return
|
||||
else:
|
||||
return
|
||||
elif len(condition) == 3:
|
||||
b1, b2, b3 = condition
|
||||
if b1 != -1 and reordered[0] != b1:
|
||||
return
|
||||
if b2 != -1 and reordered[1] != b2:
|
||||
return
|
||||
if b3 != -1 and reordered[2] != b3:
|
||||
return
|
||||
possible_passwords.append("".join(map(str, reordered)))
|
||||
return
|
||||
|
||||
for digit in range(10):
|
||||
if index == 0: # 第三位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 1: # 第一位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 2: # 第二位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
current_digits.append(digit)
|
||||
backtrack(index + 1, current_digits)
|
||||
current_digits.pop()
|
||||
|
||||
backtrack(0, [])
|
||||
return possible_passwords, tries
|
||||
|
||||
|
||||
def format_json(data):
|
||||
"""自定义 JSON 格式化函数"""
|
||||
lines = ['{']
|
||||
|
||||
# 格式化 C 线索部分
|
||||
if "C" in data:
|
||||
lines.append(' "C": [')
|
||||
for i, item in enumerate(data["C"]):
|
||||
lines.append(f" {json.dumps(item, ensure_ascii=False)}{',' if i < len(data['C']) - 1 else ''}")
|
||||
lines.append(' ],')
|
||||
|
||||
# 格式化 L 哈希值
|
||||
if "L" in data:
|
||||
lines.append(f' "L": {json.dumps(data["L"], ensure_ascii=False)},')
|
||||
|
||||
# 格式化 password
|
||||
if "password" in data:
|
||||
lines.append(f' "password": {json.dumps(data["password"], ensure_ascii=False)},')
|
||||
|
||||
# 格式化 results
|
||||
if "results" in data:
|
||||
lines.append(' "results": {')
|
||||
result_items = data["results"]
|
||||
method_names = list(result_items.keys())
|
||||
for i, method in enumerate(method_names):
|
||||
line = f' "{method}": {json.dumps(result_items[method], ensure_ascii=False)}'
|
||||
if i < len(result_items) - 1:
|
||||
line += ","
|
||||
lines.append(line)
|
||||
lines.append(' }')
|
||||
|
||||
lines.append('}')
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def main():
|
||||
# 输入和输出路径
|
||||
input_dir = r"" # path
|
||||
output_dir = r"" # path
|
||||
|
||||
# 如果输出目录不存在,则创建
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# 遍历输入目录下所有 .json 文件
|
||||
for filename in os.listdir(input_dir):
|
||||
if filename.endswith(".json"):
|
||||
input_file_path = os.path.join(input_dir, filename)
|
||||
output_file_path = os.path.join(output_dir, filename)
|
||||
|
||||
with open(input_file_path, 'r', encoding='utf-8') as f:
|
||||
try:
|
||||
# 读取原始 JSON 文本内容(保留结构)
|
||||
original_content = f.read()
|
||||
# 解析为字典用于处理
|
||||
sample_data = json.loads(original_content)
|
||||
except json.JSONDecodeError:
|
||||
print(f"跳过无效的 JSON 文件: {filename}")
|
||||
continue
|
||||
|
||||
conditions = sample_data["C"]
|
||||
stored_hash = sample_data["L"]
|
||||
|
||||
print(f"\n正在处理文件: {filename}")
|
||||
|
||||
pwd1, tries1 = crack_method1(conditions)
|
||||
pwd2, tries2 = crack_method2(conditions)
|
||||
pwd3, tries3 = crack_method3(conditions)
|
||||
|
||||
lock = PasswordLock()
|
||||
matched_passwords = []
|
||||
|
||||
for pwd in pwd1 + pwd2 + pwd3:
|
||||
if pwd not in matched_passwords and lock.verify_password(pwd, stored_hash):
|
||||
matched_passwords.append(pwd)
|
||||
|
||||
first_match = matched_passwords[0] if matched_passwords else ""
|
||||
|
||||
# 更新 JSON 内容中的结果部分
|
||||
result_update = {
|
||||
"password": first_match,
|
||||
"results": {
|
||||
"method1": {"tries": tries1, "password": list(map(int, first_match)) if first_match else []},
|
||||
"method2": {"tries": tries2, "password": list(map(int, first_match)) if first_match else []},
|
||||
"method3": {"tries": tries3, "password": list(map(int, first_match)) if first_match else []}
|
||||
}
|
||||
}
|
||||
|
||||
# 加载原始内容为 dict 并更新关键字段
|
||||
data = json.loads(original_content)
|
||||
data.update(result_update)
|
||||
|
||||
# 使用自定义格式化函数生成 JSON 字符串
|
||||
formatted_json = format_json(data)
|
||||
|
||||
# 写入文件
|
||||
with open(output_file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(formatted_json)
|
||||
|
||||
print(f"结果已保存至: {output_file_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
import json
|
||||
from Lock import PasswordLock
|
||||
|
||||
|
||||
def is_prime(n):
|
||||
"""判断一个数字是否是素数"""
|
||||
if n < 2:
|
||||
return False
|
||||
for i in range(2, int(n ** 0.5) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def satisfies_prime_unique_condition(digits):
|
||||
"""检查是否满足[-1, -1]条件:每位密码为素数且不重复"""
|
||||
return all(is_prime(d) for d in digits) and len(set(digits)) == 3
|
||||
|
||||
|
||||
def crack_method1(conditions):
|
||||
"""从高位到低位回溯(第一位→第二位→第三位)"""
|
||||
possible_passwords = []
|
||||
tries = 0
|
||||
|
||||
def backtrack(index, current_digits):
|
||||
nonlocal tries
|
||||
tries += 1
|
||||
if index == 3:
|
||||
for condition in conditions:
|
||||
if condition == [-1, -1]:
|
||||
if not satisfies_prime_unique_condition(current_digits):
|
||||
return
|
||||
elif len(condition) == 2:
|
||||
a, t = condition
|
||||
a = abs(a)
|
||||
if 1 <= a <= 3:
|
||||
if t == 0 and current_digits[a - 1] % 2 != 0:
|
||||
return
|
||||
elif t == 1 and current_digits[a - 1] % 2 != 1:
|
||||
return
|
||||
else:
|
||||
return
|
||||
elif len(condition) == 3:
|
||||
b1, b2, b3 = condition
|
||||
if b1 != -1 and current_digits[0] != b1:
|
||||
return
|
||||
if b2 != -1 and current_digits[1] != b2:
|
||||
return
|
||||
if b3 != -1 and current_digits[2] != b3:
|
||||
return
|
||||
possible_passwords.append("".join(map(str, current_digits)))
|
||||
return
|
||||
|
||||
for digit in range(10):
|
||||
if index == 0:
|
||||
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 1:
|
||||
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 2:
|
||||
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
current_digits.append(digit)
|
||||
backtrack(index + 1, current_digits)
|
||||
current_digits.pop()
|
||||
|
||||
backtrack(0, [])
|
||||
return possible_passwords, tries
|
||||
|
||||
|
||||
def crack_method2(conditions):
|
||||
"""从第二位开始回溯(第二位→第三位→第一位)"""
|
||||
possible_passwords = []
|
||||
tries = 0
|
||||
|
||||
def backtrack(index, current_digits):
|
||||
nonlocal tries
|
||||
tries += 1
|
||||
if index == 3:
|
||||
reordered = [current_digits[2], current_digits[0], current_digits[1]]
|
||||
for condition in conditions:
|
||||
if condition == [-1, -1]:
|
||||
if not satisfies_prime_unique_condition(reordered):
|
||||
return
|
||||
elif len(condition) == 2:
|
||||
a, t = condition
|
||||
a = abs(a)
|
||||
if 1 <= a <= 3:
|
||||
if t == 0 and reordered[a - 1] % 2 != 0:
|
||||
return
|
||||
elif t == 1 and reordered[a - 1] % 2 != 1:
|
||||
return
|
||||
else:
|
||||
return
|
||||
elif len(condition) == 3:
|
||||
b1, b2, b3 = condition
|
||||
if b1 != -1 and reordered[0] != b1:
|
||||
return
|
||||
if b2 != -1 and reordered[1] != b2:
|
||||
return
|
||||
if b3 != -1 and reordered[2] != b3:
|
||||
return
|
||||
possible_passwords.append("".join(map(str, reordered)))
|
||||
return
|
||||
|
||||
for digit in range(10):
|
||||
if index == 0: # 第二位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 1: # 第三位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 2: # 第一位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
current_digits.append(digit)
|
||||
backtrack(index + 1, current_digits)
|
||||
current_digits.pop()
|
||||
|
||||
backtrack(0, [])
|
||||
return possible_passwords, tries
|
||||
|
||||
|
||||
def crack_method3(conditions):
|
||||
"""从第三位开始回溯(第三位→第一位→第二位)"""
|
||||
possible_passwords = []
|
||||
tries = 0
|
||||
|
||||
def backtrack(index, current_digits):
|
||||
nonlocal tries
|
||||
tries += 1
|
||||
if index == 3:
|
||||
reordered = [current_digits[1], current_digits[2], current_digits[0]]
|
||||
for condition in conditions:
|
||||
if condition == [-1, -1]:
|
||||
if not satisfies_prime_unique_condition(reordered):
|
||||
return
|
||||
elif len(condition) == 2:
|
||||
a, t = condition
|
||||
a = abs(a)
|
||||
if 1 <= a <= 3:
|
||||
if t == 0 and reordered[a - 1] % 2 != 0:
|
||||
return
|
||||
elif t == 1 and reordered[a - 1] % 2 != 1:
|
||||
return
|
||||
else:
|
||||
return
|
||||
elif len(condition) == 3:
|
||||
b1, b2, b3 = condition
|
||||
if b1 != -1 and reordered[0] != b1:
|
||||
return
|
||||
if b2 != -1 and reordered[1] != b2:
|
||||
return
|
||||
if b3 != -1 and reordered[2] != b3:
|
||||
return
|
||||
possible_passwords.append("".join(map(str, reordered)))
|
||||
return
|
||||
|
||||
for digit in range(10):
|
||||
if index == 0: # 第三位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[2] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[2] for cond in conditions if len(cond) == 3 and cond[2] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 1: # 第一位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[0] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[0] for cond in conditions if len(cond) == 3 and cond[0] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
elif index == 2: # 第二位
|
||||
has_fixed_value = any(len(cond) == 3 and cond[1] != -1 for cond in conditions)
|
||||
if has_fixed_value:
|
||||
fixed_values = [cond[1] for cond in conditions if len(cond) == 3 and cond[1] != -1]
|
||||
if digit not in fixed_values:
|
||||
continue
|
||||
current_digits.append(digit)
|
||||
backtrack(index + 1, current_digits)
|
||||
current_digits.pop()
|
||||
|
||||
backtrack(0, [])
|
||||
return possible_passwords, tries
|
||||
|
||||
|
||||
def format_json(data):
|
||||
"""自定义 JSON 格式化函数"""
|
||||
lines = ['{']
|
||||
|
||||
# 格式化 C 线索部分
|
||||
if "C" in data:
|
||||
lines.append(' "C": [')
|
||||
for i, item in enumerate(data["C"]):
|
||||
lines.append(f" {json.dumps(item, ensure_ascii=False)}{',' if i < len(data['C']) - 1 else ''}")
|
||||
lines.append(' ],')
|
||||
|
||||
# 格式化 L 哈希值
|
||||
if "L" in data:
|
||||
lines.append(f' "L": {json.dumps(data["L"], ensure_ascii=False)},')
|
||||
|
||||
# 格式化 password
|
||||
if "password" in data:
|
||||
lines.append(f' "password": {json.dumps(data["password"], ensure_ascii=False)},')
|
||||
|
||||
# 格式化 results
|
||||
if "results" in data:
|
||||
lines.append(' "results": {')
|
||||
result_items = data["results"]
|
||||
method_names = list(result_items.keys())
|
||||
for i, method in enumerate(method_names):
|
||||
line = f' "{method}": {json.dumps(result_items[method], ensure_ascii=False)}'
|
||||
if i < len(result_items) - 1:
|
||||
line += ","
|
||||
lines.append(line)
|
||||
lines.append(' }')
|
||||
|
||||
lines.append('}')
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def main():
|
||||
# 输入和输出路径
|
||||
input_dir = r"" # path
|
||||
output_dir = r"" # path
|
||||
|
||||
# 如果输出目录不存在,则创建
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# 遍历输入目录下所有 .json 文件
|
||||
for filename in os.listdir(input_dir):
|
||||
if filename.endswith(".json"):
|
||||
input_file_path = os.path.join(input_dir, filename)
|
||||
output_file_path = os.path.join(output_dir, filename)
|
||||
|
||||
with open(input_file_path, 'r', encoding='utf-8') as f:
|
||||
try:
|
||||
# 读取原始 JSON 文本内容(保留结构)
|
||||
original_content = f.read()
|
||||
# 解析为字典用于处理
|
||||
sample_data = json.loads(original_content)
|
||||
except json.JSONDecodeError:
|
||||
print(f"跳过无效的 JSON 文件: {filename}")
|
||||
continue
|
||||
|
||||
conditions = sample_data["C"]
|
||||
stored_hash = sample_data["L"]
|
||||
|
||||
print(f"\n正在处理文件: {filename}")
|
||||
|
||||
pwd1, tries1 = crack_method1(conditions)
|
||||
pwd2, tries2 = crack_method2(conditions)
|
||||
pwd3, tries3 = crack_method3(conditions)
|
||||
|
||||
lock = PasswordLock()
|
||||
matched_passwords = []
|
||||
|
||||
for pwd in pwd1 + pwd2 + pwd3:
|
||||
if pwd not in matched_passwords and lock.verify_password(pwd, stored_hash):
|
||||
matched_passwords.append(pwd)
|
||||
|
||||
first_match = matched_passwords[0] if matched_passwords else ""
|
||||
|
||||
# 更新 JSON 内容中的结果部分
|
||||
result_update = {
|
||||
"password": first_match,
|
||||
"results": {
|
||||
"method1": {"tries": tries1, "password": list(map(int, first_match)) if first_match else []},
|
||||
"method2": {"tries": tries2, "password": list(map(int, first_match)) if first_match else []},
|
||||
"method3": {"tries": tries3, "password": list(map(int, first_match)) if first_match else []}
|
||||
}
|
||||
}
|
||||
|
||||
# 加载原始内容为 dict 并更新关键字段
|
||||
data = json.loads(original_content)
|
||||
data.update(result_update)
|
||||
|
||||
# 使用自定义格式化函数生成 JSON 字符串
|
||||
formatted_json = format_json(data)
|
||||
|
||||
# 写入文件
|
||||
with open(output_file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(formatted_json)
|
||||
|
||||
print(f"结果已保存至: {output_file_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
200
readme.md
200
readme.md
@ -1,100 +1,100 @@
|
||||
# 算法驱动的迷宫探险游戏开发
|
||||
|
||||
## 设计目标
|
||||
|
||||
***\*地图:\****
|
||||
|
||||
一个二维矩阵,采⽤分治法⽣成,⽆孤⽴区域,存在唯⼀通路。
|
||||
|
||||
输⼊:迷宫尺⼨ 。
|
||||
|
||||
输出:迷宫矩阵(可存储为JSON或CSV),包括起点Start(S)、 终点Exit(E)、墙壁(#)、通路(空格)、资源(例如⾦币G)、 陷阱Trap(T)、机关Locker(L)、BOSS(B)。
|
||||
|
||||
|
||||
|
||||
墙壁:不可通过,无value的节点
|
||||
|
||||
通路:可通过,无value的节点
|
||||
|
||||
金币:可通过,有value 的节点。BOSS战时的行动需要消耗金币
|
||||
|
||||
陷阱:可通过,但经过时会扣除玩家的金币
|
||||
|
||||
机关:三位密码锁。一般分布在某条路的尽头,必须开启所有机关后,终点大门才会打开
|
||||
|
||||
BOSS:在必经之路上,有value(血量) , 必须打败才能继续前进
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
***\*玩家:\****
|
||||
|
||||
有三种攻击方式(初始充能为0):
|
||||
|
||||
A:消耗2枚金币造成1点伤害并获得1点充能
|
||||
|
||||
E:消耗3枚金币造成3点伤害并获得1点充能,连续第三次使用E技能时少消耗1枚金币
|
||||
|
||||
Q:消耗3枚金币和3点充能,造成8点伤害
|
||||
|
||||
|
||||
|
||||
## 任务分解
|
||||
|
||||
### 1. 分治法生成迷宫
|
||||
|
||||
**需求 : **
|
||||
|
||||
输入 : 正整数 n( 100>=n>=7 ) , 生成类型选择(json或者csv)
|
||||
|
||||
输出 : json文件 , csv文件 , 表示地图 , 无孤立区 , 存在唯一通路
|
||||
|
||||
S : 起点
|
||||
|
||||
E : 终点
|
||||
|
||||
\# :墙壁
|
||||
|
||||
blank : 通路
|
||||
|
||||
G : 资源
|
||||
|
||||
T : 陷阱
|
||||
|
||||
L : 机关
|
||||
|
||||
B : Boss
|
||||
|
||||
**递归分割法生成迷宫**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 2. 动态规划进行资源收集路径规划
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 3. 贪心算法设计实时资源拾取策略
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 4. 回溯法解迷关卡
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 5. 分支限界设计boss战
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 算法驱动的迷宫探险游戏开发
|
||||
|
||||
## 设计目标
|
||||
|
||||
***\*地图:\****
|
||||
|
||||
一个二维矩阵,采⽤分治法⽣成,⽆孤⽴区域,存在唯⼀通路。
|
||||
|
||||
输⼊:迷宫尺⼨ 。
|
||||
|
||||
输出:迷宫矩阵(可存储为JSON或CSV),包括起点Start(S)、 终点Exit(E)、墙壁(#)、通路(空格)、资源(例如⾦币G)、 陷阱Trap(T)、机关Locker(L)、BOSS(B)。
|
||||
|
||||
|
||||
|
||||
墙壁:不可通过,无value的节点
|
||||
|
||||
通路:可通过,无value的节点
|
||||
|
||||
金币:可通过,有value 的节点。BOSS战时的行动需要消耗金币
|
||||
|
||||
陷阱:可通过,但经过时会扣除玩家的金币
|
||||
|
||||
机关:三位密码锁。一般分布在某条路的尽头,必须开启所有机关后,终点大门才会打开
|
||||
|
||||
BOSS:在必经之路上,有value(血量) , 必须打败才能继续前进
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
***\*玩家:\****
|
||||
|
||||
有三种攻击方式(初始充能为0):
|
||||
|
||||
A:消耗2枚金币造成1点伤害并获得1点充能
|
||||
|
||||
E:消耗3枚金币造成3点伤害并获得1点充能,连续第三次使用E技能时少消耗1枚金币
|
||||
|
||||
Q:消耗3枚金币和3点充能,造成8点伤害
|
||||
|
||||
|
||||
|
||||
## 任务分解
|
||||
|
||||
### 1. 分治法生成迷宫
|
||||
|
||||
**需求 : **
|
||||
|
||||
输入 : 正整数 n( 100>=n>=7 ) , 生成类型选择(json或者csv)
|
||||
|
||||
输出 : json文件 , csv文件 , 表示地图 , 无孤立区 , 存在唯一通路
|
||||
|
||||
S : 起点
|
||||
|
||||
E : 终点
|
||||
|
||||
\# :墙壁
|
||||
|
||||
blank : 通路
|
||||
|
||||
G : 资源
|
||||
|
||||
T : 陷阱
|
||||
|
||||
L : 机关
|
||||
|
||||
B : Boss
|
||||
|
||||
**递归分割法生成迷宫**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 2. 动态规划进行资源收集路径规划
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 3. 贪心算法设计实时资源拾取策略
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 4. 回溯法解迷关卡
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 5. 分支限界设计boss战
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user