Advertisement
astral17

draughts

Nov 2nd, 2016
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.22 KB | None | 0 0
  1. -------------SETTINGS--------------
  2. backStab=true --Can standard chess backStab
  3. kingType=false --false - king beat on INF cell; true king beat on 1 cell
  4. -----------------------------------
  5.  
  6. local gpu=require("component").gpu
  7. local event=require("event")
  8. --colB colF char
  9. local images={
  10. red={
  11.   {{"alpha"},{nil,0xff0000,"▄"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▄"},{"alpha"}},
  12.   {{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "},{0xff0000,nil," "}},
  13.   {{"alpha"},{nil,0xff0000,"▀"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▀"},{"alpha"}},
  14. },
  15. reD={
  16.   {{"alpha"},{nil,0xff0000,"▄"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▄"},{"alpha"}},
  17.   {{0xff0000,nil," "},{0xff0000,nil," "},{0xffff00,nil," "},{0xffff00,nil," "},{0xff0000,nil," "},{0xff0000,nil," "}},
  18.   {{"alpha"},{nil,0xff0000,"▀"},{0xff0000,nil," "},{0xff0000,nil," "},{nil,0xff0000,"▀"},{"alpha"}},
  19. },
  20. whi={
  21.   {{"alpha"},{nil,0xffffff,"▄"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▄"},{"alpha"}},
  22.   {{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "},{0xffffff,nil," "}},
  23.   {{"alpha"},{nil,0xffffff,"▀"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▀"},{"alpha"}},
  24. },
  25. whD={
  26.   {{"alpha"},{nil,0xffffff,"▄"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▄"},{"alpha"}},
  27.   {{0xffffff,nil," "},{0xffffff,nil," "},{0xffff00,nil," "},{0xffff00,nil," "},{0xffffff,nil," "},{0xffffff,nil," "}},
  28.   {{"alpha"},{nil,0xffffff,"▀"},{0xffffff,nil," "},{0xffffff,nil," "},{nil,0xffffff,"▀"},{"alpha"}},
  29. },
  30. }
  31.  
  32. turn="white"
  33. --turn="red"
  34. turnsM={white="red",red="white"}
  35. isF=false
  36. mustBit=false
  37. cantUnselect=false
  38. player={}
  39. -----------------------------------------------------------
  40. function centerText(x,y,w,text)
  41.   gpu.set(x+math.floor(w/2-string.len(text)/2),y,text)
  42. end
  43.  
  44. function messageBox(title,text,color)
  45.   local x,y=4*6-6,4*3-3
  46.   local len1,len2=string.len(title),string.len(text)
  47.   local len3=math.max(len1,len2)+2
  48.   gpu.setBackground(0xffffff)
  49.   gpu.fill(x,y,len3,2+3," ")
  50.   gpu.setForeground(color or 0xFF0000)
  51.   centerText(x,y+1,len3,title)
  52.   gpu.setForeground(0x000000)
  53.   centerText(x,y+3,len3,text)
  54.   gpu.setBackground(color or 0xFF0000)
  55.   gpu.setForeground(0xffffff)
  56.   gpu.fill(x,y+5,len3,3," ")
  57.   centerText(x,y+6,len3,"OK!")
  58. end
  59. ---------------authorization-------------------------------
  60. do
  61.   local obj={}
  62.  
  63.   local function testClick(self,x,y)
  64.     return (x>=self.x)and(y>=self.y)and(x<self.x+self.w)and(y<self.y+self.h)
  65.   end
  66.  
  67.   local function offObj()
  68.     return false
  69.   end
  70.  
  71.   local function newButton(x,y,w,h,id,type)
  72.     table.insert(obj,{x=x,y=y,w=w,h=h,isClicked=testClick,id=id,type=type})
  73.   end
  74.   gpu.setBackground(0x999999)
  75.   gpu.fill(1,1,10,4," ")
  76.   gpu.fill(15,1,10,4," ")
  77.   newButton(1,1,10,4)
  78.   newButton(15,1,10,4)
  79.   isRunning=true
  80.   tmp=2
  81.   while isRunning do
  82.     _,_,x,y,_,name=event.pull("touch")
  83.     for i=1,#obj do
  84.       if obj[i]:isClicked(x,y) then
  85.         player[i]=name
  86.         gpu.setBackground(0x009900)
  87.         gpu.fill(obj[i].x,obj[i].y,obj[i].w,obj[i].h," ")
  88.         gpu.set(obj[i].x,obj[i].y,player[i])
  89.         obj[i].isClicked=offObj
  90.         tmp=tmp-1
  91.       end
  92.     end
  93.     if tmp==0 then
  94.       isRunning=false
  95.     end
  96.   end
  97. end
  98. -----------------------------------------------------------
  99. --nin - noCell; non - empty; red - red; reD - redD; whi - white; whD - whiteD
  100. map={
  101.  {"nin","red","nin","red","nin","red","nin","red"},
  102.  {"red","nin","red","nin","red","nin","red","nin"},
  103.  {"nin","red","nin","red","nin","red","nin","red"},
  104.  {"non","nin","non","nin","non","nin","non","nin"},
  105.  {"nin","non","nin","non","nin","non","nin","non"},
  106.  {"whi","nin","whi","nin","whi","nin","whi","nin"},
  107.  {"nin","whi","nin","whi","nin","whi","nin","whi"},
  108.  {"whi","nin","whi","nin","whi","nin","whi","nin"}
  109. }
  110. ma22p={
  111.    {"nin","non","nin","non","nin","non","nin","non"},
  112.    {"non","nin","whD","nin","non","nin","whD","nin"},
  113.    {"nin","non","nin","non","nin","non","nin","non"},
  114.    {"non","nin","non","nin","non","nin","non","nin"},
  115.    {"nin","non","nin","non","nin","non","nin","non"},
  116.    {"non","nin","red","nin","non","nin","red","nin"},
  117.    {"nin","non","nin","non","nin","non","nin","non"},
  118.    {"non","nin","non","nin","non","nin","non","nin"}
  119. }
  120. function resetMap()
  121.   map={
  122.    {"nin","red","nin","red","nin","red","nin","red"},
  123.    {"red","nin","red","nin","red","nin","red","nin"},
  124.    {"nin","red","nin","red","nin","red","nin","red"},
  125.    {"non","nin","non","nin","non","nin","non","nin"},
  126.    {"nin","non","nin","non","nin","non","nin","non"},
  127.    {"whi","nin","whi","nin","whi","nin","whi","nin"},
  128.    {"nin","whi","nin","whi","nin","whi","nin","whi"},
  129.    {"whi","nin","whi","nin","whi","nin","whi","nin"}
  130.   }
  131. end
  132. function clearMap()
  133.   map={
  134.    {"nin","non","nin","non","nin","non","nin","non"},
  135.    {"non","nin","non","nin","non","nin","non","nin"},
  136.    {"nin","non","nin","non","nin","non","nin","non"},
  137.    {"non","nin","non","nin","non","nin","non","nin"},
  138.    {"nin","non","nin","non","nin","non","nin","non"},
  139.    {"non","nin","non","nin","non","nin","non","nin"},
  140.    {"nin","non","nin","non","nin","non","nin","non"},
  141.    {"non","nin","non","nin","non","nin","non","nin"}
  142.   }
  143. end
  144.  
  145. function drawImage(name,x,y,selectColor)
  146.   if name=="non" then
  147.     gpu.setBackground(0x000000)
  148.     gpu.fill(x*6-6+1,y*3-3+1,6,3," ")
  149.     return
  150.   end
  151.   local colB=0x000000
  152.   local colF=0xffffff
  153.   for y1=1,3 do
  154.     for x1=1,6 do
  155.       if images[name][y1][x1][1]~="alpha" then
  156.         if images[name][y1][x1][1]==nil then
  157.           colB=(selectColor or 0x000000)
  158.         else
  159.           colB=images[name][y1][x1][1]
  160.         end
  161.         if images[name][y1][x1][2]==nil then
  162.           colF=0xffffff
  163.         else
  164.           colF=images[name][y1][x1][2]
  165.         end
  166.         gpu.setBackground(colB)
  167.         gpu.setForeground(colF)
  168.         gpu.set(x+x1-1,y+y1-1,images[name][y1][x1][3])
  169.       else
  170.         gpu.setBackground(selectColor or 0x000000)
  171.         gpu.set(x+x1-1,y+y1-1," ")
  172.       end
  173.     end
  174.   end
  175. end
  176.  
  177. --drawImage("redD",5,5,0x00ff00)
  178. function drawField()
  179.   gpu.setBackground(0x000000)
  180.   require("term").clear()
  181.   for y=1,8 do
  182.     for x=1,8 do
  183.       if map[y][x]=="nin" then
  184.         gpu.setBackground(0xa0a0a0)
  185.         gpu.fill(x*6-6+1,y*3-3+1,6,3," ")
  186.       elseif map[y][x]~="non" then
  187.         drawImage(map[y][x],x*6-6+1,y*3-3+1)
  188.       end
  189.     end
  190.   end
  191. end
  192.  
  193. selected={}
  194.  
  195. function selecting(x,y,chess,beatX,beatY)
  196.   if chess=="non" then
  197.     gpu.setBackground(0x00ff00)
  198.     gpu.fill(x*6-6+1,y*3-3+1,6,3," ")
  199.   else
  200.     drawImage(chess,x*6-6+1,y*3-3+1,0x00ff00)
  201.   end
  202.   table.insert(selected,{x,y,chess,beatX,beatY})
  203. end
  204.  
  205. function select(x,y,chess,nShow)
  206.   if cantUnselect then
  207.     return
  208.   end
  209.   if nShow==nil then
  210.     nShow=false
  211.   end
  212.   local bool=testBit(x,y,true)
  213.   if bool then
  214.     nShow=false
  215.   end
  216.  if ((mustBit)and(bool))or(not mustBit) then
  217.                                          --  selecting(x,y,chess)
  218.   if not nShow then
  219.     if not bool then
  220.       if turn=="white" then
  221.         dir=-1
  222.       else
  223.         dir=1
  224.       end
  225.       if (map[y][x]=="whD")or(map[y][x]=="reD") then
  226.         tmp1,tmp2,tmp3,tmp4=true,true,true,true
  227.         for i=1,((kingType and 1)or 8) do
  228.           if (testPoint(x-i,y-i))and(tmp1) then
  229.             if map[y-i][x-i]~="non" then
  230.               tmp1=false
  231.             else
  232.               selecting(x-i,y-i,map[y-i][x-i])
  233.             end
  234.           end
  235.           if (testPoint(x-i,y+i))and(tmp2) then
  236.             if map[y+i][x-i]~="non" then
  237.               tmp2=false
  238.             else
  239.               selecting(x-i,y+i,map[y+i][x-i])
  240.             end
  241.           end
  242.           if (testPoint(x+i,y-i))and(tmp3) then
  243.             if map[y-i][x+i]~="non" then
  244.               tmp3=false
  245.             else
  246.               selecting(x+i,y-i,map[y-i][x+i])
  247.             end
  248.           end
  249.           if (testPoint(x+i,y+i))and(tmp4) then
  250.             if map[y+i][x+i]~="non" then
  251.               tmp4=false
  252.             else
  253.               selecting(x+i,y+i,map[y+i][x+i])
  254.             end
  255.           end
  256.         end
  257.       else
  258.         if x>1 then
  259.           if map[y+dir][x-1]=="non" then
  260.             selecting(x-1,y+dir,"non")
  261.           end
  262.         end
  263.         if x<8 then
  264.           if map[y+dir][x+1]=="non" then
  265.             selecting(x+1,y+dir,"non")
  266.           end
  267.         end
  268.       end
  269.       isF=true
  270.     end
  271.     selecting(x,y,chess)
  272.   end
  273.  end
  274.   return bool
  275. end
  276.  
  277. function unselectAll()
  278.   if cantUnselect then
  279.     return
  280.   end
  281.   for i=1,#selected do
  282. --    selected[i][3]=map[selected[i][2]][selected[i][1]]
  283.     if selected[i][3]=="non" then
  284.       gpu.setBackground(0x000000)
  285.       gpu.fill(selected[i][1]*6-6+1,selected[i][2]*3-3+1,6,3," ")
  286.     else
  287.       drawImage(selected[i][3],selected[i][1]*6-6+1,selected[i][2]*3-3+1)
  288.     end
  289. --    map[selected[i][2]][selected[i][1]]=selected[i][3]
  290.   end
  291.   selected={}
  292. end
  293.  
  294. function testPoint(x,y)
  295.   return (x>0)and(x<9)and(y>0)and(y<9)
  296. end
  297.  
  298. function testBit(x,y,bool)
  299.   local tmp=false
  300.   local op=""
  301.   if map[y][x]=="whi" then
  302.     op="red"
  303.     op2="reD"
  304.   elseif map[y][x]=="red" then
  305.     op="whi"
  306.     op2="whD"
  307.   elseif map[y][x]=="non" then
  308.     print("error!")
  309.   else
  310.     if map[y][x]=="whD" then
  311.       op="red"
  312.       op2="reD"
  313.       yo1="whi"
  314.       yo2="whD"
  315.     else
  316.       op="whi"
  317.       op2="whD"
  318.       yo1="red"
  319.       yo2="reD"
  320.     end
  321.     tmp1,tmp2,tmp3,tmp4=false,false,false,false --true if beating chess was found
  322.     num1,num2,num3,num4=0,0,0,0 --number special for finding beat chess
  323.     tmP1,tmP2,tmP3,tmP4=true,true,true,true --false if after beating chess was found chess which close road or your chess
  324.     for i=1,((kingType and 1)or 8) do
  325.       iPl=i+1
  326.       if (testPoint(x-iPl,y-iPl))and(tmP1) then
  327.         if ((map[y-iPl][x-iPl]~="non")and(tmp1))or((map[y-i][x-i]==yo1)or(map[y-i][x-i]==yo2)) then
  328.           tmP1=false
  329.         end
  330.         if ((map[y-i][x-i]==op)or(map[y-i][x-i]==op2))then--and(map[y-iPl][x-iPl]=="non") then
  331.           if map[y-iPl][x-iPl]=="non" then
  332.             tmp1=true
  333.             num1=i
  334.           else
  335.             tmP1=false
  336.           end
  337.         end
  338.         if (tmp1)and(tmP1)and(bool) then
  339.           selecting(x-iPl,y-iPl,"non",x-num1,y-num1)
  340.         end
  341.       end
  342.       if (testPoint(x-iPl,y+iPl))and(tmP2) then
  343.         if ((map[y+iPl][x-iPl]~="non")and(tmp2))or((map[y+i][x-i]==yo1)or(map[y+i][x-i]==yo2)) then
  344.           tmP2=false
  345.         end
  346.         if ((map[y+i][x-i]==op)or(map[y+i][x-i]==op2))then--and(map[y+iPl][x-iPl]=="non") then
  347.           if map[y+iPl][x-iPl]=="non" then
  348.             tmp2=true
  349.             num2=i
  350.           else
  351.             tmP2=false
  352.           end
  353.         end
  354.         if (tmp2)and(tmP2)and(bool) then
  355.           selecting(x-iPl,y+iPl,"non",x-num2,y+num2)
  356.         end
  357.       end
  358.       if (testPoint(x+iPl,y-iPl))and(tmP3) then
  359.         if ((map[y-iPl][x+iPl]~="non")and(tmp3))or((map[y-i][x+i]==yo1)or(map[y-i][x+i]==yo2)) then
  360.           tmP3=false
  361.         end
  362.         if ((map[y-i][x+i]==op)or(map[y-i][x+i]==op2))then--and(map[y-iPl][x+iPl]=="non") then
  363.           if map[y-iPl][x+iPl]=="non" then
  364.             tmp3=true
  365.             num3=i
  366.           else
  367.             tmP3=false
  368.           end
  369.         end
  370.         if (tmp3)and(tmP3)and(bool) then
  371.           selecting(x+iPl,y-iPl,"non",x+num3,y-num3)
  372.         end
  373.       end
  374.       if (testPoint(x+iPl,y+iPl))and(tmP4) then
  375.         if ((map[y+iPl][x+iPl]~="non")and(tmp4))or((map[y+i][x+i]==yo1)or(map[y+i][x+i]==yo2)) then
  376.           tmP4=false
  377.         end
  378.         if ((map[y+i][x+i]==op)or(map[y+i][x+i]==op2))then--and(map[y+iPl][x+iPl]=="non") then
  379.           if map[y+iPl][x+iPl]=="non" then
  380.             tmp4=true
  381.             num4=i
  382.           else
  383.             tmP4=false
  384.           end
  385.         end
  386.         if (tmp4)and(tmP4)and(bool) then
  387.           selecting(x+iPl,y+iPl,"non",x+num4,y+num4)
  388.         end
  389.       end
  390.     end
  391. --    print((tmp1)or(tmp2)or(tmp3)or(tmp4))
  392.     return (tmp1)or(tmp2)or(tmp3)or(tmp4)
  393.   end
  394.   if (x>2)and(y>2) then
  395.     if ((map[y-1][x-1]==op)or(map[y-1][x-1]==op2))and(map[y-2][x-2]=="non")and(((turn=="red")and(backStab))or(turn=="white")) then
  396.       if bool then
  397.         selecting(x-2,y-2,"non")
  398.       end
  399.       tmp=true
  400.     end
  401.   end
  402.   if (x>2)and(y<7) then
  403.     if ((map[y+1][x-1]==op)or(map[y+1][x-1]==op2))and(map[y+2][x-2]=="non")and(((turn=="white")and(backStab))or(turn=="red")) then
  404.       if bool then
  405.         selecting(x-2,y+2,"non")
  406.       end
  407.       tmp=true
  408.     end
  409.   end
  410.   if (x<7)and(y>2) then
  411.     if ((map[y-1][x+1]==op)or(map[y-1][x+1]==op2))and(map[y-2][x+2]=="non")and(((turn=="red")and(backStab))or(turn=="white")) then
  412.       if bool then
  413.         selecting(x+2,y-2,"non")
  414.       end
  415.       tmp=true
  416.     end
  417.   end
  418.   if (x<7)and(y<7) then
  419.     if ((map[y+1][x+1]==op)or(map[y+1][x+1]==op2))and(map[y+2][x+2]=="non")and(((turn=="white")and(backStab))or(turn=="red")) then
  420.       if bool then
  421.         selecting(x+2,y+2,"non")
  422.       end
  423.       tmp=true
  424.     end
  425.   end
  426. --  print(tmp)
  427.   return tmp
  428. end
  429.  
  430. function testMustBit(turn)
  431.   haveChess=false
  432.   local mas={}
  433.   if turn=="white" then
  434.     mas={"whi","whD"}
  435.   else
  436.     mas={"red","reD"}
  437.   end
  438.   for y=1,8 do
  439.     for x=1,8 do
  440.       if (map[y][x]==mas[1])or(map[y][x]==mas[2]) then
  441.         haveChess=true
  442.         if testBit(x,y,false) then
  443.           return true
  444.         end
  445.       end
  446.     end
  447.   end
  448.   if not haveChess then
  449. --    print("GAME OVER WINNER: "..turnsM[turn])
  450.     messageBox("GAME OVER","Winner: "..turnsM[turn])
  451.   end
  452.   return false
  453. end
  454.  
  455. function onClick(_,_,x,y,_,name)
  456.   if (turn=="white")and(name~=player[1]) then
  457.     return
  458.   end
  459.   if (turn=="red")and(name~=player[2]) then
  460.     return
  461.   end
  462.   x,y=math.floor((x-1)/6)+1,math.floor((y-1)/3)+1
  463.   if ((map[y][x]=="red")or(map[y][x]=="reD"))and(turn=="red") then
  464.     unselectAll()
  465.     select(x,y,map[y][x])
  466.     return
  467.   elseif ((map[y][x]=="whi")or(map[y][x]=="whD"))and(turn=="white") then
  468.     unselectAll()
  469.     select(x,y,map[y][x])
  470.     return
  471.   else
  472.     for i=1,#selected-1 do
  473.       if (selected[i][1]==x)and(selected[i][2]==y) then
  474.         if selected[i][3]~="non" then
  475. --          print("1")
  476.           unselectAll()
  477.         else
  478.           map[y][x]=selected[#selected][3]
  479.           map[selected[#selected][2]][selected[#selected][1]]="non"
  480. --          print(require("serialization").unserialize(selected[#selected]))
  481.           selected[#selected][3]="non"
  482.           selected[i][3]=map[y][x]
  483.           if math.abs(x-selected[#selected][1])>1 then
  484.             if (selected[i][4]==nil)and(mustBit) then
  485.               y2=(y+selected[#selected][2])/2
  486.               x2=(x+selected[#selected][1])/2
  487. --            print(x2,y2)
  488.               map[y2][x2]="non"
  489.               drawImage("non",x2,y2)
  490.               isF=false
  491.             elseif (mustBit) then
  492.               map[selected[i][5]][selected[i][4]]="non"
  493.               drawImage("non",selected[i][4],selected[i][5])
  494. --              print(selected[i][4],selected[i][5])
  495.               isF=false
  496.             else
  497.               isF=true
  498.             end
  499.           else
  500.             isF=true
  501.           end
  502.           if (turn=="white")and(y==1)and(map[y][x]~="whD") then
  503.             map[y][x]="whD"
  504.             selected[i][3]="whD"
  505.           end
  506.           if (turn=="red")and(y==8)and(map[y][x]~="reD") then
  507.             map[y][x]="reD"
  508.             selected[i][3]="reD"
  509.           end
  510.           cantUnselect=false
  511.           unselectAll()
  512. --          print(isF)
  513.           if not isF then
  514.             isF=not select(x,y,map[y][x],true)
  515. --            print(isF)
  516.             if not isF then
  517.               cantUnselect=true
  518.             end
  519.           end
  520.           if isF then
  521.             isF=false
  522.             turn=turnsM[turn]
  523.             mustBit=testMustBit(turn)
  524. --            unselectAll()
  525.           end
  526.         end
  527.       end
  528.     end
  529.   end
  530. end
  531.  
  532. ------------------------------------------------------------------------------
  533. drawField()
  534. event.ignore("touch",onClick)
  535. event.listen("touch",onClick)
  536. mustBit=testMustBit(turn)
  537. event.pull("key_down")
  538. event.ignore("touch",onClick)
  539. --os.sleep(5)
  540. cantUnselect=false
  541. unselectAll()
  542. gpu.setBackground(0x000000)
  543. gpu.setForeground(0xffffff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement