Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.close("left")
- rednet.open("left")
- math.randomseed(os.time())
- -- To the right P1, to the left P2
- local PLAYER_1 = 4
- local PLAYER_2 = 5
- local MAX_NUM = 50
- function dumpInv( side )
- if side == "left" then
- turtle.turnLeft()
- elseif side == "right" then
- turtle.turnRight()
- else
- return
- end
- for i=1,16 do
- turtle.select(i)
- turtle.drop()
- end
- if side == "left" then
- turtle.turnRight()
- elseif side == "right" then
- turtle.turnLeft()
- end
- end
- function dumpInvUp()
- for i=1,16 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- function replySend( id, message )
- while true do
- rednet.send(id, message)
- local e, s, d = os.pullEvent()
- if e == "rednet_message" and s == id then
- return d
- end
- end
- return nil
- end
- playerMessages = {}
- -- Are the player turtles ready?
- playerMessages["p1r"] = nil
- playerMessages["p2r"] = nil
- while playerMessages["p1r"] == nil or playerMessages["p2r"] == nil do
- if replySend(PLAYER_1, "init") == "true" then
- playerMessages["p1r"] = "true"
- end
- if replySend(PLAYER_2, "init") == "true" then
- playerMessages["p2r"] = "true"
- end
- end
- sleep(1)
- while true do
- -- Pick random number and set default guess values
- local rand = math.random(MAX_NUM)
- print("Answer:"..tostring(rand))
- playerMessages["p1g"] = nil
- playerMessages["p2g"] = nil
- playerMessages["p1g"] = replySend(PLAYER_1, "guess")
- replySend(PLAYER_1, "guessreceived")
- print("P1:"..tostring(playerMessages["p1g"]))
- playerMessages["p2g"] = replySend(PLAYER_2, "guess")
- replySend(PLAYER_2, "guessreceived")
- print("P2:"..tostring(playerMessages["p2g"]))
- if math.abs(rand - playerMessages["p1g"]) <
- math.abs(rand - playerMessages["p2g"]) then
- print("I AM THE LAW!") -- P1 wins
- dumpInv("right")
- elseif math.abs(rand - playerMessages["p1g"]) >
- math.abs(rand - playerMessages["p2g"]) then
- print("I AM THE LAW!!") -- P2 wins
- dumpInv("left")
- else
- print("I AM THE LAW!!!") -- Tie
- local tiebreaker = math.random(2)
- print("tiebreaker:P"..tostring(tiebreaker))
- if tiebreaker == 1 then dumpInv("right") end
- if tiebreaker == 2 then dumpInv("left") end
- if tiebreaker ~= 1 and tiebreaker ~=2 then dumpInvUp() end --WTF
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment