Advertisement
Aadvert

Untitled

Mar 14th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. -- (C) 2012 Sandre S. (firstrname at gmail)
  2. --
  3. -- Licensed under the Apache License, Version 2.0 (the "License");
  4. -- you may not use this file except in compliance with the License.
  5. -- You may obtain a copy of the License at
  6. --
  7. --     http://www.apache.org/licenses/LICENSE-2.0
  8. --
  9. -- Unless required by applicable law or agreed to in writing, software
  10. -- distributed under the License is distributed on an "AS IS" BASIS,
  11. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. -- See the License for the specific language governing permissions and
  13. -- limitations under the License.
  14. --
  15. local PIECE_EMPTY = " "
  16.  
  17. -- White pieces
  18. local PIECE_WPAWN = "p"
  19. local PIECE_WROOK = "r"
  20. local PIECE_WKNIGHT = "n"
  21. local PIECE_WBISHOP = "b"
  22. local PIECE_WQUEEN = "q"
  23. local PIECE_WKING = "k"
  24.  
  25. -- Black pieces
  26. local PIECE_BPAWN = "P"
  27. local PIECE_BROOK = "R"
  28. local PIECE_BKNIGHT = "N"
  29. local PIECE_BBISHOP = "B"
  30. local PIECE_BQUEEN = "Q"
  31. local PIECE_BKING = "K"
  32.  
  33. local function newBoard()
  34.     local Board = {}
  35.  
  36.     for x = 1, 8 do
  37.         Board[x] = {}
  38.         for y = 1, 8 do
  39.             Board[y] = {}
  40.         end
  41.     end
  42.  
  43.     -- Pawns
  44.     for i = 1, 8 do
  45.         Board[2][i] = PIECE_WPAWN
  46.         Board[7][i] = PIECE_BPAWN
  47.     end
  48.     -- White pieces
  49.     Board[1][1] = PIECE_WROOK
  50.     Board[1][2] = PIECE_WKNIGHT
  51.     Board[1][3] = PIECE_WBISHOP
  52.     Board[1][4] = PIECE_WKING
  53.     Board[1][5] = PIECE_WQUEEN
  54.     Board[1][6] = PIECE_WBISHOP
  55.     Board[1][7] = PIECE_WKNIGHT
  56.     Board[1][8] = PIECE_WROOK
  57.  
  58.     -- Black pieces
  59.     Board[8][1] = PIECE_BROOK
  60.     Board[8][2] = PIECE_BKNIGHT
  61.     Board[8][3] = PIECE_BBISHOP
  62.     Board[8][4] = PIECE_BQUEEN
  63.     Board[8][5] = PIECE_BKING
  64.     Board[8][6] = PIECE_BBISHOP
  65.     Board[8][7] = PIECE_BKNIGHT
  66.     Board[8][8] = PIECE_BROOK
  67.  
  68.     return Board
  69. end
  70.  
  71. function chessMove(Board, from, to)
  72.  
  73. end
  74.  
  75. local BoardTemplate = {}
  76. BoardTemplate[1]    =   " |1|2|3|4|5|6|7|8"
  77. BoardTemplate[2]    =   "A|$ $ $ $ $ $ $ $"
  78. BoardTemplate[3]    =   "B|$ $ $ $ $ $ $ $"
  79. BoardTemplate[4]    =   "C|$ $ $ $ $ $ $ $"
  80. BoardTemplate[5]    =   "D|$ $ $ $ $ $ $ $"
  81. BoardTemplate[6]    =   "E|$ $ $ $ $ $ $ $"
  82. BoardTemplate[7]    =   "F|$ $ $ $ $ $ $ $"
  83. BoardTemplate[8]    =   "G|$ $ $ $ $ $ $ $"
  84. BoardTemplate[9]    =   "H|$ $ $ $ $ $ $ $"
  85.  
  86. function renderBoard(Board)
  87.     term.clear()
  88.     term.setCursorPos(1,1)
  89.     local n = 1
  90.     for i = 1, #BoardTemplate do
  91.         local line = BoardTemplate[i]
  92.         if string.match(line, "%$") then
  93.             local P = 0
  94.             line = string.gsub(line, "(%$)", function(c) P = P + 1; return Board[n][P] or " " end)
  95.             n = n + 1
  96.         end
  97.         print(line)
  98.     end
  99. end
  100.  
  101. local b = newBoard()
  102.  
  103. renderBoard(b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement