Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- term.setCursorPos(1,1)
- os.loadAPI(shell.resolve('.')..'/add')
- local tPieces=add.board()
- local tTaken={white={},black={}}
- table.insert(tPieces,add.R(1,8,true))
- table.insert(tPieces,add.R(8,8,true))
- table.insert(tPieces,add.H(2,8,true))
- table.insert(tPieces,add.H(7,8,true))
- table.insert(tPieces,add.B(3,8,true))
- table.insert(tPieces,add.B(6,8,true))
- table.insert(tPieces,add.Q(4,8,true))
- table.insert(tPieces,add.K(5,8,true))
- table.insert(tPieces,add.R(1,1,false))
- table.insert(tPieces,add.R(8,1,false))
- table.insert(tPieces,add.H(2,1,false))
- table.insert(tPieces,add.H(7,1,false))
- table.insert(tPieces,add.B(3,1,false))
- table.insert(tPieces,add.B(6,1,false))
- table.insert(tPieces,add.K(4,1,false))
- table.insert(tPieces,add.Q(5,1,false))
- for i=1,8 do
- table.insert(tPieces,add.P(i,7,true))
- table.insert(tPieces,add.P(i,2,false))
- end
- local turn=true
- local offsetx,offsety=2,3
- local function render(x,y,piece,check)
- term.setCursorPos(2,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- if check then
- write('-CHECK-')
- end
- term.setCursorPos(2,2)
- for k,v in pairs(tTaken.white) do
- write(v)
- end
- term.setCursorPos(2,offsety+10)
- for k,v in pairs(tTaken.black) do
- write(v)
- end
- x=x or 1
- y=y or 1
- for cy=1,8 do
- for cx=1,8 do
- term.setCursorPos(x+cx,y+cy)
- term.setBackgroundColor((cx+cy)%2==0 and colors.gray or colors.black)
- term.setTextColor(tPieces(cx,cy).col and colors.white or colors.lightBlue)
- write(tPieces(cx,cy).sym)
- end
- end
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- for cy=0,9 do
- term.setCursorPos(x,y+cy)
- write(' ')
- term.setCursorPos(x+9,y+cy)
- write(' ')
- end
- term.setCursorPos(x,y)
- write(' '..(turn and 'white' or 'black')..string.rep(' ',4))
- term.setCursorPos(x,y+9)
- write(string.rep(' ',10))
- if type(piece)=='table' and piece.x then
- term.setCursorPos(piece.x+x,piece.y+y)
- term.setBackgroundColor(colors.purple)
- term.setTextColor(tPieces(piece.x,piece.y).col and colors.white or colors.lightBlue)
- write(piece.sym)
- term.setBackgroundColor(colors.blue)
- for num,t in pairs(piece()) do
- term.setCursorPos(x+t[1],y+t[2])
- term.setTextColor(tPieces(cx,cy).col and colors.white or colors.lightBlue)
- write(tPieces(t[1],t[2]).sym)
- end
- end
- end
- local function chk(turn)
- local king
- for pNum,piece in pairs(tPieces) do
- if piece.sym=='K' and piece.col==turn then king=piece end
- end
- if king==nil then error((turn and 'Black' or 'White')..' player wins!') end
- for pNum,piece in pairs(tPieces) do
- for tNum,t in pairs(piece()) do
- if piece.col~=turn and t[1]==king.x and t[2]==king.y then
- return true
- end
- end
- end
- end
- local function swap(t)
- local piece,num=tPieces(t.x,t.y)
- term.setCursorPos(13,2)
- write('pick your new piece')
- term.setCursorPos(13,3)
- write('R H B Q')
- local tLoc={R={13,3},H={15,3},B={17,3},Q={19,3}}
- for k,v in pairs(tLoc) do
- term.setCursorPos(unpack(v))
- write(k)
- end
- while true do
- local input={os.pullEvent()}
- if input[1]=='mouse_click' and input[2]==1 then
- for k,v in pairs(tLoc) do
- if input[3]==v[1] and input[4]==v[2] then
- tPieces[num]=add[k](piece.x,piece.y,piece.col)
- return true
- end
- end
- end
- end
- end
- local function move(x,y,piece)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(x,y)
- write('x')
- while true do
- local input={os.pullEvent()}
- if input[1]=='mouse_click' and input[2]==1 then
- if input[3]==x and input[4]==y or input[3]==x+piece.x and input[4]==y+piece.y then
- break
- else
- for num,t in pairs(piece()) do
- if t[1]+x==input[3] and t[2]+y==input[4] then
- local target,pNum=tPieces(t[1],t[2])
- if pNum then tPieces[pNum]=nil tTaken[target.col and 'white' or 'black'][pNum]=target.sym end
- local oldx,oldy=piece.x,piece.y
- piece.x=t[1]
- piece.y=t[2]
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if chk(turn) then
- term.setCursorPos(2,1)
- print('NOPE, will leave you in check')
- if pNum then tPieces[pNum]=target tTaken[target.col and 'white' or 'black'][pNum]=nil end
- piece.x=oldx
- piece.y=oldy
- else
- term.setCursorPos(2,1)
- term.clearLine()
- piece.hasMoved=true
- if piece.sym=='P' and (piece.col==true and piece.y==1 or piece.col==false and piece.y==8) then
- swap(piece)
- end
- return true
- end
- end
- end
- end
- end
- end
- end
- render(offsetx,offsety)
- while true do
- local input={os.pullEvent()}
- if input[1]=='mouse_click' and input[2]==1 then
- local piece=tPieces(input[3]-offsetx,input[4]-offsety)
- if piece.col==turn then
- render(offsetx,offsety,piece)
- if move(offsetx,offsety,piece) then turn=(not turn) end
- render(offsetx,offsety,nil,chk(turn))
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment