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

[Old Topic] My First Program

Started by tonkku107, 07 May 2013 - 08:11 AM
tonkku107 #1
Posted 07 May 2013 - 10:11 AM
This is a old topic! New topic here: http://www.computercraft.info/forums2/index.php?/topic/12847-tonkku107s-programs/#PassLock
Old Topic in this spoiler:
SpoilerSo, here is my only, and the first program i made. It's called PassLock (sorry i'm terrible at naming, ideas?) and it's an door password lock.

>lock
Spoiler

--[[ PassLock v.2.2 by tonkku107 ]]--

os.pullEvent = os.pullEventRaw

rednet.open("top")

local password = "Pass"
local opentime = 5
local myId = os.computerID()
local done = false
local serverId = 7
local ver = 2.2

function gui()
term.clear()
term.setCursorPos(1,1)
print("PassLock v."..ver)
term.setCursorPos(1,3)
end

while done == false do
gui()
write("Password: ")
local input = read("*")
if input == "AdminPass" then
  gui()
  textutils.slowPrint("Access Granted!")
  sleep(2)
  rednet.close("top")
  term.clear()
  term.setCursorPos(1,1)
  print("CraftOS 1.5")
  done = true
else
  rednet.send(serverId,input)
  senderId,message,distance = rednet.receive(5)
  if senderId == serverId then
   if message == "Valid" then
	gui()
print("Password Correct!")
rs.setOutput("back",true)
sleep(5)
rs.setOutput("back",false)
os.shutdown()
   else
	gui()
print("Password Incorrect!")
sleep(2)
os.shutdown()
   end
end
end
end

So it uses an password server on computer 7 (for me) and that was found from the internet.

>lock (without server)
Spoiler

os.pullEvent = os.pullEventRaw

local password = "Pass"
local opentime = 5
local done = false
local ver = 2.0

function gui()
term.clear()
term.setCursorPos(1,1)
print("PassLock v."..ver)
term.setCursorPos(1,3)
end

while done == false do
gui()
write("Password: ")
local input = read("*")
if input == password then
  term.clear()
  term.setCursorPos(1,1)
  print("PassLock v. 2,0")
  term.setCursorPos(1,3)
  print("Password Correct!")
  rs.setOutput("back",true)
  sleep(opentime)
  rs.setOutput("back",false)
  os.shutdown()
elseif input == "AdminPass" then
  gui()
  print("Access Granted!")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  done = true
else
  gui()
  print("Password Incorrect!")
  sleep(2)
  os.shutdown()
end
end

Then i have an exit script because buttons and plessure plates are for noobs

>door
Spoiler

os.pullEvent = os.pullEventRaw

local door = "exit"
local serverId = 7
local myId = os.computerID()
local ver = 2.2
local done = false

function gui()
term.clear()
term.setCursorPos(1,1)
print("PassLock v."..ver)
term.setCursorPos(1,3)
end

rednet.open("top")

while done == false do
gui()
write("Press enter to open the door...")
io.read()
rednet.send(serverId,door)
   senderId,message,distance = rednet.receive(5)
   if senderId == serverId then
	if message == "Valid" then
	 gui()
	 write("Door open!")
	 rs.setOutput("back",true)
	 sleep(5)
	 rs.setOutput("back",false)
	 os.shutdown()
	else
	os.shutdown()
	end
   end
end

I also need help for getting a secret button (combo) to get to the command mode. I have a topic for that too: http://www.computerc...-key-detection/

I'm kinda noob in lua so if you have any tips that could make this better, tell me. Also tell me if this is a good code or not ;)/>
svdragster #2
Posted 07 May 2013 - 03:08 PM
Cool, but I think it's supposed to go into the "Programs" section?
H4X0RZ #3
Posted 07 May 2013 - 04:13 PM
Wow, this is your first program?

My first has looked like this:

print("Hello")
:D/>
diegodan1893 #4
Posted 07 May 2013 - 04:17 PM
Wow, this is your first program?

My first has looked like this:

print("Hello")
:D/>

Mine was:

print("Hello World!!!")
Well that was my first program in CC, my first program ever was:

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}
tonkku107 #5
Posted 08 May 2013 - 12:15 PM
Cool, but I think it's supposed to go into the "Programs" section?
whoops
Wow, this is your first program?

My first has looked like this:

print("Hello")
:D/>
well that was the first lua code i knew
Wow, this is your first program?

My first has looked like this:

print("Hello")
:D/>

Mine was:

print("Hello World!!!")
Well that was my first program in CC, my first program ever was:

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}
really?
tonkku107 #6
Posted 11 May 2013 - 08:06 AM
And you can make it only accessable from the computer with just little redstone!
http://tonkunservu.netau.net/Files/CC/CCSafe/
InputUsername #7
Posted 12 May 2013 - 03:07 AM
For a first program, a door lock with server is really nice! As long as you don't keep publishing door locks :)/>

My first-ever Lua program was an attempt at a calculator, that allowed you to calculate something once and then crashed the entire world.
M4sh3dP0t4t03 #8
Posted 12 May 2013 - 03:29 AM
My first program was
print("Hello World!)
and my first program in Lua was
print("Hello World")
Do you know what programming language i used for my first program?(Tip: it was not Lua)
1lann #9
Posted 12 May 2013 - 04:14 AM
My first program was
print("Hello World!)
and my first program in Lua was
print("Hello World")
Do you know what programming language i used for my first program?(Tip: it was not Lua)
Well it looked like it would have resulted in a syntax error since you missed that quote at the end. I'm assuming python.
M4sh3dP0t4t03 #10
Posted 12 May 2013 - 04:52 AM
My first program was
print("Hello World!)
and my first program in Lua was
print("Hello World")
Do you know what programming language i used for my first program?(Tip: it was not Lua)
Well it looked like it would have resulted in a syntax error since you missed that quote at the end. I'm assuming python.
Nope it's BASIC. But in python it would probably work the same, if I didn't forgot the quote.
NeptunasLT #11
Posted 12 May 2013 - 05:11 AM
not bad i like it.
Download? :D/>
cogilv25 #12
Posted 12 May 2013 - 07:32 AM
Wow, this is your first program?

My first has looked like this:

print("Hello")
:D/>

Mine was:

print("Hello World!!!")
Well that was my first program in CC, my first program ever was:

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}

Yay C++ my favorite of horribly difficult languages :D/>
tonkku107 #13
Posted 14 May 2013 - 08:23 AM
not bad i like it.
Download? :D/>
Download??!!??
You mean pastebin?
deleted #14
Posted 19 May 2013 - 11:47 AM

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}
Just a saying at first when i looked at it i thought it was Java from using eclipse but i realized that #include was not really java.. It was import. Also int (variable); was java. But its different. And personaly i thought most other languages for programing didnt have constructors. Also last thing what does all that code mean? So thats just what i was saying because i use java a bit more than i do lua.
Also i saw someone say it was c++
improvshark #15
Posted 19 May 2013 - 04:27 PM
very nice
jesusthekiller #16
Posted 19 May 2013 - 05:32 PM

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}
Just a saying at first when i looked at it i thought it was Java from using eclipse but i realized that #include was not really java.. It was import. Also int (variable); was java. But its different. And personaly i thought most other languages for programing didnt have constructors. Also last thing what does all that code mean? So thats just what i was saying because i use java a bit more than i do lua.
Also i saw someone say it was c++

It is C++ indeed, why it looks like java? Java's syntax is based on C++ syntax.

And it just print "Hola Mundo" to console :3


My first program was

@echo off
echo It works!
:P/>
deleted #17
Posted 20 May 2013 - 09:47 AM

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}
Just a saying at first when i looked at it i thought it was Java from using eclipse but i realized that #include was not really java.. It was import. Also int (variable); was java. But its different. And personaly i thought most other languages for programing didnt have constructors. Also last thing what does all that code mean? So thats just what i was saying because i use java a bit more than i do lua.
Also i saw someone say it was c++

It is C++ indeed, why it looks like java? Java's syntax is based on C++ syntax.

And it just print "Hola Mundo" to console :3


My first program was

@echo off
echo It works!
:P/>/>
Well i though it was java because you have your constructors. Because I'm not that much of a c++ guy. Its just too hard to understand sometimes. Thats why i don't do it, so i use Lua and Java. And thought it was that.
diegodan1893 #18
Posted 20 May 2013 - 11:36 AM
Just a saying at first when i looked at it i thought it was Java from using eclipse but i realized that #include was not really java.. It was import. Also int (variable); was java. But its different. And personaly i thought most other languages for programing didnt have constructors. Also last thing what does all that code mean? So thats just what i was saying because i use java a bit more than i do lua.
Also i saw someone say it was c++

