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

I need some programming help...

Started by Ignisfactorum, 24 April 2014 - 09:29 PM
Ignisfactorum #1
Posted 24 April 2014 - 11:29 PM
I am a noob, see, and I am just starting with Lua. I am trying to write a basic game program, as I have written on my TI-83.
Here is the code, and I will point out where I run aground.
  • –Translation Notes: The 'x' in the TI version
  • –has become 'b'
  • term.clear()
  • term.setCursorPos(1, 1)
  • local n
  • local m
  • n = 1000
  • m = 30
  • print( "Highest Number: " ..n)
  • print( "Max Turns: " ..m)
  • os.sleep(3)
  • local b
  • local x
  • local a
  • local t
  • a = "nil"
  • x = math.random(1, n)
  • x = b
  • for t = 1, 3, 1 do –Make 3 'm' before finishing
  • term.clear()
  • term.setCursorPos(1, 1)
  • print( b )
  • print( "Turn: "..t )
  • print( "Last Guess: "..a )
  • print( " " )
  • print( "Guess a number!" )
  • a = read()
  • if a==b then
  • term.clear()
  • term.setCursorPos(1, 1)
  • print( "You win!" )
  • os.sleep(1)
  • break
  • end
  • end

I run aground right here:
  • if a==b then

I am going to assume that this is a problem with Lua being unable to check variables against themselves, or me not using the right command, or whatever.


ASIDE:

I also have a problem here:

  • x = b

But that part of the code can be cut out, and I will just not use 'b' at all. The only reason I did use it was to check if variables could carry over data like that. They can't.

Program can be found here: http://pastebin.com/9qSL2S09
axel.codeFail() #2
Posted 25 April 2014 - 02:50 AM

--[[Translation Notes: The 'x' in the TI version
has become 'b'
]]

term.clear()
term.setCursorPos(1, 1)
local n
local m
n = 1000
m = 30
print( "Highest Number: " ..n)
print( "Max Turns: " ..m)
os.sleep(3)
local b
local x
local a
local t
a = "nil"
x =math.random(1, n)
x = b
for t = 1, 3, 1 do --Make 3 'm' before finishing
  term.clear()
  term.setCursorPos(1, 1)
  print( b )
  print( "Turn: "..t )
  print( "Last Guess: "..a )
  print( " " )
  print( "Guess a number!" )
  a =read()
  if a==b then
	term.clear()
	term.setCursorPos(1, 1)
	print( "You win!" )
	os.sleep(1)
	break
  end
end

Just to make it easier to view. :)/>


Edit:
In the line


if a == b then

You are comparing a string to a number. I'd suggest either

if tonumber(a) == b then

or

if a == tostring(B)/> then
Edited on 25 April 2014 - 01:23 AM
axel.codeFail() #3
Posted 25 April 2014 - 02:57 AM
'b' is a nil value.

when you use

x = b

You are setting 'x' equal to 'b', not the other way around.
Change it to

b = x
and all of your problems will be fixed. (Hopefully)


Edit:

Just to clean it up a bit, use

local n = 1000
local m = 30
local b = x
local x = math.random(1, n)
local a = "nil"

And in the case of 't', when you use a for loop, you don't need to predefine the variable used by it.
Edited on 25 April 2014 - 01:05 AM
CCJJSax #4
Posted 25 April 2014 - 07:51 PM
I am a noob, see, and I am just starting with Lua. I am trying to write a basic game program, as I have written on my TI-83.
Here is the code, and I will point out where I run aground.
I am going to assume that this is a problem with Lua being unable to check variables against themselves, or me not using the right command, or whatever.

–snip

But that part of the code can be cut out, and I will just not use 'b' at all. The only reason I did use it was to check if variables could carry over data like that. They can't.

Program can be found here: http://pastebin.com/9qSL2S09

When you post code here, you should use code tags.

you do this by using [ code] 
(no spaces)
 any piece of code 
and if something is especially long then
Spoiler
 your code here 
(no spaces)

it makes it easier and that is how they have been making that white box.


you don't have to put nil in a string. it is a lua keyword that is built in.


a = nil
Edited on 25 April 2014 - 05:54 PM
Ignisfactorum #5
Posted 26 April 2014 - 12:22 AM
Thanks for all of the helpful feedback guys! And again, I apologize for my incredibly stupid mistake of 'x=b'
As has already been stated, I coded this program on my TI originally, (yeah, I'm a pro programmer, I know) and to set 'x' to 'b,' you would have to use: 'x->b.' I happened to just be going fast, and made that simple error.

But yeah, this is honestly the most feedback I have ever gotten from a mod-related issue! I think that ComputerCraft must have the best community of any mods I have ever used!

 Just testing this 
Spoiler
 And this 

By the way, is there anyway that I can do a… label? I will give you an example.
This is how it would work on my TI:
 Label A
Do stuff
Goto A 
I don't know if that is a thing, but that would be great if it was.
Edited on 26 April 2014 - 02:00 AM
axel.codeFail() #6
Posted 28 April 2014 - 03:15 AM
I think what you're talking about is a function. i.e.

function A()
  --Code
end

A()

Whatever Code you put into it, it will perform.
HometownPotato #7
Posted 28 April 2014 - 03:18 AM
He seems to want an infinite loop. (Or goto, which is in only Lua 5.2 and some other languages as well)
axel.codeFail() #8
Posted 28 April 2014 - 03:19 AM
-snip-
Edit:
I misread
Edited on 28 April 2014 - 01:20 AM
CCJJSax #9
Posted 28 April 2014 - 05:26 PM
an infinite loop is simple.


while true do -- this is the basis for an infinite loop
  print("this is your code section") -- this will specifically print out "this is your code section" several times
  sleep(.5) -- it's important to use sleeps
end -- ends the while loop