Advertisement
Guest User

checkers

a guest
Apr 3rd, 2013
3,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.09 KB | None | 0 0
  1. os.loadAPI("button")
  2. m = peripheral.wrap("top")
  3. rednet.open("bottom")
  4. local board={}
  5. local currentTurn
  6. local turtles={}
  7. local redRem=0
  8. local blkRem=0
  9.  
  10. function loadBoard()
  11.   for i = 1,8 do
  12.      board[i] = {}
  13.      turtles[i] = {}
  14.   end
  15. end
  16.  
  17. function setPiece(a,b,color)
  18.   board[a][b]=color
  19. end
  20.  
  21. function setTurtle(a,b,id)
  22.   turtles[a][b]=id
  23. end
  24.  
  25. function getTurtle(a,b)
  26.    return(turtles[a][b])
  27. end
  28.  
  29. function getPiece(a,b)
  30.   return(board[a][b])
  31. end
  32.  
  33. function newGame()
  34.   for i = 1,7,2 do
  35.      tInc = (i-1)/2
  36.      setPiece(i,1,colors.gray)
  37.      setTurtle(i,1,236+tInc)
  38.      setPiece(i+1,2,colors.gray)
  39.      setTurtle(i+1,2,232+tInc)
  40.      setPiece(i,3,colors.gray)
  41.      setTurtle(i,3,228+tInc)
  42.      setPiece(i+1,6,colors.pink)
  43.      setTurtle(i+1,6,216+tInc)
  44.      setPiece(i,7,colors.pink)
  45.      setTurtle(i,7,220+tInc)
  46.      setPiece(i+1,8,colors.pink)
  47.      setTurtle(i+1,8,224+tInc)
  48.   end
  49.   currentTurn = colors.pink
  50.   for i = 216,239 do
  51.      rednet.send(i,"goHome")
  52.   end
  53. end
  54.  
  55. function switchTurn()
  56.    if currentTurn == colors.pink then
  57.       currentTurn = colors.gray
  58.    else
  59.       currentTurn = colors.pink
  60.    end
  61.    dispTurn()
  62. end
  63.  
  64. function dispTurn()
  65.   m.setBackgroundColor(colors.white)
  66.   m.setTextColor(colors.green)
  67.   m.setCursorPos(29,2)
  68.   m.write("Turn: ")
  69.   m.setTextColor(currentTurn)
  70.   if currentTurn == colors.pink then
  71.      m.write("Red  ")
  72.   else
  73.      m.write("Black")
  74.   end
  75.   dispRemaining()
  76. end
  77.  
  78. function dispRemaining()
  79.   m.setBackgroundColor(colors.white)
  80.   m.setTextColor(colors.green)
  81.   m.setCursorPos(29,7)
  82.   m.write("Pieces Left:")
  83.   m.setTextColor(colors.pink)
  84.   m.setCursorPos(29,8)
  85.   m.write("R:"..redRem.." : ")
  86.   m.setTextColor(colors.black)
  87.   m.write("B:"..blkRem.."  ")
  88. end
  89.  
  90. function drawPieces()
  91.   local tempcolor
  92.   for a = 1,8 do
  93.     for b = 1,8 do
  94.       tempcolor = getPiece(a,b)
  95.       if (tempcolor ~= nil) then
  96.         m.setTextColor(tempcolor)
  97.         drawPiece(a,b)    
  98.       end
  99.     end
  100.   end    
  101. end
  102.  
  103. function getXY(a,b)
  104.    x = a*3
  105.    y = b*2
  106.    return x,y
  107. end
  108.  
  109. function drawPiece(a,b)
  110.   x,y = getXY(a,b)
  111.   m.setBackgroundColor(getColor(x,y))
  112.   x,y = getCorner(x,y)
  113.   x = x+1
  114.   m.setCursorPos(x,y)
  115.   m.write("X")
  116. end
  117.  
  118. function getCorner(x,y)
  119.   if y%2~=0 then y=y-1 end
  120.   if x%3~=0 then x=x-1 end
  121.   if x%3~=0 then x=x-1 end
  122.   return x,y
  123. end
  124.  
  125. function getBoardPos(x,y)
  126.   x,y = getCorner(x,y)
  127.   x = x/3
  128.   y = y/2
  129.   return x,y
  130. end
  131.  
  132. function getColor(x,y)
  133.   x,y = getCorner(x,y)
  134.   local invert = false
  135.   if y%4 == 0 then invert = true end
  136.   if (x%2==0) then
  137.      if invert then return(colors.black) else return(colors.red) end
  138.   else
  139.      if invert then return(colors.red) else return(colors.black) end
  140.   end
  141. end
  142.  
  143. function background(bgcolor)
  144.    m.setBackgroundColor(bgcolor)
  145.    for j = 1, 20 do
  146.       for i = 1,39 do
  147.         m.setCursorPos(i,j)
  148.         m.write(" ")
  149.       end
  150.    end
  151. end
  152.  
  153. function drawBoard()
  154.    for y = 2,17 do
  155.      for x = 3,26 do
  156.        m.setBackgroundColor(getColor(x,y))
  157.        m.setCursorPos(x,y)
  158.        m.write(" ")
  159.      end
  160.    end
  161.    message(30,5,"clear",colors.red)
  162. end
  163.  
  164. function getClick()
  165.    event,side,x,y = os.pullEvent("monitor_touch")
  166.    return x,y
  167. end
  168.  
  169. function anyJumps()
  170.    local jump = false
  171.    for a = 1,8 do
  172.       for b = 1,8 do
  173.          if canDouble(a,b) and myTurn(getPiece(a,b)) then
  174.             jump = true
  175.          end
  176.       end
  177.    end
  178.    return jump
  179. end
  180.  
  181. function waitPlayer()
  182.    local tempColor
  183.    local valid = false
  184.    x,y = getClick()
  185.    if button.checkxy(x,y) then return true end
  186.    a,b = getBoardPos(x,y)
  187.    if a<1 or b<1 or a>8 or b>8 then return false end
  188.    if getPiece(a,b) ~= nil and myTurn(getPiece(a,b)) then
  189.      message(30,3,a..":"..b,colors.blue)
  190.      x,y = getClick()
  191.      a2,b2 = getBoardPos(x,y)
  192.      message(30,4,a2..":"..b2,colors.green)
  193.      if tryMove(a,b,a2,b2) == false then
  194.        if tryJump(a,b,a2,b2) then
  195.           switchTurn()
  196.        end
  197.      end
  198.    end
  199.    message(30,3,"clear",colors.blue)
  200.    message(30,4,"clear",colors.blue)
  201. end
  202.  
  203. function myTurn(color)
  204.    if currentTurn == colors.pink then
  205.       if color ~= colors.pink and color ~= colors.blue then return false end
  206.    else
  207.       if color ~= colors.gray and color ~= colors.white then return false end
  208.    end
  209.    return true
  210. end
  211.  
  212. function isKing(a,b)
  213.   if getPiece(a,b) == colors.white or getPiece(a,b) == colors.blue then
  214.      return true
  215.   else
  216.      return false
  217.   end
  218. end
  219.  
  220. function isValidMove(a,b,a2,b2)
  221.    local tempColor = getPiece(a,b)
  222.    if getPiece(a2,b2) ~= nil then
  223.      return false
  224.    end
  225.    if tempColor == colors.pink then
  226.       if b < b2 then return false end
  227.    elseif tempColor == colors.gray then
  228.       if b > b2 then return false end
  229.    end
  230.    return true
  231. end  
  232.  
  233. function tryMove(a,b,a2,b2)
  234.    if math.abs(a-a2) ~= 1 or math.abs(b-b2) ~= 1 then
  235.      return false
  236.    end
  237.    if anyJumps() then
  238.       message(30,5,"Must Jump!",colors.red)
  239.       return false
  240.    end
  241.    if isValidMove(a,b,a2,b2) then
  242.       movePiece(a,b,a2,b2)
  243.       switchTurn()
  244.       return true
  245.    end
  246.    message(30,5,"clear",colors.red)
  247.    return false
  248. end
  249.  
  250. function isValidJump(a,b,a2,b2)
  251.    if a<1 or a>8 or b<1 or b>8 then return false end
  252.    if a2<1 or a2>8 or b2<1 or b2>8 then return false end
  253.    if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
  254.       return false
  255.    end
  256.    local tempColor = getPiece(a,b)
  257.    local jumpColor = getPiece((a+a2)/2,(b+b2)/2)
  258.    if isValidMove(a,b,a2,b2) == false then return false end
  259.    if jumpColor == nil then return false end
  260.    if tempColor == colors.gray or tempColor == colors.white then
  261.       if jumpColor~=colors.pink and jumpColor~=colors.blue then
  262.         return false
  263.       end  
  264.    else
  265.       if jumpColor~=colors.gray and jumpColor~=colors.white then
  266.         return false
  267.       end
  268.    end
  269.    return true
  270. end
  271.  
  272. function canDouble(a2,b2)
  273.   double = false
  274.   if isValidJump(a2,b2,a2+2,b2+2) then double = true end
  275.   if isValidJump(a2,b2,a2-2,b2+2) then double = true end
  276.   if isValidJump(a2,b2,a2+2,b2-2) then double = true end
  277.   if isValidJump(a2,b2,a2-2,b2-2) then double = true end
  278.   return double
  279. end  
  280.  
  281. function updScore(color)
  282.    if color == colors.blue or color == colors.pink then
  283.       redRem = redRem-1
  284.    else
  285.       blkRem = blkRem-1
  286.    end
  287. end
  288.  
  289. function tryJump(a,b,a2,b2)
  290.    if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
  291.       return false
  292.    end
  293.    if isValidJump(a,b,a2,b2) then
  294.       rednet.send(getTurtle((a+a2)/2,(b+b2)/2),"goOut")
  295.       updScore(getPiece((a+a2)/2,(b+b2)/2))
  296.       setPiece((a+a2)/2,(b+b2)/2,nil)
  297.       setTurtle((a+a2)/2,(b+b2)/2,nil)
  298.       movePiece(a,b,a2,b2)
  299.       while canDouble(a2,b2) do
  300.         a = a2
  301.         b = b2
  302.         message(30,5,"Double!",colors.red)
  303.         x,y = getClick()
  304.         a3,b3 = getBoardPos(x,y)
  305.         tryJump(a,b,a3,b3)
  306.       end
  307.       message(30,5,"clear",colors.red)
  308.       return true
  309.    end
  310.    message(30,5,"clear",colors.blue)
  311.    return false
  312. end
  313.  
  314. function message(x,y,text,color)
  315.    m.setCursorPos(x,y)
  316.    m.setTextColor(color)
  317.    m.setBackgroundColor(colors.white)
  318.    if text == "clear" then
  319.       m.write("          ")
  320.    else
  321.       m.write(text)
  322.    end
  323. end
  324.  
  325. function isRed(color)
  326.    if color == colors.pink or color == colors.blue then
  327.       return true
  328.    else
  329.       return false
  330.    end
  331. end
  332.  
  333. function turtleMove(a,b,a2,b2)
  334.    local tempColor = getPiece(a,b)
  335.    local tempTurtle = getTurtle(a,b)
  336.    local msg = ""
  337.    if a<a2 and b>b2 then
  338.       if isRed(tempColor) then msg="fr" else msg="bl" end
  339.    elseif a>a2 and b>b2 then
  340.       if isRed(tempColor) then msg="fl" else msg="br" end
  341.    elseif a<a2 and b<b2 then
  342.       if isRed(tempColor) then msg="br" else msg="fl" end
  343.    elseif a>a2 and b<b2 then
  344.       if isRed(tempColor) then msg="bl" else msg="fr" end
  345.    end
  346.    if math.abs(a-a2) == 1 and math.abs(b-b2) == 1 then
  347.       msg=msg
  348.    else
  349.       msg="j"..msg
  350.    end    
  351.    print(a..","..b..":"..msg..":"..getTurtle(a,b))
  352.    rednet.send(getTurtle(a,b),msg)
  353.    local id,msg,dist = rednet.receive(10)
  354.    setTurtle(a,b,nil)
  355.    setTurtle(a2,b2,tempTurtle)
  356.    print(msg)
  357. end
  358.  
  359. function movePiece(a,b,a2,b2)
  360.    local tempColor = getPiece(a,b)
  361.    if tempColor==colors.pink and b2==1 then
  362.       tempColor=colors.blue
  363.    elseif tempColor==colors.gray and b2==8 then
  364.       tempColor = colors.white
  365.    end
  366.    turtleMove(a,b,a2,b2)
  367.    setPiece(a2,b2,tempColor)
  368.    setPiece(a,b,nil)
  369.    drawBoard()
  370.    drawPieces()
  371. end
  372.  
  373. function quit()
  374.    shell.run("testOut")
  375.    m.setBackgroundColor(colors.black)
  376.    m.clear()
  377.    os.pullEvent("monitor_touch")
  378.    background(colors.white)
  379.    drawBoard()
  380.    button.setTable("NewGame",new,30,38,15,17)
  381.    button.setTable("Quit",quit,2,7,19,19)  
  382.    m.setTextColor(colors.black)
  383.    button.screen()
  384. end
  385.  
  386. function new()
  387.   redRem = 12
  388.   blkRem = 12
  389.   background(colors.white)
  390.   drawBoard()
  391.   loadBoard()
  392.   newGame()
  393.   drawPieces()
  394.   dispTurn()
  395.   m.setTextColor(colors.black)
  396.   button.setTable("NewGame",new,30,38,15,17)
  397.   button.setTable("Quit",quit,2,7,19,19)
  398.   button.screen()
  399. end
  400.  
  401. quit()
  402. while true do waitPlayer() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement