Advertisement
Guest User

chess

a guest
Feb 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.19 KB | None | 0 0
  1. function enterSquare(i,j)
  2.   a = setmetatable({},square)
  3.   a.x = i
  4.   a.y = j
  5.   a.moved = false
  6.   if math.floor(i/2) ~= i/2 and math.floor(j/2) ~= j/2 or math.floor(i/2) == i/2 and math.floor(j/2) == j/2 then
  7.     a.color = "brown"
  8.   else
  9.     a.color = "yellow"
  10.   end
  11.   if j == 2 then
  12.     a.piece = 'P'
  13.     a.pc = 'white'
  14.   elseif j == 7 then
  15.     a.piece = 'P'
  16.     a.pc = 'black'
  17.   elseif j == 1 then
  18.     if i == 1 or i == 8 then
  19.       a.piece = 'R'
  20.       a.pc = 'white'
  21.     elseif i == 2 or i == 7 then
  22.       a.piece = 'H'
  23.       a.pc = 'white'
  24.     elseif i == 3 or i == 6 then
  25.       a.piece = 'B'
  26.       a.pc = 'white'
  27.     elseif i == 4 then
  28.       a.piece = 'Q'
  29.       a.pc = 'white'
  30.     elseif i == 5 then
  31.       a.piece = 'K'
  32.       a.pc = 'white'
  33.     end
  34.   elseif j == 8 then
  35.     if i == 1 or i == 8 then
  36.       a.piece = 'R'
  37.       a.pc = 'black'
  38.     elseif i == 2 or i == 7 then
  39.       a.piece = 'H'
  40.       a.pc = 'black'
  41.     elseif i == 3 or i == 6 then
  42.       a.piece = 'B'
  43.       a.pc = 'black'
  44.     elseif i == 4 then
  45.       a.piece = 'Q'
  46.       a.pc = 'black'
  47.     elseif i == 5 then
  48.       a.piece = 'K'
  49.       a.pc = 'black'
  50.     end
  51.   else
  52.     a.pc = 'neutral'
  53.     a.piece = " "
  54.   end
  55.   squares[#squares+1] = a
  56. end
  57.  
  58. function ini()
  59.   m = peripheral.wrap('right')
  60.   m.setBackgroundColor(32768)
  61.   m.clear()
  62.   m.setTextScale(5)
  63.   square = {}
  64.   squares = {}
  65.   square.__index = square
  66.   availMovesX = {}
  67.   availMovesY = {}
  68.   showMoves = false
  69.   blackCheck = false
  70.   whiteCheck = false
  71.   blackCheckMate = false
  72.   whiteCheckMate = false
  73.   playerTurn = 'White'
  74.   for i = 1,8 do
  75.     for j = 1,8 do
  76.       enterSquare(i,j)
  77.     end
  78.   end
  79. end
  80.  
  81. function drawBoard()
  82.   m.setBackgroundColor(32768)
  83.   m.clear()
  84.   for i = 1,#squares do
  85.     bleet = squares[i]
  86.     m.setCursorPos(bleet.x,bleet.y)
  87.     if bleet.color == 'brown' then
  88.       m.setBackgroundColor(4096)
  89.     else  
  90.       m.setBackgroundColor(16)
  91.     end
  92.     if showMoves then
  93.       if available(bleet.x,bleet.y) then
  94.         if bleet.color == 'brown' then
  95.             m.setBackgroundColor(8192)
  96.         else
  97.             m.setBackgroundColor(32)
  98.         end
  99.       end
  100.     end
  101.     if bleet.pc == 'white' then
  102.       m.setTextColor(1)
  103.     else
  104.       m.setTextColor(32768)
  105.     end
  106.     if whiteCheck and bleet.piece == 'K' and bleet.pc == 'white' then
  107.       m.setBackgroundColor(16384)
  108.     end
  109.     if blackCheck and bleet.piece == 'K' and bleet.pc == 'black' then
  110.       m.setBackgroundColor(16384)
  111.     end
  112.     m.write(bleet.piece)
  113.   end
  114.   m.setCursorPos(1,9)
  115.   m.setBackgroundColor(32768)
  116.   m.setTextColor(1)
  117.   m.write(playerTurn.."'s turn!")
  118.   if whiteCheck or blackCheck then
  119.     m.setCursorPos(1,10)
  120.     m.setBackgroundColor(32768)
  121.     m.setTextColor(16384)
  122.     m.write('Check!')
  123.   end
  124.   if whiteCheckMate or blackCheckMate then
  125.     m.setCursorPos(1,10)
  126.     m.setBackgroundColor(32768)
  127.     m.setTextColor(16384)
  128.     m.write('Checkmate!')
  129.   end
  130. end
  131.  
  132. function available(x,y)
  133.   for i = 1,#availMovesX do
  134.     if x == availMovesX[i] and y == availMovesY[i] then
  135.       return true
  136.     end
  137.   end
  138. end
  139.  
  140. function getPiece(x,y)
  141.   for i = 1,#squares do
  142.     deet = squares[i]
  143.     if deet.x == x then
  144.       if deet.y == y then
  145.         return deet,i
  146.       end
  147.     end
  148.   end
  149. end
  150.  
  151. function getAvail(x,y)
  152.   availMovesX = {}
  153.   availMovesY = {}
  154.   for i = 1,8 do
  155.     for j = 1,8 do
  156.       if checkMove2(x,y,i,j) then
  157.         availMovesX[#availMovesX+1] = i
  158.         availMovesY[#availMovesY+1] = j
  159.       end
  160.     end
  161.   end
  162. end
  163.  
  164. function getSudoAvail(x,y)
  165.   sudoAvailX = {}
  166.   sudoAvailY = {}
  167.   sheet = getPiece(x,y)
  168.   if sheet.piece == 'B' or sheet.piece == 'Q' then
  169.     for i = 1,4 do
  170.       for j = 1,8 do
  171.         if i == 1 then
  172.           sudox = sheet.x + j
  173.           sudoy = sheet.y + j
  174.           if sudoy < 9 and sudox < 9 then
  175.             beat = getPiece(sudox,sudoy)
  176.             if beat.piece ~= ' ' then
  177.               if beat.pc ~= sheet.pc then
  178.                 sudoAvailX[#sudoAvailX+1] = sudox
  179.                 sudoAvailY[#sudoAvailY+1] = sudoy
  180.               end
  181.               break
  182.             else
  183.               sudoAvailX[#sudoAvailX+1] = sudox
  184.               sudoAvailY[#sudoAvailY+1] = sudoy
  185.             end
  186.           end
  187.         elseif i == 2 then
  188.           sudox = sheet.x - j
  189.           sudoy = sheet.y + j
  190.           if sudoy < 9 and sudox > 0 then
  191.             beat = getPiece(sudox,sudoy)
  192.             if beat.piece ~= ' ' then
  193.               if beat.pc ~= sheet.pc then
  194.                 sudoAvailX[#sudoAvailX+1] = sudox
  195.                 sudoAvailY[#sudoAvailY+1] = sudoy
  196.               end
  197.               break
  198.             else
  199.               sudoAvailX[#sudoAvailX+1] = sudox
  200.               sudoAvailY[#sudoAvailY+1] = sudoy
  201.             end
  202.           end
  203.         elseif i == 3 then
  204.           sudox = sheet.x + j
  205.           sudoy = sheet.y - j
  206.           if sudox < 9 and sudoy > 0 then
  207.             beat = getPiece(sudox,sudoy)
  208.             if beat.piece ~= ' ' then
  209.               if beat.pc ~= sheet.pc then
  210.                 sudoAvailX[#sudoAvailX+1] = sudox
  211.                 sudoAvailY[#sudoAvailY+1] = sudoy
  212.               end
  213.               break
  214.             else
  215.               sudoAvailX[#sudoAvailX+1] = sudox
  216.               sudoAvailY[#sudoAvailY+1] = sudoy
  217.             end
  218.           end
  219.         else
  220.           sudox = sheet.x - j
  221.           sudoy = sheet.y - j
  222.           if sudoy > 0 and sudox > 0 then
  223.             beat = getPiece(sudox,sudoy)
  224.             if beat.piece ~= ' ' then
  225.               if beat.pc ~= sheet.pc then
  226.                 sudoAvailX[#sudoAvailX+1] = sudox
  227.                 sudoAvailY[#sudoAvailY+1] = sudoy
  228.               end
  229.               break
  230.             else
  231.               sudoAvailX[#sudoAvailX+1] = sudox
  232.               sudoAvailY[#sudoAvailY+1] = sudoy
  233.             end
  234.           end
  235.         end
  236.       end
  237.     end
  238.   end
  239.   if sheet.piece == 'R' or sheet.piece == 'Q' then
  240.     for i = 1,4 do
  241.       for j = 1,8 do
  242.         if i == 1 then
  243.           sudox = sheet.x + j
  244.           sudoy = sheet.y
  245.           if sudox < 9 then
  246.             beat = getPiece(sudox,sudoy)
  247.             if beat.piece ~= ' ' then
  248.               if beat.pc ~= sheet.pc then
  249.                 sudoAvailX[#sudoAvailX+1] = sudox
  250.                 sudoAvailY[#sudoAvailY+1] = sudoy
  251.               end
  252.               break
  253.             else
  254.               sudoAvailX[#sudoAvailX+1] = sudox
  255.               sudoAvailY[#sudoAvailY+1] = sudoy
  256.             end
  257.           end
  258.         elseif i == 2 then
  259.           sudox = sheet.x - j
  260.           sudoy = sheet.y
  261.           if sudox > 0 then
  262.             beat = getPiece(sudox,sudoy)
  263.             if beat.piece ~= ' ' then
  264.               if beat.pc ~= sheet.pc then
  265.                 sudoAvailX[#sudoAvailX+1] = sudox
  266.                 sudoAvailY[#sudoAvailY+1] = sudoy
  267.               end
  268.               break
  269.             else
  270.               sudoAvailX[#sudoAvailX+1] = sudox
  271.               sudoAvailY[#sudoAvailY+1] = sudoy
  272.             end
  273.           end
  274.         elseif i == 3 then
  275.           sudox = sheet.x
  276.           sudoy = sheet.y + j
  277.           if sudoy < 9 then
  278.             beat = getPiece(sudox,sudoy)
  279.             if beat.piece ~= ' ' then
  280.               if beat.pc ~= sheet.pc then
  281.                 sudoAvailX[#sudoAvailX+1] = sudox
  282.                 sudoAvailY[#sudoAvailY+1] = sudoy
  283.               end
  284.               break
  285.             else
  286.               sudoAvailX[#sudoAvailX+1] = sudox
  287.               sudoAvailY[#sudoAvailY+1] = sudoy
  288.             end
  289.           end
  290.         else
  291.           sudox = sheet.x
  292.           sudoy = sheet.y - j
  293.           if sudoy > 0 then
  294.             beat = getPiece(sudox,sudoy)
  295.             if beat.piece ~= ' ' then
  296.               if beat.pc ~= sheet.pc then
  297.                 sudoAvailX[#sudoAvailX+1] = sudox
  298.                 sudoAvailY[#sudoAvailY+1] = sudoy
  299.               end
  300.               break
  301.             else
  302.               sudoAvailX[#sudoAvailX+1] = sudox
  303.               sudoAvailY[#sudoAvailY+1] = sudoy
  304.             end
  305.           end
  306.         end
  307.       end
  308.     end
  309.   end
  310.   return sudoAvailX,sudoAvailY
  311. end
  312.  
  313. function checkMove(x1,y1,x2,y2)
  314.   local yeet = getPiece(x1,y1)
  315.   local neet = getPiece(x2,y2)
  316.   if yeet.piece == ' ' then
  317.     return false
  318.   end
  319.   if yeet.pc == neet.pc and neet.piece ~= ' ' then
  320.     return false
  321.   end
  322.   if x1 == x2 and y2 == y1 then
  323.     return false
  324.   end
  325.   if x2 > 8 or x2 <1 or y2 > 8 or y2 < 1 then
  326.     return false
  327.   end
  328.   if yeet.piece == 'B' or yeet.piece == 'R' or yeet.piece == 'Q' then
  329.     tabx,taby = getSudoAvail(x1,y1)
  330.     for i = 1,#tabx do
  331.       if x2 == tabx[i] and y2 == taby[i] then
  332.         return true
  333.       end
  334.     end
  335.     return false
  336.   end
  337.   if yeet.piece == 'H' then
  338.     if x2 == x1 + 2 then
  339.       if y2 == y1 + 1 or y2 == y1 - 1 then
  340.         return true
  341.       end
  342.     elseif x2 == x1 - 2 then
  343.       if y2 == y1 + 1 or y2 == y1 - 1 then
  344.         return true
  345.       end
  346.     elseif y2 == y1 + 2 then
  347.       if x2 == x1 + 1 or x2 == x1 - 1 then
  348.         return true
  349.       end
  350.     elseif y2 == y1 - 2 then
  351.       if x2 == x1 + 1 or x2 == x1 - 1 then
  352.         return true
  353.       end
  354.     end
  355.   end
  356.   if yeet.piece == 'K' then
  357.     if y2 == y1 + 1 and x1 == x2 or y2 == y1 - 1 and x1 == x2 or x2 == x1 + 1 and y1 == y2 or x2 == x1 - 1 and y1 == y2 or x1 + 1 == x2 and y1 + 1 == y2 or x1 - 1 == x2 and y1 + 1 == y2 or x1 + 1 == x2 and y1 - 1 == y2 or x1 - 1 == x2 and y1 - 1 == y2 then
  358.       return true
  359.     elseif x2 == x1 + 2 and y2 == y1 or x2 == x1 - 2 and y2 == y1 then
  360.         if yeet.moved then
  361.             return false
  362.         end
  363.         if yeet.pc == 'white' then
  364.             if checkWhite then
  365.                 return false
  366.             end
  367.             if x2 == x1 + 2 and y2 == y1 then
  368.                 temp1 = getPiece(7,1)
  369.                 temp2 = getPiece(6,1)
  370.                 if temp1.piece ~= ' ' then
  371.                     return false
  372.                 end
  373.                 if temp2.piece ~= ' ' then
  374.                     return false
  375.                 end
  376.                 rook = getPiece(8,1)
  377.                 if rook.piece == 'R' and not rook.moved then
  378.                     castleTopRight = true
  379.                     return true
  380.                 end
  381.             elseif x2 == x1 - 2 and y2 == y1 then
  382.                 temp1 = getPiece(4,1)
  383.                 temp2 = getPiece(3,1)
  384.                 temp3 = getPiece(2,1)
  385.                 if temp1.piece ~= ' ' then
  386.                     return false
  387.                 end
  388.                 if temp2.piece ~= ' ' then
  389.                     return false
  390.                 end
  391.                 if temp3.piece ~= ' ' then
  392.                     return false
  393.                 end
  394.                 rook = getPiece (1,1)
  395.                 if rook.piece == 'R' and not rook.moved then
  396.                     castleTopLeft = true
  397.                     return true
  398.                 end
  399.             end
  400.         elseif yeet.pc == 'black' then
  401.             if checkBlack then
  402.                 return false
  403.             end
  404.             if x2 == x1 + 2 and y2 == y1 then
  405.                 temp1 = getPiece(7,8)
  406.                 temp2 = getPiece(6,8)
  407.                 if temp1.piece ~= ' ' then
  408.                     return false
  409.                 end
  410.                 if temp2.piece ~= ' ' then
  411.                     return false
  412.                 end
  413.                 rook = getPiece(8,8)
  414.                 if rook.piece == 'R' and not rook.moved then
  415.                     castleBottomRight = true
  416.                     return true
  417.                 end
  418.             elseif x2 == x1 - 2 and y2 == y1 then
  419.                 temp1 = getPiece(4,8)
  420.                 temp2 = getPiece(3,8)
  421.                 temp3 = getPiece(2,8)
  422.                 if temp1.piece ~= ' ' then
  423.                     return false
  424.                 end
  425.                 if temp2.piece ~= ' ' then
  426.                     return false
  427.                 end
  428.                 if temp3.piece ~= ' ' then
  429.                     return false
  430.                 end
  431.                 rook = getPiece (1,8)
  432.                 if rook.piece == 'R' and not rook.moved then
  433.                     castleBottomLeft = true
  434.                     return true
  435.                 end
  436.             end
  437.         end
  438.     end
  439.   end
  440.   if yeet.piece == 'P' and yeet.pc == 'white' then
  441.     leet = getPiece(x1,y1+1)
  442.     if y2 == y1 + 1 and neet.piece == ' ' and x2 == x1  then
  443.       return true
  444.     elseif y2 == y1 + 2 and neet.piece == ' ' and leet.piece == ' ' and not yeet.moved and x2 == x1 then
  445.       return true
  446.     elseif y2 == y1 + 1 and x2 == x1 + 1 and neet.piece ~= ' ' then
  447.       return true
  448.     elseif y2 == y1 + 1 and x2 == x1 - 1 and neet.piece ~= ' ' then
  449.       return true
  450.     end
  451.   end
  452.   if yeet.piece == 'P' and yeet.pc == 'black' then
  453.     leet = getPiece(x1,y1-1)
  454.     if y2 == y1 - 1 and neet.piece == ' ' and x2 == x1 then
  455.       return true
  456.     elseif y2 == y1 - 2 and neet.piece == ' ' and leet.piece == ' ' and not yeet.moved and x2 == x1 then
  457.       return true
  458.     elseif y2 == y1 - 1 and x2 == x1 + 1 and neet.piece ~= ' ' then
  459.       return true
  460.     elseif y2 == y1 - 1 and x2 == x1 - 1 and neet.piece ~= ' ' then
  461.       return true
  462.     end
  463.   end
  464.  
  465. end
  466.  
  467. function checkMove2(x1,y1,x2,y2)
  468.   heat = getPiece(x1,y1)
  469.   backupBoard()
  470.   if checkMove(x1,y1,x2,y2) then
  471.     moveHandler(x1,y1,x2,y2)
  472.     heat = getPiece(x2,y2)
  473.     if heat.pc == 'white' then
  474.       if checkCheckWhite() then
  475.         recoverBoard()
  476.         return false
  477.       else
  478.         recoverBoard()
  479.       end
  480.     else
  481.       if checkCheckBlack() then
  482.         recoverBoard()
  483.         return false
  484.       else
  485.         recoverBoard()
  486.       end
  487.     end
  488.   else
  489.     return false
  490.   end
  491.   return true
  492. end
  493.  
  494. function checkCheckWhite()
  495.   for i = 1,#squares do
  496.     if squares[i].piece == 'K' and squares[i].pc == 'white' then
  497.       king = squares[i]
  498.       break
  499.     end
  500.   end
  501.   xt = king.x
  502.   yt = king.y
  503.   for i = 1,#squares do
  504.     if checkMove(squares[i].x,squares[i].y,xt,yt) then
  505.       return true
  506.     end
  507.   end
  508. end
  509.  
  510. function checkCheckMateWhite()
  511.   backupBoard()
  512.   for i = 1,#squares do
  513.     feet = squares[i]
  514.     if feet.pc == 'white' then
  515.       for x = 1,8 do
  516.         for y = 1,8 do
  517.           if checkMove(feet.x,feet.y,x,y) then
  518.             moveHandler(feet.x,feet.y,x,y)
  519.             if not checkCheckWhite() then
  520.               recoverBoard()
  521.               return false
  522.             end
  523.             recoverBoard()
  524.           end
  525.         end
  526.       end    
  527.     end
  528.   end
  529.   return true    
  530. end
  531.  
  532. function checkCheckBlack()
  533.   for i = 1,#squares do
  534.     if squares[i].piece == 'K' and squares[i].pc == 'black' then
  535.       king = squares[i]
  536.       break
  537.     end
  538.   end
  539.   xt = king.x
  540.   yt = king.y
  541.   for i = 1,#squares do
  542.     if checkMove(squares[i].x,squares[i].y,xt,yt) then
  543.       return true
  544.     end
  545.   end
  546. end
  547.  
  548. function checkCheckMateBlack()
  549.   backupBoard()
  550.   for i = 1,#squares do
  551.     feet = squares[i]
  552.     if feet.pc == 'black' then
  553.       for x = 1,8 do
  554.         for y = 1,8 do
  555.           if checkMove(feet.x,feet.y,x,y) then
  556.             moveHandler(feet.x,feet.y,x,y)
  557.             if not checkCheckBlack() then
  558.               recoverBoard()
  559.               return false
  560.             end
  561.             recoverBoard()
  562.           end
  563.         end
  564.       end    
  565.     end
  566.   end
  567.   return true    
  568. end
  569.  
  570. function backupBoard()
  571.   bBoard = {}
  572.   for i = 1,#squares do
  573.     bBoard[i] = {}
  574.     for k,v in pairs(squares[i]) do
  575.       bBoard[i][k] = squares[i][k]
  576.     end
  577.   end
  578. end
  579.  
  580. function recoverBoard()
  581.   squares = {}
  582.   for i = 1,#bBoard do
  583.     squares[i] = {}
  584.     for k,v in pairs(bBoard[i]) do
  585.       squares[i][k] = bBoard[i][k]
  586.     end
  587.   end
  588. end
  589.  
  590. function moveHandler(x1,y1,x2,y2)
  591.   p1,num1 = getPiece(x1,y1)
  592.   p2,num2 = getPiece(x2,y2)
  593.   p2.pc = p1.pc
  594.   p2.piece = p1.piece
  595.   p2.moved = true
  596.   p1.pc = 'neutral'
  597.   p1.piece = ' '
  598.   squares[num1].pc = p1.pc
  599.   squares[num1].piece = p1.piece
  600.   squares[num1].moved = p1.moved
  601.   squares[num2].pc = p2.pc
  602.   squares[num2].piece = p2.piece
  603.   squares[num2].moved = p2.moved
  604. end
  605.  
  606. function turnHandler(player)
  607.   while true do
  608.     event,key,x1,y1 = os.pullEvent('monitor_touch')
  609.     jeet = getPiece(x1,y1)
  610.     if jeet.pc == player then
  611.       getAvail(x1,y1)
  612.       showMoves = true
  613.       drawBoard()
  614.       while true do
  615.         event,key,x2,y2 = os.pullEvent('monitor_touch')
  616.         if checkMove2(x1,y1,x2,y2) then
  617.           temp = getPiece(x1,y1)
  618.           if temp.piece == 'K' and x2 == x1 + 2 and y2 == y1 or temp.piece == 'K' and x2 == x1 - 2 and y2 == y1 then
  619.             if temp.pc == 'white' then
  620.                 if x2 == 7 and y2 == 1 and castleTopRight then
  621.                     ctr = true
  622.                 elseif x2 == 3 and y2 == 1 and castleTopLeft then
  623.                     moveHandler(1,1,4,1)
  624.                 end
  625.             else
  626.                 if x2 == 7 and y2 == 8 and castleBottomRight then
  627.                     cbr = true
  628.                 elseif x2 == 3 and y2 == 8 and castleBottomLeft then
  629.                     moveHandler(1,8,4,8)
  630.                 end
  631.             end
  632.           end
  633.           moveHandler(x1,y1,x2,y2)
  634.           if ctr then
  635.             moveHandler(8,1,6,1)
  636.             ctr = false
  637.           end
  638.           if cbr then
  639.             moveHandler(8,8,6,8)
  640.             cbr = false
  641.           end
  642.           showMoves = false
  643.           if player == 'white' then
  644.             playerTurn = 'Black'
  645.             return
  646.           else
  647.             playerTurn = 'White'
  648.             return
  649.           end
  650.         else
  651.           showMoves = false
  652.           drawBoard()
  653.           break
  654.         end
  655.       end
  656.     end
  657.   end
  658. end
  659.  
  660. ini()
  661.  
  662. while true do
  663.  drawBoard()
  664.  if playerTurn == 'White' then
  665.    turnHandler('white')
  666.    if checkCheckBlack() then
  667.     if checkCheckMateBlack() then
  668.         blackCheckMate = true
  669.         drawBoard()
  670.         break
  671.     else
  672.         blackCheck = true
  673.     end
  674.  end
  675.  whiteCheck = false
  676.  else
  677.    turnHandler('black')
  678.    if checkCheckWhite() then
  679.     if checkCheckMateWhite() then
  680.         whiteCheckMate = true
  681.         drawBoard()
  682.         break
  683.     else
  684.         whiteCheck = true
  685.     end
  686.    end
  687.    blackCheck = false
  688.  end
  689. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement