Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.14 KB | None | 0 0
  1. platform.apilevel="1.0"
  2.  
  3. new=false
  4. screen=1
  5. game=0
  6.  
  7. themes={{{0,0,255},{0,255,0},{0,0,0},{230,230,150},{0,0,0},{0,0,0}},{{0,0,255},{0,255,0},{255,255,255},{0,0,0},{50,50,50},{0,0,255}},{{200,200,200},{100,100,100},{0,0,0},{255,255,255},{0,0,0},{0,0,0}}}
  8. colors=themes[1]
  9. titlecolors={{0,0,255},{255,0,0},{0,255,0}}
  10.  
  11. w = 318
  12.  
  13. lang = locale.name()
  14.  
  15. textpaint = {{fr="ISOLA",en="ISOLA"},{fr="appuyez sur enter",en="press enter"},{fr="Joueur ",en="Player "},{fr="Move",en="Move"},{fr="Block",en="Block"}}
  16. textdial  = {{fr="QUITTER?",en="QUIT?"},{fr="RECOMMENCER",en="RESET?"},{fr="y : Yes",en="y : Yes"},{fr="n : No",en="n : No"},{fr="Comment jouer",en="How to play"},{fr="A votre tour, déplacez",en="On your turn, move"},{fr="vous à votre guise",en="straight or diagonal"},{fr="placez ensuite un trou pour tenter",en="then place a hole to try"},{fr="de piéger votre adversaire.",en="to trap your opponent."},{fr="A propos",en="About"},{fr="Jeu de LD Studio",en="Game by LD Studio"},{fr="contact.ldstudios@gmail.com",en="contact.ldstudios@gmail.com"},{fr="www.ld-studios.weebly.com",en="www.ld-studios.weebly.com"},{fr="NOUVELLE PARTIE",en="NEW GAME"},{fr="1: 1 joueur",en="1: 1 player"},{fr="2: 2 joueurs",en="2: 2 players"},{fr="Joueur ",en="Player "},{fr="a gagné!",en="wins!"}}
  17.  
  18. function drawCenteredString(gc,string,y)
  19.     if lang == "fr" then
  20.         gc:drawString(string.fr,w/2-gc:getStringWidth(string.fr)/2,y)
  21.     else
  22.         gc:drawString(string.en,w/2-gc:getStringWidth(string.en)/2,y)
  23.     end
  24. end
  25.  
  26. function drawString2(gc,string,x,y)
  27.     if lang == "fr" then
  28.         gc:drawString(string.fr,x,y)
  29.     else
  30.         gc:drawString(string.en,x,y)
  31.     end
  32. end
  33.  
  34. function on.paint(gc)
  35.     if screen==1 then
  36.         gc:setColorRGB(255,255,155)
  37.         gc:fillRect(0,0,318,218)
  38.         gc:setColorRGB(0,0,0)
  39.         gc:setFont("sansserif","b",24)
  40.         drawCenteredString(gc,textpaint[1],100)
  41.         gc:setFont("sansserif","r",12)
  42.         drawCenteredString(gc,textpaint[2],110)
  43.         gc:setColorRGB(unpack(titlecolors[math.random(1,3)]))
  44.         gc:fillRect(155,120,15,15)
  45.         gc:setPen("medium")
  46.         gc:drawRect(155,120,15,15)
  47.     else
  48.         gc:setColorRGB(unpack(colors[4]))
  49.         gc:fillRect(90,35,9*15,9*15)
  50.         for i=2,10 do
  51.             local v=board[i]
  52.             for ii=2,10 do
  53.                 if v[ii]>0 then
  54.                     gc:setColorRGB(unpack(colors[v[ii]]))
  55.                     gc:fillRect(ii*15+60,i*15+5,15,15)            
  56.                 end
  57.                 gc:setColorRGB(unpack(colors[5]))    
  58.                 gc:drawRect(ii*15+60,i*15+5,15,15)                    
  59.             end
  60.         end
  61.         gc:setPen("medium")  
  62.         gc:setColorRGB(unpack(colors[6]))
  63.         gc:drawRect(x*15+60,y*15+5,15,15)
  64.         gc:setColorRGB(0,0,0)                                  
  65.         gc:setFont("sansserif","b",12)
  66.         if lang=="fr" then
  67.             tmp = textpaint[2]
  68.             tmp2 = textpaint[3]
  69.             gc:drawString(tmp.fr..tmp2.fr..":",240,100)
  70.         else
  71.             tmp = textpaint[2]
  72.             tmp2 = textpaint[3]
  73.             gc:drawString(tmp.en..tmp2.en..":",240,100)
  74.         end
  75.         if turn[1]==1 then
  76.             drawString2(gc,textpaint[4],250,120)
  77.         else
  78.             drawString2(gc,textpaint[5],247,120)
  79.         end
  80.         if game>0 or box>0 or exit==true or re==true or new==true then
  81.             drawDialog(gc)
  82.         end
  83.     end        
  84. end        
  85.  
  86. function drawDialog(gc)
  87.     gc:setColorRGB(155,155,155)
  88.     gc:fillRect(80,50,155,100)
  89.     gc:setColorRGB(0,0,0)
  90.     gc:drawRect(80,50,155,100)
  91.     if exit==true or re==true then
  92.         gc:setFont("sansserif","b",24)
  93.         if exit==true then
  94.             drawCenteredString(gc,textdial[1],100)
  95.         elseif re==true then
  96.             drawCenteredString(gc,textdial[2],100)
  97.         end
  98.         gc:setFont("sansserif","b",10)
  99.         drawString2(gc,textdial[3],140,125)
  100.         drawString2(gc,textdial[4],142,140)        
  101.     elseif box>0 then
  102.         gc:setFont("sansserif","b",12)    
  103.         if box==1 then
  104.             drawCenteredString(gc,textdial[5],75)
  105.             gc:setFont("sansserif","b",10)
  106.             drawCenteredString(gc,textdial[6],95)
  107.             drawCenteredString(gc,textdial[7],110)
  108.             drawCenteredString(gc,textdial[8],125)
  109.             drawCenteredString(gc,textdial[9],140)
  110.         elseif box==2 then
  111.             drawCenteredString(gc,textdial[10],75)
  112.             gc:setFont("sansserif","b",10)
  113.             drawCenteredString(gc,textdial[11],95)
  114.             gc:setFont("sansserif","b",8)
  115.             drawCenteredString(gc,textdial[12],110)
  116.             drawCenteredString(gc,textdial[13],125)
  117.         end    
  118.     elseif new==true then
  119.         gc:setFont("sansserif","b",12)
  120.         drawCenteredString(gc,textdial[14],80)        
  121.         gc:setFont("sansserif","b",10)
  122.         drawString2(gc,textdial[15],125,110)
  123.         drawString2(gc,textdial[16],125,130)
  124.     elseif game>0 then
  125.         gc:setFont("sansserif","b",12)
  126.         if lang=="fr" then
  127.             temp = textdial[17]
  128.             temp2 = textdial[18]
  129.             gc:drawString(temp.fr..game..temp2.fr,105,80)        
  130.         else
  131.             temp = textdial[17]
  132.             temp2 = textdial[18]
  133.             gc:drawString(temp.en..game..temp2.en,105,80)
  134.         end
  135.         gc:setFont("sansserif","b",10)
  136.         drawCenteredString(gc,textdial[19],140)
  137.         gc:setColorRGB(unpack(colors[game]))
  138.         gc:fillRect(150,95,15,15)
  139.         gc:setColorRGB(0,0,0)
  140.         gc:setPen("medium")
  141.         gc:drawRect(150,95,15,15)
  142.     end
  143. end
  144.  
  145. function on.mouseDown(x,y)
  146.     click()
  147. end    
  148.  
  149. function on.mouseMove(mx,my)
  150.     x=math.ceil((mx-90)/15)+1
  151.     y=math.ceil((my-35)/15)+1
  152.     if x>10 then
  153.         x=10
  154.     elseif x<2 then
  155.         x=2
  156.     end    
  157.     if y>10 then
  158.         y=10
  159.     elseif y<2 then
  160.         y=2
  161.     end
  162.     platform.window:invalidate()      
  163. end
  164. function on.arrowLeft()
  165.     left()
  166. end
  167. function on.arrowRight()
  168.     right()
  169. end
  170. function on.arrowUp()
  171.     up()
  172. end
  173. function on.arrowDown()
  174.     down()
  175. end
  176. function on.charIn(char)
  177.     if char=="8" then
  178.         up()
  179.     elseif char=="6" then
  180.         right()
  181.     elseif char=="4" then
  182.         left()
  183.     elseif (char=="2" or char=="5") and new==false then
  184.         down()
  185.     end    
  186.     if new==true and char=="1" then
  187.         players=1
  188.         new=false
  189.     elseif new==true and char=="2" then
  190.         players=2
  191.         new=false
  192.     end    
  193.     if char=="y" and exit==true then
  194.        quit()
  195.     elseif char=="y" and re==true then
  196.        new=true
  197.        reset()
  198.     elseif char=="n" then
  199.        exit=false
  200.        re=false
  201.        if game>0 then
  202.            game=0
  203.            new=true              
  204.            reset()
  205.        end    
  206.     end
  207.     platform.window:invalidate()            
  208. end    
  209. function left()    
  210.     if x>2 then
  211.         x=x-1
  212.         platform.window:invalidate()
  213.     end
  214. end
  215.  
  216. function right()
  217.     if x<10 then
  218.         x=x+1
  219.         platform.window:invalidate()
  220.     end
  221. end
  222.  
  223. function up()
  224.     if y>2 then
  225.         y=y-1
  226.         platform.window:invalidate()
  227.     end
  228. end
  229.    
  230. function down()
  231.     if y<10 then
  232.         y=y+1
  233.         platform.window:invalidate()
  234.     end
  235. end    
  236.  
  237. function on.escapeKey()
  238.     ask("","Exit")
  239. end    
  240.  
  241. function on.enterKey()
  242.     click()
  243. end
  244.  
  245. function click()  
  246.     re,exit,box=false,false,0
  247.     if new==false then
  248.     if game>0 then
  249.         game=0
  250.         new=true  
  251.         reset()
  252.     end    
  253.     if screen==1 then
  254.         screen=2
  255.         new=true      
  256.         reset()
  257.     elseif screen==2 then
  258.         if game>0 then
  259.             new=true          
  260.             reset()
  261.         end    
  262.         if turn[1]==1 then
  263.             if board[y][x]==0 and math.abs(x-p[turn[2]][1])<2 and math.abs(y-p[turn[2]][2])<2  then
  264.                 board[p[turn[2]][2]][p[turn[2]][1]]=0
  265.                 p[turn[2]]={x,y}
  266.                 board[y][x]=turn[2]
  267.                 turn[1]=2
  268.             end
  269.         elseif turn[1]==2 then
  270.             if board[y][x]==0 then
  271.                 board[y][x]=3
  272.                 turn[1]=1
  273.                 turn[2]=(((turn[2]^2)-1)/-3)+2
  274.                 x=p[turn[2]][1]
  275.                 y=p[turn[2]][2]
  276.                 checkDeath(p[1][1],p[1][2],p[2][1],p[2][2])
  277.             end
  278.         end
  279.     end
  280.     if turn[2]==2 and players==1 then
  281.         ai()
  282.     end    
  283.     end
  284.     platform.window:invalidate()      
  285. end        
  286.  
  287. function checkDeath(px1,py1,px2,py2)
  288.     game=2
  289.     for i=-1,1 do
  290.         for ii=-1,1 do
  291.             if board[py1+i][px1+ii]==0 then
  292.                 game=0
  293.             end
  294.         end
  295.     end
  296.     if game==0 then
  297.     game=1
  298.     for i=-1,1 do
  299.         for ii=-1,1 do
  300.             if board[py2+i][px2+ii]==0 then
  301.                 game=0
  302.             end
  303.         end
  304.     end
  305.     end              
  306. end
  307.  
  308. function quit()
  309.     screen=1
  310.     toolpalette.register({})      
  311.     platform.window:invalidate()
  312. end    
  313.  
  314. function changeTheme(parent,option)
  315.     if option=="Classic" then
  316.         colors=themes[1]
  317.     elseif option=="Dark" then
  318.         colors=themes[2]
  319.     elseif option=="84" then
  320.         colors=themes[3]
  321.     end        
  322.     platform.window:invalidate()
  323. end
  324.  
  325. function reset()
  326.     board= {{3,3,3,3,3,3,3,3,3,3,3},
  327.             {3,0,0,0,0,2,0,0,0,0,3},
  328.             {3,0,0,0,0,0,0,0,0,0,3},
  329.             {3,0,0,0,0,0,0,0,0,0,3},
  330.             {3,0,0,0,0,0,0,0,0,0,3},
  331.             {3,0,0,0,0,0,0,0,0,0,3},
  332.             {3,0,0,0,0,0,0,0,0,0,3},
  333.             {3,0,0,0,0,0,0,0,0,0,3},
  334.             {3,0,0,0,0,0,0,0,0,0,3},
  335.             {3,0,0,0,0,1,0,0,0,0,3},
  336.             {3,3,3,3,3,3,3,3,3,3,3}}
  337.     x=6
  338.     y=10
  339.     game=0
  340.     exit,re,box=false,false,0
  341.     p={{6,10},{6,2}}        
  342.     turn={1,1}
  343.     toolpalette.register(MenuEn)          
  344.     platform.window:invalidate()
  345. end
  346.  
  347. function ai()
  348.         options={}
  349.         for i=-1,1,1 do
  350.             for ii=-1,1,1 do
  351.                 if board[p[2][2]+i][p[2][1]+ii]==0 and (ii~=0 or i~=0) then
  352.                     table.insert(options,{p[2][1]+ii,p[2][2]+i})
  353.                 end
  354.             end
  355.         end
  356.         if table.getn(options)>0 then
  357.             board[p[2][2]][p[2][1]]=0
  358.             options=options[math.random(1,table.getn(options))]
  359.             p[2]=options
  360.             turn[1]=2
  361.             board[p[2][2]][p[2][1]]=2
  362.         else
  363.             game=1
  364.             return
  365.         end    
  366.         options={}
  367.         if p[1][1]==2 or p[1][1]==10 or p[1][2]==1 or p[1][2]==10 then
  368.             range=-1
  369.         else
  370.             range=-2
  371.         end    
  372.         for i=range,math.abs(range) do
  373.             for ii=range,math.abs(range) do
  374.                if board[p[1][2]+i][p[1][1]+ii]==0  then
  375.                    table.insert(options,{p[1][1]+ii,p[1][2]+i})        
  376.                end
  377.             end      
  378.         end
  379.         if table.getn(options)>0 then          
  380.             options=options[math.random(1,table.getn(options))]
  381.             board[options[2]][options[1]]=3    
  382.             turn={1,1}
  383.         end
  384.         checkDeath(p[1][2],p[1][1],p[1][2],p[1][2])              
  385. end        
  386.  
  387. function forfeit()
  388.     re,exit,box=false,false,0
  389.     game=(((turn[2]^2)-1)/-3)+2
  390.     platform.window:invalidate()            
  391. end
  392.  
  393. function ask(parent,option)
  394.     re,exit,box=false,false,0
  395.     if option=="Exit" then
  396.         exit=true
  397.     elseif option=="Reset" then
  398.         re=true
  399.     elseif option=="How to Play" then
  400.         box=1
  401.     elseif option=="About" then
  402.         box=2
  403.     end
  404.     platform.window:invalidate()
  405. end    
  406.  
  407. MenuEn={{"Game",
  408.         {"Exit",ask},
  409.         {"Reset",ask},
  410.         {"Foreit",forfeit}},
  411.      {"Theme",
  412.         {"Classic",changeTheme},
  413.         {"Dark",changeTheme},
  414.         {"84",changeTheme}},
  415.      {"Help",
  416.         {"How to Play",ask},
  417.         {"About",ask}}
  418.       }
  419.      
  420. MenuFr={{"Jeu",
  421.         {"Quitter",ask},
  422.         {"Restaurer",ask},
  423.         {"Abandonner",forfeit}},
  424.      {"Theme",
  425.         {"Classic",changeTheme},
  426.         {"Sombre",changeTheme},
  427.         {"84",changeTheme}},
  428.      {"Aide",
  429.         {"Comment jouer",ask},
  430.         {"A propos",ask}}
  431.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement