Jump to content




I need some programming help...


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

#1 Ignisfactorum

  • New Members
  • 2 posts

Posted 24 April 2014 - 10: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

#2 axel.codeFail()

  • Members
  • 47 posts
  • LocationA bit lost

Posted 25 April 2014 - 01: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 by axel.codeFail(), 25 April 2014 - 02:23 AM.


#3 axel.codeFail()

  • Members
  • 47 posts
  • LocationA bit lost

Posted 25 April 2014 - 01: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 by axel.codeFail(), 25 April 2014 - 02:05 AM.


#4 CCJJSax

  • Members
  • 262 posts

Posted 25 April 2014 - 06:51 PM

View PostIgnisfactorum, on 24 April 2014 - 10:29 PM, said:

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] [ /code] (no spaces)
[ code] any piece of code [ /code]
and if something is especially long then
[ spoiler] [ code] your code here [ /code] [ /spoiler] (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 by CCJJSax, 25 April 2014 - 06:54 PM.


#5 Ignisfactorum

  • New Members
  • 2 posts

Posted 25 April 2014 - 11:22 PM

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

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 by Ignisfactorum, 26 April 2014 - 03:00 AM.


#6 axel.codeFail()

  • Members
  • 47 posts
  • LocationA bit lost

Posted 28 April 2014 - 02: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.

#7 HometownPotato

  • Members
  • 62 posts

Posted 28 April 2014 - 02:18 AM

He seems to want an infinite loop. (Or goto, which is in only Lua 5.2 and some other languages as well)

#8 axel.codeFail()

  • Members
  • 47 posts
  • LocationA bit lost

Posted 28 April 2014 - 02:19 AM

-snip-
Edit:
I misread

Edited by axel.codeFail(), 28 April 2014 - 02:20 AM.


#9 CCJJSax

  • Members
  • 262 posts

Posted 28 April 2014 - 04: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users