Author Topic: [Lua] Mancala  (Read 13331 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 #45 on: January 14, 2012, 10:08:04 am »
Cool!

How much has to happen in order to get a stack overflow?
Its in an area where some functions call each other, so it kinda makes sense :P

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #46 on: January 14, 2012, 05:11:05 pm »
@adriweb I thought table.getn was removed after Lua 5.0 and replaced with the # operator.

@epic7 It depends on how Lua was set up on the system. I don't know the details because that gets into complicated C stuff.
I can take a look at your code to see if I can spot an optimization.
« Last Edit: January 14, 2012, 05:17:44 pm by 3rik »
Userbars

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #47 on: January 14, 2012, 05:18:21 pm »
on.create doesn't work with oclua? No wonder... That should be on Inspired Lua, it'd help a lot. Is there any advantage with using on.create?

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: [Lua] Mancala
« Reply #48 on: January 14, 2012, 05:28:57 pm »
Is there any advantage with using on.create?

This is what the Nspire Scripting Application Programming Interface document says.

Quote from: Nspire Scripting Application Programming Interface
15.13     create
on.create()
This routine is called when the script application is created. The window size and graphics context are valid at this point.
Use this routine to initialize graphical objects based on the window size. Don’t actually paint the screen in this routine since the on.paint event handler will be called soon after this routine finishes.

If you are making an app that depends on the screen size you can then use platform.window:height() or platform.window:width() to do calculations. I suppose you could call it later in the program again. I was just testing to see if I had any issues with it.
« Last Edit: January 14, 2012, 05:29:27 pm by 3rik »
Userbars

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #49 on: January 14, 2012, 05:36:30 pm »
Ok. Thanks!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55933
  • Rating: +3153/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: [Lua] Mancala
« Reply #50 on: January 14, 2012, 06:47:31 pm »
I never played that game so I have no clue how it goes on, but I'll probably give this a try once it's finished to see how it is. Glad to see you working on Nspire projects. :)

Offline AzNg0d1030

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 522
  • Rating: +45/-4
  • Hardcore anime watcher.
    • View Profile
Re: [Lua] Mancala
« Reply #51 on: January 14, 2012, 08:18:51 pm »
I never played that game so I have no clue how it goes on, but I'll probably give this a try once it's finished to see how it is. Glad to see you working on Nspire projects. :)
Same.  I don't think I've ever even heard of it... Looks confusing O.O
You just lost the game.



Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: [Lua] Mancala
« Reply #52 on: January 17, 2012, 01:26:59 pm »
Cool!

How much has to happen in order to get a stack overflow?
Its in an area where some functions call each other, so it kinda makes sense :P
Maybe you're using local variables like they where global variables...

Like in the following part of the code, who is "d"?
Code: [Select]
function turn() --function finished/near finished
  get()
  count[land]=0 --reset count
  land=click --set to change table pos to click
  for e=1, d do
  land=land+1
  set(find[e])
  platform.window:invalidate()
  for pause=1, 250 do
  end
  end
  check()
end

Edit: I see now that there's a "d" inside the get() function... Try to set d=0 when the function turn() starts, maybe that's just what it needed... (Since is not reset on the other function...)
« Last Edit: January 18, 2012, 02:22:03 pm by someone »

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: [Lua] Mancala
« Reply #53 on: February 10, 2012, 02:04:32 pm »
Hi there epic7, I wanted to PM you but couldn't find how to do it. (I wanted to ask about the progress of this game)

So, I was bored & decided to check the code & fix it (it was the problem I mentioned before that "d" was not reset). Also added a few functions & tweaks, hope you don't mind.

Code: [Select]
-- GLOBALS --
click=1
land=1

player1=true
extra=false
canMove=true

color={}
find={}
coord={}
coordL={0}
coordR={0}
count={3,3,3,3,3,3,nil,3,3,3,3,3,3,nil}

--------------------------------------------------------------------------------
--function on.create()
--------------------------------------------------------------------------------
    local pos=0
    for a=1, 14 do
        for b=1, 3 do
            pos=a*9+b*3-11
            if a~=7 and a~=14 then
                coord[pos]=a
                coord[pos+1]=math.random(0,16)
                coord[pos+2]=math.random(0,14)
            else
                coord[pos]=a
                coord[pos+1]=nil
                coord[pos+2]=nil
            end
        end
    end
    
    --For the beans colors
    for i=1, 42 do
        color[i]={math.random(0,255), math.random(0,255), math.random(0,255)}
    end
--end
--------------------------------------------------------------------------------
function on.paint(gc)
--------------------------------------------------------------------------------
    paintBoard(gc)
    
    local pos=0
    for a=1, 42 do
        pos=a*3-2
        if coord[pos]~=7 and coord[pos]~=14 then
            if coord[pos]>7 then
                paintSeeds(a, gc, (coord[pos]-7)*34+coord[pos+1]+25, coord[pos+2]+ 49, 10, 10, 0, 360)
            else
                paintSeeds(a, gc, (coord[pos]-0)*34+coord[pos+1]+25, coord[pos+2]+141, 10, 10, 0, 360)
            end
        elseif coord[pos]==7 then
            if coordR[1]~=0 then
                for c=2, coordR[1] do
                    if coordR[c]==nil then
                        coordR[c*2]=math.random(0,35)
                        coordR[c*2+1]=math.random(45,112)
                    end
                    paintSeeds(a, gc, coordR[c*2]+263, coordR[c*2+1], 10, 10, 0, 360)
                end
            end
        elseif coord[pos]==14 then
            if coordL[1]~=0 then
                for c=2, coordL[1] do
                    if coordL[c]==nil then
                        coordL[c*2]=math.random(0,35)
                        coordL[c*2+1]=math.random(0,122)
                    end
                    paintSeeds(a, gc, coordL[c*2]+20, coordL[c*2+1], 10, 10, 360)
                end
            end
        end
    end
end
--------------------------------------------------------------------------------
function paintBoard(GC)
--------------------------------------------------------------------------------
    --Background
    GC:setColorRGB(210,240,255)
    GC:fillRect(0,0,318,212)

    --Text
    GC:setColorRGB(0,30,60)
    GC:setFont("serif","b",30)
    GC:drawString("Mancala",95,0,"top")
    GC:setFont("serif","b",10)
    GC:drawString("by epic7",255,190,"top")
    
    --Board Fill
    GC:setColorRGB(245,222,179)
    GC:fillRect(15,40,288,132)
    
    --Board Details
    GC:setColorRGB(125,102,59)
    GC:drawRect(14,39,289,133)  -- Outer border
    GC:drawRect(20,45,35,122)   -- Left pit
    GC:drawRect(263,45,35,122)  -- Right pit
    
    --Circles
    local width=30
    local height=50
    for row=0, 1 do
        for circle=1, 6 do
            GC:drawArc(circle*34+25, row*(122-height)+45, width, height, 0, 360)
        end
    end
end
--------------------------------------------------------------------------------
function paintSeeds(index, GC, x, y)
--------------------------------------------------------------------------------
    local width=10
    local height=6
    
    GC:setColorRGB(0, 0, 0)                 --border
    GC:drawArc(x-1, y-1, width+1, height+1, 0, 360)
    
    GC:setColorRGB(unpack(color[index]))    --Colored beans
    GC:fillArc(x, y, width, height, 0, 360)
end
--------------------------------------------------------------------------------
function setClick() --function finished
--------------------------------------------------------------------------------
    if player1 then
        click=1
    else
        click=8
    end
end
--------------------------------------------------------------------------------
function win(player)
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
function on.enterKey() --function finished
--------------------------------------------------------------------------------
    if canMove then
        turn()
        canMove=false
    end
end
--------------------------------------------------------------------------------
function on.arrowKey(arrow) --function unfinished
--------------------------------------------------------------------------------
    if canMove then
        if arrow=="left" then
            if click>1 then
                click=click-1
            end
        elseif arrow=="right" then
            if click<6 then
                click=click+1
            end
        end
    end