Yes, as jesusthekiller said it's C++. It just prints "Hola Mundo" (hello world in spanish) to the console.
M4sh3dP0t4t03 #19
Posted 20 May 2013 - 12:26 PM
Wow, this is your first program?

My first has looked like this:

print("Hello")
:D/>/>

Mine was:

print("Hello World!!!")
Well that was my first program in CC, my first program ever was:

#include <iostream>
using namespace std;
int main() {
	cout << "Hola Mundo" << endl;
	return 0;
}
But why are you returning 0? In the main function it does it automatically.
theoriginalbit #20
Posted 20 May 2013 - 01:48 PM
But why are you returning 0? In the main function it does it automatically.
Because main states it's going to return an int, and it doesn't actually return a number automatically. You would get a compiler error if that line wasn't there, it has to return something. Normally a return result of 0 tells the OS the program has finished successfully, any other number means that it has not finished successfully.

Here is a link on the C++ forum asking the question of why return 0?
M4sh3dP0t4t03 #21
Posted 20 May 2013 - 01:50 PM
But why are you returning 0? In the main function it does it automatically.
Because main states it's going to return an int, and it doesn't actually return a number automatically. You would get a compiler error if that line wasn't there, it has to return something. Normally a return result of 0 tells the OS the program has finished successfully, any other number means that it has not finished successfully.

Here is a link on the C++ forum asking the question of why return 0?
Yes, but with all compilers I used it returns 0 automaticly if it's in the main function
theoriginalbit #22
Posted 20 May 2013 - 01:55 PM
Yes, but with all compilers I used it returns 0 automaticly
I've never had a compiler automatically do it for me. All my compilers add the line of code in, and if i remove it show an error.
cogilv25 #23
Posted 21 May 2013 - 06:35 PM
Yes, but with all compilers I used it returns 0 automaticly
I've never had a compiler automatically do it for me. All my compilers add the line of code in, and if i remove it show an error.

Yes that's the same with me you have to return 0.

I've used: VC++ 2010, Dev-C++ and Code::Blocks and that is how they have all worked… what do you use KingOfNoobs?
tonkku107 #24
Posted 23 May 2013 - 11:59 AM
Yes, but with all compilers I used it returns 0 automaticly
I've never had a compiler automatically do it for me. All my compilers add the line of code in, and if i remove it show an error.
Yes that's the same with me you have to return 0.
What's wrong with return 0?
theoriginalbit #25
Posted 23 May 2013 - 12:11 PM
What's wrong with return 0?
Nothing, KingOfNoobs has asked why it was there.
zabouth #26
Posted 27 May 2013 - 06:28 AM
It would be a good idea to add a password to reset os.pullEvent so you can access the system using CTRL-T if you need to. Something like this


Disable CTRL-T

local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw



Re-enable CTRL-T


os.pullEvent = pullEvent
alakazard12 #27
Posted 27 May 2013 - 08:52 AM
But why are you returning 0? In the main function it does it automatically.
Because main states it's going to return an int, and it doesn't actually return a number automatically. You would get a compiler error if that line wasn't there, it has to return something. Normally a return result of 0 tells the OS the program has finished successfully, any other number means that it has not finished successfully.

Here is a link on the C++ forum asking the question of why return 0?

Actually, with the lastest updates of C++, returning anything is no longer needed.

This topic is the most off-topic post I've ever read :P/>/>.

On topic:
Nice for your first program, you're one of the few who actually lock terminating. (Although the way you did it would make terminate never work in any programs. Use zabouth's suggestion.)
tonkku107 #28
Posted 27 May 2013 - 12:59 PM
It would be a good idea to add a password to reset os.pullEvent so you can access the system using CTRL-T if you need to. Something like this


Disable CTRL-T

local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw



Re-enable CTRL-T


os.pullEvent = pullEvent
On topic:
Nice for your first program, you're one of the few who actually lock terminating. (Although the way you did it would make terminate never work in any programs. Use zabouth's suggestion.)
There is a "AdminPass" in it so if you input that, you get to the terminal. Try it
BTW this is a old topic. Main post has everything you need (link)!