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

0 Members and 1 Guest 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 #30 on: January 12, 2012, 04:09:16 pm »
My intent wasn't to overwrite some things in the table. I think what you have is right.
I changed it to multiply a by 9 rather than 3, but still I get an error.

And nick, does that mean that something like
blah={}
blah[1]=1347
won't work? ???

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: [Lua] Mancala
« Reply #31 on: January 12, 2012, 04:16:00 pm »
yep, correct..

for that you have to do table.insert(blah,1,1347)

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #32 on: January 12, 2012, 04:25:47 pm »
I made a test program without table insert and it still worked.

Also, it gets through writing to coord, and the error isn't until it reads from coord.

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #33 on: January 12, 2012, 07:36:02 pm »
blah = {}
blah[1] = 1234

That code with work. I use it in my Hangman program.

I would try just removing the on.create function and just initializing all the variables in front of the rest of the program. I don't know if it'll work, but it doesn't hurt to try.
« Last Edit: January 12, 2012, 07:36:23 pm by cyanophycean314 »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #34 on: January 13, 2012, 12:11:20 am »
Looked through the code a bit more and edited a couple things
Code: [Select]
function on.create()
coord = {}
for a=1, 14 do
for b=1, 3 do
if a~=7 and a~=14 then
coord[a*9+b*3-11]=a --Changed all the 3s in front of the as to 9s also changed the 5, 4, and 3 to 11, 10, and 9 so that it would still start at coord[1]
coord[a*9+b*3-10]=math.random(0,16)
coord[a*9+b*3-9]=math.random(0,14)
else
coord[a*9+b*3-11]=a
end
end
end
end

function on.paint(gc)
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]-7)*38+coord[a*3-1],coord[a*3]+49,10,10,0,360) --subtracted 7 from coord[a*3-2] to line up the pieces
else
gc:fillArc(coord[a*3-2]*38+coord[a*3-1],coord[a*3]+141,10,10,0,360)
end
end
end
end

Using this code, I got the screenshot below. I made a couple extra tweaks to the numbers so that the pieces would line up.

As for adding things to tables, as long as the table has been initialized with a table constructor, any valid key can be paired with any valid value.

Code: [Select]
example = {}
example[1] = 1
example[1337] = 3.141592
example["The Game"] = on.paint
example[on.create] = "Never Gonna Give You Up"
example[true] = false
example.Omnimaga = {1, 2, 3, 4} --Same as example["Omnimaga"] = {1, 2, 3, 4}

Be wary though, #example ~= 1337 until example[2] through example[1336] have non-nil values

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #35 on: January 13, 2012, 04:52:17 pm »
That was the pieces generating code! That looks pretty nice, I don't know about on.create, but personally I never use it.  :P

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: [Lua] Mancala
« Reply #36 on: January 13, 2012, 06:55:21 pm »
@3rik
Why is the order of the variables before the letters? In Math you always see that constants go before variables, so that's a little confusing for me...

I prefer to make the code more readable, so IMO this is better:
Code: [Select]
function on.create()
  local array=0
  coord = {}
  for a=1, 14 do
    for b=1, 3 do
      array = a*9+b*3-11
      if a~=7 and a~=14 then
        coord[array]=a
        coord[array+1]=math.random(0,16)
        coord[array+2]=math.random(0,14)
      else
        coord[array]=a
      end
    end
  end
end

function on.paint(gc)
  local array=0
  for a=1, 42 do
    array = a*3-2
    if coord[array]~=7 and coord[array]~=14 then
      if coord[array]>7 then
        paintSeeds(gc,(coord[array]-7)*38+coord[array+1],coord[array+2]+ 49,10,10,0,360)
      else
        paintSeeds(gc,(coord[array]-0)*38+coord[array+1],coord[array+2]+141,10,10,0,360)
      end
    end
  end
end

