Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("button")
- m = peripheral.wrap("top")
- rednet.open("bottom")
- local board={}
- local currentTurn
- local turtles={}
- local redRem=0
- local blkRem=0
- function loadBoard()
- for i = 1,8 do
- board[i] = {}
- turtles[i] = {}
- end
- end
- function setPiece(a,b,color)
- board[a][b]=color
- end
- function setTurtle(a,b,id)
- turtles[a][b]=id
- end
- function getTurtle(a,b)
- return(turtles[a][b])
- end
- function getPiece(a,b)
- return(board[a][b])
- end
- function newGame()
- for i = 1,7,2 do
- tInc = (i-1)/2
- setPiece(i,1,colors.gray)
- setTurtle(i,1,236+tInc)
- setPiece(i+1,2,colors.gray)
- setTurtle(i+1,2,232+tInc)
- setPiece(i,3,colors.gray)
- setTurtle(i,3,228+tInc)
- setPiece(i+1,6,colors.pink)
- setTurtle(i+1,6,216+tInc)
- setPiece(i,7,colors.pink)
- setTurtle(i,7,220+tInc)
- setPiece(i+1,8,colors.pink)
- setTurtle(i+1,8,224+tInc)
- end
- currentTurn = colors.pink
- for i = 216,239 do
- rednet.send(i,"goHome")
- end
- end
- function switchTurn()
- if currentTurn == colors.pink then
- currentTurn = colors.gray
- else
- currentTurn = colors.pink
- end
- dispTurn()
- end
- function dispTurn()
- m.setBackgroundColor(colors.white)
- m.setTextColor(colors.green)
- m.setCursorPos(29,2)
- m.write("Turn: ")
- m.setTextColor(currentTurn)
- if currentTurn == colors.pink then
- m.write("Red ")
- else
- m.write("Black")
- end
- dispRemaining()
- end
- function dispRemaining()
- m.setBackgroundColor(colors.white)
- m.setTextColor(colors.green)
- m.setCursorPos(29,7)
- m.write("Pieces Left:")
- m.setTextColor(colors.pink)
- m.setCursorPos(29,8)
- m.write("R:"..redRem.." : ")
- m.setTextColor(colors.black)
- m.write("B:"..blkRem.." ")
- end
- function drawPieces()
- local tempcolor
- for a = 1,8 do
- for b = 1,8 do
- tempcolor = getPiece(a,b)
- if (tempcolor ~= nil) then
- m.setTextColor(tempcolor)
- drawPiece(a,b)
- end
- end
- end
- end
- function getXY(a,b)
- x = a*3
- y = b*2
- return x,y
- end
- function drawPiece(a,b)
- x,y = getXY(a,b)
- m.setBackgroundColor(getColor(x,y))
- x,y = getCorner(x,y)
- x = x+1
- m.setCursorPos(x,y)
- m.write("X")
- end
- function getCorner(x,y)
- if y%2~=0 then y=y-1 end
- if x%3~=0 then x=x-1 end
- if x%3~=0 then x=x-1 end
- return x,y
- end
- function getBoardPos(x,y)
- x,y = getCorner(x,y)
- x = x/3
- y = y/2
- return x,y
- end
- function getColor(x,y)
- x,y = getCorner(x,y)
- local invert = false
- if y%4 == 0 then invert = true end
- if (x%2==0) then
- if invert then return(colors.black) else return(colors.red) end
- else
- if invert then return(colors.red) else return(colors.black) end
- end
- end
- function background(bgcolor)
- m.setBackgroundColor(bgcolor)
- for j = 1, 20 do
- for i = 1,39 do
- m.setCursorPos(i,j)
- m.write(" ")
- end
- end
- end
- function drawBoard()
- for y = 2,17 do
- for x = 3,26 do
- m.setBackgroundColor(getColor(x,y))
- m.setCursorPos(x,y)
- m.write(" ")
- end
- end
- message(30,5,"clear",colors.red)
- end
- function getClick()
- event,side,x,y = os.pullEvent("monitor_touch")
- return x,y
- end
- function anyJumps()
- local jump = false
- for a = 1,8 do
- for b = 1,8 do
- if canDouble(a,b) and myTurn(getPiece(a,b)) then
- jump = true
- end
- end
- end
- return jump
- end
- function waitPlayer()
- local tempColor
- local valid = false
- x,y = getClick()
- if button.checkxy(x,y) then return true end
- a,b = getBoardPos(x,y)
- if a<1 or b<1 or a>8 or b>8 then return false end
- if getPiece(a,b) ~= nil and myTurn(getPiece(a,b)) then
- message(30,3,a..":"..b,colors.blue)
- x,y = getClick()
- a2,b2 = getBoardPos(x,y)
- message(30,4,a2..":"..b2,colors.green)
- if tryMove(a,b,a2,b2) == false then
- if tryJump(a,b,a2,b2) then
- switchTurn()
- end
- end
- end
- message(30,3,"clear",colors.blue)
- message(30,4,"clear",colors.blue)
- end
- function myTurn(color)
- if currentTurn == colors.pink then
- if color ~= colors.pink and color ~= colors.blue then return false end
- else
- if color ~= colors.gray and color ~= colors.white then return false end
- end
- return true
- end
- function isKing(a,b)
- if getPiece(a,b) == colors.white or getPiece(a,b) == colors.blue then
- return true
- else
- return false
- end
- end
- function isValidMove(a,b,a2,b2)
- local tempColor = getPiece(a,b)
- if getPiece(a2,b2) ~= nil then
- return false
- end
- if tempColor == colors.pink then
- if b < b2 then return false end
- elseif tempColor == colors.gray then
- if b > b2 then return false end
- end
- return true
- end
- function tryMove(a,b,a2,b2)
- if math.abs(a-a2) ~= 1 or math.abs(b-b2) ~= 1 then
- return false
- end
- if anyJumps() then
- message(30,5,"Must Jump!",colors.red)
- return false
- end
- if isValidMove(a,b,a2,b2) then
- movePiece(a,b,a2,b2)
- switchTurn()
- return true
- end
- message(30,5,"clear",colors.red)
- return false
- end
- function isValidJump(a,b,a2,b2)
- if a<1 or a>8 or b<1 or b>8 then return false end
- if a2<1 or a2>8 or b2<1 or b2>8 then return false end
- if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
- return false
- end
- local tempColor = getPiece(a,b)
- local jumpColor = getPiece((a+a2)/2,(b+b2)/2)
- if isValidMove(a,b,a2,b2) == false then return false end
- if jumpColor == nil then return false end
- if tempColor == colors.gray or tempColor == colors.white then
- if jumpColor~=colors.pink and jumpColor~=colors.blue then
- return false
- end
- else
- if jumpColor~=colors.gray and jumpColor~=colors.white then
- return false
- end
- end
- return true
- end
- function canDouble(a2,b2)
- double = false
- if isValidJump(a2,b2,a2+2,b2+2) then double = true end
- if isValidJump(a2,b2,a2-2,b2+2) then double = true end
- if isValidJump(a2,b2,a2+2,b2-2) then double = true end
- if isValidJump(a2,b2,a2-2,b2-2) then double = true end
- return double
- end
- function updScore(color)
- if color == colors.blue or color == colors.pink then
- redRem = redRem-1
- else
- blkRem = blkRem-1
- end
- end
- function tryJump(a,b,a2,b2)
- if math.abs(a-a2)~=2 or math.abs(b-b2)~=2 then
- return false
- end
- if isValidJump(a,b,a2,b2) then
- rednet.send(getTurtle((a+a2)/2,(b+b2)/2),"goOut")
- updScore(getPiece((a+a2)/2,(b+b2)/2))
- setPiece((a+a2)/2,(b+b2)/2,nil)
- setTurtle((a+a2)/2,(b+b2)/2,nil)
- movePiece(a,b,a2,b2)
- while canDouble(a2,b2) do
- a = a2
- b = b2
- message(30,5,"Double!",colors.red)
- x,y = getClick()
- a3,b3 = getBoardPos(x,y)
- tryJump(a,b,a3,b3)
- end
- message(30,5,"clear",colors.red)
- return true
- end
- message(30,5,"clear",colors.blue)
- return false
- end
- function message(x,y,text,color)
- m.setCursorPos(x,y)
- m.setTextColor(color)
- m.setBackgroundColor(colors.white)
- if text == "clear" then
- m.write(" ")
- else
- m.write(text)
- end
- end
- function isRed(color)
- if color == colors.pink or color == colors.blue then
- return true
- else
- return false
- end
- end
- function turtleMove(a,b,a2,b2)
- local tempColor = getPiece(a,b)
- local tempTurtle = getTurtle(a,b)
- local msg = ""
- if a<a2 and b>b2 then
- if isRed(tempColor) then msg="fr" else msg="bl" end
- elseif a>a2 and b>b2 then
- if isRed(tempColor) then msg="fl" else msg="br" end
- elseif a<a2 and b<b2 then
- if isRed(tempColor) then msg="br" else msg="fl" end
- elseif a>a2 and b<b2 then
- if isRed(tempColor) then msg="bl" else msg="fr" end
- end
- if math.abs(a-a2) == 1 and math.abs(b-b2) == 1 then
- msg=msg
- else
- msg="j"..msg
- end
- print(a..","..b..":"..msg..":"..getTurtle(a,b))
- rednet.send(getTurtle(a,b),msg)
- local id,msg,dist = rednet.receive(10)
- setTurtle(a,b,nil)
- setTurtle(a2,b2,tempTurtle)
- print(msg)
- end
- function movePiece(a,b,a2,b2)
- local tempColor = getPiece(a,b)
- if tempColor==colors.pink and b2==1 then
- tempColor=colors.blue
- elseif tempColor==colors.gray and b2==8 then
- tempColor = colors.white
- end
- turtleMove(a,b,a2,b2)
- setPiece(a2,b2,tempColor)
- setPiece(a,b,nil)
- drawBoard()
- drawPieces()
- end
- function quit()
- shell.run("testOut")
- m.setBackgroundColor(colors.black)
- m.clear()
- os.pullEvent("monitor_touch")
- background(colors.white)
- drawBoard()
- button.setTable("NewGame",new,30,38,15,17)
- button.setTable("Quit",quit,2,7,19,19)
- m.setTextColor(colors.black)
- button.screen()
- end
- function new()
- redRem = 12
- blkRem = 12
- background(colors.white)
- drawBoard()
- loadBoard()
- newGame()
- drawPieces()
- dispTurn()
- m.setTextColor(colors.black)
- button.setTable("NewGame",new,30,38,15,17)
- button.setTable("Quit",quit,2,7,19,19)
- button.screen()
- end
- quit()
- while true do waitPlayer() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement