Sxw1212

TICTACSERVER

Aug 4th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. function getPos(x,y,b)
  2. if x==1 then
  3. return b[y]
  4. elseif x==2 then
  5. return b[y+3]
  6. else
  7. return b[y+6]
  8. end
  9. end
  10. function COL(c,b)
  11. print("Checking for coloum win")
  12. if c==1 then
  13. local fp=b[1]
  14. if b[2]==fp and b[3]==fp then
  15. return fp
  16. end
  17. elseif c==2 then
  18. local fp=b[4]
  19. if b[5]==fp and b[6]==fp then
  20. return fp
  21. end
  22. else
  23. local fp=b[7]
  24. if fp==b[8] and b[9]==fp then
  25. return fp
  26. end
  27. return false
  28. end
  29. end
  30. function ROW(r,b)
  31. print("Checking for row win")
  32. local fp=b[r]
  33. if fp==b[r+3] and fp==b[r+6] then
  34. return fp
  35. end
  36. return false
  37. end
  38. function DIAG(d,b)
  39. print("Checking for diagonal win")
  40. if d==1 then
  41. local fp=b[1]
  42. if fp==b[5] and fp==b[9] then
  43. return fp
  44. end
  45. else
  46. local fp=b[3]
  47. if fp==b[5] and fp==b[7] then
  48. return fp
  49. end
  50. return false
  51. end
  52. end
  53.  
  54. function isWin(b)
  55. print("Checking for win…")
  56. if COL(1,b) then
  57. return COL(1,b)
  58. elseif COL(2,b) then
  59. return COL(2,b)
  60. elseif COL(3,b) then
  61. return COL(3,b)
  62. elseif ROW(1,b) then
  63. return ROW(1,b)
  64. elseif ROW(2,b) then
  65. return ROW(2,b)
  66. elseif ROW(3,b) then
  67. return ROW(3,b)
  68. elseif DIAG(1,b) then
  69. return DIAG(1,b)
  70. elseif DIAG(2,b) then
  71. return DIAG(2,b)
  72. end
  73. return false
  74. end
  75. function isDraw(b)
  76. print("Checking for draw.")
  77. for i=1,9 do
  78. if b[i]==" " then
  79. return false
  80. end
  81. end
  82. return true
  83. end
  84. function fromID(id)
  85. while true do
  86. tid,msg=rednet.receive()
  87. if tid==id then
  88. return msg
  89. end
  90. end
  91. end
  92.  
  93. --Code Starts Here
  94. function clear()
  95. term.clear()
  96. term.setCursorPos(1,1)
  97. end
  98. clear()
  99. print("Server ID:"..os.getComputerID())
  100. write("Client One:")
  101. cOne=read()+0
  102. write("Client Two:")
  103. cTwo=read()+0
  104. write("Rednet side:")
  105. rednet.open(read())
  106. clear()
  107. write("Game will start in 20 seconds. Please tell clients to connect to server ID:"..os.getComputerID())
  108. sleep(20)
  109. clear()
  110. print("Game started.")
  111. rednet.broadcast("start")
  112. sleep(5)
  113. board={}
  114. for i=1,9 do
  115. board[(i)]=" "
  116. end
  117. turn=true
  118. while true do
  119. if turn then
  120. turn=false
  121. rednet.send(cOne,"Play")
  122. play=fromID(cOne)+0
  123. print("Client one has played.")
  124. if board[play]==" " then
  125. board[play]="X"
  126. rednet.broadcast("Place")
  127. rednet.broadcast(play.."")
  128. rednet.broadcast("X")
  129. end
  130. else
  131. turn=true
  132. rednet.send(cTwo,"Play")
  133. play=fromID(cTwo)+0
  134. print("Client Two has played.")
  135. if board[play]==" " then
  136. board[play]="O"
  137. rednet.broadcast("Place")
  138. rednet.broadcast(play.."")
  139. rednet.broadcast("O")
  140. end
  141. end
  142. if isDraw(board) then
  143. rednet.broadcast("lose")
  144. end
  145. print("DEBUG:Checkpoint 1.")
  146. if isWin(board) =="X" then
  147. rednet.send(cOne,"win")
  148. rednet.send(cTwo,"lose")
  149. elseif isWin(board)=="O" then
  150. rednet.send(cOne,"lose")
  151. rednet.send(cTwo,"win")
  152. end
  153. end
Advertisement
Add Comment
Please, Sign In to add comment