Guest User

cutil

a guest
Sep 22nd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 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,color = colors.white},
  47.         ["piece2"] = {x = 1,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  48.         ["piece3"] = {x = 1,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  49.         ["piece4"] = {x = 1,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  50.         ["piece5"] = {x = 2,y = 2,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  51.         ["piece6"] = {x = 2,y = 4,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  52.         ["piece7"] = {x = 2,y = 6,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  53.         ["piece8"] = {x = 2,y = 8,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  54.         ["piece9"] = {x = 3,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  55.         ["piece10"] = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  56.         ["piece11"] = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
  57.         ["piece12"] = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white}
  58.     },
  59.     ["black"] = {
  60.         ["piece13"] = {x = 1,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  61.         ["piece14"] = {x = 1,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  62.         ["piece15"] = {x = 1,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  63.         ["piece16"] = {x = 1,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  64.         ["piece17"] = {x = 2,y = 2,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  65.         ["piece18"] = {x = 2,y = 4,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  66.         ["piece19"] = {x = 2,y = 6,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  67.         ["piece20"] = {x = 2,y = 8,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  68.         ["piece21"] = {x = 3,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  69.         ["piece22"] = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  70.         ["piece23"] = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
  71.         ["piece24"] = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.black}
  72.     }
  73. }
  74.  
  75. alive = {["white"] = {},["black"] = {}}
  76. dead = {["white"] = {},["black"] = {}}
  77.  
  78. function getAlive()
  79.     for i in pairs(pieces) do
  80.         for k,v in ipairs(pieces[i]) do
  81.             if v.alive then
  82.                 table.insert(alive[i],k)
  83.             elseif not v.alive then
  84.                 table.insert(dead[i],k)
  85.                 pieces[i][k].alive = "false"
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. function getRealCoords(x,y)
  92.     return boardX + x - 1,boardY + y - 1
  93. end
  94.  
  95. function cntrdTxt(txt,x,y)
  96.     mon.setBackgroundColor(colors.black)
  97.     mon.setTextColor(colors.white)
  98.     mon.setCursorPos(offset(-1 * (math.floor(string.len(txt) / 2 + x ) - 1),"x"),1 + x)
  99.     mon.write(txt)
  100. end
  101.  
  102. mods = {}
  103.  
  104. function drawRow(side,x,y,ln)
  105.     mon.setCursorPos(x,y)
  106.     for i=1,math.floor(ln / 2) do
  107.         if side == 1 then
  108.             mon.setBackgroundColor(colors.red)
  109.             mon.write(" ")
  110.             mon.setBackgroundColor(colors.black)
  111.             mon.write(" ")
  112.         elseif side == 2 then
  113.             mon.setBackgroundColor(colors.black)
  114.             mon.write(" ")
  115.             mon.setBackgroundColor(colors.red)
  116.             mon.write(" ")
  117.         end
  118.     end
  119. end
  120.  
  121. function board(wdth,hgt)
  122.     local y = boardY
  123.     for i=1,math.floor(hgt / 2) do
  124.         drawRow(1,boardX,y,wdth)
  125.         y = y + 1
  126.         drawRow(2,boardX,y,wdth)
  127.         y = y + 1
  128.     end
  129. end
  130.  
  131. function mainMenu()
  132.     cntrdTxt("Checkers",0,0)
  133.     button.define("lcl","Local",offset(-6,"x"),offset(-1,"y"),colors.white,colors.red,true)
  134.     button.define("glbl","Globlal",offset(2,"x"),offset(-1,"y"),colors.lightGray,colors.gray,true)
  135.     button.draw("all")
  136.     button.deactivate("glbl")
  137.     local pressed = button.click()
  138.     button.deactivate("all")
  139.     button.erase("all")
  140.     return pressed
  141. end
  142.  
  143. function findPlayer()
  144.     local id = rednet.lookup("cg","cg")
  145.     if type(x) == number then
  146.         os.queueEvent("found_player",id)
  147.     end
  148. end
  149. print("Checkers util API (by minecraftwarlock) loaded")
  150.  
  151. function getMods(path)
  152.     if fs.exists(path) and fs.isDir(path) then
  153.         local rawmods = fs.list(path)
  154.         local mods = {}
  155.         for index,mod in ipairs(rawmods) do
  156.             if not fs.isDir(path.."/"..mod) then
  157.                 table.insert(mods,mod)
  158.             end
  159.         end
  160.         return mods
  161.     else
  162.         error("directory: "..path.." not found")
  163.     end
  164. end
  165.  
  166. function definePieces()
  167.     for k,v in ipairs(pieces) do   
  168.         if not piecesInit then 
  169.             for key,value in ipairs(pieces[k]) do
  170.                 local x1,y1 = getRealCoords(value.x,value.y)
  171.                 if not value.king then
  172.                     button.define(key,"O",x1,y1,value.color,colors.red,false)
  173.                 else
  174.                     button.define(key,"K",x1,y1,value.color,colors.red,false)
  175.                 end
  176.             end
  177.         else
  178.             for key,value in ipairs(dead[k]) do
  179.                 button.delete(value)
  180.             end
  181.         end
  182.     end
  183.     dead = {white = {},black = {}}
  184. end
  185.  
  186. function nextTurn()
  187.     toggleTurn()
  188.     getAlive()
  189.     definePieces()
  190.     if turnNum == 1 then
  191.         for key,value in ipairs(pieces) do
  192.             for k in pairs(value) do
  193.                 button.draw(k)
  194.             end
  195.             button.deactivate(alive["white"])
  196.         end
  197.     else
  198.         if turn == "black" then
  199.             button.activate(alive["black"])
  200.             button.deactivate(alive["white"])
  201.         else
  202.             button.activate(alive.white)
  203.             button.deactivate(alive.black)
  204.         end
  205.     end
  206.     turnNum = turnNum + 1
  207. end
Advertisement
Add Comment
Please, Sign In to add comment