Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top") --enable modem on the right side of the PC
- local idinternet = 13
- term.clear()
- term.setCursorPos(1,1)
- print("==================")
- print(" Tic Tac Toe ")
- print(" Created by fews ")
- print("==================")
- print("1. Start Game")
- print("2. Exit ")
- io.write("Select menu : ") local menu = read()
- if menu == "1" then
- local map = {}
- for i=1,3 do
- map[i]={}
- for j=1,3 do
- map[i][j] = "-";
- end
- end
- io.write("IP adress player 2 : ") local ip_adress = read()
- rednet.send(idinternet,"internet;tictactoe;"..ip_adress)
- id,message = rednet.receive()
- print(message)
- print("Waiting Response from "..ip_adress)
- id,message = rednet.receive()
- print(message)
- rednet.send(idinternet,"internet;tictactoe_start;"..ip_adress)
- sleep(5)
- local yourturn=false
- if string.find(message,"Accept") then
- yourturn=true
- end
- local moveke=0
- local win=false
- local turn ="X"
- while win~=true do
- term.clear()
- term.setCursorPos(1,1)
- print(" x 1 2 3")
- print(" -------")
- print("y|")
- for i=1,3 do
- io.write(tostring(i).."|")
- for j=1,3 do
- io.write(" "..map[i][j])
- end
- print("")
- end
- moveke = moveke+1
- --check win
- if map[1][1]==turn and map[1][2]==turn and map[1][3]==turn or
- map[2][1]==turn and map[2][2]==turn and map[2][3]==turn or
- map[3][1]==turn and map[3][2]==turn and map[3][3]==turn or
- map[1][1]==turn and map[2][1]==turn and map[3][1]==turn or
- map[1][2]==turn and map[2][2]==turn and map[3][2]==turn or
- map[1][3]==turn and map[2][3]==turn and map[3][3]==turn or
- map[1][1]==turn and map[2][2]==turn and map[3][3]==turn or
- map[1][3]==turn and map[2][2]==turn and map[3][1]==turn then
- win = true
- print("Player "..turn.." Win")
- yourturn=true
- rednet.send(idinternet,"finishtictactoe")
- elseif moveke>=10 then
- win = true
- print("Draw")
- yourturn=true
- rednet.send(idinternet,"finishtictactoe")
- else
- if turn == "X" then
- turn = "O"
- else
- turn = "X"
- end
- end
- if win == false and yourturn == true then
- local truemove = false
- while truemove ~= true do
- print("Player "..turn.." turn")
- io.write("input pos x : ") local x = read()
- io.write("input pos y : ") local y = read()
- if tonumber(x)<4 and tonumber(y)<4 and tonumber(x)>0 and tonumber(y)>0 then --check move
- if map[tonumber(y)][tonumber(x)] == "-" then
- map[tonumber(y)][tonumber(x)] = turn
- truemove =true
- --jalan
- rednet.send(idinternet,"movetictactoe;"..turn..";"..x..";"..y)-- send data to internet turn;x;y
- yourturn=false;
- else
- print("invalid move")
- end
- else
- print("invalid move")
- end
- end
- elseif yourturn == false then
- print("Waiting your opponent turn")
- id,message = rednet.receive()
- turn = string.sub(message,15,15)
- x = string.sub(message,17,17)
- y = string.sub(message,19,19)
- print("turn x y : "..turn.." "..x.." "..y)
- map[tonumber(y)][tonumber(x)] = turn
- yourturn=true
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement