革命尚未成功!同志仍需努力!
有了各个海域的精确位置以及分布情况,就可以根据经纬度在大海上定位了
function make_ocean_chart(origin_map, current_map, width, position)
local ocean = classfy_sea_area(origin_map, current_map, width)
local ocean_index = 0
for i1, v1 in ipairs(ocean) do
for i2, v2 in ipairs(v1) do
if (position >= v2[1]) and (position <= v2[2]) then
ocean_index = i1
break
end
end
end
if ocean_index ~= 0 then
local t = current_map
for i, v in ipairs(ocean[ocean_index]) do
t = make_coastline(origin_map, t, width, v)
end
return t, compare_map(current_map, t)
end
return current_map, ""
end
描绘大海的同时还要记得把海岸线也画出来。
function make_coastline(origin_map, current_map, width, sea_area)
local sea_area_west = sea_area[1]
local sea_area_east = sea_area[2]
if sea_area[1] % width ~= 1 then
sea_area_west = sea_area_west - 1
end
if sea_area[2] % width ~= 0 then
sea_area_east = sea_area_east + 1
end
if sea_area_west - width >= 1 then
current_map = string.sub(current_map, 1, sea_area_west - width - 1) ..
string.sub(origin_map, sea_area_west - width, sea_area_east - width) ..
string.sub(current_map, sea_area_east - width + 1, -1)
end
current_map = string.sub(current_map, 1, sea_area_west - 1) ..
string.sub(origin_map, sea_area_west, sea_area_east) ..
string.sub(current_map, sea_area_east + 1, -1)
if sea_area_east + width <= #current_map then
current_map = string.sub(current_map, 1, sea_area_west + width -1) ..
string.sub(origin_map, sea_area_west + width, sea_area_east + width) ..
string.sub(current_map, sea_area_east + width + 1, -1)
end
return current_map
end
至此,扫雷游戏的算法全部描述完毕