function paintSeeds(GC, x, y, width, height, startAngle, angle)
  GC:setColorRGB(0, 0, 0)    --change colors as pleased
  GC:drawArc(x-1, y-1, width+1, height+1, startAngle, angle)
  GC:setColorRGB(128,128,128)  --change colors as pleased
  GC:fillArc(x, y, width, height, startAngle, angle)
end

I've never played this game, so I want to see it soon :)

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #37 on: January 13, 2012, 08:31:09 pm »
I still get the same error with 3ric's edits D:

Even though the a*9 was a something I did really need to add :D

It says

Line 43 is
Code: [Select]
if coord[a*3-2]~=7 and coord[a*3-2]~=14 then Unless the "43:" means something else...

(entire code if anybody can spot anything)
« Last Edit: January 13, 2012, 08:33:23 pm by epic7 »

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #38 on: January 13, 2012, 09:23:52 pm »
As soon as I removed the on.create, it worked for me, screenie:

Just try putting all the information in on.create and move it outside. Please just try it...
« Last Edit: January 13, 2012, 09:24:23 pm by cyanophycean314 »

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #39 on: January 13, 2012, 09:28:00 pm »
Oh it works O.O

Thanks!

Now the coordinate range needs a bit of adjusting :P
Edit: Now they're aligned!

Also on the enter key function it says "stack overflow" What does that mean?"
Edit: I know now and what the problem is

And also, is there an easy way to add something to the end of the table and count the number of things in a table?
« Last Edit: January 13, 2012, 09:56:43 pm by epic7 »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #40 on: January 13, 2012, 11:04:01 pm »
@someone I'd agree that that code looks neater. I was just being lazy and I only made the minimum amount of changes from epic7's code.

@ epic7 and cyanophycean314 I see that you're testing the program in oclua. By the time you paste the code in the program, the event on.create() has already been called so coord was never initialized.
Userbars

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #41 on: January 13, 2012, 11:17:24 pm »
Oh, is on create called once oclua opens up, not the pasted code?
* epic7 goes to fix an error that he calls "function ping-pong" :P

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #42 on: January 13, 2012, 11:51:11 pm »
And also, is there an easy way to add something to the end of the table and count the number of things in a table?

There are a few ways of determining the length of a table.

The most obvious way is using the operator #. It basically starts at 1 and keeps adding 1 until it reaches a nil value. If there are any holes in your table this will give a smaller value.

Another way to get a length is to use table.maxn(). It returns the key with the highest value. In my previous example #example == 1 but table.maxn(example) == 1337. This can also handle decimal numbers in the keys.

One way to count all the elements in a table would be to use a function like this:
Code: [Select]
function length(example)
local index = 0
for _, __ in pairs(example)
index = index + 1
end
return index
end

This will count every element in the table (except nil values, of course). I'll bet this way will be quite slow as the number of elements increases.

If you wanted to add signal to show the end you could do something like this:
Code: [Select]
stuffs = {1, 3, 5, 3, nil, "end"}

function length2(example)
local index = 1
while example[index] ~= "end" do
index = index + 1
end
return index - 1
end
length2(stuffs) would return 5. However, in order to place the "end" into the table, you would have had to know where to put it and if you add or take away any elements you would have to adjust where "end" goes.
« Last Edit: January 13, 2012, 11:52:14 pm by 3rik »
Userbars

Offline hoffa

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 322
  • Rating: +131/-13
    • View Profile
Re: [Lua] Mancala
« Reply #43 on: January 14, 2012, 05:10:33 am »
And also, is there an easy way to add something to the end of the table and count the number of things in a table?
Yeah, table.insert() and the # operator.

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: [Lua] Mancala
« Reply #44 on: January 14, 2012, 05:51:53 am »
table.getn(tablename) is also a good way to retrieve the number of items in a table in some cases of non-standard (?) (or just  customized key/objects) tables. Indeed, for some multi-types tables, # won't get the right number...
« Last Edit: January 14, 2012, 05:52:00 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation