Jump to content


Xab's Content

There have been 8 items by Xab (Search limited from 29-March 23)


By content type

See this member's

Sort by                Order  

#196827 [recipe] Getting User Input

Posted by Xab on 23 October 2014 - 08:24 PM in APIs and Utilities

Here's a pretty neat recipe for when you want to get the user's input for something.
If the user enters the wrong kind of input in the promptt function, it will erase their entered text and lets the user try again, without starting a new line. It also provides a default value for a user: pressing enter will use the default value, or typing anything else will let the user enter their own value. Finally, I've included a function - promptYesNo - for answering simple yes/no questions in a similar way as promptt.

Spoiler

pastebin: pastebin get u5K7DCXi



#196476 Turtle Walker

Posted by Xab on 19 October 2014 - 01:48 AM in Turtle Programs

It just goes forward and back n times. Nice presentation though :rolleyes:



#196471 Patrol

Posted by Xab on 19 October 2014 - 12:52 AM in Turtle Programs

doing turtle.forward() turtle.attack() 6 times is the same as
for i=1,6 do
  turtle.forward()
  turtle.attack()
end
Which makes it easier to see what the code is doing and easier to configure. Say you wanted the turtle to patrol a 1000 square area, all you'd have to do is change the 6 to 1000. See if you can write code to allow the user to adjust the length of the patrol area.



#196470 Mining Turtle that mines just like wyld

Posted by Xab on 19 October 2014 - 12:32 AM in Turtle Programs

Works pretty well. Now it needs a way refuel when it's low/empty, and to go back to its starting position.
I notice you have a lot of "while not turtle.forward() do turtle.dig() end" in your code. As a general rule, if you have 2 or more places where your code repeats, it's probably a good idea to turn it into a function.



#184918 Yet Another Door Lock (Y.A.D.L.)

Posted by Xab on 20 June 2014 - 08:58 PM in Programs

you must wait for forgiveness
lol'd



#184897 [HELP !] turtle follows a path

Posted by Xab on 20 June 2014 - 06:47 PM in Ask a Pro

The API code looks fine to me, unless I'm missing something. Did you want to change variable "stop" outside of the API? If so, you'll need a function to do it, ie: function stop() stop = true end



#184874 Fake Name (testing os.pullevent)

Posted by Xab on 20 June 2014 - 05:14 PM in Programs

Wrote a fun program to get used to using os.pullevent, and to make a prompt with a bit of personality :)
--[[
fakeName
Prompts user for name, but writes out a different name.
--]]

function prompt(message)
  message = message or ""
  write(tostring(message))
  return read()
end

function promptYesNo(message)
  local value = string.upper(prompt(message))
  if value == "Y" or value == "YES" then return true end
  if value == "N" or value == "NO" then return false end
  return nil
end

function main()
  write("What is your name?: ")
  local fName = "Doofus"		  -- fake name
  local rName = ""				-- real name
  local fnIndex = 0			   -- fake name letter index location
  local fnLen = string.len(fName) -- fake name length
  while true do
	local event, v = os.pullEvent()
	if event == "key" then
	  if v == keys.enter and string.len(rName) > 0 then break end  
	elseif event == "char" then
	  rName = rName..v
	  fnIndex = fnIndex + 1
	  if fnIndex <= fnLen then
		write(string.sub(fName,fnIndex,fnIndex))
	  end
	end
  end
  -- finish writing fake name
  if fnIndex < fnLen then
	write(string.sub(fName,fnIndex+1,fnLen))
	sleep(1)
  end

  if not promptYesNo("\nHaha, just kidding. Is your name "..tostring(rName).."?(y/n): ") then
	rName = prompt("What is your name?: ")
  end
  print("Nice to meet you "..rName..".")
end

main()



#168957 Password Protected Door (Members only)

Posted by Xab on 20 March 2014 - 04:46 AM in Turtle Programs

First post :)
Here the first program I've worked on for your reading pleasure. User enters their name and password, and the door opens if they're in the member/leader list. Users in the blacklist get something else..
pastebin: http://pastebin.com/qF2H6AUM
Spoiler
Note: You can still ctrl+t to terminate the program, so don't trust your valuables with it just yet.