Randomness maker

Discussion in 'ComputerCraft Programming' started by gknova61, 18 January 2013.

  1. gknova61

    gknova61 Farbes Lover

    Joined:
    17 March 2012
    Messages:
    1.238
    Likes Received:
    350
    Just a demonstration of the new touchscreen monitors in ComputerCraft! Needs an advanced computer with at least 1 advanced monitor hooked up to it, and the HTTP API optionally enabled for more phrases!

    Screenshot:

    [​IMG]

    Instructions:
    Nothin' to it, really! Just hookup at least 1 advanced monitor to an advanced computer, run the program, and start right clicking the monitor!

    TIP: You can clear the monitor easily by giving the computer redstone
    Code:
    --Forums messes up my indentations :P
    if not term.isColor() then
      error("You need an advanced computer!")
    end
    local function readLines(sPath)
      local file = fs.open(sPath, "r")
      if file then
            local tLines = {}
            local sLine = file.readLine()
            while sLine do
              table.insert(tLines, sLine)
              sLine = file.readLine()
            end
            file.close()
            return tLines
      end
      return nil
    end
     
    local sFile = "phrases"
    if http then
    fs.delete( shell.resolve(sFile) )
    print("Fetching phrase list...")
    local response = http.get("http://pastebin.com/raw.php?i=hTQ6Z2H5")
    if response then
      local sResponse = response.readAll()
    response.close()
    local file = fs.open( shell.resolve(sFile), "w" )
    file.write( sResponse )
    file.close()
    phrases = readLines(shell.resolve(sFile))
    print("Have fun!")
    else
    error("Check your internet connection")
    end
    else
    print("HTTP Not enabled. Oh wellz")
    phrases = {
    "i am asian",
    "you like eduardo",
    "skcraft rocks",
    "your mom hates me :C",
    "this is just annoying",
    "Luv it",
    "you wuv cameron walker!",
    "<3",
    "need moar clicks!",
    "do that again bby",
    "ooooooohhhhh",
    "ahhhhhhhhhh",
    ":P",
    ":(",
    "hit me bby one more time",
    "OMG",
    "i luv u <3",
    "happy happy oh wait...",
    "happy bday?",
    "merry christmas",
    "happy new year",
    "Hi person",
    "hey there",
    "stop it, motherfucker",
    "nuuu",
    "tell sk to enable http",
    "meet vincent!",
    "ZOMG"
    }
    end
     
    colors = {
    "red",
    "blue",
    "orange",
    "magenta",
    "green",
    "brown",
    "purple",
    "cyan",
    "pink",
    "lime",
    "yellow"
    }
     
    math.randomseed(os.time() * os.clock() / os.getComputerID() * 5 + 7)
     
    for k,v in pairs(rs.getSides()) do
    if peripheral.getType(v) == "monitor" then
    monitor = peripheral.wrap(v)
    found = true
    break
    end
    end
    if found then
    else
    error("You need a monitor hooked up to this computer!")
    end
     
    while true do
    ev = {os.pullEventRaw()}
    if ev[1] == "monitor_touch" then
    monitor.setCursorPos(ev[3],ev[4])
    monitor.setTextColour(colours[colors[math.random(1,#colors)]])
    monitor.write(phrases[math.random(1,#phrases)])
    elseif ev[1] == "char" then
    if ev[2]:lower() == "c" then
    term.clear()
    term.setCursorPos(1,1)
    monitor.clear()
    monitor.setCursorPos(1,1)
    elseif ev[2]:lower() == "t" then
    monitor.clear()
    monitor.setCursorPos(1,1)
    term.clear()
    term.setCursorPos(1,1)
    term.setTextColour(colours.red)
    print("Terminated")
    term.setTextColour(colours.white)
    return "terminated"
    end
    elseif ev[1] == "mouse_click" or ev[1] == "mouse_drag" or ev[1] == "mouse_scroll" then
    term.setCursorPos(ev[3],ev[4])
    term.setTextColour(colours[colors[math.random(1,#colors)]])
    term.write(phrases[math.random(1,#phrases)])
    elseif ev[1] == "redstone" then
    for k,v in pairs(rs.getSides()) do
    if rs.getInput(v) then
    term.clear()
    term.setCursorPos(1,1)
    monitor.clear()
    monitor.setCursorPos(1,1)
    break
    end
    end
    end
    end
    
    Feel free to suggest more phrases below!
     
    Ali likes this.
  2. Chaeris

    Chaeris Active Member

    Joined:
    8 March 2012
    Messages:
    766
    Likes Received:
    89
    Could you fix the bug causing messages to overwrite on other messages ?
    I'm not planning to use the code, but it is a suggestion for you to get a "better" program :D
     
  3. Neonbeta

    Neonbeta Person who did stuff and things

    Joined:
    2 March 2012
    Messages:
    2.603
    Likes Received:
    757
    It's just a demonstration program, there's really no need to fix any bugs since it has no major use.
     
  4. gknova61

    gknova61 Farbes Lover

    Joined:
    17 March 2012
    Messages:
    1.238
    Likes Received:
    350
    It's kinda ment to do that so it doesnt fill up the screen so fast.