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

Need help with a VERY advanced program using modems

Started by AntonLVerd, 06 March 2015 - 12:42 PM
AntonLVerd #1
Posted 06 March 2015 - 01:42 PM
So, the principal of my school gave me an assignment to make a math exercise in minecraft and decided to make it in Computercraft with extremely limited knowledge of lua. So i found this calculator made by IMMIBIS and my plan was for the main computer to generate two random numbers, take the numbers and randomly pick either a +, -, / or * and send it to the calculator which would solve it and send the answer back to the main computer along with the random numbers and print them onto the monitor which would ask for the answer and when an answer is entered it would see if the entered number and the answer match. But with limited knowledge of how this works im stuck. (T.T)

CALCULATOR CODE:
http://pastebin.com/C68PEiec

I want to edit this code (^) so it can recieve information from a modem and enter it as if it was a player and also take the answer and send it back to the computer whose code this is.
(BTW: The code below here is pretty much the extent of my knowledge so i need all the help i can get)

MAIN COMPUTER CODE:

Spoiler

local m = peripheral.wrap("left")
local m2 = peripheral.wrap("right")
local modem = peripheral.wrap("back")
redstone.setOutput("top", false)
redstone.setOutput("bottom", true)
redstone.setOutput("right", false)
sleep(3)
m.setTextColor(colors.white)
m.setBackgroundColor(colors.black)
m.clear()
redstone.setOutput("right", true)
redstone.setOutput("top", true)
redstone.setOutput("bottom", false)
sleep(2)
m.setCursorPos(14,2)
m.write("Hej")
  --"Hi"
  sleep(2)
  m.setCursorPos(2,5)
  m.write("Välkommen till Matte-Datorn")
  --"Welcome to the Math PC"
	sleep(2)
	m.setCursorPos(1,9)
	m.write("Av ****** ********* (**, ***)")
	--("By ****** ********* (**, ***)")
--The stars indicate personal information (Name, (Class, School) and it isnt important.
	sleep(7)
	  m.clear()
	  m.setCursorPos(12,2)
	  m.write("Ditt Mål:")
	  --"Your goal:"
	  sleep(2)
		m.setCursorPos(7,6)
		m.write("Svara rätt på så")
		--"Try to answer"
		m.setCursorPos(3,7)
		m.write("många frågor som möjligt!")
		--"As many questions right as possible"
		  sleep(6)
		  m.clear()
		  m.setCursorPos(2,2)
		  m.write("Så...")
		  --"So..."
		  m.setCursorPos(7,2)
		  sleep(3)
		  m.write(" Vilken klass går du i?")
		  --"Which class are you in"
		  sleep(1)
		  m.setTextColor(colors.gray)
		  m.setCursorPos(2,2)
		  m.write("Så...")
		  --"So..."
		  m.setCursorPos(7,2)
		  m.write(" Vilken klass går du i?")
		  --"Which class are you in"
			m.setCursorPos(14,5)
			sleep(1)
			m.setTextColor(colors.green)
			m.write(" 6 ")
			  sleep(1)
			  m.setCursorPos(14,7)
			  m.setTextColor(colors.purple)
			  m.write(" 7 ")
				sleep(1)
				m.setCursorPos(14,9)
				m.setTextColor(colors.orange)
				m.write(" 8 ")
				  sleep(1)
				  m.setCursorPos(14,11)
				  m.setTextColor(colors.pink)
				  m.write(" 9 ")
					m.clear()
					  m.setBackgroundColor(colors.white)
						m.clear()
						m.setCursorPos(2,2)
						m.setTextColor(colors.gray)
					  --m.setBackgroundColor(colors.lightGray)
						m.write("Så...")
						--Same as earlier
						m.setCursorPos(7,2)
						m.write(" Vilken klass går du i?")
						--Once again, same as earlier
						--m.setBackgroundColor(colors.yellow)
						  m.setCursorPos(14,5)
						  m.setBackgroundColor(colors.black)
						  m.setTextColor(colors.green)
						  m.write(" 6 ")
							m.setCursorPos(14,7)
							m.setTextColor(colors.purple)
							m.write(" 7 ")
							  m.setCursorPos(14,9)
							  m.setTextColor(colors.orange)
							  m.write(" 8 ")
								m.setCursorPos(14,11)
								m.setTextColor(colors.blue)
								m.write(" 9 ")
								
while true do
  redstone.getAnalogInput("back")
  if redstone.getAnalogInput("back") == 15 then
	  shell.run("reboot")
  else
	  shell.run("menu1")
  end
end

This code so far only has one purpose and its to print text onto a 3x2 advanced monitor since im stuck on the send and recieve information process

Here are some things i can say so you wont ask:

1, The "menu1" program is just a peripheral wrap and i dont remember what it was intended to be.

2. I know i can break the code instead of using shell.run to reboot the system but i didn't know it at the time and i dont really care.

(Also i will keep track of this thread so please ask or answer anything you want.)
CometWolf #2
Posted 06 March 2015 - 06:11 PM
Any reason you need to even use IMMBIS' calculator for this? It's not like the calculation part of his program is some magical non-Lua component.
Edited on 06 March 2015 - 05:13 PM
MKlegoman357 #3
Posted 06 March 2015 - 06:18 PM
It looks like you don't really know a lot about programming languages, do you :P/>? Every programming language has built-in math functions. For example:


local a = 5
local b = 8

print( a + b ) --> 13
print( a * b ) --> 40

local ans = b / 2

print( ans ) --> 4
TheOddByte #4
Posted 06 March 2015 - 06:33 PM
I'm wondering, why do you even need rednet and modems for this? If you're trying to create a math exercise you could easily do it with one computer.
And as show above you can see that you don't need to use any special code for calculation.

You could easily wrap up a simple math exercise in one single program using a loop

while true do
    term.clear()
    term.setCursorPos( 1, 1 )
    term.write( "Första numret: " )
    local nummer1 = tonumber( read() ) --# Get the input and convert it to a number
    print( "" )
    term.write( "Andra numret: " )
    local nummer2 = tonumber( read() ) --# Same here
    local resultat   = nummer1 + nummer2
    print( "\nResultat: " .. nummer1 .. " + " .. nummer2 .. " = " .. resultat )
    os.pullEvent( "key" ) --# Pause the loop, so it doesn't clear the screen and start all over again
end
This is just a simple example, you can do much more advanced stuff, but I hope you understood something from it atleast :P/>
The_Cat #5
Posted 06 March 2015 - 09:02 PM
Hi, so i tried to make what you said but my program can be easily broken but just entering a letter or space.
I made this in about 10 minutes: http://pastebin.com/xPmgHT7A
Hope It helps! (Towards the end program :)/> )