end
--------------------------------------------------------------------------------
function turn() --function finished/near finished
--------------------------------------------------------------------------------
    bean=0
    get()
    
    count[land]=0 --reset count
    land=click --set to change table pos to click
    for a=1, bean do
        land=land+1
        set(find[a])
        platform.window:invalidate()
        for pause=1, 250 do
        end
    end
    check()
end
--------------------------------------------------------------------------------
function get() --function finished
--------------------------------------------------------------------------------
    for a=1, 42 do
        if coord[a*3-2]==click then
            bean=1
        while find[bean] do
            bean=bean+1
        end
        find[bean]=a
        end
    end
end
--------------------------------------------------------------------------------
function set(num)  --function finished
--------------------------------------------------------------------------------
    toset=num*3-2
    if land~=7 and land~=14 then --if not near mancala
        count[land]=count[land]+1
        coord[toset]=coord[toset]+1
        setrand() --set random coordinates
    elseif land==7 then --if before right side mancala
        if player1 then --if player 1
            coord[toset]=7 --go into mancala
            coordR[1]=coordR[1]+1 --add one point
        else --if player 2
            count[8]=count[8]+1 --add to next slot
            coord[toset]=8 --set to 8
            land=8 --skip 7
            setrand() --set random coordinates
        end
    elseif land==14 then --same but for left side mancala
        if not player1 then
            coord[toset]=14
            coordL[1]=coordL[1]+1
        else
            count[1]=count[1]+1
            coord[toset]=1
            land=1
            setrand()
        end
    end
end
--------------------------------------------------------------------------------
function setrand() --function finished
--------------------------------------------------------------------------------
    coord[toset+1]=math.random(0,16)
    coord[toset+2]=math.random(0,14)
end
--------------------------------------------------------------------------------
function check()  --function unfinished
--------------------------------------------------------------------------------
    if count[land]==0 then
        --add capture
        player1=not player1
        canMove=true
        setClick()
    else
        turn()
    end
        
    if land==7 or land==14 then
        canMove=true
        setClick()
    end
    
    filled=false
    ch=1
    while ch<7 and not filled do
        if count[ch]==0 then
            ch=ch+1
        else
            filled=true
        end
    end
    
    if not filled then
        win(1)
    end
    
    ch=8
    filled=false
    
    while ch<14 and not filled do
        if count[ch]==0 then
            ch=ch+1
        else
            filled=true
        end
    end
    if not filled then
        win(2)
    end
end
« Last Edit: February 10, 2012, 06:06:31 pm by someone »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [Lua] Mancala
« Reply #54 on: February 10, 2012, 02:08:50 pm »
I think you have to have made 5 posts before you gain the option to PM (to prevent spambots from sending messages to users and whatnot).

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: [Lua] Mancala
« Reply #55 on: February 10, 2012, 04:41:55 pm »
I think the progress of all Lua development has slowed. I'm really enjoying playing Game Boy games on my Nspire, you know?  ;)

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: [Lua] Mancala
« Reply #56 on: February 10, 2012, 05:52:14 pm »
Cool! Ill put that in my code, which currently fails at some stack overflow errors

I haven't done much programming lately
« Last Edit: February 10, 2012, 05:53:08 pm by epic7 »

Offline someone

  • LV3 Member (Next: 100)
  • ***
  • Posts: 49
  • Rating: +9/-0
    • View Profile
Re: [Lua] Mancala
« Reply #57 on: February 10, 2012, 06:14:13 pm »
Yea, usually that's why topics become inactive, so I wanted to send a PM since I added some tweaks to the code & wouldn't knew if it bothered you or was ok.

Anyway, keep it up epic7 & hope you can finish it soon :)

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: [Lua] Mancala
« Reply #58 on: February 15, 2012, 03:16:50 pm »
Gawd i used to play this game all the time!
Thanks for bringing back a classic! :D