Author Topic: [Lua] Mancala  (Read 19580 times)

0 Members and 2 Guests are viewing this topic.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #15 on: January 09, 2012, 08:41:12 pm »
Hmm... Should I make a poll or just keep all the rules that are there?

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #16 on: January 09, 2012, 08:56:33 pm »
Take a poll on taking a poll.  ;D I think just keeping the rules should be fine.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #17 on: January 09, 2012, 09:04:14 pm »
Ya I'll keep them, and probably add an optioins menu :D

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #18 on: January 11, 2012, 07:21:18 pm »
I'd have 2 player mode somewhat close to finished if it wasn't for errors...

Here is in the beginning
Code: [Select]
for a=1, 14 do
for b=1, 3 do
if a~=7 and a~=14 then
coord[a*3+b*3-5]=a
coord[a*3+b*3-4]=math.random(0,16)
coord[a*3+b*3-3]=math.random(0,14)
else
coord[a*3+b*3-5]=a
--coord[a*3+b*3-4] and coord[a*3+b*3-3 will be nil.
end
end
end


And here is where the error occurs

Code: [Select]
for a=1, 42 do
if coord[a*3-2]~=7 and coord[a*3-2]~=14 then
if coord[a*3-2]>7 then
gc:fillArc(coord[a*3-2]*38+coord[a*3-1],coord[a*3]+49,10,10,0,360)
-- (above) attempt to index global 'coord' (a nil value)
else
gc:fillArc(coord[a*3-2]*38+coord[a*3-1],coord[a*3]+141,10,10,0,360)
end
-- moar stuffs

Coord shouldn't be nil. The only times there will be a nil value in that is supposed to be only when coord[a*3-2] is equal to 7 or 14. That shouldn't happen because it is under
Code: [Select]
if coord[a*3-2]~=7 and coord[a*3-2]~=14 then Unless coord[a*3+b*3-5] (from first code) isn't the same as coord[a*3-2]...
« Last Edit: January 11, 2012, 07:22:07 pm by epic7 »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #19 on: January 11, 2012, 09:28:33 pm »
You may need a line to initialize coord as a table before you can index it in the for loops.

Try adding this at the beginning:
Code: [Select]
coord = {}
Hopefully an easy fix. :)
Userbars

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #20 on: January 11, 2012, 09:34:51 pm »
I already have that D:

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #21 on: January 11, 2012, 09:42:25 pm »
Hmmm... That's usually the problem with "attempt to index global 'coord' (a nil value)."

If everything is spelled and capitalized correctly and the coord = {} is in the correct spot then the error might be in a different part of the code.

Is coord defined in a function? If so, on.paint may be called before coord is initialized.
« Last Edit: January 11, 2012, 09:47:06 pm by 3rik »
Userbars

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #22 on: January 11, 2012, 09:50:50 pm »
Its defined inside on.create()

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #23 on: January 11, 2012, 09:56:43 pm »
Are there any local coord variables?
Userbars

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #24 on: January 11, 2012, 10:00:46 pm »
I don't really know what that means.. So Im gonna say no :P

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #25 on: January 11, 2012, 10:23:37 pm »
I always have problems with on.create(). I never use it, but I think it's just my fault.

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #26 on: January 11, 2012, 10:27:25 pm »
I noticed that some values in the coord table are being overwritten. Like when a=1 and b=2 coord[4]==1 but when a=2 and b=1 coord[4]==2.

I'm running out of ideas for what is causing this error without seeing more code.

Local variables are variables that are initialized with the word local in front of them. They only last until the end of the block they are created in. They can share the name of global variables. A description of this  can be found here: http://www.lua.org/pil/4.2.html
Userbars

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #27 on: January 11, 2012, 10:28:43 pm »
@cyan Idk, Nick edited a basic program of mine and added on create. I just assumed that was the right way :p

@3ric I guess that means I don't have any local vars. And with the overwriting, you're probably right :o
« Last Edit: January 11, 2012, 10:31:13 pm by epic7 »

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: [Lua] Mancala
« Reply #28 on: January 12, 2012, 01:18:04 am »
it doesn't really amtter if it's in on.create(), it doesn't have to be there, if you just initialize it it should be working..

the only reason for failure here might be that the value you try to create, the coord[a*3-2] or any other assignment, doesn't exist in the table itself.
The first time you do the loop, a is 1, so you get coord[1], but if this hasn't been initialized before, it has no value, and you cannot read/write to it..

if you want to add a value to a table, you have to use table.insert(tablename, position) to add it

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: [Lua] Mancala
« Reply #29 on: January 12, 2012, 01:03:29 pm »
I'd have 2 player mode somewhat close to finished if it wasn't for errors...

Here is in the beginning
Code: [Select]
for a=1, 14 do
  for b=1, 3 do
    if a~=7 and a~=14 then
      coord[a*3+b*3-5]=a
      coord[a*3+b*3-4]=math.random(0,16)
      coord[a*3+b*3-3]=math.random(0,14)
    else
      coord[a*3+b*3-5]=a
    --coord[a*3+b*3-4] and coord[a*3+b*3-3 will be nil.
    end
  end
end


And here is where the error occurs

Code: [Select]
for a=1, 42 do
  if coord[a*3-2]~=7 and coord[a*3-2]~=14 then
    if coord[a*3-2]>7 then
      gc:fillArc(coord[a*3-2]*38+coord[a*3-1],coord[a*3]+49,10,10,0,360)
      -- (above) attempt to index global 'coord' (a nil value)
    else
      gc:fillArc(coord[a*3-2]*38+coord[a*3-1],coord[a*3]+141,10,10,0,360)
    end
-- moar stuffs

Coord shouldn't be nil. The only times there will be a nil value in that is supposed to be only when coord[a*3-2] is equal to 7 or 14. That shouldn't happen because it is under
Code: [Select]
if coord[a*3-2]~=7 and coord[a*3-2]~=14 then Unless coord[a*3+b*3-5] (from first code) isn't the same as coord[a*3-2]...

Looks to me that on the first section of the code, you're populating what's inside coord[] more than once in several cases, so you might end with a lower size of an array than you wanted (e.g. when a=2 & b=1 you get on line 4 of the code the same as if a=1 & b=2)

Have you tried initializing the array like this?
Code: [Select]
coord = {}
for i=1, 128 do coord[i]=0 end
The 128 is because 128 > (14*3*3)


So, I think the correct formula should be:
Code: [Select]
for a=1, 14 do
  for b=1, 3 do
    if a%7 ~= 0 then
      coord[9*(a-1)+3*(b-1)+1] = a
      coord[9*(a-1)+3*(b-1)+2] = math.random(0,16)
      coord[9*(a-1)+3*(b-1)+3] = math.random(0,14)
    else
      coord[9*(a-1)+3*(b-1)+1] = a
    end
  end
end
« Last Edit: January 12, 2012, 01:09:30 pm by someone »