-- Math Program test
function genRandomNum() --# Random Function
num1 = math.random(10)
num2 = math.random(10)
sign = math.random(4)
print("Question: "..questionOn) --# Prints current question the user is on
if sign == 1 then --# Loop for getting random sign
  answer = num1 + num2
  print("Whats "..num1.." + "..num2.." ?")
elseif sign == 2 then
  answer = num1 - num2
  print("Whats "..num1.." - "..num2.." ?")
elseif sign == 3 then
  answer = num1 / num2
  print("Whats "..num1.." / "..num2.." ?")
elseif sign == 4 then
  answer = num1 * num2
  print("Whats "..num1.." * "..num2.." ?")
end

end
r = 0
w = 0
write("How many question do you want to be asked? ")
numQuestion = tonumber(io.read()) --# Reads how many questions the user wants
term.clear()
for x = 1, numQuestion do --# Loops until reaches the users amount of questions
questionOn = x
term.clear()
term.setCursorPos(1,1)
genRandomNum()
term.setCursorPos(1, 3)

write("Answer: ")

userAnswer = tonumber(io.read())
if userAnswer == answer then
  r = r + 1 --# Adds to questions correct
else
  w = w + 1 --# Adds to questions incorrect
end
end
--[[ Prints final results when program has finished ]]--
term.clear()
term.setCursorPos(1,1)
print("Total Questions: "..numQuestion)
print("You got "..r.." Correct!")
print("You got "..w.." Wrong. ")
HPWebcamAble #6
Posted 07 March 2015 - 01:53 AM
my program can be easily broken but just entering a letter or space.

The BEST thing to do is to only listen for numbers with os.pullEvent(), but it might be a bit advanced for you.

But that's Ok, you can still do it with just read() (Which is the same as io.read())

Instead of

userAnswer = tonumber(io.read())

Try this:

repeat
  userAnswer = tonumber(read())
until userAnswer

'until userAnswer' is the same as 'until userAnswer ~= nil' by the way

Other than that, it's a really good first try :)/>
Edited on 08 March 2015 - 10:53 PM
Dragon53535 #7
Posted 07 March 2015 - 04:47 AM
my program can be easily broken but just entering a letter or space.

The BEST thing to do is to only listen for numbers with os.pullEvent(), but it might be a bit advanced for you.

But that's Ok, you can still do it with just read() (Which is the same as io.read())

Instead of

userAnswer = tonumber(io.read())

Try this:

repeat
  local userAnswer = tonumber(read())
until userAnswer

'until userAnswer' is the same as 'until userAnswer ~= nil' by the way

Other than that, it's a really good first try :)/>
Ummh, that code isn't going to work exactly.

local userAnswer
repeat
  userAnswer = tonumber(read())
until userAnswer
you can't have userAnswer be local to the loop or else it WILL be nil after the loop even if you enter a number
HPWebcamAble #8
Posted 07 March 2015 - 06:17 AM
you can't have userAnswer be local to the loop or else it WILL be nil after the loop even if you enter a number

derp, of course
The_Cat #9
Posted 07 March 2015 - 08:17 PM
my program can be easily broken but just entering a letter or space.
The BEST thing to do is to only listen for numbers with os.pullEvent(), but it might be a bit advanced for you.

os.pullEvent() isnt that advanced i have used it plenty of times ;)/>

I just didnt think of that :D/> so good call, Thanks.
AntonLVerd #10
Posted 08 March 2015 - 06:24 PM
It looks like you don't really know a lot about programming languages, do you :P/>? Every programming language has built-in math functions. For example:
I may have mentioned in the post that im bad at this