Advertisement
podoko_Lua

Awale [v1.0]

Aug 16th, 2014
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. --[[Awale [v1.0]
  2.    
  3.     Remplacez Joueur1 et Joueur2 par les pseudos des joueurs pour la partie d'awalé (ligne 8)
  4.    
  5.     ]]
  6.  
  7.  
  8. players = {"Joueur1", "Joueur2" }
  9. score = {0,0}
  10.  
  11. function init()
  12.    
  13.     tab = {4,4,4,4,4,4,4,4,4,4,4,4}
  14.    
  15.    
  16.     local playerL = players
  17.     local p1, p2 = playerL[1], playerL[2]
  18.     local coordL = { 130, 230, 330, 430, 530, 630 }
  19.    
  20.    
  21.     local addT = ui.addTA
  22.     local upTA = ui.updateTextArea
  23.     ui.addTextArea(0, "", playerL, 100, 100, 600, 120, 0x324650,0x324650,0.8,true)
  24.     ui.addTextArea(-1, "", playerL, 100, 70, 600, 20, 0x324650,0x324650,0.8,true)
  25.     updateScore()
  26.    
  27.     for id, x in ipairs(coordL) do 
  28.         addT(id, id, p1, x, 180, 100, 30,0, 0, 0, true)
  29.         addT(id+6, id+6, p1, 760-x, 120, 100, 30,0, 0, 0, true)
  30.        
  31.         addT(id, id, p2, 760-x, 120, 100, 30,0, 0, 0, true)
  32.         addT(id+6, id+6, p2, x, 180, 100, 30,0, 0, 0, true)
  33.     end
  34.    
  35.     for id, v in ipairs(tab) do
  36.         upTA(id, "<a href='event:"..tostring(id).."'>"..stringP(tab[id]).."</a>", playerL)
  37.     end
  38.    
  39.     round = 1
  40.    
  41. end
  42.  
  43.  
  44.  
  45.  
  46.  
  47. function eventTextAreaCallback (id, name, call)
  48.    
  49.     if name == players[round] then
  50.        
  51.         if not(tab[id] == 0) and ((round==2 and id>6 and id<13) or (round==1 and id>0 and id<7)) then
  52.             empty(id)
  53.  
  54.             round = (round%2)+1
  55.             if testEndGame(round) then
  56.                 print('end')
  57.                 round = 3
  58.                 local s1, s2, p1, p2 = score[1], score[2], players[1], players[2]
  59.                 ui.updateTextArea(-1, s1==s2 and "Égalité" or s1>s2 and (p1..' remporte la partie : '..s1..'-'..s2) or (p2..' remporte la partie : '..s2..'-'..s1), players)
  60.             end
  61.                
  62.         else
  63.             print("mauvaise case")
  64.         end
  65.  
  66.     end
  67. end
  68.  
  69.  
  70.  
  71.  
  72. function empty(id)
  73.     print("in")
  74.     toGive = tab[id]
  75.     tab[id] = 0
  76.    
  77.     toDeliver(toGive, (id%12)+1)
  78.    
  79.     ui.updateTextArea(id, "<a href='event:"..tostring(id).."'>"..stringP(tab[id]).."</a>", players)
  80. end
  81.  
  82.  
  83. function toDeliver(nb, id)
  84.    
  85.     if nb == 1 then
  86.         tab[id] = tab[id]+1
  87.         if ( (round==1 and id>6 and id<13) or (round==2 and id>0 and id<7) ) and (tab[id] == 2 or tab[id] == 3) then -- si c'est bien sur une des cases de l'adversaire
  88.             takeOut(id)
  89.         end
  90.     else
  91.         tab[id] = tab[id]+1
  92.         toDeliver(nb-1, (id%12)+1)
  93.     end
  94.    
  95.     ui.updateTextArea(id, "<a href='event:"..tostring(id).."'>"..stringP(tab[id]).."</a>", players)
  96. end
  97.  
  98. function takeOut(id)
  99.    
  100.     score[round] = score[round]+tab[id]
  101.     tab[id] = 0
  102.    
  103.     if (id-1)%6 ~= 0 and (tab[id-1] == 3 or tab[id-1] == 2) then
  104.         takeOut(id-1)
  105.     else
  106.         updateScore()
  107.     end
  108.    
  109. end
  110.  
  111.  
  112.  
  113. function stringP(nb)
  114.    
  115.     if nb == 0 then return "0"
  116.     elseif nb == 1 then return "•"
  117.     else return "•"..stringP(nb-1)
  118.     end
  119.    
  120. end
  121.  
  122. function updateScore()
  123.     ui.updateTextArea(-1, players[1]..' : '..score[1]..'\t'..players[2]..' : '..score[2], playerL)
  124. end
  125.  
  126. function testEndGame(roundA)
  127.    
  128.     pieces = 0
  129.     for i=(roundA-1)*6+1, roundA*6 do  
  130.         pieces = pieces+tab[i]
  131.     end
  132.     print(pieces)
  133.     return pieces == 0
  134.    
  135. end
  136.  
  137.  
  138.  
  139. ui.addPp = ui.addPopup
  140. ui.addTA = ui.addTextArea
  141. ui.updateTA = ui.updateTextArea
  142. ui.removeTA = ui.removeTextArea
  143.  
  144. function ui.removeTA(id, name)
  145.     if type(name) == "table" then
  146.         local fct = ui.removeTA
  147.         for _, n in ipairs(name) do
  148.             fct(id, n)
  149.         end
  150.     else ui.removeTA(id, name)
  151.     end
  152. end
  153.  
  154. function ui.updateTextArea(id, text, name)
  155.     if type(name) == "table" then
  156.         local fct = ui.updateTA
  157.         for _, n in ipairs(name) do
  158.             fct(id, text, n)
  159.         end
  160.     else ui.updateTA(id, text, name)
  161.     end
  162. end
  163.  
  164.  
  165. function ui.addTextArea(id, text, name, x, y, w, h, c1, c2, alp, fix)
  166.     if type(name) == "table" then
  167.         local fct = ui.addTA
  168.         for _,n in ipairs(name) do
  169.             print(n)
  170.             fct(id, text, n, x, y, w, h, c1, c2, alp, fix)
  171.         end
  172.     else ui.addTA(id, text, name, x, y, w, h, c1, c2, alp, fix)
  173.     end
  174.    
  175. end
  176.  
  177. function ui.addPopup(id, t, text, name, x, y, w, fix)
  178.     if type(name) == "table" then
  179.         local fct = ui.addPp
  180.         for _, n in ipairs(name) do
  181.             fct(id, t, text, n, x, y, fix)
  182.         end
  183.     else ui.addPp(id, t, text, name, x, y, fix)
  184.     end
  185.    
  186. end
  187.  
  188. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement