Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - someone

Pages: 1 2 3 [4]
46
TI-Nspire / Re: [Lua] Mancala
« 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

47
TI-Nspire / Re: [Lua] Mancala
« 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...)

48
TI-Nspire / Re: [Lua] Mancala
« 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 :)

49
TI-Nspire / Re: [Lua] Mancala
« 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

Pages: 1 2 3 [4]