Advertisement
Guest User

cutil

a guest
Sep 22nd, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1. if not os.loadAPI("button") then
  2.     error("Checkers util API requires the button API by minecraftwarlock to run")
  3. end
  4.  
  5. mon = peripheral.wrap("left")
  6.  
  7. button.default()
  8.  
  9. xSize,ySize = mon.getSize()
  10.  
  11. turn = "white"
  12. piecesInit = false
  13. turnNum = 1
  14.  
  15. function getKeys(tbl)
  16.     local out = {}
  17.     for k,v in ipairs(tbl) do
  18.         table.insert(out,k)
  19.     end
  20.     return out
  21. end
  22.  
  23. function toggleTurn()
  24.     if turn == "white" then
  25.         turn = "black"
  26.     else
  27.         turn = "white"
  28.     end
  29. end
  30.  
  31. function offset(x,coord)
  32.     if string.lower(coord) == "x" then
  33.         return math.floor(xSize / 2) + x
  34.     elseif string.lower(coord) == "y" then
  35.         return math.floor(ySize / 2) + x
  36.     end
  37. end
  38.  
  39. boardX = offset(-3,"x")
  40. boardY = offset(-3,"y")
  41.  
  42. menuButtons = {}
  43.  
  44. pieces = {
  45. white = {
  46. "piece1" = {x = 1 , y = 1 , moves = 0 , taken = 0 , alive = true , king = false} , "piece2" = {x = 1 , y = 3 , moves = 0 , taken = 0 , alive = true , king = false} ,
  47. "piece3" = {x = 1,y = 5,moves = 0,taken = 0,alive = true,king = false},"piece4" = {x = 1,y = 7,moves = 0,taken = 0,alive = true,king = false},
  48. "piece5" = {x = 2,y = 2,moves = 0,taken = 0,alive = true,king = false},"piece6" = {x = 2,y = 4,moves = 0,taken = 0,alive = true,king = false},
  49. "piece7" = {x = 2,y = 6,moves = 0,taken = 0,alive = true,king = false},"piece8" = {x = 2,y = 8,moves = 0,taken = 0,alive = true,king = false},
  50. "piece9" = {x = 3,y = 1,moves = 0,taken = 0,alive = true,king = false},"piece10" = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false},
  51. "piece11" = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false},"piece12" = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false}
  52. },
  53. black = {
  54. "piece13" = {x = 1,y = 1,moves = 0,taken = 0,alive = true,king = false},"piece14" = {x = 1,y = 3,moves = 0,taken = 0,alive = true,king = false},
  55. "piece15" = {x = 1,y = 5,moves = 0,taken = 0,alive = true,king = false},"piece16" = {x = 1,y = 7,moves = 0,taken = 0,alive = true,king = false},
  56. "piece17" = {x = 2,y = 2,moves = 0,taken = 0,alive = true,king = false},"piece18" = {x = 2,y = 4,moves = 0,taken = 0,alive = true,king = false},
  57. "piece19" = {x = 2,y = 6,moves = 0,taken = 0,alive = true,king = false},"piece20" = {x = 2,y = 8,moves = 0,taken = 0,alive = true,king = false},
  58. "piece21" = {x = 3,y = 1,moves = 0,taken = 0,alive = true,king = false},"piece22" = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false},
  59. "piece23" = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false},"piece24" = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false}
  60. }
  61. }
  62.  
  63. alive = {white = {},black = {}}
  64. dead = {white = {},black = {}}
  65.  
  66. function getAlive()
  67.     for i in pairs(pieces) 
  68.         for k,v in ipairs(pieces.i) do
  69.             if v.alive then
  70.                 table.insert(alive.i,k)
  71.             elseif not v.alive
  72.                 table.insert(dead.i,k)
  73.                 pieces.k.alive = "false"
  74.             end
  75.         end
  76.     end
  77. end
  78.  
  79. function getRealCoords(x,y)
  80.     return boardX + x - 1,boardY + y - 1
  81. end
  82.  
  83. function cntrdTxt(txt,x,y)
  84.     mon.setBackgroundColor(colors.black)
  85.     mon.setTextColor(colors.white)
  86.     mon.setCursorPos(offset(-1 * (math.floor(string.len(txt) / 2 + x ) - 1),"x"),1 + x)
  87.     mon.write(txt)
  88. end
  89.  
  90. mods = {}
  91.  
  92. function drawRow(side,x,y)
  93.     mon.setCursorPos(x,y)
  94.     for i=1,4 do
  95.         if side == 1 then
  96.             mon.setBackgroundColor(colors.red)
  97.             mon.write(" ")
  98.             mon.setBackgroundColor(colors.black)
  99.             mon.write(" ")
  100.         elseif side == 2 then
  101.             mon.setBackgroundColor(colors.black)
  102.             mon.write(" ")
  103.             mon.setBackgroundColor(colors.red)
  104.             mon.write(" ")
  105.         end
  106.     end
  107. end
  108.  
  109. function board()
  110.     local y = boardY
  111.     for i=1,4 do
  112.         drawRow(1,boardX,y)
  113.         y = y + 1
  114.         drawRow(2,boardX,y)
  115.         y = y + 1
  116.     end
  117. end
  118.  
  119. function mainMenu()
  120.     cntrdTxt("Checkers",0,0)
  121.     button.define("lcl","Local",offset(-6,"x"),offset(-1,"y"),colors.white,colors.red,true)
  122.     button.define("glbl","Globlal",offset(2,"x"),offset(-1,"y"),colors.lightGray,colors.gray,true)
  123.     button.draw("all")
  124.     button.deactivate("glbl")
  125.     local pressed = button.click()
  126.     button.deactivate("all")
  127.     button.erase("all")
  128.     return pressed
  129. end
  130.  
  131. function findPlayer()
  132.     local id = rednet.lookup("cg","cg")
  133.     if type(x) == number then
  134.         os.queueEvent("found_player",id)
  135.     end
  136. end
  137. print("Checkers util API (by minecraftwarlock) loaded")
  138.  
  139. function getMods(path)
  140.     if fs.exists(path) and fs.isDir(path) then
  141.         local rawmods = fs.list(path)
  142.         local mods = {}
  143.         for index,mod in ipairs(rawmods) do
  144.             if not fs.isDir(path.."/"..mod) then
  145.                 table.insert(mods,mod)
  146.             end
  147.         end
  148.         return mods
  149.     else
  150.         error("directory: "..path.." not found")
  151.     end
  152. end
  153.  
  154. function definePieces()
  155.     for k,v in ipairs(pieces) do   
  156.         if not piecesInit then 
  157.             for key,value in ipairs(pieces.k) do
  158.                 local x1,y1 = getRealCoords(value.x,value.y)
  159.                 if not value.king then
  160.                     button.define(key,"O",x1,y1,colors.k,colors.red,false)
  161.                 else
  162.                     button.define(key,"K",x1,y1,colors.k,colors.red,false)
  163.             end
  164.         else
  165.             for key,value in ipairs(dead.k) do
  166.                 button.delete(value)
  167.             end
  168.         end
  169.     end
  170.     dead = {white = {},black = {}}
  171. end
  172.  
  173. function nextTurn()
  174.     toggleTurn()
  175.     getAlive()
  176.     definePieces()
  177.     if turnNum == 1 then
  178.         local pieceKeys = {table.unpack(alive.white),table.unpack(alive.black)}
  179.         button.draw(pieceKeys)
  180.         button.deactivate("white")
  181.     else
  182.         if turn == "black" then
  183.             button.draw(alive.black)
  184.             button.deactivate(alive.white)
  185.         else
  186.             button.draw(alive.white)
  187.             button.deactivate(alive.black)
  188.         end
  189.     end
  190. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement