Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function enterSquare(i,j)
- a = setmetatable({},square)
- a.x = i
- a.y = j
- a.moved = false
- if math.floor(i/2) ~= i/2 and math.floor(j/2) ~= j/2 or math.floor(i/2) == i/2 and math.floor(j/2) == j/2 then
- a.color = "brown"
- else
- a.color = "yellow"
- end
- if j == 2 then
- a.piece = 'P'
- a.pc = 'white'
- elseif j == 7 then
- a.piece = 'P'
- a.pc = 'black'
- elseif j == 1 then
- if i == 1 or i == 8 then
- a.piece = 'R'
- a.pc = 'white'
- elseif i == 2 or i == 7 then
- a.piece = 'H'
- a.pc = 'white'
- elseif i == 3 or i == 6 then
- a.piece = 'B'
- a.pc = 'white'
- elseif i == 4 then
- a.piece = 'Q'
- a.pc = 'white'
- elseif i == 5 then
- a.piece = 'K'
- a.pc = 'white'
- end
- elseif j == 8 then
- if i == 1 or i == 8 then
- a.piece = 'R'
- a.pc = 'black'
- elseif i == 2 or i == 7 then
- a.piece = 'H'
- a.pc = 'black'
- elseif i == 3 or i == 6 then
- a.piece = 'B'
- a.pc = 'black'
- elseif i == 4 then
- a.piece = 'Q'
- a.pc = 'black'
- elseif i == 5 then
- a.piece = 'K'
- a.pc = 'black'
- end
- else
- a.pc = 'neutral'
- a.piece = " "
- end
- squares[#squares+1] = a
- end
- function ini()
- m = peripheral.wrap('right')
- m.setBackgroundColor(32768)
- m.clear()
- m.setTextScale(5)
- square = {}
- squares = {}
- square.__index = square
- availMovesX = {}
- availMovesY = {}
- showMoves = false
- blackCheck = false
- whiteCheck = false
- blackCheckMate = false
- whiteCheckMate = false
- playerTurn = 'White'
- for i = 1,8 do
- for j = 1,8 do
- enterSquare(i,j)
- end
- end
- end
- function drawBoard()
- m.setBackgroundColor(32768)
- m.clear()
- for i = 1,#squares do
- bleet = squares[i]
- m.setCursorPos(bleet.x,bleet.y)
- if bleet.color == 'brown' then
- m.setBackgroundColor(4096)
- else
- m.setBackgroundColor(16)
- end
- if showMoves then
- if available(bleet.x,bleet.y) then
- if bleet.color == 'brown' then
- m.setBackgroundColor(8192)
- else
- m.setBackgroundColor(32)
- end
- end
- end
- if bleet.pc == 'white' then
- m.setTextColor(1)
- else
- m.setTextColor(32768)
- end
- if whiteCheck and bleet.piece == 'K' and bleet.pc == 'white' then
- m.setBackgroundColor(16384)
- end
- if blackCheck and bleet.piece == 'K' and bleet.pc == 'black' then
- m.setBackgroundColor(16384)
- end
- m.write(bleet.piece)
- end
- m.setCursorPos(1,9)
- m.setBackgroundColor(32768)
- m.setTextColor(1)
- m.write(playerTurn.."'s turn!")
- if whiteCheck or blackCheck then
- m.setCursorPos(1,10)
- m.setBackgroundColor(32768)
- m.setTextColor(16384)
- m.write('Check!')
- end
- if whiteCheckMate or blackCheckMate then
- m.setCursorPos(1,10)
- m.setBackgroundColor(32768)
- m.setTextColor(16384)
- m.write('Checkmate!')
- end
- end
- function available(x,y)
- for i = 1,#availMovesX do
- if x == availMovesX[i] and y == availMovesY[i] then
- return true
- end
- end
- end
- function getPiece(x,y)
- for i = 1,#squares do
- deet = squares[i]
- if deet.x == x then
- if deet.y == y then
- return deet,i
- end
- end
- end
- end
- function getAvail(x,y)
- availMovesX = {}
- availMovesY = {}
- for i = 1,8 do
- for j = 1,8 do
- if checkMove2(x,y,i,j) then
- availMovesX[#availMovesX+1] = i
- availMovesY[#availMovesY+1] = j
- end
- end
- end
- end
- function getSudoAvail(x,y)
- sudoAvailX = {}
- sudoAvailY = {}
- sheet = getPiece(x,y)
- if sheet.piece == 'B' or sheet.piece == 'Q' then
- for i = 1,4 do
- for j = 1,8 do
- if i == 1 then
- sudox = sheet.x + j
- sudoy = sheet.y + j
- if sudoy < 9 and sudox < 9 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- elseif i == 2 then
- sudox = sheet.x - j
- sudoy = sheet.y + j
- if sudoy < 9 and sudox > 0 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- elseif i == 3 then
- sudox = sheet.x + j
- sudoy = sheet.y - j
- if sudox < 9 and sudoy > 0 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- else
- sudox = sheet.x - j
- sudoy = sheet.y - j
- if sudoy > 0 and sudox > 0 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- end
- end
- end
- end
- if sheet.piece == 'R' or sheet.piece == 'Q' then
- for i = 1,4 do
- for j = 1,8 do
- if i == 1 then
- sudox = sheet.x + j
- sudoy = sheet.y
- if sudox < 9 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- elseif i == 2 then
- sudox = sheet.x - j
- sudoy = sheet.y
- if sudox > 0 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- elseif i == 3 then
- sudox = sheet.x
- sudoy = sheet.y + j
- if sudoy < 9 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- else
- sudox = sheet.x
- sudoy = sheet.y - j
- if sudoy > 0 then
- beat = getPiece(sudox,sudoy)
- if beat.piece ~= ' ' then
- if beat.pc ~= sheet.pc then
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- break
- else
- sudoAvailX[#sudoAvailX+1] = sudox
- sudoAvailY[#sudoAvailY+1] = sudoy
- end
- end
- end
- end
- end
- end
- return sudoAvailX,sudoAvailY
- end
- function checkMove(x1,y1,x2,y2)
- local yeet = getPiece(x1,y1)
- local neet = getPiece(x2,y2)
- if yeet.piece == ' ' then
- return false
- end
- if yeet.pc == neet.pc and neet.piece ~= ' ' then
- return false
- end
- if x1 == x2 and y2 == y1 then
- return false
- end
- if x2 > 8 or x2 <1 or y2 > 8 or y2 < 1 then
- return false
- end
- if yeet.piece == 'B' or yeet.piece == 'R' or yeet.piece == 'Q' then
- tabx,taby = getSudoAvail(x1,y1)
- for i = 1,#tabx do
- if x2 == tabx[i] and y2 == taby[i] then
- return true
- end
- end
- return false
- end
- if yeet.piece == 'H' then
- if x2 == x1 + 2 then
- if y2 == y1 + 1 or y2 == y1 - 1 then
- return true
- end
- elseif x2 == x1 - 2 then
- if y2 == y1 + 1 or y2 == y1 - 1 then
- return true
- end
- elseif y2 == y1 + 2 then
- if x2 == x1 + 1 or x2 == x1 - 1 then
- return true
- end
- elseif y2 == y1 - 2 then
- if x2 == x1 + 1 or x2 == x1 - 1 then
- return true
- end
- end
- end
- if yeet.piece == 'K' then
- if y2 == y1 + 1 and x1 == x2 or y2 == y1 - 1 and x1 == x2 or x2 == x1 + 1 and y1 == y2 or x2 == x1 - 1 and y1 == y2 or x1 + 1 == x2 and y1 + 1 == y2 or x1 - 1 == x2 and y1 + 1 == y2 or x1 + 1 == x2 and y1 - 1 == y2 or x1 - 1 == x2 and y1 - 1 == y2 then
- return true
- elseif x2 == x1 + 2 and y2 == y1 or x2 == x1 - 2 and y2 == y1 then
- if yeet.moved then
- return false
- end
- if yeet.pc == 'white' then
- if checkWhite then
- return false
- end
- if x2 == x1 + 2 and y2 == y1 then
- temp1 = getPiece(7,1)
- temp2 = getPiece(6,1)
- if temp1.piece ~= ' ' then
- return false
- end
- if temp2.piece ~= ' ' then
- return false
- end
- rook = getPiece(8,1)
- if rook.piece == 'R' and not rook.moved then
- castleTopRight = true
- return true
- end
- elseif x2 == x1 - 2 and y2 == y1 then
- temp1 = getPiece(4,1)
- temp2 = getPiece(3,1)
- temp3 = getPiece(2,1)
- if temp1.piece ~= ' ' then
- return false
- end
- if temp2.piece ~= ' ' then
- return false
- end
- if temp3.piece ~= ' ' then
- return false
- end
- rook = getPiece (1,1)
- if rook.piece == 'R' and not rook.moved then
- castleTopLeft = true
- return true
- end
- end
- elseif yeet.pc == 'black' then
- if checkBlack then
- return false
- end
- if x2 == x1 + 2 and y2 == y1 then
- temp1 = getPiece(7,8)
- temp2 = getPiece(6,8)
- if temp1.piece ~= ' ' then
- return false
- end
- if temp2.piece ~= ' ' then
- return false
- end
- rook = getPiece(8,8)
- if rook.piece == 'R' and not rook.moved then
- castleBottomRight = true
- return true
- end
- elseif x2 == x1 - 2 and y2 == y1 then
- temp1 = getPiece(4,8)
- temp2 = getPiece(3,8)
- temp3 = getPiece(2,8)
- if temp1.piece ~= ' ' then
- return false
- end
- if temp2.piece ~= ' ' then
- return false
- end
- if temp3.piece ~= ' ' then
- return false
- end
- rook = getPiece (1,8)
- if rook.piece == 'R' and not rook.moved then
- castleBottomLeft = true
- return true
- end
- end
- end
- end
- end
- if yeet.piece == 'P' and yeet.pc == 'white' then
- leet = getPiece(x1,y1+1)
- if y2 == y1 + 1 and neet.piece == ' ' and x2 == x1 then
- return true
- elseif y2 == y1 + 2 and neet.piece == ' ' and leet.piece == ' ' and not yeet.moved and x2 == x1 then
- return true
- elseif y2 == y1 + 1 and x2 == x1 + 1 and neet.piece ~= ' ' then
- return true
- elseif y2 == y1 + 1 and x2 == x1 - 1 and neet.piece ~= ' ' then
- return true
- end
- end
- if yeet.piece == 'P' and yeet.pc == 'black' then
- leet = getPiece(x1,y1-1)
- if y2 == y1 - 1 and neet.piece == ' ' and x2 == x1 then
- return true
- elseif y2 == y1 - 2 and neet.piece == ' ' and leet.piece == ' ' and not yeet.moved and x2 == x1 then
- return true
- elseif y2 == y1 - 1 and x2 == x1 + 1 and neet.piece ~= ' ' then
- return true
- elseif y2 == y1 - 1 and x2 == x1 - 1 and neet.piece ~= ' ' then
- return true
- end
- end
- end
- function checkMove2(x1,y1,x2,y2)
- heat = getPiece(x1,y1)
- backupBoard()
- if checkMove(x1,y1,x2,y2) then
- moveHandler(x1,y1,x2,y2)
- heat = getPiece(x2,y2)
- if heat.pc == 'white' then
- if checkCheckWhite() then
- recoverBoard()
- return false
- else
- recoverBoard()
- end
- else
- if checkCheckBlack() then
- recoverBoard()
- return false
- else
- recoverBoard()
- end
- end
- else
- return false
- end
- return true
- end
- function checkCheckWhite()
- for i = 1,#squares do
- if squares[i].piece == 'K' and squares[i].pc == 'white' then
- king = squares[i]
- break
- end
- end
- xt = king.x
- yt = king.y
- for i = 1,#squares do
- if checkMove(squares[i].x,squares[i].y,xt,yt) then
- return true
- end
- end
- end
- function checkCheckMateWhite()
- backupBoard()
- for i = 1,#squares do
- feet = squares[i]
- if feet.pc == 'white' then
- for x = 1,8 do
- for y = 1,8 do
- if checkMove(feet.x,feet.y,x,y) then
- moveHandler(feet.x,feet.y,x,y)
- if not checkCheckWhite() then
- recoverBoard()
- return false
- end
- recoverBoard()
- end
- end
- end
- end
- end
- return true
- end
- function checkCheckBlack()
- for i = 1,#squares do
- if squares[i].piece == 'K' and squares[i].pc == 'black' then
- king = squares[i]
- break
- end
- end
- xt = king.x
- yt = king.y
- for i = 1,#squares do
- if checkMove(squares[i].x,squares[i].y,xt,yt) then
- return true
- end
- end
- end
- function checkCheckMateBlack()
- backupBoard()
- for i = 1,#squares do
- feet = squares[i]
- if feet.pc == 'black' then
- for x = 1,8 do
- for y = 1,8 do
- if checkMove(feet.x,feet.y,x,y) then
- moveHandler(feet.x,feet.y,x,y)
- if not checkCheckBlack() then
- recoverBoard()
- return false
- end
- recoverBoard()
- end
- end
- end
- end
- end
- return true
- end
- function backupBoard()
- bBoard = {}
- for i = 1,#squares do
- bBoard[i] = {}
- for k,v in pairs(squares[i]) do
- bBoard[i][k] = squares[i][k]
- end
- end
- end
- function recoverBoard()
- squares = {}
- for i = 1,#bBoard do
- squares[i] = {}
- for k,v in pairs(bBoard[i]) do
- squares[i][k] = bBoard[i][k]
- end
- end
- end
- function moveHandler(x1,y1,x2,y2)
- p1,num1 = getPiece(x1,y1)
- p2,num2 = getPiece(x2,y2)
- p2.pc = p1.pc
- p2.piece = p1.piece
- p2.moved = true
- p1.pc = 'neutral'
- p1.piece = ' '
- squares[num1].pc = p1.pc
- squares[num1].piece = p1.piece
- squares[num1].moved = p1.moved
- squares[num2].pc = p2.pc
- squares[num2].piece = p2.piece
- squares[num2].moved = p2.moved
- end
- function turnHandler(player)
- while true do
- event,key,x1,y1 = os.pullEvent('monitor_touch')
- jeet = getPiece(x1,y1)
- if jeet.pc == player then
- getAvail(x1,y1)
- showMoves = true
- drawBoard()
- while true do
- event,key,x2,y2 = os.pullEvent('monitor_touch')
- if checkMove2(x1,y1,x2,y2) then
- temp = getPiece(x1,y1)
- if temp.piece == 'K' and x2 == x1 + 2 and y2 == y1 or temp.piece == 'K' and x2 == x1 - 2 and y2 == y1 then
- if temp.pc == 'white' then
- if x2 == 7 and y2 == 1 and castleTopRight then
- ctr = true
- elseif x2 == 3 and y2 == 1 and castleTopLeft then
- moveHandler(1,1,4,1)
- end
- else
- if x2 == 7 and y2 == 8 and castleBottomRight then
- cbr = true
- elseif x2 == 3 and y2 == 8 and castleBottomLeft then
- moveHandler(1,8,4,8)
- end
- end
- end
- moveHandler(x1,y1,x2,y2)
- if ctr then
- moveHandler(8,1,6,1)
- ctr = false
- end
- if cbr then
- moveHandler(8,8,6,8)
- cbr = false
- end
- showMoves = false
- if player == 'white' then
- playerTurn = 'Black'
- return
- else
- playerTurn = 'White'
- return
- end
- else
- showMoves = false
- drawBoard()
- break
- end
- end
- end
- end
- end
- ini()
- while true do
- drawBoard()
- if playerTurn == 'White' then
- turnHandler('white')
- if checkCheckBlack() then
- if checkCheckMateBlack() then
- blackCheckMate = true
- drawBoard()
- break
- else
- blackCheck = true
- end
- end
- whiteCheck = false
- else
- turnHandler('black')
- if checkCheckWhite() then
- if checkCheckMateWhite() then
- whiteCheckMate = true
- drawBoard()
- break
- else
- whiteCheck = true
- end
- end
- blackCheck = false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement