Advertisement
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} , "piece2" = {x = 1 , y = 3 , moves = 0 , taken = 0 , alive = true , king = false} ,
- "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},
- "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},
- "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},
- "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},
- "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}
- },
- black = {
- "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},
- "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},
- "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},
- "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},
- "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},
- "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}
- }
- }
- alive = {white = {},black = {}}
- dead = {white = {},black = {}}
- function getAlive()
- for i in pairs(pieces)
- for k,v in ipairs(pieces.i) do
- if v.alive then
- table.insert(alive.i,k)
- elseif not v.alive
- table.insert(dead.i,k)
- pieces.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)
- mon.setCursorPos(x,y)
- for i=1,4 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()
- local y = boardY
- for i=1,4 do
- drawRow(1,boardX,y)
- y = y + 1
- drawRow(2,boardX,y)
- 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,colors.k,colors.red,false)
- else
- button.define(key,"K",x1,y1,colors.k,colors.red,false)
- 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
- local pieceKeys = {table.unpack(alive.white),table.unpack(alive.black)}
- button.draw(pieceKeys)
- button.deactivate("white")
- else
- if turn == "black" then
- button.draw(alive.black)
- button.deactivate(alive.white)
- else
- button.draw(alive.white)
- button.deactivate(alive.black)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement