Advertisement
Guest User

checkers

a guest
Jul 15th, 2015
2,990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.65 KB | None | 0 0
  1. os.loadAPI("button")
  2. local ccCoord = {}
  3. ccCoord["x"], ccCoord["y"], ccCoord["z"] = commands.getBlockPosition()
  4. local blockData = {}
  5. local blockData2 = {}
  6. local facing = ""
  7. local fx = 0
  8. local fz = 0
  9. local zombieMeta = 0
  10. local villagerMeta = 0
  11. local swapxz = false
  12. local dualMonitor = false
  13. local monitors = {}
  14. local m = {}
  15. local board={}
  16. local currentTurn
  17. local redRem=0
  18. local blkRem=0
  19. local trophies = false
  20. local player1Name = "pahimar"
  21. local player2Name = "soaryn"
  22. local mainMenuBool = true
  23.  
  24. function checkBlocksFile()
  25.     local file
  26.     file = io.open("blocks", "r")
  27.     if file then
  28.         file:close()
  29.         return true
  30.     else
  31.         return false
  32.     end
  33. end
  34.  
  35. function choosePlayers()
  36.     local players = {}
  37.     local playerCount = 0
  38.     local screenx, screeny,x,y
  39.    
  40.     _, users = commands.xp(0, "@a")
  41.     for a,b in pairs(users) do
  42.         local name = ""
  43.         strStart, strEnd = string.find(b,"to ")
  44.         name = string.sub(b,strEnd+1)
  45.         players[playerCount] = name
  46.         playerCount = playerCount+1
  47.     end
  48.     --players[1] = "Soaryn"
  49.     --players[2] = "Pahimar"
  50.     --players[3] = "Notch"
  51.     --players[4] = "Zeldo"
  52.     button.clearTable()
  53.     m.setBackgroundColor(colors.black)
  54.     m.clear()
  55.     m.setTextColor(colors.white)
  56.     m.setCursorPos(1,1)
  57.     button.label(7,1,"Please choose Player 1 below")
  58.     screenx = 3
  59.     screeny = 3
  60.     playerCount = 0
  61.     for i,j in pairs(players) do
  62.         button.setTable(j, setPlayer1, j, screenx, screenx+15, screeny, screeny)
  63.         playerCount = playerCount +1
  64.         if playerCount % 2 == 0 then
  65.             screenx = 3
  66.             screeny = screeny+2
  67.         else
  68.             screenx = screenx + 20
  69.         end
  70.     end
  71.     button.screen()
  72.     x,y,side = getClick()
  73.     while not button.checkxy(x,y) do
  74.         x,y = getClick()
  75.     end
  76.     --button.checkxy(x,y)
  77.     button.clearTable()
  78.     m.setBackgroundColor(colors.black)
  79.     m.clear()
  80.     m.setTextColor(colors.white)
  81.     m.setCursorPos(1,1)
  82.     button.label(7,1,"Please choose Player 2 below")
  83.     screenx = 3
  84.     screeny = 3
  85.     playerCount = 0
  86.     for i,j in pairs(players) do
  87.         button.setTable(j, setPlayer2, j, screenx, screenx+15, screeny, screeny)
  88.         playerCount = playerCount +1
  89.         if playerCount % 2 == 0 then
  90.             screenx = 3
  91.             screeny = screeny+2
  92.         else
  93.             screenx = screenx + 20
  94.         end
  95.     end
  96.     button.screen()
  97.     x,y,side = getClick()
  98.     while not button.checkxy(x,y) do
  99.         x,y = getClick()
  100.     end
  101. end
  102.  
  103. function setPlayer1(playerName)
  104.     player1Name = playerName
  105.     print("Player 1: "..player1Name)
  106. end
  107.  
  108. function setPlayer2(playerName)
  109.     player2Name = playerName
  110.     print("Player 2: "..player2Name)
  111. end
  112.  
  113. function connectMonitors()
  114.     monitors = {peripheral.find("monitor")}
  115.     for funcName,_ in pairs(monitors[1]) do
  116.         m[funcName] = function(...)
  117.             for i=1,#monitors-1 do monitors[i][funcName](unpack(arg)) end
  118.             return monitors[#monitors][funcName](unpack(arg))
  119.         end
  120.     end
  121. end
  122.  
  123. function mainMenu()
  124.     mainMenuBool = true
  125.     button.clearTable()
  126.     m.setBackgroundColor(colors.black)
  127.     m.clear()
  128.     m.setTextColor(colors.white)
  129.     m.setCursorPos(1,1)
  130.     button.label(7,1,"Welcome to MineCheckers 2.0")
  131.     button.label(15,2,"by Direwolf20")
  132.     button.setTable("Place Board", placeBoardArea, "", 3, 18, 6, 8)
  133.     button.setTable("Reset Area", resetBoardArea, "", 22, 37, 6, 8)
  134.     button.setTable("Start Game", new, "", 10, 30, 12, 14)
  135.     --button.setTable("Save Area", saveBoardArea, "", 10,30,19,19)
  136.     button.screen()
  137. end
  138.  
  139. function getFacing()
  140.     local directions = {"unknown", "north", "south", "west", "east"}
  141.     local blockInfo = commands.getBlockInfo( commands.getBlockPosition() )
  142.     print( "My facing is ", directions[ blockInfo.metadata ], "." )
  143.     facing = directions[ blockInfo.metadata ]
  144.     --print(facing)
  145. end
  146.  
  147. function setModifiers()
  148.     if facing == "west" then
  149.         fx = 1
  150.         fz = 1
  151.         zombieMeta = 3
  152.         villagerMeta = 1
  153.         playerSkullRot = "Rot:4"
  154.         zombieSkullRot = "Rot:12"
  155.         swapxz = true
  156.     elseif facing == "east" then
  157.         fx = -1
  158.         fz = -1
  159.         zombieMeta = 1
  160.         villagerMeta = 3
  161.         playerSkullRot = "Rot:12"
  162.         zombieSkullRot = "Rot:4"
  163.         swapxz = true
  164.     elseif facing == "north" then
  165.         fx = -1
  166.         fz = 1
  167.         zombieMeta = 2
  168.         villagerMeta = 4
  169.         playerSkullRot = "Rot:8"
  170.         zombieSkullRot = "Rot:0"
  171.         swapxz = false
  172.     elseif facing == "south" then
  173.         fx = 1
  174.         fz = -1
  175.         zombieMeta = 4
  176.         villagerMeta = 2
  177.         playerSkullRot = "Rot:0"
  178.         zombieSkullRot = "Rot:8"
  179.         swapxz = false
  180.     end
  181. end
  182.  
  183.  
  184. function saveBlock(x,y,z)
  185.     local data = commands.getBlockInfo(x,y,z)
  186.     if not blockData[x] then
  187.         blockData[x] = {}
  188.     end
  189.     if not blockData[x][y] then
  190.         blockData[x][y] = {}
  191.     end
  192.     if not blockData[x][y][z] then
  193.         blockData[x][y][z] = {}
  194.     end
  195.     blockData[x][y][z]["name"] = data["name"]
  196.     blockData[x][y][z]["metadata"] = data["metadata"]
  197. end
  198.  
  199. function writeBlocks()
  200.     file = io.open("blocks", "w")
  201.     file:write(textutils.serialize(blockData))
  202.     file:close()
  203. end
  204.  
  205. function readBlocks()
  206.     file = io.open("blocks", "r")
  207.     blockData2 = textutils.unserialize(file:read("*a"))
  208.     file:close()
  209. end
  210.  
  211. function saveSpace(xStart, xEnd, yStart, yEnd, zStart, zEnd)
  212.     --print(xStart..xEnd..yStart..yEnd..zStart..zEnd)
  213.     for x = xStart, xEnd,fx do
  214.         for y = yStart, yEnd do
  215.             for z = zStart, zEnd,fz do
  216.                 saveBlock(x,y,z)
  217.             end
  218.         end
  219.     end
  220.     writeBlocks()
  221. end
  222.  
  223. function clearSpace(xStart, xEnd, yStart, yEnd, zStart, zEnd)
  224.     --print(xStart..xEnd..yStart..yEnd..zStart..zEnd)
  225.     for x = xStart, xEnd,fx do
  226.         for y = yEnd, yStart,-1 do
  227.             for z = zStart, zEnd,fz do
  228.                 --print(x..","..y..","..z)
  229.                 commands.async.setBlock(x,y,z,"minecraft:air")
  230.             end
  231.         end
  232.     end
  233.     commands.async.setBlock(500,63,1586,"minecraft:air")
  234. end
  235.  
  236. function returnBlocks(xStart, xEnd, yStart, yEnd, zStart, zEnd)
  237.     readBlocks()
  238.     for x = xStart, xEnd,fx do
  239.         for y = yStart, yEnd do
  240.             for z = zStart, zEnd,fz do
  241.                 commands.async.setBlock(x,y,z,blockData2[x][y][z]["name"], blockData2[x][y][z]["metadata"])
  242.             end
  243.         end
  244.     end
  245. end
  246.  
  247. function placeBoard()
  248.     local red = false
  249.     for x = 0,9 do
  250.         for z = 0,9 do
  251.             a,b,c=relXYZnoswap(x,-1,z)
  252.             commands.async.setBlock(a,b,c, "minecraft:stonebrick")
  253.         end
  254.     end
  255.  
  256.     for x = 1,8 do
  257.         for z = 1,8 do
  258.             a,b,c=relXYZnoswap(x,-1,z)
  259.             if red then
  260.                 commands.async.setBlock(a,b,c, "minecraft:wool", 14)
  261.             else
  262.                 commands.async.setBlock(a,b,c, "minecraft:wool", 15)
  263.             end
  264.             red = not red
  265.         end
  266.         red = not red
  267.     end
  268.     sleep(0.1)
  269. end
  270.  
  271. function relXYZnoswap(x,y,z)
  272.     --print("NoSwap: "..ccCoord["x"]+(x+1)*fx..":"..ccCoord["y"]+(y)..":"..ccCoord["z"]+(z+1)*fz)
  273.     return ccCoord["x"]+(x+1)*fx, ccCoord["y"]+(y), ccCoord["z"]+(z+1)*fz
  274. end
  275.  
  276. function relXYZ(x,y,z)
  277.     if not swapxz then
  278.         return ccCoord["x"]+(x+1)*fx, ccCoord["y"]+(y), ccCoord["z"]+(z+1)*fz
  279.     else
  280.         return ccCoord["x"]+(z+1)*fz, ccCoord["y"]+(y), ccCoord["z"]+(x+1)*fx
  281.     end
  282. end
  283.  
  284. function placeVillager(x,y,z)
  285.     x,y,z = relXYZ(x,y,z)
  286.     if trophies then
  287.         local villager = "{trophyIndex:9}"
  288.         commands.setBlock(x,y,z,"OpenBlocks:trophy",villagerMeta, "replace", villager)
  289.     else
  290.         local player = "{SkullType:3,"..playerSkullRot..",ExtraType:"..player1Name.."}"
  291.         commands.setBlock(x,y,z,"minecraft:skull",9,"replace",player)
  292.     end
  293. end
  294.  
  295. function placeZombie(x,y,z)
  296.     x,y,z = relXYZ(x,y,z)
  297.     if trophies then
  298.         local zombie = "{trophyIndex:7}"
  299.         commands.setBlock(x,y,z,"OpenBlocks:trophy",zombieMeta, "replace", zombie)
  300.     else
  301.         local player = "{SkullType:3,"..zombieSkullRot..",ExtraType:"..player2Name.."}"
  302.         commands.setBlock(x,y,z,"minecraft:skull",9,"replace",player)
  303.     end
  304. end
  305.  
  306. function placeWitch(x,y,z)
  307.     x,y,z = relXYZ(x,y,z)
  308.     if trophies then
  309.         local witch = "{trophyIndex:8}"
  310.         commands.setBlock(x,y,z,"OpenBlocks:trophy",villagerMeta, "replace", witch)
  311.     else
  312.         --local creeper = "{SkullType:4,"..playerSkullRot.."}"
  313.         --commands.setBlock(x,y,z,"minecraft:skull",playerSkullMeta,"replace",creeper)
  314.         local player = "{SkullType:3,"..playerSkullRot..",ExtraType:"..player1Name.."}"
  315.         commands.setBlock(x,y,z,"minecraft:skull",1,"replace",player)
  316.         commands.setBlock(x,y+1,z,"minecraft:skull",1,"replace",player)
  317.     end
  318. end
  319.  
  320. function placePigZombie(x,y,z)
  321.     x,y,z = relXYZ(x,y,z)
  322.     if trophies then
  323.         local pigZombie = "{trophyIndex:5}"
  324.         commands.setBlock(x,y,z,"OpenBlocks:trophy",zombieMeta, "replace", pigZombie)
  325.     else
  326.         local player = "{SkullType:3,"..zombieSkullRot..",ExtraType:"..player2Name.."}"
  327.         commands.setBlock(x,y,z,"minecraft:skull",1,"replace",player)
  328.         commands.setBlock(x,y+1,z,"minecraft:skull",1,"replace",player)
  329.     end
  330. end
  331.  
  332. function clearPiece(x,y,z)
  333.     x,y,z = relXYZ(x,y,z)
  334.     commands.setBlock(x,y,z,"minecraft:air")
  335.     commands.setBlock(x,y+1,z,"minecraft:air")
  336. end
  337.  
  338. function placePieces()
  339.     for x = 1,7,2 do
  340.         for z = 1,8 do
  341.             if z < 4 then
  342.                 if z % 2 == 0 then
  343.                     placeVillager(x+1, 0, z)
  344.                 else
  345.                     placeVillager(x, 0, z)
  346.                 end
  347.             elseif z > 5 then
  348.                 if z % 2 == 0 then
  349.                     placeZombie(x+1, 0, z)
  350.                 else
  351.                     placeZombie(x, 0, z)
  352.                 end
  353.             end
  354.         end
  355.     end
  356. end
  357.  
  358. function clearBoardArea()
  359.     local x,y,z,x2,y2,z2
  360.     x,y,z = relXYZnoswap(0,-1,0)
  361.     x2,y2,z2 = relXYZnoswap(9,5,9)
  362.     print("Clear: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
  363.     clearSpace(x,x2,y,y2,z,z2)
  364. end
  365.  
  366. function clearPieces()
  367.     local x,y,z,x2,y2,z2
  368.     x,y,z = relXYZnoswap(0,0,0)
  369.     x2,y2,z2 = relXYZnoswap(9,1,9)
  370.     print("Clear: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
  371.     clearSpace(x,x2,y,y2,z,z2)
  372. end
  373.  
  374. function placeBoardArea()
  375.     button.flash("Place Board")
  376.     clearBoardArea()
  377.     sleep(0.5)
  378.     placeBoard()
  379.     --placePieces()
  380. end
  381.  
  382. function resetBoardArea()
  383.     button.flash("Reset Area")
  384.     clearBoardArea()
  385.     sleep(1)
  386.     local x,y,z,x2,y2,z2
  387.     x,y,z = relXYZnoswap(0,-1,0)
  388.     x2,y2,z2 = relXYZnoswap(9,5,9)
  389.     print("Reset: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
  390.     returnBlocks(x,x2,y,y2,z,z2)
  391. end
  392.  
  393. function saveBoardArea()
  394.     button.toggleButton("Save Area")
  395.     print("Saving board area, this may take a moment....")
  396.     local x,y,z,x2,y2,z2
  397.     x,y,z = relXYZnoswap(0,-1,0)
  398.     x2,y2,z2 = relXYZnoswap(9,5,9)
  399.     print("Save: "..x..":"..x2..", "..y..":"..y2..", "..z..":"..z2)
  400.     saveSpace(x,x2,y,y2,z,z2)
  401.     print("Saving board area complete.")
  402.     button.toggleButton("Save Area")
  403. end
  404.  
  405. ------------------------------------------------------------------------------------
  406. -----------Checkers Code------------------------------------------------------------
  407. ------------------------------------------------------------------------------------
  408.  
  409. function background(bgcolor)
  410.     m.setBackgroundColor(bgcolor)
  411.     for y = 1,18 do
  412.         for x = 1,28 do
  413.         m.setCursorPos(x,y)
  414.         m.write(" ")
  415.         end
  416.     end
  417.    --for j = 1, 20 do
  418.       --for i = 1,39 do
  419.       --  m.setCursorPos(i,j)
  420.        -- m.write(" ")
  421.       --end
  422.    --end
  423. end
  424.  
  425. function getCorner(x,y)
  426.   if y%2~=0 then y=y-1 end
  427.   if x%3~=0 then x=x-1 end
  428.   if x%3~=0 then x=x-1 end
  429.   return x,y
  430. end
  431.  
  432. function getColor(x,y)
  433.   x,y = getCorner(x,y)
  434.   local invert = true
  435.   if y%4 == 0 then invert = false end
  436.   if (x%2==0) then
  437.      if invert then return(colors.black) else return(colors.red) end
  438.   else
  439.      if invert then return(colors.red) else return(colors.black) end
  440.   end
  441. end
  442.  
  443. function message(x,y,text,color)
  444.    m.setTextColor(color)
  445.    m.setBackgroundColor(colors.black)
  446.    if text == "clear" then
  447.       button.label(x,y,"          ")
  448.    else
  449.       button.label(x,y,text)
  450.    end
  451. end
  452.  
  453. function drawBoard()
  454.    for y = 2,17 do
  455.      for x = 3,26 do
  456.        m.setBackgroundColor(getColor(x,y))
  457.        m.setCursorPos(x,y)
  458.        m.write(" ")
  459.      end
  460.    end
  461.    message(30,5,"clear",colors.red)
  462. end
  463.  
  464. function loadBoard()
  465.   for i = 1,8 do
  466.      board[i] = {}
  467.   end
  468. end
  469.  
  470. function setPiece(a,b,color)
  471.   board[a][b]=color
  472. end
  473.  
  474. function getPiece(a,b)
  475.   return(board[a][b])
  476. end
  477.  
  478. function newGame()
  479.   for i = 1,7,2 do
  480.      --tInc = (i-1)/2
  481.      setPiece(i,1,colors.pink)
  482.      setPiece(i+1,2,colors.pink)
  483.      setPiece(i,3,colors.pink)
  484.      setPiece(i+1,6,colors.gray)
  485.      setPiece(i,7,colors.gray)
  486.      setPiece(i+1,8,colors.gray)
  487.   end
  488.   currentTurn = colors.pink
  489. end
  490.  
  491. function getXY(a,b)
  492.    x = a*3
  493.    y = 18-b*2
  494.    return x,y
  495. end
  496.  
  497. function drawPiece(a,b)
  498.   x,y = getXY(a,b)
  499.   m.setBackgroundColor(getColor(x,y))
  500.   x,y = getCorner(x,y)
  501.   x = x+1
  502.   button.label(x,y,"X")
  503. end
  504.  
  505. function drawPieces()
  506.   local tempcolor
  507.   for a = 1,8 do
  508.     for b = 1,8 do
  509.       tempcolor = getPiece(a,b)
  510.       if (tempcolor ~= nil) then
  511.         m.setTextColor(tempcolor)
  512.         drawPiece(a,b)    
  513.       end
  514.     end
  515.   end    
  516. end
  517.  
  518. function dispTurn()
  519.   m.setBackgroundColor(colors.black)
  520.   m.setTextColor(colors.green)
  521.   button.label(29,2, "Turn: ")
  522.  
  523.   m.setTextColor(colors.white)
  524.   if currentTurn == colors.pink then
  525.      m.write("Red  ")
  526.   else
  527.      m.write("Black")
  528.   end
  529.   dispRemaining()
  530. end
  531.  
  532. function dispRemaining()
  533.   m.setBackgroundColor(colors.black)
  534.   m.setTextColor(colors.green)
  535.   button.label(29,7,"Pieces Left:")
  536.   m.setTextColor(colors.pink)
  537.   button.label(29,8,"R:"..redRem.."   ")
  538.   m.setTextColor(colors.white)
  539.   m.write("B:"..blkRem.."  ")
  540. end
  541.  
  542. function new()
  543.     choosePlayers()
  544.     mainMenuBool = false
  545.     button.clearTable()
  546.     m.clear()
  547.     redRem = 12
  548.     blkRem = 12
  549.     background(colors.lightGray)
  550.     drawBoard()
  551.     loadBoard()
  552.     newGame()
  553.     placeBoard()
  554.     clearPieces()
  555.     placePieces()
  556.     drawPieces()
  557.     dispTurn()
  558.     m.setTextColor(colors.white)
  559.     button.setTable("NewGame",new,"",30,38,15,17)
  560.     button.setTable("Quit",mainMenu,"",2,7,19,19)
  561.     button.screen()
  562. end
  563.  
  564. function getBoardPos(x,y)
  565.   x,y = getCorner(x,y)
  566.   x = x/3
  567.   y = 9-y/2
  568.   return x,y
  569. end
  570.  
  571. function anyJumps()
  572.    local jump = false
  573.    for a = 1,8 do
  574.       for b = 1,8 do
  575.          if canDouble(a,b) and myTurn(getPiece(a,b)) then
  576.             jump = true
  577.          end
  578.       end
  579.    end
  580.    return jump
  581. end
  582.  
  583. function isValidMove(a,b,a2,b2)
  584.    local tempColor = getPiece(a,b)
  585.    if getPiece(a2,b2) ~= nil then
  586.      return false
  587.    end
  588.    if tempColor == colors.pink then
  589.       if b > b2 then return false end
  590.    elseif tempColor == colors.gray then
  591.       if b < b2 then return false end
  592.    end
  593.    return true
  594. end  
  595.  
  596. function isValidJump(a,b,a2,b2)
  597.    if a<1 or a>8 or b<1 or b>8 then return false end
  598.    if a2<1 or a2>8 or b2<1 or b2>8 then return false end
  599.    if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
  600.       return false
  601.    end
  602.    local tempColor = getPiece(a,b)
  603.    local jumpColor = getPiece((a+a2)/2,(b+b2)/2)
  604.    if isValidMove(a,b,a2,b2) == false then return false end
  605.    if jumpColor == nil then return false end
  606.    if tempColor == colors.gray or tempColor == colors.white then
  607.       if jumpColor~=colors.pink and jumpColor~=colors.blue then
  608.         return false
  609.       end  
  610.    else
  611.       if jumpColor~=colors.gray and jumpColor~=colors.white then
  612.         return false
  613.       end
  614.    end
  615.    print("Valid: "..a..","..b..":"..a2..","..b2)
  616.    return true
  617. end
  618.  
  619. function canDouble(a2,b2)
  620.   double = false
  621.   if isValidJump(a2,b2,a2+2,b2+2) then double = true end
  622.   if isValidJump(a2,b2,a2-2,b2+2) then double = true end
  623.   if isValidJump(a2,b2,a2+2,b2-2) then double = true end
  624.   if isValidJump(a2,b2,a2-2,b2-2) then double = true end
  625.   return double
  626. end  
  627.  
  628. function tryMove(a,b,a2,b2)
  629.     if math.abs(a-a2) ~= 1 or math.abs(b-b2) ~= 1 then
  630.         return false
  631.     end
  632.     if anyJumps() then
  633.         message(30,5,"Must Jump!",colors.red)
  634.         return false
  635.     end
  636.     if isValidMove(a,b,a2,b2) then
  637.         moveBoardPiece(a,b,a2,b2,false)
  638.         movePiece(a,b,a2,b2)
  639.         switchTurn()
  640.         return true
  641.     end
  642.     message(30,5,"clear",colors.red)
  643.     return false
  644. end
  645.  
  646. function movePiece(a,b,a2,b2)
  647.    local tempColor = getPiece(a,b)
  648.    if tempColor==colors.pink and b2==8 then
  649.       tempColor=colors.blue
  650.       pieceMove(a2,0,b2,a2,0,b2,placeWitch)
  651.    elseif tempColor==colors.gray and b2==1 then
  652.       tempColor = colors.white
  653.       pieceMove(a2,0,b2,a2,0,b2,placePigZombie)
  654.    end
  655.    setPiece(a2,b2,tempColor)
  656.    setPiece(a,b,nil)
  657.    drawBoard()
  658.    drawPieces()
  659. end
  660.  
  661. function pieceMove(x,y,z,x2,y2,z2,func)
  662.     clearPiece(x,y,z)
  663.     func(x2,y2,z2)
  664. end
  665.  
  666. function moveBoardPiece(a,b,a2,b2,jump)
  667.     local a3, b3
  668.     a3 = a
  669.     b3 = b
  670.     if jump then count = 2 else count = 1 end
  671.     local func = placeZombie
  672.     print(getPiece(a,b))
  673.     if getPiece(a,b) == colors.pink then
  674.         func = placeVillager
  675.     elseif getPiece(a,b) == colors.blue then
  676.         func = placeWitch
  677.     elseif getPiece(a,b) == colors.white then
  678.         func = placePigZombie
  679.     end
  680.     for i = 1,count do
  681.         sleep(0.1)
  682.         pieceMove(a,0,b,a,1,b,func)
  683.         sleep(0.1)
  684.         if a2 > a then
  685.             a3 = a3+1
  686.         else
  687.             a3 = a3-1
  688.         end
  689.         pieceMove(a,1,b,a3,1,b,func)
  690.         sleep(0.1)
  691.         if b2 > b then
  692.             b3 = b3+1
  693.         else
  694.             b3 = b3-1
  695.         end
  696.         pieceMove(a3,1,b,a3,1,b3,func)
  697.         sleep(0.1)
  698.         pieceMove(a3,1,b3,a3,0,b3,func)
  699.         a = a3
  700.         b = b3
  701.     end
  702. end
  703.  
  704. function isKing(a,b)
  705.   if getPiece(a,b) == colors.white or getPiece(a,b) == colors.blue then
  706.      return true
  707.   else
  708.      return false
  709.   end
  710. end
  711.  
  712. function updScore(color)
  713.    if color == colors.blue or color == colors.pink then
  714.       redRem = redRem-1
  715.    else
  716.       blkRem = blkRem-1
  717.    end
  718. end
  719.  
  720. function tryJump(a,b,a2,b2)
  721.     local a3,b3
  722.     if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
  723.         return false
  724.     end
  725.     if isValidJump(a,b,a2,b2) then
  726.         updScore(getPiece((a+a2)/2,(b+b2)/2))
  727.         setPiece((a+a2)/2,(b+b2)/2,nil)
  728.         moveBoardPiece(a,b,a2,b2,true)
  729.         movePiece(a,b,a2,b2)
  730.         while canDouble(a2,b2) do
  731.             a = a2
  732.             b = b2
  733.             message(30,5,"Double!",colors.red)
  734.             x,y = getClick()
  735.             a3,b3 = getBoardPos(x,y)
  736.             tryJump(a,b,a3,b3)
  737.         end
  738.         message(30,5,"clear",colors.red)
  739.         return true
  740.     end
  741.     message(30,5,"clear",colors.blue)
  742.     return false
  743. end
  744.  
  745. function switchTurn()
  746.    if currentTurn == colors.pink then
  747.       currentTurn = colors.gray
  748.    else
  749.       currentTurn = colors.pink
  750.    end
  751.    dispTurn()
  752. end
  753.  
  754. function myTurn(color)
  755.    if currentTurn == colors.pink then
  756.       if color ~= colors.pink and color ~= colors.blue then return false end
  757.    else
  758.       if color ~= colors.gray and color ~= colors.white then return false end
  759.    end
  760.    return true
  761. end
  762.  
  763. function getClick()
  764.     local event, side, x, y = os.pullEvent("monitor_touch")
  765.     return x,y,side
  766. end
  767.  
  768. function waitPlayer()
  769.    local tempColor
  770.    local valid = false
  771.    x,y = getClick()
  772.    if button.checkxy(x,y) then return true end
  773.    if mainMenuBool then return true end
  774.    a,b = getBoardPos(x,y)
  775.    if a<1 or b<1 or a>8 or b>8 then return false end
  776.    if getPiece(a,b) ~= nil and myTurn(getPiece(a,b)) then
  777.      message(30,3,a..":"..b,colors.blue)
  778.      x,y = getClick()
  779.      a2,b2 = getBoardPos(x,y)
  780.      message(30,4,a2..":"..b2,colors.green)
  781.      if tryMove(a,b,a2,b2) == false then
  782.        if tryJump(a,b,a2,b2) then
  783.           switchTurn()
  784.        end
  785.      end
  786.    end
  787.    message(30,3,"clear",colors.blue)
  788.    message(30,4,"clear",colors.blue)
  789. end
  790.  
  791. if not checkBlocksFile() then
  792.     print("Blocks file not found. Please download and run the saveArea program!")
  793. else
  794.     connectMonitors()
  795.     getFacing()
  796.     setModifiers()
  797.     mainMenu()
  798.     while true do waitPlayer() end
  799. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement