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"
- opponent = "black"
- piecesInit = false
- turnNum = 1
- winner = false
- function getKeys(tbl)
- local out = {}
- for k,v in pairs(tbl) do
- table.insert(out,k)
- end
- return out
- end
- function toggleTurn()
- if turn == "white" then
- turn = "black"
- opponent = "white"
- else
- turn = "white"
- opponent = "black"
- end
- end
- function isPresent(i,tbl)
- for k,v in ipairs(tbl) do
- if v == i then
- return true
- end
- end
- return false
- 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
- function onBoard(X,Y)
- if X < 1 or X > 8 or Y < 1 or Y > 8 then
- return false
- end
- return true
- end
- function getLength(tbl)
- local out = 0
- for i in ipairs(tbl) do
- out = out + 1
- end
- end
- boardX = offset(-3,"x")
- boardY = offset(-3,"y")
- pieces = {
- ["white"] = {
- ["piece1"] = {x = 7,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece2"] = {x = 7,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece3"] = {x = 7,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece4"] = {x = 5,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece5"] = {x = 5,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece6"] = {x = 5,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece7"] = {x = 3,y = 7,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece8"] = {x = 3,y = 5,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece9"] = {x = 3,y = 3,moves = 0,taken = 0,alive = true,king = false,color = colors.white},
- ["piece10"] = {x = 5,y = 3,moves = 0,taken = 0,alive = false,king = false,color = colors.white},
- ["piece11"] = {x = 3,y = 5,moves = 0,taken = 0,alive = false,king = false,color = colors.white},
- ["piece12"] = {x = 3,y = 7,moves = 0,taken = 0,alive = false,king = false,color = colors.white}
- },
- ["black"] = {
- ["piece13"] = {x = 6,y = 2,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece14"] = {x = 6,y = 4,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece15"] = {x = 6,y = 6,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece16"] = {x = 6,y = 8,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece17"] = {x = 7,y = 1,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece18"] = {x = 7,y = 3,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece19"] = {x = 7,y = 5,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece20"] = {x = 7,y = 7,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece21"] = {x = 8,y = 2,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece22"] = {x = 8,y = 4,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece23"] = {x = 8,y = 6,moves = 0,taken = 0,alive = false,king = false,color = colors.black},
- ["piece24"] = {x = 8,y = 8,moves = 0,taken = 0,alive = true,king = true,color = colors.black}
- }
- }
- alive = {["white"] = {},["black"] = {}}
- dead = {["white"] = {},["black"] = {}}
- function getAlive()
- for i in pairs(pieces) do
- for k,v in pairs(pieces[i]) do
- if v.alive and not isPresent(k,alive[i]) 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 getBoardCoords(x,y)
- return x - boardX + 1,y - boardY + 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)
- button.bg(boardX - 1,boardY - 1,10,10,colors.gray)
- 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 definePieces()
- for k,v in pairs(pieces) do
- if not piecesInit then
- for key,value in pairs(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 occupied(X,Y)
- for i in pairs(alive) do
- for k,v in ipairs(alive[i]) do
- if X == pieces[i][v].x and Y == pieces[i][v].y then
- return true,i,v
- end
- end
- end
- return false
- end
- function isJump(piece,x1,y1)
- local jumps = {}
- local X = pieces[turn][piece].x
- local Y = pieces[turn][piece].y
- if type(x1) == "number" and type(y1) == "number" then
- X,Y = x1,y1
- end
- local val,color,p = occupied(X - 1,Y + 1)
- local val2 = occupied(X - 2,Y + 2)
- if val and color == opponent and not val2 and onBoard(X - 2,Y + 2) then
- table.insert(jumps,{-2,2,p})
- end
- val,color,p = occupied(X - 1,Y - 1)
- val2 = occupied(X - 2,Y - 2)
- if val and color == opponent and not val2 and onBoard(X - 2,Y - 2) then
- table.insert(jumps,{-2,-2,p})
- end
- if pieces[turn][piece].king then
- val,color,p = occupied(X + 1,Y + 1)
- val2 = occupied(X + 2,Y + 2)
- if val and color == opponent and not val2 and onBoard(X + 2,Y + 2) then
- table.insert(jumps,{2,2,p})
- end
- val,color,p = occupied(X + 1,Y - 1)
- val2 = occupied(X + 2,Y - 2)
- if val and color == opponent and not val2 and onBoard(X + 2,Y - 2) then
- table.insert(jumps,{2,-2,p})
- end
- end
- return jumps
- end
- function getMoves(piece)
- local moves = {}
- local X = pieces[turn][piece].x
- local Y = pieces[turn][piece].y
- local val,color,p = occupied(X - 1,Y + 1)
- local x2,y2 = X,Y
- local jumps = isJump(piece)
- for k2,v2 in ipairs(jumps) do
- local x2,y2 = X + v2[1],Y + v2[2]
- local jumps2 = isJump(piece,x2,y2)
- table.insert(moves,{x2,y2,"jump",{v2[3]}})
- for k3,v3 in ipairs(jumps2) do
- local x3,y3 = x2 + v3[1],y2 + v3[2]
- local jumps3 = isJump(piece,x3,y3)
- table.insert(moves,{x3,y3,"jump",{v2[3],v3[3]}})
- for k4,v4 in ipairs(jumps3) do
- local x4,y4 = x3 + v4[1],y3 + v4[2]
- local jumps4 = isJump(piece,x4,y4)
- table.insert(moves,{x4,y4,"jump",{v2[3],v3[3],v4[3]}})
- for k5,v5 in ipairs(jumps4) do
- local x5,y5 = x4 + v5[1],y4 + v5[2]
- local jumps5 = isJump(piece,x5,y5)
- table.insert(moves,{x5,y5,"jump",{v2[3],v3[3],v4[3],v5[3]}})
- for k6,v6 in ipairs(jumps5) do
- local x6,y6 = x5 + v6[1],y5 + v6[2]
- local jumps6 = isJump(piece,x6,y6)
- table.insert(moves,{x6,y6,"jump",{v2[3],v3[3],v4[3],v5[3],v6[3]}})
- for k7,v7 in ipairs(jumps6) do
- local x7,y7 = x6 + v7[1],y6 + v7[2]
- local jumps7 = isJump(piece,x7,y7)
- table.insert(moves,{x7,y7,"jump",{v2[3],v3[3],v4[3],v5[3],v6[3],v7[3]}})
- for k8,v8 in ipairs(jumps7) do
- local x8,y8 = x7 + v8[1],y7 + v8[2]
- local jumps8 = isJump(piece,x8,y8)
- table.insert(moves,{x8,y8,"jump",{v2[3],v3[3],v4[3],v5[3],v6[3],v7[3],v8[3]}})
- for k9,v9 in ipairs(jumps8) do
- local x9,y9 = x8 + v9[1],y8 + v9[2]
- local jumps9 = isJump(piece,x9,y9)
- table.insert(moves,{x9,y9,"jump",{v2[3],v3[3],v4[3],v5[3],v6[3],v7[3],v8[3],v9[3]}})
- for k10,v10 in ipairs(jumps9) do
- local x10,y10 = x9 + v10[1],y9 + v10[2]
- table.insert(moves,{x10,y10,"jump",{v2[3],v3[3],v4[3],v5[3],v6[3],v7[3],v8[3],v9[3],v10[3]}})
- end
- end
- end
- end
- end
- end
- end
- end
- end
- if not val and onBoard(X - 1,Y + 1) then
- table.insert(moves,{X - 1,Y + 1,"move",{}})
- end
- val,color,p = occupied(X - 1,Y - 1)
- if not val and onBoard(X - 1,Y - 1) then
- table.insert(moves,{X - 1,Y - 1,"move",{}})
- end
- if pieces[turn][piece].king then
- val,color,p = occupied(X + 1,Y + 1)
- if not val and onBoard(X + 1,Y + 1) then
- table.insert(moves,{X + 1,Y + 1,"move",{}})
- end
- val,color,p = occupied(X + 1,Y - 1)
- if not val and onBoard(X + 1,Y - 1) then
- table.insert(moves,{X + 1,Y - 1,"move",{}})
- end
- end
- local hasJump = false
- for key,val in ipairs(moves) do
- if val[3] == "jump" then
- hasJump = true
- end
- end
- local newMoves = moves
- if hasJump then
- for key,val in ipairs(moves) do
- if val[3] == "move" then
- table.remove(newMoves,key)
- end
- end
- end
- return newMoves
- end
- function displayMoves(piece)
- button.color(piece,pieces[turn][piece].color,colors.lime)
- for k,v in ipairs(getMoves(piece)) do
- local X,Y = getRealCoords(v[1],v[2])
- button.define("move"..tostring(k)," ",X,Y,colors.white,colors.lime,false)
- button.draw("move"..tostring(k))
- end
- end
- function doMove()
- local going = true
- while going do
- local piece = button.click()
- local moves = getMoves(piece)
- displayMoves(piece)
- button.deactivate("all")
- button.activate(piece)
- local move = button.click()
- print("move initialized as "..move)
- if move == piece then
- print("move = piece")
- button.color(piece,colors.white,colors.red)
- mon.setBackgroundColor(colors.red)
- for key,value in ipairs(moves) do
- button.delete("move"..tostring(key))
- end
- else
- print("move = "..move)
- for k,v in ipairs(moves[move][4]) do
- pieces[opponent][v].alive = false
- pieces[turn][piece].taken = pieces[turn][piece].taken + 1
- end
- for key,value in ipairs(moves) do
- button.delete("move"..tostring(key))
- end
- local x,y = getRealCoords(moves[move][1],moves[move][2])
- button.move(piece,x,y)
- pieces[turn][piece].x = moves[move][1]
- pieces[turn][piece].y = moves[move][2]
- going = false
- end
- end
- end
- function nextTurn()
- toggleTurn()
- getAlive()
- definePieces()
- button.draw(alive[turn])
- button.draw(alive[opponent])
- button.deactivate(alive[opponent])
- doMove()
- turnNum = turnNum + 1
- end
- function noWinner()
- for k,v in pairs(alive) do
- if getLength(v) == 0 then
- winner = k
- return false
- end
- end
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment