pixellover2

chess (monitor)

Oct 18th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.95 KB | None | 0 0
  1. --[[ Chess Watch
  2.  
  3. The fun way to watch Minecraft Chess :)
  4.  
  5. Written by NitrogenFingers
  6. Art curtosy of NN(1998)
  7. ]]--
  8.  
  9. local monitorSide = "left"
  10. local side = nil
  11.  
  12. local board = {
  13.   --        a     b     c     d     e     f     g     h
  14.   [8] = { "-C",  "-N",  "-B",  "-Q",  "-K",  "-B",  "-N",  "-C" },
  15.   [7] = { "-p",  "-p",  "-p",  "-p",  "-p",  "-p",  "-p",  "-p" },
  16.   [6] = { " ",  " ",  " ",  " ",  " ",  " ",  " ",  " " },
  17.   [5] = { " ",  " ",  "",  " ",  " ",  " ",  " ",  " " },
  18.   [4] = { " ",  " ",  " ",  " ",  " ",  " ",  " ",  " " },
  19.   [3] = { " ",  " ",  " ",  " ",  " ",  " ",  " ",  " " },
  20.   [2] = { "+p",  "+p",  "+p",  "+p",  "+p",  "+p",  "+p",  "+p" },
  21.   [1] = { "+C",  "+N",  "+B",  "+Q",  "+K",  "+B",  "+N",  "+C" }
  22.   --        a     b     c     d     e     f     g     h
  23. }
  24. local pawnList = {
  25.     "(Q)",
  26.     "/Q\\",
  27.     "==="
  28. }
  29. local knightList = {
  30.     " ,^. ",
  31.     "(QQ'\\",
  32.     "|QQ\\ ",
  33.     "==== "
  34. }
  35. local bishopList = {
  36.     " o  ",
  37.     "(Q/)",
  38.     "/QQ\\",
  39.     "===="
  40. }
  41. local castleList = {
  42.     "WWWW",
  43.     "|QQ|",
  44.     "/QQ\\",
  45.     "===="
  46. }
  47. local queenList = {
  48.     "www",
  49.     "\\Q/",
  50.     "(Q)",
  51.     "/Q\\",
  52.     "==="
  53. }
  54. local kingList = {
  55.     "_+_",
  56.     "\\Q/",
  57.     "(Q)",
  58.     "/Q\\",
  59.     "==="
  60. }
  61.  
  62. local backboard = {
  63.         " _____     _____     _____     _____     ",
  64.         "|     :::::     :::::     :::::     :::::|",
  65.         "|     :::::     :::::     :::::     :::::|",
  66.         "|     :::::     :::::     :::::     :::::|",
  67.         "|:::::     :::::     :::::     :::::     |",
  68.         "|:::::     :::::     :::::     :::::     |",
  69.         "|:::::     :::::     :::::     :::::     |",
  70.         "|     :::::     :::::     :::::     :::::|",
  71.         "|     :::::     :::::     :::::     :::::|",
  72.         "|     :::::     :::::     :::::     :::::|",
  73.         "|:::::     :::::     :::::     :::::     |",
  74.         "|:::::     :::::     :::::     :::::     |",
  75.         "|:::::     :::::     :::::     :::::     |",
  76.         "|     :::::     :::::     :::::     :::::|",
  77.         "|     :::::     :::::     :::::     :::::|",
  78.         "|     :::::     :::::     :::::     :::::|",
  79.         "|:::::     :::::     :::::     :::::     |",
  80.         "|:::::     :::::     :::::     :::::     |",
  81.         "|:::::     :::::     :::::     :::::     |",
  82.         "|     :::::     :::::     :::::     :::::|",
  83.         "|     :::::     :::::     :::::     :::::|",
  84.         "|     :::::     :::::     :::::     :::::|",
  85.         "|:::::     :::::     :::::     :::::     |",
  86.         "|:::::     :::::     :::::     :::::     |",
  87.         "|:::::_____:::::_____:::::_____:::::_____|"
  88.     }
  89.  
  90. function printBoard()
  91.     mcall("clear")
  92.     for i = 1, #backboard do
  93.         mcall("setCursorPos", 9,7 + i)
  94.         mcall("write", backboard[i])
  95.     end
  96.    
  97.     for i = 1, 8 do
  98.         for j = 1, 8 do
  99.             printPiece(board[9-i][j], 13 + (j-1)*5, 10 + (i-1)*3)
  100.         end
  101.     end
  102. end
  103.  
  104. function printPiece(piece, x, y)
  105.     white = false
  106.     if string.find(piece, "+") == 1 then white = true end
  107.    
  108.     piece = string.sub(piece, 2)
  109.     list = nil
  110.    
  111.     if piece == "p" then list = pawnList
  112.     elseif piece == "N" then list = knightList
  113.     elseif piece == "B" then list = bishopList
  114.     elseif piece == "C" then list = castleList
  115.     elseif piece == "Q" then list = queenList
  116.     elseif piece == "K" then list = kingList
  117.     else return end
  118.  
  119.     for i=1,#list do
  120.         local nextString = list[i]
  121.         while nextString:find("Q") do
  122.             if white then
  123.                 nextString = string.gsub(nextString, "Q", "#")
  124.             else
  125.                 nextString = string.gsub(nextString, "Q", " ") end
  126.         end
  127.         mcall("setCursorPos", x - #nextString/2, y - #list + i)
  128.         mcall("write", nextString)
  129.     end
  130. end
  131.  
  132. --A combination of the parseInput and the essentials of the makeMove method from chess.lua
  133. function makeMove(input)
  134.     local oldx = convertLetterToCoordinate(string.sub(input, 1, 1))
  135.     local oldy = tonumber(string.sub(input, 2, 2))
  136.     local newx = convertLetterToCoordinate(string.sub(input, 4, 4))
  137.     local newy = tonumber(string.sub(input, 5, 5))
  138.    
  139.     if not oldx or not oldy or not newx or not newy then return end
  140.    
  141.     local oldsub = board[oldy][oldx]
  142.     local newsub = board[newy][newx]
  143.    
  144.     board[newy][newx] = board[oldy][oldx]
  145.     board[oldy][oldx] = " "
  146.    
  147.     --en passant is handled here
  148.     if board[newy][newx] == " " and math.abs(oldx-newx) == 1 and
  149.       board[oldy][oldx]:sub(2) == "p" and board[oldy][newx]:sub(2) == "p" then
  150.         board[oldy][newx] = " "
  151.     end
  152.    
  153.     --Castling is handled here
  154.     if board[newy][newx] == "+K" then
  155.         if newx - oldx == 2 then
  156.             board[1][6] = board[1][8]
  157.             board[1][8] = " "
  158.         elseif newx - oldx == -2 then
  159.             board[1][4] = board[1][1]
  160.             board[1][1] = " "
  161.         end
  162.     end
  163.     if board[newy][newx] == "-K" then
  164.         blackKing = { x = newx, y = newy }
  165.         if newx - oldx == 2 then
  166.             board[8][6] = board[8][8]
  167.             board[8][8] = " "
  168.         elseif newx - oldx == -2 then
  169.             board[8][4] = board[8][1]
  170.             board[8][1] = " "
  171.         end
  172.     end
  173. end
  174.  
  175. function convertLetterToCoordinate(input)
  176.   if input=="a" then return 1
  177.   elseif input=="b" then return 2
  178.   elseif input=="c" then return 3
  179.   elseif input=="d" then return 4
  180.   elseif input=="e" then return 5
  181.   elseif input=="f" then return 6
  182.   elseif input=="g" then return 7
  183.   elseif input=="h" then return 8
  184.   else return nil end
  185. end
  186.  
  187. --Runs a term application on the monitor. A time-saving method, and we only ever use 3 parameters.
  188. function mcall(f,p1,p2,p3)
  189.     peripheral.call(monitorSide, f, p1, p2, p3)
  190. end
  191.  
  192. --Finds the side the modem is on, and opens it
  193. function openModem()
  194.     for i=1,#rs.getSides() do
  195.         if peripheral.isPresent(rs.getSides()[i]) and peripheral.getType(rs.getSides()[i]) == "modem" then
  196.             side = rs.getSides()[i]
  197.             rednet.open(side)
  198.             return true
  199.         end
  200.     end
  201.     return false
  202. end
  203. --Finds the side the monitor is on
  204. function findMonitor()
  205.     for i=1,#rs.getSides() do
  206.         if peripheral.isPresent(rs.getSides()[i]) and peripheral.getType(rs.getSides()[i]) == "monitor" then
  207.             monitorSide = rs.getSides()[i]
  208.             return true
  209.         end
  210.     end
  211.     return false
  212. end
  213.  
  214. --This promotes a pawn from a network message
  215. function handleRemotePawnPromotion(msg)
  216.     local x = tonumber(string.sub(msg, 1, 1))
  217.     local y = tonumber(string.sub(msg, 2, 2))
  218.     local piece = string.sub(msg, 3)
  219.    
  220.     board[y][x] = piece
  221.     movesMade[#movesMade].promotion = string.sub(msg, 4)
  222. end
  223.  
  224. function runMonitor()
  225.     while true do
  226.         printBoard()
  227.         local proMsg = nil
  228.         local key,msg = rednet.receive()
  229.         if msg == "#Q" then
  230.             break
  231.         elseif string.find(msg, "#P") == 1 then
  232.             proMsg = string.sub(msg, 3)
  233.         else
  234.             makeMove(msg)
  235.             if proMsg then handleRemovePawnPromotion(proMsg) end
  236.         end
  237.     end
  238.    
  239. end
  240.  
  241. function prepareMonitor()
  242.  
  243.     --This short program is used for monitor displays
  244.     term.write("Enter ID of host: ")
  245.     local number = tonumber(io.read())
  246.     if not number then
  247.         print("Bad input!")
  248.         return
  249.     end
  250.  
  251.     if not openModem() then
  252.         print("No modem on your computer!")
  253.         return
  254.     end
  255.  
  256.     if not findMonitor() then
  257.         print("No monitor on your computer!")
  258.         return
  259.     end
  260.  
  261.     rednet.send(number, "#M")
  262.     local key,msg = rednet.receive(5)
  263.  
  264.     if not key then
  265.         print("No response.")
  266.         return
  267.     end
  268.  
  269.     if msg == "#Y" then
  270.         print("Accepted- starting monitor application")
  271.         runMonitor()
  272.     end
  273. end
  274.  
  275. prepareMonitor()
Add Comment
Please, Sign In to add comment