Skip to content

Instantly share code, notes, and snippets.

@Forecaster
Created May 12, 2020 12:50

Revisions

  1. Forecaster created this gist May 12, 2020.
    111 changes: 111 additions & 0 deletions switch_game.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    local sides = require("sides")
    local event = require("event")
    local component = require("component")
    local serialization = require("serialization")

    local max_lights_on_min = 4 -- >= 1
    local max_lights_on_max = 6 -- <= 16

    local switchboards = {}
    local light_board = component.list("light_board")()
    if light_board then
    light_board = component.proxy(light_board)
    end

    for address,_ in component.list("switch_board") do
    local sw = component.proxy(address)
    if sw then
    if sw.isActive(1) then
    switchboards[1] = sw
    elseif sw.isActive(2) then
    switchboards[2] = sw
    elseif sw.isActive(3) then
    switchboards[3] = sw
    elseif sw.isActive(4) then
    switchboards[4] = sw
    end
    end
    end
    if #switchboards < 4 then
    error("Not enough switch boards found (" .. #switchboards .. "/4)")
    end

    local last

    for x=1,4 do
    local sw = switchboards[x]
    for y=1,4 do
    if last then
    switchboards[last.x].setActive(last.y, false)
    end
    sw.setActive(y, true)
    last = { x=x, y=y }
    os.sleep(0.5)
    end
    end
    switchboards[4].setActive(4, false)

    local fun = true
    local function onInterrupted()
    fun = false
    print("Exiting, please wait...")
    end

    event.listen("interrupted", onInterrupted)

    print("Game start!")
    while fun do
    local ok = true
    for x=1,4 do
    for y=1,4 do
    if switchboards[x].isActive(y) then
    ok = false
    switchboards[x].setActive(y, false)
    end
    end
    end
    if ok then
    component.redstone.setOutput(sides.up, 15)
    else
    component.redstone.setOutput(sides.up, 0)
    end
    local count = 0
    local max_lights_on = math.random(max_lights_on_min, max_lights_on_max)
    while count < max_lights_on do
    local x = math.random(1,4)
    local y = math.random(1,4)
    if not switchboards[x].isActive(y) then
    switchboards[x].setActive(y, true)
    count = count + 1
    end
    end
    if light_board then
    for l=1,light_board.light_count do
    light_board.setActive(l, false)
    end
    for l=1,light_board.light_count do
    light_board.setActive(l, true)
    os.sleep(1)
    end
    else
    os.sleep(6)
    end
    end

    if light_board then
    for l=1,light_board.light_count do
    light_board.setActive(l, false)
    end
    end
    for x=1,4 do
    for y=1,4 do
    if x==y then
    switchboards[x].setActive(y,true)
    else
    switchboards[x].setActive(y,false)
    end
    end
    end

    event.ignore("interrupted", onInterrupted)
    --eof