Jump to content




Need help with a VERY advanced program using modems

help lua

  • You cannot reply to this topic
9 replies to this topic

#1 AntonLVerd

  • New Members
  • 2 posts

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

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.)

#2 CometWolf

  • Members
  • 1,283 posts

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 by CometWolf, 06 March 2015 - 06:13 PM.


#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

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


#4 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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

#5 The_Cat

  • Members
  • 119 posts

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. ")


#6 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 07 March 2015 - 01:53 AM

View PostThe_Cat, on 06 March 2015 - 09:02 PM, said:

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 by HPWebcamAble, 08 March 2015 - 11:53 PM.


#7 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 07 March 2015 - 04:47 AM

View PostHPWebcamAble, on 07 March 2015 - 01:53 AM, said:

View PostThe_Cat, on 06 March 2015 - 09:02 PM, said:

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

#8 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 07 March 2015 - 06:17 AM

View PostDragon53535, on 07 March 2015 - 04:47 AM, said:

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

#9 The_Cat

  • Members
  • 119 posts

Posted 07 March 2015 - 08:17 PM

View PostHPWebcamAble, on 07 March 2015 - 01:53 AM, said:

View PostThe_Cat, on 06 March 2015 - 09:02 PM, said:

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.

#10 AntonLVerd

  • New Members
  • 2 posts

Posted 08 March 2015 - 06:24 PM

View PostMKlegoman357, on 06 March 2015 - 06:18 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users