Skip to content

Latest commit

 

History

History
116 lines (93 loc) · 2.95 KB

Лекция-15.md

File metadata and controls

116 lines (93 loc) · 2.95 KB

Лекция 15

Рекурсия

include("horizonside.jl")

function move_recurs!(robot,side)
    if isborder(robot,side)
        move!(robot,right(side))
        move_recurs!(robot,side)
        move!(robot,left(side))
    else
        move!(robot,side)
    end
end

#move_recurs!(robot,Nord)

function dist_double!(robot,side)
    if !isborder(robot,side)
        move!(robot,side)
        dist_double(robot,side)
        move!(robot,inverse(side))
        move!(robot,inverse(side))
    end
end


function move_symmetric!(robot,side)  
    if !isborder(robot,inverse(side))
        move!(robot,inverse(side))
        move_symmetric!(robot,side) 
        move!(robot,inverse(side))
    else
        while !isborder(robot,side)
            move!(robot,side)
        end
    end
end


mark_labirint!(robot, index_side = -1) = 
    if !ismarker(robot)
        putmarker!(robot)

        for side in HorizonSide.(setdiff!([0,1,2,3], index_side))
            move!(robot,side)
            mark_labirint!(robot, Int(inverse(side)))
            move!(robot,inverse(side))
        end
    end

#---------------------------------------------------------------------- 
#=
    for side in HorizonSide.(0:3)
        ...
    end
=#

#-----------------------------------------------------------------------

Base.iterate(side::HorizonSide) = (side, (Int(side), mod(Int(side)+1,4))
Base.iterate(side::HorizonSide, state) =
    if state[1] == state[2]
        return nothing
    else
        return HorizonSide(state), (state[1], mod(state[2]+1,4))
    end

#=
Теперь можно будет присать так:

for side if HorizonSide(0) # | HorizonSide(1) | HorizonSide(2) | HorizonSide(3)
    ...
end

А также можно будет использовать функцию setdiff!(Set(HorizonSide.(0:3)), inverse(robot.input_side))
=#

struct RobotLabirinTracker{TypeRobot} <: AbstractDecartRobot
    robot::TypeRobot
    decart::Decart
    markered_positions::Set{Tuple{Int,Int}}
    RobotLabirinTracker{TypeRobot}(robot::TypeRobot) where TypeRobot = new(roobot, Decart(), Set{Tuple{Int,Int}}())
end

setmarker!(robot) = push!(robot.markered_positions, coordinates(robot)
ismarkered(robot::RobotLabirinTracker) = (coordinates(robot.robot_decart) in robot.markered_positions)

function tracke!(action::Function, robot::RobotLabirinTracker)
    if !ismarkered(robot)
        setmarker!(robot)
        for side in HorizonSide(0)
            tracke!(action, robot.robot_decart, side)
        end
    end
end

function tracke!(action::Function, robot::RobotLabirinTracker, side)
    if !ismarkered(robot)
        setmarker!(robot)
        for side in setdiff!(Set(HorizonSide.(0:3)), inverse(robot.input_side))
            tracke!(action, robot.robot_decart, side)
        end
    end
end

#=
#Base.iterate(robot::RobotLabirinTracker) = (move!(robot,nothing) ???????? 
#Base.iterate(robot::RobotLabirinTracker, state) = (robot,nothing) ???????? 
=#

Пример косвенной рекурсии