Advertisement
Fews

Tic Tac Toe

Jul 24th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. rednet.open("top") --enable modem on the right side of the PC
  2. local idinternet = 13
  3. term.clear()
  4. term.setCursorPos(1,1)
  5. print("==================")
  6. print(" Tic Tac Toe ")
  7. print(" Created by fews ")
  8. print("==================")
  9. print("1. Start Game")
  10. print("2. Exit ")
  11. io.write("Select menu : ") local menu = read()
  12.  
  13. if menu == "1" then
  14. local map = {}
  15. for i=1,3 do
  16. map[i]={}
  17. for j=1,3 do
  18. map[i][j] = "-";
  19. end
  20. end
  21.  
  22. io.write("IP adress player 2 : ") local ip_adress = read()
  23. rednet.send(idinternet,"internet;tictactoe;"..ip_adress)
  24. id,message = rednet.receive()
  25. print(message)
  26. print("Waiting Response from "..ip_adress)
  27. id,message = rednet.receive()
  28. print(message)
  29. rednet.send(idinternet,"internet;tictactoe_start;"..ip_adress)
  30. sleep(5)
  31.  
  32. local yourturn=false
  33. if string.find(message,"Accept") then
  34. yourturn=true
  35. end
  36.  
  37. local moveke=0
  38. local win=false
  39. local turn ="X"
  40. while win~=true do
  41. term.clear()
  42. term.setCursorPos(1,1)
  43. print(" x 1 2 3")
  44. print(" -------")
  45. print("y|")
  46. for i=1,3 do
  47. io.write(tostring(i).."|")
  48. for j=1,3 do
  49. io.write(" "..map[i][j])
  50. end
  51. print("")
  52. end
  53. moveke = moveke+1
  54.  
  55. --check win
  56. if map[1][1]==turn and map[1][2]==turn and map[1][3]==turn or
  57. map[2][1]==turn and map[2][2]==turn and map[2][3]==turn or
  58. map[3][1]==turn and map[3][2]==turn and map[3][3]==turn or
  59. map[1][1]==turn and map[2][1]==turn and map[3][1]==turn or
  60. map[1][2]==turn and map[2][2]==turn and map[3][2]==turn or
  61. map[1][3]==turn and map[2][3]==turn and map[3][3]==turn or
  62. map[1][1]==turn and map[2][2]==turn and map[3][3]==turn or
  63. map[1][3]==turn and map[2][2]==turn and map[3][1]==turn then
  64. win = true
  65. print("Player "..turn.." Win")
  66. yourturn=true
  67. rednet.send(idinternet,"finishtictactoe")
  68. elseif moveke>=10 then
  69. win = true
  70. print("Draw")
  71. yourturn=true
  72. rednet.send(idinternet,"finishtictactoe")
  73. else
  74. if turn == "X" then
  75. turn = "O"
  76. else
  77. turn = "X"
  78. end
  79. end
  80. if win == false and yourturn == true then
  81. local truemove = false
  82. while truemove ~= true do
  83. print("Player "..turn.." turn")
  84. io.write("input pos x : ") local x = read()
  85. io.write("input pos y : ") local y = read()
  86. if tonumber(x)<4 and tonumber(y)<4 and tonumber(x)>0 and tonumber(y)>0 then --check move
  87. if map[tonumber(y)][tonumber(x)] == "-" then
  88. map[tonumber(y)][tonumber(x)] = turn
  89. truemove =true
  90. --jalan
  91. rednet.send(idinternet,"movetictactoe;"..turn..";"..x..";"..y)-- send data to internet turn;x;y
  92. yourturn=false;
  93.  
  94. else
  95. print("invalid move")
  96. end
  97. else
  98. print("invalid move")
  99. end
  100. end
  101.  
  102. elseif yourturn == false then
  103. print("Waiting your opponent turn")
  104. id,message = rednet.receive()
  105. turn = string.sub(message,15,15)
  106. x = string.sub(message,17,17)
  107. y = string.sub(message,19,19)
  108. print("turn x y : "..turn.." "..x.." "..y)
  109. map[tonumber(y)][tonumber(x)] = turn
  110. yourturn=true
  111. end
  112. end
  113.  
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement