Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getPos(x,y,b)
- if x==1 then
- return b[y]
- elseif x==2 then
- return b[y+3]
- else
- return b[y+6]
- end
- end
- function COL(c,b)
- print("Checking for coloum win")
- if c==1 then
- local fp=b[1]
- if b[2]==fp and b[3]==fp then
- return fp
- end
- elseif c==2 then
- local fp=b[4]
- if b[5]==fp and b[6]==fp then
- return fp
- end
- else
- local fp=b[7]
- if fp==b[8] and b[9]==fp then
- return fp
- end
- return false
- end
- end
- function ROW(r,b)
- print("Checking for row win")
- local fp=b[r]
- if fp==b[r+3] and fp==b[r+6] then
- return fp
- end
- return false
- end
- function DIAG(d,b)
- print("Checking for diagonal win")
- if d==1 then
- local fp=b[1]
- if fp==b[5] and fp==b[9] then
- return fp
- end
- else
- local fp=b[3]
- if fp==b[5] and fp==b[7] then
- return fp
- end
- return false
- end
- end
- function isWin(b)
- print("Checking for win…")
- if COL(1,b) then
- return COL(1,b)
- elseif COL(2,b) then
- return COL(2,b)
- elseif COL(3,b) then
- return COL(3,b)
- elseif ROW(1,b) then
- return ROW(1,b)
- elseif ROW(2,b) then
- return ROW(2,b)
- elseif ROW(3,b) then
- return ROW(3,b)
- elseif DIAG(1,b) then
- return DIAG(1,b)
- elseif DIAG(2,b) then
- return DIAG(2,b)
- end
- return false
- end
- function isDraw(b)
- print("Checking for draw.")
- for i=1,9 do
- if b[i]==" " then
- return false
- end
- end
- return true
- end
- function fromID(id)
- while true do
- tid,msg=rednet.receive()
- if tid==id then
- return msg
- end
- end
- end
- --Code Starts Here
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- clear()
- print("Server ID:"..os.getComputerID())
- write("Client One:")
- cOne=read()+0
- write("Client Two:")
- cTwo=read()+0
- write("Rednet side:")
- rednet.open(read())
- clear()
- write("Game will start in 20 seconds. Please tell clients to connect to server ID:"..os.getComputerID())
- sleep(20)
- clear()
- print("Game started.")
- rednet.broadcast("start")
- sleep(5)
- board={}
- for i=1,9 do
- board[(i)]=" "
- end
- turn=true
- while true do
- if turn then
- turn=false
- rednet.send(cOne,"Play")
- play=fromID(cOne)+0
- print("Client one has played.")
- if board[play]==" " then
- board[play]="X"
- rednet.broadcast("Place")
- rednet.broadcast(play.."")
- rednet.broadcast("X")
- end
- else
- turn=true
- rednet.send(cTwo,"Play")
- play=fromID(cTwo)+0
- print("Client Two has played.")
- if board[play]==" " then
- board[play]="O"
- rednet.broadcast("Place")
- rednet.broadcast(play.."")
- rednet.broadcast("O")
- end
- end
- if isDraw(board) then
- rednet.broadcast("lose")
- end
- print("DEBUG:Checkpoint 1.")
- if isWin(board) =="X" then
- rednet.send(cOne,"win")
- rednet.send(cTwo,"lose")
- elseif isWin(board)=="O" then
- rednet.send(cOne,"lose")
- rednet.send(cTwo,"win")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment