Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("button")
- local ccCoord = {}
- ccCoord["x"], ccCoord["y"], ccCoord["z"] = commands.getBlockPosition()
- local blockData = {}
- local blockData2 = {}
- local facing = ""
- local fx = 0
- local fz = 0
- local zombieMeta = 0
- local villagerMeta = 0
- local swapxz = false
- local dualMonitor = false
- local monitors = {}
- local m = {}
- local board={}
- local currentTurn
- local redRem=0
- local blkRem=0
- local trophies = false
- local player1Name = "pahimar"
- local player2Name = "soaryn"
- local mainMenuBool = true
- function checkBlocksFile()
- local file
- file = io.open("blocks", "r")
- if file then
- file:close()
- return true
- else
- return false
- end
- end
- function choosePlayers()
- local players = {}
- local playerCount = 0
- local screenx, screeny,x,y
- _, users = commands.xp(0, "@a")
- for a,b in pairs(users) do
- local name = ""
- strStart, strEnd = string.find(b,"to ")
- name = string.sub(b,strEnd+1)
- players[playerCount] = name
- playerCount = playerCount+1
- end
- --players[1] = "Soaryn"
- --players[2] = "Pahimar"
- --players[3] = "Notch"
- --players[4] = "Zeldo"
- button.clearTable()
- m.setBackgroundColor(colors.black)
- m.clear()
- m.setTextColor(colors.white)
- m.setCursorPos(1,1)
- button.label(7,1,"Please choose Player 1 below")
- screenx = 3
- screeny = 3
- playerCount = 0
- for i,j in pairs(players) do
- button.setTable(j, setPlayer1, j, screenx, screenx+15, screeny, screeny)
- playerCount = playerCount +1
- if playerCount % 2 == 0 then
- screenx = 3
- screeny = screeny+2
- else
- screenx = screenx + 20
- end
- end
- button.screen()
- x,y,side = getClick()
- while not button.checkxy(x,y) do
- x,y = getClick()
- end
- --button.checkxy(x,y)
- button.clearTable()
- m.setBackgroundColor(colors.black)
- m.clear()
- m.setTextColor(colors.white)
- m.setCursorPos(1,1)
- button.label(7,1,"Please choose Player 2 below")
- screenx = 3
- screeny = 3
- playerCount = 0
- for i,j in pairs(players) do
- button.setTable(j, setPlayer2, j, screenx, screenx+15, screeny, screeny)
- playerCount = playerCount +1
- if playerCount % 2 == 0 then
- screenx = 3
- screeny = screeny+2
- else
- screenx = screenx + 20
- end
- end
- button.screen()
- x,y,side = getClick()
- while not button.checkxy(x,y) do
- x,y = getClick()
- end
- end
- function setPlayer1(playerName)
- player1Name = playerName
- print("Player 1: "..player1Name)
- end
- function setPlayer2(playerName)
- player2Name = playerName
- print("Player 2: "..player2Name)
- end
- function connectMonitors()
- monitors = {peripheral.find("monitor")}
- for funcName,_ in pairs(monitors[1]) do
- m[funcName] = function(...)
- for i=1,#monitors-1 do monitors[i][funcName](unpack(arg)) end
- return monitors[#monitors][funcName](unpack(arg))
- end
- end
- end
- function mainMenu()
- mainMenuBool = true
- button.clearTable()
- m.setBackgroundColor(colors.black)
- m.clear()
- m.setTextColor(colors.white)
- m.setCursorPos(1,1)
- button.label(7,1,"Welcome to MineCheckers 2.0")
- button.label(15,2,"by Direwolf20")
- button.setTable("Place Board", placeBoardArea, "", 3, 18, 6, 8)
- button.setTable("Reset Area", resetBoardArea, "", 22, 37, 6, 8)
- button.setTable("Start Game", new, "", 10, 30, 12, 14)
- --button.setTable("Save Area", saveBoardArea, "", 10,30,19,19)
- button.screen()
- end
- function getFacing()
- local directions = {"unknown", "north", "south", "west", "east"}
- local blockInfo = commands.getBlockInfo( commands.getBlockPosition() )
- print( "My facing is ", directions[ blockInfo.metadata ], "." )
- facing = directions[ blockInfo.metadata ]
- --print(facing)
- end
- function setModifiers()
- if facing == "west" then
- fx = 1
- fz = 1
- zombieMeta = 3
- villagerMeta = 1
- playerSkullRot = "Rot:4"
- zombieSkullRot = "Rot:12"
- swapxz = true
- elseif facing == "east" then
- fx = -1
- fz = -1
- zombieMeta = 1
- villagerMeta = 3
- playerSkullRot = "Rot:12"
- zombieSkullRot = "Rot:4"
- swapxz = true
- elseif facing == "north" then
- fx = -1
- fz = 1
- zombieMeta = 2
- villagerMeta = 4
- playerSkullRot = "Rot:8"
- zombieSkullRot = "Rot:0"
- swapxz = false
- elseif facing == "south" then
- fx = 1
- fz = -1
- zombieMeta = 4
- villagerMeta = 2
- playerSkullRot = "Rot:0"
- zombieSkullRot = "Rot:8"
- swapxz = false
- end
- end
- function saveBlock(x,y,z)
- local data = commands.getBlockInfo(x,y,z)
- if not blockData[x] then
- blockData[x] = {}
- end
- if not blockData[x][y] then
- blockData[x][y] = {}
- end
- if not blockData[x][y][z] then
- blockData[x][y][z] = {}
- end
- blockData[x][y][z]["name"] = data["name"]
- blockData[x][y][z]["metadata"] = data["metadata"]
- end
- function writeBlocks()
- file = io.open("blocks", "w")
- file:write(textutils.serialize(blockData))
- file:close()
- end
- function readBlocks()
- file = io.open("blocks", "r")
- blockData2 = textutils.unserialize(file:read("*a"))
- file:close()
- end
- function saveSpace(xStart, xEnd, yStart, yEnd, zStart, zEnd)
- --print(xStart..xEnd..yStart..yEnd..zStart..zEnd)
- for x = xStart, xEnd,fx do
- for y = yStart, yEnd do
- for z = zStart, zEnd,fz do
- saveBlock(x,y,z)
- end
- end
- end
- writeBlocks()
- end
- function clearSpace(xStart, xEnd, yStart, yEnd, zStart, zEnd)
- --print(xStart..xEnd..yStart..yEnd..zStart..zEnd)
- for x = xStart, xEnd,fx do
- for y = yEnd, yStart,-1 do
- for z = zStart, zEnd,fz do
- --print(x..","..y..","..z)
- commands.async.setBlock(x,y,z,"minecraft:air")
- end
- end
- end
- commands.async.setBlock(500,63,1586,"minecraft:air")
- end
- function returnBlocks(xStart, xEnd, yStart, yEnd, zStart, zEnd)
- readBlocks()
- for x = xStart, xEnd,fx do
- for y = yStart, yEnd do
- for z = zStart, zEnd,fz do
- commands.async.setBlock(x,y,z,blockData2[x][y][z]["name"], blockData2[x][y][z]["metadata"])
- end
- end
- end
- end
- function placeBoard()
- local red = false
- for x = 0,9 do
- for z = 0,9 do
- a,b,c=relXYZnoswap(x,-1,z)
- commands.async.setBlock(a,b,c, "minecraft:stonebrick")
- end
- end
- for x = 1,8 do
- for z = 1,8 do
- a,b,c=relXYZnoswap(x,-1,z)
- if red then
- commands.async.setBlock(a,b,c, "minecraft:wool", 14)
- else
- commands.async.setBlock(a,b,c, "minecraft:wool", 15)
- end
- red = not red
- end
- red = not red
- end
- sleep(0.1)
- end
- function relXYZnoswap(x,y,z)
- --print("NoSwap: "..ccCoord["x"]+(x+1)*fx..":"..ccCoord["y"]+(y)..":"..ccCoord["z"]+(z+1)*fz)
- return ccCoord["x"]+(x+1)*fx, ccCoord["y"]+(y), ccCoord["z"]+(z+1)*fz
- end
- function relXYZ(x,y,z)
- if not swapxz then
- return ccCoord["x"]+(x+1)*fx, ccCoord["y"]+(y), ccCoord["z"]+(z+1)*fz
- else
- return ccCoord["x"]+(z+1)*fz, ccCoord["y"]+(y), ccCoord["z"]+(x+1)*fx
- end
- end
- function placeVillager(x,y,z)
- x,y,z = relXYZ(x,y,z)
- if trophies then
- local villager = "{trophyIndex:9}"
- commands.setBlock(x,y,z,"OpenBlocks:trophy",villagerMeta, "replace", villager)
- else
- local player = "{SkullType:3,"..playerSkullRot..",ExtraType:"..player1Name.."}"
- commands.setBlock(x,y,z,"minecraft:skull",9,"replace",player)
- end
- end
- function placeZombie(x,y,z)
- x,y,z = relXYZ(x,y,z)
- if trophies then
- local zombie = "{trophyIndex:7}"
- commands.setBlock(x,y,z,"OpenBlocks:trophy",zombieMeta, "replace", zombie)
- else
- local player = "{SkullType:3,"..zombieSkullRot..",ExtraType:"..player2Name.."}"
- commands.setBlock(x,y,z,"minecraft:skull",9,"replace",player)
- end
- end
- function placeWitch(x,y,z)
- x,y,z = relXYZ(x,y,z)
- if trophies then
- local witch = "{trophyIndex:8}"
- commands.setBlock(x,y,z,"OpenBlocks:trophy",villagerMeta, "replace", witch)
- else
- --local creeper = "{SkullType:4,"..playerSkullRot.."}"
- --commands.setBlock(x,y,z,"minecraft:skull",playerSkullMeta,"replace",creeper)
- local player = "{SkullType:3,"..playerSkullRot..",ExtraType:"..player1Name.."}"
- commands.setBlock(x,y,z,"minecraft:skull",1,"replace",player)
- commands.setBlock(x,y+1,z,"minecraft:skull",1,"replace",player)
- end
- end
- function placePigZombie(x,y,z)
- x,y,z = relXYZ(x,y,z)
- if trophies then
- local pigZombie = "{trophyIndex:5}"
- commands.setBlock(x,y,z,"OpenBlocks:trophy",zombieMeta, "replace", pigZombie)
- else
- local player = "{SkullType:3,"..zombieSkullRot..",ExtraType:"..player2Name.."}"
- commands.setBlock(x,y,z,"minecraft:skull",1,"replace",player)
- commands.setBlock(x,y+1,z,"minecraft:skull",1,"replace",player)
- end
- end
- function clearPiece(x,y,z)
- x,y,z = relXYZ(x,y,z)
- commands.setBlock(x,y,z,"minecraft:air")
- commands.setBlock(x,y+1,z,"minecraft:air")
- end
- function placePieces()
- for x = 1,7,2 do
- for z = 1,8 do
- if z < 4 then
- if z % 2 == 0 then
- placeVillager(x+1, 0, z)
- else
- placeVillager(x, 0, z)
- end
- elseif z > 5 then
- if z % 2 == 0 then
- placeZombie(x+1, 0, z)
- else
- placeZombie(x, 0, z)
- end
- end
- end
- end
- end
- function clearBoardArea()
- local x,y,z,x2,y2,z2
- x,y,z = relXYZnoswap(0,-1,0)
- x2,y2,z2 = relXYZnoswap(9,5,9)
- print("Clear: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
- clearSpace(x,x2,y,y2,z,z2)
- end
- function clearPieces()
- local x,y,z,x2,y2,z2
- x,y,z = relXYZnoswap(0,0,0)
- x2,y2,z2 = relXYZnoswap(9,1,9)
- print("Clear: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
- clearSpace(x,x2,y,y2,z,z2)
- end
- function placeBoardArea()
- button.flash("Place Board")
- clearBoardArea()
- sleep(0.5)
- placeBoard()
- --placePieces()
- end
- function resetBoardArea()
- button.flash("Reset Area")
- clearBoardArea()
- sleep(1)
- local x,y,z,x2,y2,z2
- x,y,z = relXYZnoswap(0,-1,0)
- x2,y2,z2 = relXYZnoswap(9,5,9)
- print("Reset: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
- returnBlocks(x,x2,y,y2,z,z2)
- end
- function saveBoardArea()
- button.toggleButton("Save Area")
- print("Saving board area, this may take a moment....")
- local x,y,z,x2,y2,z2
- x,y,z = relXYZnoswap(0,-1,0)
- x2,y2,z2 = relXYZnoswap(9,5,9)
- print("Save: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
- saveSpace(x,x2,y,y2,z,z2)
- print("Saving board area complete.")
- button.toggleButton("Save Area")
- end
- ------------------------------------------------------------------------------------
- -----------Checkers Code------------------------------------------------------------
- ------------------------------------------------------------------------------------
- function background(bgcolor)
- m.setBackgroundColor(bgcolor)
- for y = 1,18 do
- for x = 1,28 do
- m.setCursorPos(x,y)
- m.write(" ")
- end
- end
- --for j = 1, 20 do
- --for i = 1,39 do
- -- m.setCursorPos(i,j)
- -- m.write(" ")
- --end
- --end
- end
- function getCorner(x,y)
- if y%2~=0 then y=y-1 end
- if x%3~=0 then x=x-1 end
- if x%3~=0 then x=x-1 end
- return x,y
- end
- function getColor(x,y)
- x,y = getCorner(x,y)
- local invert = true
- if y%4 == 0 then invert = false end
- if (x%2==0) then
- if invert then return(colors.black) else return(colors.red) end
- else
- if invert then return(colors.red) else return(colors.black) end
- end
- end
- function message(x,y,text,color)
- m.setTextColor(color)
- m.setBackgroundColor(colors.black)
- if text == "clear" then
- button.label(x,y," ")
- else
- button.label(x,y,text)
- end
- end
- function drawBoard()
- for y = 2,17 do
- for x = 3,26 do
- m.setBackgroundColor(getColor(x,y))
- m.setCursorPos(x,y)
- m.write(" ")
- end
- end
- message(30,5,"clear",colors.red)
- end
- function loadBoard()
- for i = 1,8 do
- board[i] = {}
- end
- end
- function setPiece(a,b,color)
- board[a][b]=color
- end
- function getPiece(a,b)
- return(board[a][b])
- end
- function newGame()
- for i = 1,7,2 do
- --tInc = (i-1)/2
- setPiece(i,1,colors.pink)
- setPiece(i+1,2,colors.pink)
- setPiece(i,3,colors.pink)
- setPiece(i+1,6,colors.gray)
- setPiece(i,7,colors.gray)
- setPiece(i+1,8,colors.gray)
- end
- currentTurn = colors.pink
- end
- function getXY(a,b)
- x = a*3
- y = 18-b*2
- return x,y
- end
- function drawPiece(a,b)
- x,y = getXY(a,b)
- m.setBackgroundColor(getColor(x,y))
- x,y = getCorner(x,y)
- x = x+1
- button.label(x,y,"X")
- end
- function drawPieces()
- local tempcolor
- for a = 1,8 do
- for b = 1,8 do
- tempcolor = getPiece(a,b)
- if (tempcolor ~= nil) then
- m.setTextColor(tempcolor)
- drawPiece(a,b)
- end
- end
- end
- end
- function dispTurn()
- m.setBackgroundColor(colors.black)
- m.setTextColor(colors.green)
- button.label(29,2, "Turn: ")
- m.setTextColor(colors.white)
- if currentTurn == colors.pink then
- m.write("Red ")
- else
- m.write("Black")
- end
- dispRemaining()
- end
- function dispRemaining()
- m.setBackgroundColor(colors.black)
- m.setTextColor(colors.green)
- button.label(29,7,"Pieces Left:")
- m.setTextColor(colors.pink)
- button.label(29,8,"R:"..redRem.." ")
- m.setTextColor(colors.white)
- m.write("B:"..blkRem.." ")
- end
- function new()
- choosePlayers()
- mainMenuBool = false
- button.clearTable()
- m.clear()
- redRem = 12
- blkRem = 12
- background(colors.lightGray)
- drawBoard()
- loadBoard()
- newGame()
- placeBoard()
- clearPieces()
- placePieces()
- drawPieces()
- dispTurn()
- m.setTextColor(colors.white)
- button.setTable("NewGame",new,"",30,38,15,17)
- button.setTable("Quit",mainMenu,"",2,7,19,19)
- button.screen()
- end
- function getBoardPos(x,y)
- x,y = getCorner(x,y)
- x = x/3
- y = 9-y/2
- return x,y
- end
- function anyJumps()
- local jump = false
- for a = 1,8 do
- for b = 1,8 do
- if canDouble(a,b) and myTurn(getPiece(a,b)) then
- jump = true
- end
- end
- end
- return jump
- end
- function isValidMove(a,b,a2,b2)
- local tempColor = getPiece(a,b)
- if getPiece(a2,b2) ~= nil then
- return false
- end
- if tempColor == colors.pink then
- if b > b2 then return false end
- elseif tempColor == colors.gray then
- if b < b2 then return false end
- end
- return true
- end
- function isValidJump(a,b,a2,b2)
- if a<1 or a>8 or b<1 or b>8 then return false end
- if a2<1 or a2>8 or b2<1 or b2>8 then return false end
- if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
- return false
- end
- local tempColor = getPiece(a,b)
- local jumpColor = getPiece((a+a2)/2,(b+b2)/2)
- if isValidMove(a,b,a2,b2) == false then return false end
- if jumpColor == nil then return false end
- if tempColor == colors.gray or tempColor == colors.white then
- if jumpColor~=colors.pink and jumpColor~=colors.blue then
- return false
- end
- else
- if jumpColor~=colors.gray and jumpColor~=colors.white then
- return false
- end
- end
- print("Valid: "..a..","..b..":"..a2..","..b2)
- return true
- end
- function canDouble(a2,b2)
- double = false
- if isValidJump(a2,b2,a2+2,b2+2) then double = true end
- if isValidJump(a2,b2,a2-2,b2+2) then double = true end
- if isValidJump(a2,b2,a2+2,b2-2) then double = true end
- if isValidJump(a2,b2,a2-2,b2-2) then double = true end
- return double
- end
- function tryMove(a,b,a2,b2)
- if math.abs(a-a2) ~= 1 or math.abs(b-b2) ~= 1 then
- return false
- end
- if anyJumps() then
- message(30,5,"Must Jump!",colors.red)
- return false
- end
- if isValidMove(a,b,a2,b2) then
- moveBoardPiece(a,b,a2,b2,false)
- movePiece(a,b,a2,b2)
- switchTurn()
- return true
- end
- message(30,5,"clear",colors.red)
- return false
- end
- function movePiece(a,b,a2,b2)
- local tempColor = getPiece(a,b)
- if tempColor==colors.pink and b2==8 then
- tempColor=colors.blue
- pieceMove(a2,0,b2,a2,0,b2,placeWitch)
- elseif tempColor==colors.gray and b2==1 then
- tempColor = colors.white
- pieceMove(a2,0,b2,a2,0,b2,placePigZombie)
- end
- setPiece(a2,b2,tempColor)
- setPiece(a,b,nil)
- drawBoard()
- drawPieces()
- end
- function pieceMove(x,y,z,x2,y2,z2,func)
- clearPiece(x,y,z)
- func(x2,y2,z2)
- end
- function moveBoardPiece(a,b,a2,b2,jump)
- local a3, b3
- a3 = a
- b3 = b
- if jump then count = 2 else count = 1 end
- local func = placeZombie
- print(getPiece(a,b))
- if getPiece(a,b) == colors.pink then
- func = placeVillager
- elseif getPiece(a,b) == colors.blue then
- func = placeWitch
- elseif getPiece(a,b) == colors.white then
- func = placePigZombie
- end
- for i = 1,count do
- sleep(0.1)
- pieceMove(a,0,b,a,1,b,func)
- sleep(0.1)
- if a2 > a then
- a3 = a3+1
- else
- a3 = a3-1
- end
- pieceMove(a,1,b,a3,1,b,func)
- sleep(0.1)
- if b2 > b then
- b3 = b3+1
- else
- b3 = b3-1
- end
- pieceMove(a3,1,b,a3,1,b3,func)
- sleep(0.1)
- pieceMove(a3,1,b3,a3,0,b3,func)
- a = a3
- b = b3
- end
- end
- function isKing(a,b)
- if getPiece(a,b) == colors.white or getPiece(a,b) == colors.blue then
- return true
- else
- return false
- end
- end
- function updScore(color)
- if color == colors.blue or color == colors.pink then
- redRem = redRem-1
- else
- blkRem = blkRem-1
- end
- end
- function tryJump(a,b,a2,b2)
- local a3,b3
- if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
- return false
- end
- if isValidJump(a,b,a2,b2) then
- updScore(getPiece((a+a2)/2,(b+b2)/2))
- setPiece((a+a2)/2,(b+b2)/2,nil)
- moveBoardPiece(a,b,a2,b2,true)
- movePiece(a,b,a2,b2)
- while canDouble(a2,b2) do
- a = a2
- b = b2
- message(30,5,"Double!",colors.red)
- x,y = getClick()
- a3,b3 = getBoardPos(x,y)
- tryJump(a,b,a3,b3)
- end
- message(30,5,"clear",colors.red)
- return true
- end
- message(30,5,"clear",colors.blue)
- return false
- end
- function switchTurn()
- if currentTurn == colors.pink then
- currentTurn = colors.gray
- else
- currentTurn = colors.pink
- end
- dispTurn()
- end
- function myTurn(color)
- if currentTurn == colors.pink then
- if color ~= colors.pink and color ~= colors.blue then return false end
- else
- if color ~= colors.gray and color ~= colors.white then return false end
- end
- return true
- end
- function getClick()
- local event, side, x, y = os.pullEvent("monitor_touch")
- return x,y,side
- end
- function waitPlayer()
- local tempColor
- local valid = false
- x,y = getClick()
- if button.checkxy(x,y) then return true end
- if mainMenuBool then return true end
- a,b = getBoardPos(x,y)
- if a<1 or b<1 or a>8 or b>8 then return false end
- if getPiece(a,b) ~= nil and myTurn(getPiece(a,b)) then
- message(30,3,a..":"..b,colors.blue)
- x,y = getClick()
- a2,b2 = getBoardPos(x,y)
- message(30,4,a2..":"..b2,colors.green)
- if tryMove(a,b,a2,b2) == false then
- if tryJump(a,b,a2,b2) then
- switchTurn()
- end
- end
- end
- message(30,3,"clear",colors.blue)
- message(30,4,"clear",colors.blue)
- end
- if not checkBlocksFile() then
- print("Blocks file not found. Please download and run the saveArea program!")
- else
- connectMonitors()
- getFacing()
- setModifiers()
- mainMenu()
- while true do waitPlayer() end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement