KaoSDlanor

Chess

Nov 22nd, 2012
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. os.loadAPI(shell.resolve('.')..'/add')
  4. local tPieces=add.board()
  5. local tTaken={white={},black={}}
  6. table.insert(tPieces,add.R(1,8,true))
  7. table.insert(tPieces,add.R(8,8,true))
  8. table.insert(tPieces,add.H(2,8,true))
  9. table.insert(tPieces,add.H(7,8,true))
  10. table.insert(tPieces,add.B(3,8,true))
  11. table.insert(tPieces,add.B(6,8,true))
  12. table.insert(tPieces,add.Q(4,8,true))
  13. table.insert(tPieces,add.K(5,8,true))
  14. table.insert(tPieces,add.R(1,1,false))
  15. table.insert(tPieces,add.R(8,1,false))
  16. table.insert(tPieces,add.H(2,1,false))
  17. table.insert(tPieces,add.H(7,1,false))
  18. table.insert(tPieces,add.B(3,1,false))
  19. table.insert(tPieces,add.B(6,1,false))
  20. table.insert(tPieces,add.K(4,1,false))
  21. table.insert(tPieces,add.Q(5,1,false))
  22. for i=1,8 do
  23.     table.insert(tPieces,add.P(i,7,true))
  24.     table.insert(tPieces,add.P(i,2,false))
  25. end
  26. local turn=true
  27. local offsetx,offsety=2,3
  28.  
  29. local function render(x,y,piece,check)
  30.     term.setCursorPos(2,1)
  31.     term.setTextColor(colors.white)
  32.     term.setBackgroundColor(colors.black)
  33.     term.clear()
  34.     if check then
  35.         write('-CHECK-')
  36.     end
  37.     term.setCursorPos(2,2)
  38.     for k,v in pairs(tTaken.white) do
  39.         write(v)
  40.     end
  41.     term.setCursorPos(2,offsety+10)
  42.     for k,v in pairs(tTaken.black) do
  43.         write(v)
  44.     end
  45.     x=x or 1
  46.     y=y or 1
  47.     for cy=1,8 do
  48.         for cx=1,8 do
  49.             term.setCursorPos(x+cx,y+cy)
  50.             term.setBackgroundColor((cx+cy)%2==0 and colors.gray or colors.black)
  51.             term.setTextColor(tPieces(cx,cy).col and colors.white or colors.lightBlue)
  52.             write(tPieces(cx,cy).sym)
  53.         end
  54.     end
  55.     term.setBackgroundColor(colors.white)
  56.     term.setTextColor(colors.black)
  57.     for cy=0,9 do
  58.         term.setCursorPos(x,y+cy)
  59.         write(' ')
  60.         term.setCursorPos(x+9,y+cy)
  61.         write(' ')
  62.     end
  63.     term.setCursorPos(x,y)
  64.     write(' '..(turn and 'white' or 'black')..string.rep(' ',4))
  65.     term.setCursorPos(x,y+9)
  66.     write(string.rep(' ',10))
  67.     if type(piece)=='table' and piece.x then
  68.         term.setCursorPos(piece.x+x,piece.y+y)
  69.         term.setBackgroundColor(colors.purple)
  70.         term.setTextColor(tPieces(piece.x,piece.y).col and colors.white or colors.lightBlue)
  71.         write(piece.sym)
  72.         term.setBackgroundColor(colors.blue)
  73.         for num,t in pairs(piece()) do
  74.             term.setCursorPos(x+t[1],y+t[2])
  75.             term.setTextColor(tPieces(cx,cy).col and colors.white or colors.lightBlue)
  76.             write(tPieces(t[1],t[2]).sym)
  77.         end
  78.     end
  79. end
  80.  
  81. local function chk(turn)
  82.     local king
  83.     for pNum,piece in pairs(tPieces) do
  84.         if piece.sym=='K' and piece.col==turn then king=piece end
  85.     end
  86.     if king==nil then error((turn and 'Black' or 'White')..' player wins!') end
  87.     for pNum,piece in pairs(tPieces) do
  88.         for tNum,t in pairs(piece()) do
  89.             if piece.col~=turn and t[1]==king.x and t[2]==king.y then
  90.                 return true
  91.             end
  92.         end
  93.     end
  94. end
  95.  
  96. local function swap(t)
  97.     local piece,num=tPieces(t.x,t.y)
  98.     term.setCursorPos(13,2)
  99.     write('pick your new piece')
  100.     term.setCursorPos(13,3)
  101.     write('R H B Q')
  102.     local tLoc={R={13,3},H={15,3},B={17,3},Q={19,3}}
  103.     for k,v in pairs(tLoc) do
  104.         term.setCursorPos(unpack(v))
  105.         write(k)
  106.     end
  107.     while true do
  108.         local input={os.pullEvent()}
  109.         if input[1]=='mouse_click' and input[2]==1 then
  110.             for k,v in pairs(tLoc) do
  111.                 if input[3]==v[1] and input[4]==v[2] then
  112.                     tPieces[num]=add[k](piece.x,piece.y,piece.col)
  113.                     return true
  114.                 end
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. local function move(x,y,piece)
  121.     term.setBackgroundColor(colors.white)
  122.     term.setTextColor(colors.black)
  123.     term.setCursorPos(x,y)
  124.     write('x')
  125.     while true do
  126.         local input={os.pullEvent()}
  127.         if input[1]=='mouse_click' and input[2]==1 then
  128.             if input[3]==x and input[4]==y or input[3]==x+piece.x and input[4]==y+piece.y then
  129.                 break
  130.             else
  131.                 for num,t in pairs(piece()) do
  132.                     if t[1]+x==input[3] and t[2]+y==input[4] then
  133.                         local target,pNum=tPieces(t[1],t[2])
  134.                         if pNum then tPieces[pNum]=nil tTaken[target.col and 'white' or 'black'][pNum]=target.sym end
  135.                         local oldx,oldy=piece.x,piece.y
  136.                         piece.x=t[1]
  137.                         piece.y=t[2]
  138.                         term.setBackgroundColor(colors.black)
  139.                         term.setTextColor(colors.white)
  140.                         if chk(turn) then
  141.                             term.setCursorPos(2,1)
  142.                             print('NOPE, will leave you in check')
  143.                             if pNum then tPieces[pNum]=target tTaken[target.col and 'white' or 'black'][pNum]=nil end
  144.                             piece.x=oldx
  145.                             piece.y=oldy
  146.                         else
  147.                             term.setCursorPos(2,1)
  148.                             term.clearLine()
  149.                             piece.hasMoved=true
  150.                             if piece.sym=='P' and (piece.col==true and piece.y==1 or piece.col==false and piece.y==8) then
  151.                                 swap(piece)
  152.                             end
  153.                             return true
  154.                         end
  155.                     end
  156.                 end
  157.             end
  158.         end
  159.     end
  160. end
  161.  
  162.  
  163. render(offsetx,offsety)
  164. while true do
  165.     local input={os.pullEvent()}
  166.     if input[1]=='mouse_click' and input[2]==1 then
  167.         local piece=tPieces(input[3]-offsetx,input[4]-offsety)
  168.         if piece.col==turn then
  169.             render(offsetx,offsety,piece)
  170.             if move(offsetx,offsety,piece) then turn=(not turn) end
  171.             render(offsetx,offsety,nil,chk(turn))
  172.         end
  173.     end
  174. end
Advertisement
Add Comment
Please, Sign In to add comment