Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Maze-generation/Julia/maze-generation-1.julia
Normal file
20
Task/Maze-generation/Julia/maze-generation-1.julia
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Random
|
||||
check(bound::Vector) = cell -> all([1, 1] .≤ cell .≤ bound)
|
||||
neighbors(cell::Vector, bound::Vector, step::Int=2) =
|
||||
filter(check(bound), map(dir -> cell + step * dir, [[0, 1], [-1, 0], [0, -1], [1, 0]]))
|
||||
|
||||
function walk(maze::Matrix, nxtcell::Vector, visited::Vector=[])
|
||||
push!(visited, nxtcell)
|
||||
for neigh in shuffle(neighbors(nxtcell, collect(size(maze))))
|
||||
if neigh ∉ visited
|
||||
maze[round.(Int, (nxtcell + neigh) / 2)...] = 0
|
||||
walk(maze, neigh, visited)
|
||||
end
|
||||
end
|
||||
maze
|
||||
end
|
||||
function maze(w::Int, h::Int)
|
||||
maze = collect(i % 2 | j % 2 for i in 1:2w+1, j in 1:2h+1)
|
||||
firstcell = 2 * [rand(1:w), rand(1:h)]
|
||||
return walk(maze, firstcell)
|
||||
end
|
||||
13
Task/Maze-generation/Julia/maze-generation-2.julia
Normal file
13
Task/Maze-generation/Julia/maze-generation-2.julia
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
pprint(matrix) = for i = 1:size(matrix, 1) println(join(matrix[i, :])) end
|
||||
function printmaze(maze)
|
||||
walls = split("╹ ╸ ┛ ╺ ┗ ━ ┻ ╻ ┃ ┓ ┫ ┏ ┣ ┳ ╋")
|
||||
h, w = size(maze)
|
||||
f = cell -> 2 ^ ((3cell[1] + cell[2] + 3) / 2)
|
||||
wall(i, j) = if maze[i,j] == 0 " " else
|
||||
walls[Int(sum(f, filter(x -> maze[x...] != 0, neighbors([i, j], [h, w], 1)) .- [[i, j]]))]
|
||||
end
|
||||
mazewalls = collect(wall(i, j) for i in 1:2:h, j in 1:w)
|
||||
pprint(mazewalls)
|
||||
end
|
||||
|
||||
printmaze(maze(10, 10))
|
||||
Loading…
Add table
Add a link
Reference in a new issue