Rodmastercraft

Knight tour

Jan 17th, 2015
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. function load()
  2.     term.clear()
  3.  
  4.     moves = 0
  5.  
  6.     x = math.random(1, 15);
  7.     y = math.random(1, 15);
  8.  
  9.     tPlayer = {
  10.         x = x;
  11.         y = y;
  12.     }
  13.  
  14.     tDone = {(x+10)..":"..(y+2)}
  15. end
  16.  
  17. function getVals( sVals )
  18.     sep = string.find(sVals, ":")
  19.     x = tonumber(string.sub(sVals, 1, (sep-1)))
  20.     y = tonumber(string.sub(sVals, (sep+1), #sVals))
  21.     return x, y
  22. end
  23.  
  24. function drawBoard()
  25.     for i = 3, 17 do
  26.         term.setBackgroundColor(colors.brown)
  27.         term.setCursorPos(11, i)
  28.         term.write("                ")
  29.     end
  30. end
  31.  
  32. function drawDone()
  33.     for k, v in pairs(tDone) do
  34.         x, y = getVals(v)
  35.         term.setCursorPos(x, y)
  36.         term.setBackgroundColor(colors.red)
  37.         term.write(" ")
  38.     end
  39. end
  40.  
  41. function drawPlayer()
  42.     term.setCursorPos(10 + tPlayer.x, 2 + tPlayer.y)
  43.     term.setBackgroundColor(colors.white)
  44.     term.write(" ")
  45. end
  46.  
  47. function drawPossible()
  48.     tPossible = {}
  49.  
  50.     x = tPlayer.x + 10 + 2
  51.     y = tPlayer.y + 2 + 1
  52.  
  53.     tPossible[1] = x..":"..y
  54.  
  55.     term.setCursorPos(x, y)
  56.     term.setBackgroundColor(colors.lime)
  57.     term.write(" ")
  58.  
  59. ----------------------------------
  60.  
  61.     x = tPlayer.x + 10 - 2
  62.     y = tPlayer.y + 2 + 1
  63.  
  64.     tPossible[2] = x..":"..y
  65.  
  66.     term.setCursorPos(x, y)
  67.     term.setBackgroundColor(colors.lime)
  68.     term.write(" ")
  69.  
  70. ----------------------------------
  71.  
  72.     x = tPlayer.x + 10 + 2
  73.     y = tPlayer.y + 2 - 1
  74.  
  75.     tPossible[3] = x..":"..y
  76.  
  77.     term.setCursorPos(x, y)
  78.     term.setBackgroundColor(colors.lime)
  79.     term.write(" ")
  80. -----------------------------------
  81.     x = tPlayer.x + 10 - 2
  82.     y = tPlayer.y + 2 - 1
  83.  
  84.     tPossible[4] = x..":"..y
  85.  
  86.     term.setCursorPos(x, y)
  87.     term.setBackgroundColor(colors.lime)
  88.     term.write(" ")
  89.  
  90. --=================================================
  91. --=================================================
  92.  
  93.     x = tPlayer.x + 10 + 1
  94.     y = tPlayer.y + 2 + 2
  95.  
  96.     tPossible[5] = x..":"..y
  97.  
  98.     term.setCursorPos(x, y)
  99.     term.setBackgroundColor(colors.lime)
  100.     term.write(" ")
  101. ------------------------------------------------
  102.     x = tPlayer.x + 10 - 1
  103.     y = tPlayer.y + 2 + 2
  104.  
  105.     tPossible[6] = x..":"..y
  106.  
  107.     term.setCursorPos(x, y)
  108.     term.setBackgroundColor(colors.lime)
  109.     term.write(" ")
  110. ---------------------------------------------
  111.     x = tPlayer.x + 10 + 1
  112.     y = tPlayer.y + 2 - 2
  113.  
  114.     tPossible[7] = x..":"..y
  115.  
  116.     term.setCursorPos(x, y)
  117.     term.setBackgroundColor(colors.lime)
  118.     term.write(" ")
  119. --------------------------------------------
  120.     x = tPlayer.x + 10 - 1
  121.     y = tPlayer.y + 2 - 2
  122.  
  123.     tPossible[8] = x..":"..y
  124.  
  125.     term.setCursorPos(x, y)
  126.     term.setBackgroundColor(colors.lime)
  127.     term.write(" ")
  128. end
  129.  
  130. function drawMoves()
  131.     term.setCursorPos(1,1)
  132.     term.write("Moves: "..moves)
  133. end
  134.  
  135. function drawBorder()
  136.     term.setBackgroundColor(colors.black)
  137.     term.setCursorPos(9, 1)
  138.     term.write("                     ")
  139.     term.setCursorPos(9, 2)
  140.     term.write("                     ")
  141.     for i = 0, 16 do
  142.         term.setCursorPos(8, i + 3)
  143.         term.write("   ")
  144.         term.setCursorPos(27, i + 3)
  145.         term.write("   ")
  146.     end
  147.  
  148.     term.setCursorPos(9, 18)
  149.     term.write("                   ")
  150.     term.setCursorPos(9, 19)
  151.     term.write("                   ")
  152. end
  153.  
  154. load()
  155.  
  156. while true do
  157.    
  158.     drawBoard()
  159.    
  160.    
  161.     drawPossible()
  162.     drawDone(tDone)
  163.     drawPlayer()
  164.     drawBorder()
  165.     drawMoves()
  166.  
  167.     local event, button, xPos, yPos = os.pullEvent("mouse_click")
  168.     step = true
  169.     inBoard = true
  170.     if xPos > 10 and xPos < 27 and yPos >= 3 and yPos < 18 then
  171.         inBoard = true
  172.     else
  173.         inBoard = false
  174.     end
  175.     for key, val in pairs(tDone) do
  176.         dx, dy = getVals(val)
  177.         if (xPos-10) == dx-10 and (yPos-2) == dy-2 then
  178.             step = false
  179.         end
  180.     end
  181.  
  182.     for k, v in pairs(tPossible) do
  183.     x, y = getVals(v)
  184.         if xPos == x and yPos == y and step and inBoard then
  185.  
  186.                 moves = moves + 1
  187.                 tDone[#tDone+1] = x..":"..y
  188.                 tPlayer.x = x - 10
  189.                 tPlayer.y = y - 2
  190.            
  191.         end
  192.     end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment