Робот-перфекционист.
Автор
"""
# type Robot
- Julia version: 1.10.4
- Author: joseph
- Date: 2024-08-31
# Examples
```jldoctest
julia>
```
"""
mutable struct Direction
id
x
y
X
Y
next
set
function Direction(id)
X = [-1, 0, 1, 0]
Y = [0, -1, 0, 1]
function next(d::Direction, n = 1)
d.id += n
d.id = (d.id - 1) % 4 + 1
d.x = d.X[d.id]
d.y = d.Y[d.id]
end
function set(d::Direction, n::Int)
d.id = n
d.x = d.X[d.id]
d.y = d.Y[d.id]
end
b = new(id, X[id], Y[id], X, Y, next, set)
println(b)
return(b)
end
end
mutable struct Robot
start_x
start_y
x::Int
y::Int
dir
known_plane
path
room
step
stepT
move_forward
function Robot(r::Room)
s_x, s_y = r.generate_start_point(r)
known_plane = r.generate_zero_plane(r)
# known_plane[s_x, s_y] = 1
dir = nearest_perimetr(r, s_x, s_y)
function step(r::Robot, hard = true)
d = r.dir;
d.next(d, 3)
for i in 1:4
x = r.x + d.x;
y = r.y + d.y;
if hard && r.known_plane[x, y] == 0.5
continue
end
if r.room.plane[x, y] == 0
r.x, r.y = x, y
if r.known_plane[r.x, r.y] != 1
if r.known_plane[r.x, r.y] > 0
r.known_plane[r.x, r.y] *= 1.5
else
r.known_plane[r.x, r.y] = 0.5
end
end
push!(r.path, [x, y])
return true
else
r.known_plane[x, y] = 0.8
end
d.next(d);
end
return false
end
function stepT(r::Robot)
d = r.dir;
dirs = []
for i in 1:4
x = r.x + d.x;
y = r.y + d.y;
if r.room.plane[x, y] == 0
push!(dirs,[r.known_plane[x, y], x, y, d.id])
else
r.known_plane[x, y] = 0.8
end
d.next(d, i);
end
i = argmin([x[1] for x in dirs])
r.x, r.y = dirs[i][2], dirs[i][3]
r.dir.set(r.dir, Int(dirs[i][4]))
if r.known_plane[r.x, r.y] > 0
r.known_plane[r.x, r.y] *= 1.5
else
r.known_plane[r.x, r.y] = 0.2
end
push!(r.path, [r.x, r.y])
return true
end
function move_forward(r::Robot)
println("move_forward. dir: ", r.dir.id)
d = r.dir;
x, y = r.x + d.x, r.y + d.y;
while r.room.plane[x, y] == 0
r.x, r.y = x, y
r.known_plane[r.x, r.y] = 0.2
push!(r.path, [x, y])
x, y = r.x + d.x, r.y + d.y;
end
r.known_plane[x, y] = 0.8
d.next(d)
end
robot = new(s_x, s_y, s_x, s_y, Direction(dir), known_plane, [[s_x, s_y]], r,
step, stepT, move_forward)
r.robot = robot
return robot
end
end
function nearest_perimetr(r, s_x, s_y)
println("nearest_perimetr. ", s_x, " ", s_y)
a = [s_x, s_y, r.width - s_x, r.height - s_y]
println(a)
return argmin(a)
end