This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
libraryaddict's profile picture

Turning a function that depends on repetition to tables

Started by libraryaddict, 05 July 2012 - 12:50 PM
libraryaddict #1
Posted 05 July 2012 - 02:50 PM
I have this code from :
This function gives problems as it features repetitions.
This is normally not a problem.. Unless you want a big maze.


function Maze:_process_neighbor_cells(x, y)
    self.cells[y][x].is_visited = true

    local neighbor_cells = self:_shuffle(self:_get_neighbor_cells(x, y))

    for index, neighbor_cell in ipairs(neighbor_cells) do
        if self.cells[neighbor_cell.y][neighbor_cell.x].is_visited == false then
            if neighbor_cell.x > x     then     -- open wall with right neighbor
                self.cells[y][x].right_wall                              = false
                self.cells[neighbor_cell.y][neighbor_cell.x].left_wall   = false
            elseif neighbor_cell.x < x then     -- open wall with left neighbor
                self.cells[y][x].left_wall                               = false
                self.cells[neighbor_cell.y][neighbor_cell.x].right_wall  = false
            elseif neighbor_cell.y > y then     -- open wall with bottom neighbor
                self.cells[y][x].bottom_wall                             = false
                self.cells[neighbor_cell.y][neighbor_cell.x].top_wall    = false
            elseif neighbor_cell.y < y then     -- open wall with top neighbor
                self.cells[y][x].top_wall                                = false
                self.cells[neighbor_cell.y][neighbor_cell.x].bottom_wall = false
            end

            -- recursively process this cell
            self:_process_neighbor_cells(neighbor_cell.x, neighbor_cell.y)
        end
    end

    return
end

If anyone is able to turn this to NOT use repetition.
I would be very very grateful.

I've tried to do this a few times but rage quitted..
If you would like to know how this works
Its kind of simple.


Function is called on
Function checks all sides around the sides it was given
If sides matches sides then it does some stuff then calls on this function again
If not then it returns
Function ends

Basically its function inception..
Pinkishu #2
Posted 05 July 2012 - 03:09 PM
something like


function Maze:_process_neighbor_cells(ox, oy)
    local cellList = { {ox,oy} }
    while #cellList > 0 do

        os.queueEvent("go")
        coroutine.yield()
        local cell = table.remove(cellList,1)
        local x,y = cell[1],cell[2]

        if not self.cells[y][x].is_visited then
            self.cells[y][x].is_visited = true

            local neighbor_cells = self:_shuffle(self:_get_neighbor_cells(x, y))

            for index, neighbor_cell in ipairs(neighbor_cells) do
                if self.cells[neighbor_cell.y][neighbor_cell.x].is_visited == false then
                    if neighbor_cell.x > x     then     -- open wall with right neighbor
                        self.cells[y][x].right_wall                              = false
                        self.cells[neighbor_cell.y][neighbor_cell.x].left_wall   = false
                    elseif neighbor_cell.x < x then     -- open wall with left neighbor
                        self.cells[y][x].left_wall                               = false
                        self.cells[neighbor_cell.y][neighbor_cell.x].right_wall  = false
                    elseif neighbor_cell.y > y then     -- open wall with bottom neighbor
                        self.cells[y][x].bottom_wall                             = false
                        self.cells[neighbor_cell.y][neighbor_cell.x].top_wall    = false
                    elseif neighbor_cell.y < y then     -- open wall with top neighbor
                        self.cells[y][x].top_wall                                = false
                        self.cells[neighbor_cell.y][neighbor_cell.x].bottom_wall = false
                    end

            -- recursively process this cell
                    table.insert(cellList,1,{neighbor_cell.x,neighbor_cell.y})
                --self:_process_neighbor_cells(neighbor_cell.x, neighbor_cell.y)
                end
            end
        end
    end

    return
end

maybe?