Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not os.loadAPI("button") then
- error("Checkers util API requires the button API by minecraftwarlock to run")
- end
- mon = peripheral.wrap("left")
- button.default()
- xSize,ySize = mon.getSize()
- turn = "white"
- piecesInit = false
- turnNum = 1
- function getKeys(tbl)
- local out = {}
- for k,v in ipairs(tbl) do
- table.insert(out,k)
- end
- return out
- end
- function toggleTurn()
- if turn == "white" then
- turn = "black"
- else
- turn = "white"
- end
- end
- function offset(x,coord)
- if string.lower(coord) == "x" then
- return math.floor(xSize / 2) + x
- elseif string.lower(coord) == "y" then
- return math.floor(ySize / 2) + x
- end
- end
- boardX = offset(-3,"x")
- boardY = offset(-3,"y")
- menuButtons = {}
- pieces = {
- ["white"] = {
- ["piece1"] = {x = 1,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece2"] = {x = 1,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece3"] = {x = 1,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece4"] = {x = 1,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece5"] = {x = 2,y = 2,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece6"] = {x = 2,y = 4,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece7"] = {x = 2,y = 6,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece8"] = {x = 2,y = 8,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece9"] = {x = 3,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece10"] = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece11"] = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece12"] = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white}
- },
- ["black"] = {
- ["piece13"] = {x = 1,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece14"] = {x = 1,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece15"] = {x = 1,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece16"] = {x = 1,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece17"] = {x = 2,y = 2,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece18"] = {x = 2,y = 4,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece19"] = {x = 2,y = 6,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece20"] = {x = 2,y = 8,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece21"] = {x = 3,y = 1,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece22"] = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece23"] = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.black},
- ["piece24"] = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.black}
- }
- }
- alive = {["white"] = {},["black"] = {}}
- dead = {["white"] = {},["black"] = {}}
- function getAlive()
- for i in pairs(pieces) do
- for k,v in ipairs(pieces[i]) do
- if v.alive then
- table.insert(alive[i],k)
- elseif not v.alive then
- table.insert(dead[i],k)
- pieces[i][k].alive = "false"
- end
- end
- end
- end
- function getRealCoords(x,y)
- return boardX + x - 1,boardY + y - 1
- end
- function cntrdTxt(txt,x,y)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.setCursorPos(offset(-1 * (math.floor(string.len(txt) / 2 + x ) - 1),"x"),1 + x)
- mon.write(txt)
- end
- mods = {}
- function drawRow(side,x,y,ln)
- mon.setCursorPos(x,y)
- for i=1,math.floor(ln / 2) do
- if side == 1 then
- mon.setBackgroundColor(colors.red)
- mon.write(" ")
- mon.setBackgroundColor(colors.black)
- mon.write(" ")
- elseif side == 2 then
- mon.setBackgroundColor(colors.black)
- mon.write(" ")
- mon.setBackgroundColor(colors.red)
- mon.write(" ")
- end
- end
- end
- function board(wdth,hgt)
- local y = boardY
- for i=1,math.floor(hgt / 2) do
- drawRow(1,boardX,y,wdth)
- y = y + 1
- drawRow(2,boardX,y,wdth)
- y = y + 1
- end
- end
- function mainMenu()
- cntrdTxt("Checkers",0,0)
- button.define("lcl","Local",offset(-6,"x"),offset(-1,"y"),colors.white,colors.red,true)
- button.define("glbl","Globlal",offset(2,"x"),offset(-1,"y"),colors.lightGray,colors.gray,true)
- button.draw("all")
- button.deactivate("glbl")
- local pressed = button.click()
- button.deactivate("all")
- button.erase("all")
- return pressed
- end
- function findPlayer()
- local id = rednet.lookup("cg","cg")
- if type(x) == number then
- os.queueEvent("found_player",id)
- end
- end
- print("Checkers util API (by minecraftwarlock) loaded")
- function getMods(path)
- if fs.exists(path) and fs.isDir(path) then
- local rawmods = fs.list(path)
- local mods = {}
- for index,mod in ipairs(rawmods) do
- if not fs.isDir(path.."/"..mod) then
- table.insert(mods,mod)
- end
- end
- return mods
- else
- error("directory: "..path.." not found")
- end
- end
- function definePieces()
- for k,v in ipairs(pieces) do
- if not piecesInit then
- for key,value in ipairs(pieces[k]) do
- local x1,y1 = getRealCoords(value.x,value.y)
- if not value.king then
- button.define(key,"O",x1,y1,value.color,colors.red,false)
- else
- button.define(key,"K",x1,y1,value.color,colors.red,false)
- end
- end
- else
- for key,value in ipairs(dead[k]) do
- button.delete(value)
- end
- end
- end
- dead = {white = {},black = {}}
- end
- function nextTurn()
- toggleTurn()
- getAlive()
- definePieces()
- if turnNum == 1 then
- for key,value in ipairs(pieces) do
- for k in pairs(value) do
- button.draw(k)
- end
- button.deactivate(alive["white"])
- end
- else
- if turn == "black" then
- button.activate(alive["black"])
- button.deactivate(alive["white"])
- else
- button.activate(alive.white)
- button.deactivate(alive.black)
- end
- end
- turnNum = turnNum + 1
- end
Advertisement
Add Comment
Please, Sign In to add comment