Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Variables
- --functions
- emptyField=function(c)
- paintutils.drawBox(18,2,33,14,colors[c])
- paintutils.drawBox(17,2,18,14,colors[c])
- paintutils.drawBox(33,2,34,14,colors[c])
- paintutils.drawBox(23,2,23,14,colors[c])
- paintutils.drawBox(28,2,28,14,colors[c])
- paintutils.drawBox(18,6,33,6,colors[c])
- paintutils.drawBox(18,10,33,10,colors[c])
- end
- BotPlay=function()
- danger=false
- chance=false
- --kannBotVerlieren???
- for i=0,2 do
- if S[i*3+1]==yC and S[3+i*3]=="b" and S[1+i*3]==S[2+i*3] then danger=true sol=3+i*3 end
- if S[i*3+2]==yC and S[1+i*3]=="b" and S[2+i*3]==S[3+i*3] then danger=true sol=1+i*3 end
- if S[i*3+3]==yC and S[2+i*3]=="b" and S[1+i*3]==S[3+i*3] then danger=true sol=2+i*3 end
- if S[i+1]==yC and S[7+i]=="b" and S[1+i]==S[4+i] then danger=true sol=7+i end
- if S[i+4]==yC and S[1+i]=="b" and S[4+i]==S[7+i] then danger=true sol=1+i end
- if S[i+7]==yC and S[4+i]=="b" and S[1+i]==S[7+i] then danger=true sol=4+i end
- end
- if S[1]==yC and S[9]=="b" and S[1]==S[5] then danger=true sol=9 end
- if S[5]==yC and S[1]=="b" and S[5]==S[9] then danger=true sol=1 end
- if S[9]==yC and S[5]=="b" and S[1]==S[9] then danger=true sol=5 end
- if S[3]==yC and S[7]=="b" and S[3]==S[5] then danger=true sol=7 end
- if S[5]==yC and S[3]=="b" and S[5]==S[7] then danger=true sol=3 end
- if S[7]==yC and S[5]=="b" and S[3]==S[7] then danger=true sol=5 end
- --kannBotGewinnen???
- for i=0,2 do
- if S[i*3+1]==hC and S[3+i*3]=="b" and S[1+i*3]==S[2+i*3] then chance=true sol=3+i*3 end
- if S[i*3+2]==hC and S[1+i*3]=="b" and S[2+i*3]==S[3+i*3] then chance=true sol=1+i*3 end
- if S[i*3+3]==hC and S[2+i*3]=="b" and S[1+i*3]==S[3+i*3] then chance=true sol=2+i*3 end
- if S[i+1]==hC and S[7+i]=="b" and S[1+i]==S[4+i] then chance=true sol=7+i end
- if S[i+4]==hC and S[1+i]=="b" and S[4+i]==S[7+i] then chance=true sol=1+i end
- if S[i+7]==hC and S[4+i]=="b" and S[1+i]==S[7+i] then chance=true sol=4+i end
- end
- if S[1]==hC and S[9]=="b" and S[1]==S[5] then chance=true sol=9 end
- if S[5]==hC and S[1]=="b" and S[5]==S[9] then chance=true sol=1 end
- if S[9]==hC and S[5]=="b" and S[1]==S[9] then chance=true sol=5 end
- if S[3]==hC and S[7]=="b" and S[3]==S[5] then chance=true sol=7 end
- if S[5]==hC and S[3]=="b" and S[5]==S[7] then chance=true sol=3 end
- if S[7]==hC and S[5]=="b" and S[7]==S[3] then chance=true sol=5 end
- rand=math.random(1,10)
- if danger==true and chance==false then
- drawSquare(sol,hisColor)
- S[sol]=hisColor
- end
- if chance==true then
- drawSquare(sol,hisColor)
- S[sol]=hisColor
- end
- if chance==false and danger==false then
- lol=true
- while lol==true do
- rand2=math.random(1,9)
- if S[rand2]=="b" then
- drawSquare(rand2,hisColor)
- S[rand2]=hisColor
- lol=false
- end
- end
- end
- end
- draw1=function(c1,x,y)
- paintutils.drawFilledBox(x,y,(x+3),(y+2),colors[c1])
- end
- draw2=function(c2,x,y)
- paintutils.drawFilledBox(x,y,(x+3),(y+2),colors[c2])
- end
- showTurn=function(yourTurn)
- paintutils.drawBox(1,1,25,1,colors.black)
- term.setCursorPos(2,1)
- if yourTurn==true then
- term.setTextColor(colors[yourColor])
- term.setBackgroundColor(colors.black)
- write("your Turn")
- else
- term.setTextColor(colors[hisColor])
- term.setBackgroundColor(colors.black)
- write("enemys Turn")
- end
- end
- resetScreen=function()
- term.setBackgroundColor(colors.black)
- term.clear()
- emptyField("white")
- paintutils.drawFilledBox(1,16,51,19,colors[yourColor])
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.setCursorPos(42,1)
- write("Quit Game")
- end
- testSquare=function(x,y)
- if x>30 and y==1 then
- term.setBackgroundColor(colors.black)
- shell.run("clear")
- error()
- end
- if x>18 and x<23 and y>2 and y<6 then
- Square=1
- end
- if x>18 and x<23 and y>6 and y<10 then
- Square=2
- end
- if x>18 and x<23 and y>10 and y<14 then
- Square=3
- end
- if x>23 and x<28 and y>2 and y<6 then
- Square=4
- end
- if x>23 and x<28 and y>6 and y<10 then
- Square=5
- end
- if x>23 and x<28 and y>10 and y<14 then
- Square=6
- end
- if x>28 and x<33 and y>2 and y<6 then
- Square=7
- end
- if x>28 and x<33 and y>6 and y<10 then
- Square=8
- end
- if x>28 and x<33 and y>10 and y<14 then
- Square=9
- end
- end
- testVictory=function(S,turn)
- if S[1]==S[2] and S[2]==S[3] and not(S[1]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[4]==S[5] and S[5]==S[6] and not(S[5]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[7]==S[8] and S[8]==S[9] and not(S[7]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[1]==S[4] and S[4]==S[7] and not(S[1]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[2]==S[5] and S[5]==S[8] and not(S[5]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[3]==S[6] and S[6]==S[9] and not(S[3]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[1]==S[5] and S[5]==S[9] and not(S[5]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- if S[7]==S[5] and S[5]==S[3] and not(S[5]=="b") then
- if turn==true then victory=true won=won+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
- if turn==false then victory=true lost=lost+1 sleep(2)
- paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
- end
- end
- drawSquare=function(S,c)
- if S==1 then
- paintutils.drawFilledBox(19,3,22,5,colors[c])
- elseif S==2 then
- paintutils.drawFilledBox(19,7,22,9,colors[c])
- elseif S==3 then
- paintutils.drawFilledBox(19,11,22,13,colors[c])
- elseif S==4 then
- paintutils.drawFilledBox(24,3,27,5,colors[c])
- elseif S==5 then
- paintutils.drawFilledBox(24,7,27,9,colors[c])
- elseif S==6 then
- paintutils.drawFilledBox(24,11,27,13,colors[c])
- elseif S==7 then
- paintutils.drawFilledBox(29,3,32,5,colors[c])
- elseif S==8 then
- paintutils.drawFilledBox(29,7,32,9,colors[c])
- elseif S==9 then
- paintutils.drawFilledBox(29,11,32,13,colors[c])
- end
- end
- --functions
- term.setBackgroundColor(colors.white)
- term.clear()
- paintutils.drawFilledBox(1,1,51,5,colors.blue)
- paintutils.drawFilledBox(1,16,51,19,colors.blue)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(20,9)
- write("Tic Tac Toe")
- term.setCursorPos(19,11)
- write("by MaxintoshOS")
- sleep(1)
- for i=1,26 do
- paintutils.drawBox(1,1,i,19,colors.black)
- paintutils.drawBox(51-i ,1,51,19)
- sleep(0.1)
- end
- term.setTextColor(colors.red)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(21,8)
- write("< Campain >")
- term.setTextColor(colors.blue)
- term.setCursorPos(20,9)
- write(" Multiplayer ")
- term.setCursorPos(21,10)
- write(" Quit Game ")
- startscreen=true
- chosen=1
- while startscreen==true do
- event,c=os.pullEvent("key")
- if c==28 then
- startscreen=false
- else
- if chosen==1 then
- term.setTextColor(colors.blue)
- term.setCursorPos(22,8)
- term.clearLine()
- write(" Campain ")
- term.setCursorPos(19,9)
- term.setTextColor(colors.red)
- term.clearLine()
- write("< Multiplayer >")
- chosen=2
- elseif chosen==2 then
- term.setTextColor(colors.blue)
- term.setCursorPos(20,9)
- term.clearLine()
- write(" Multiplayer ")
- term.setTextColor(colors.red)
- term.setCursorPos(20,10)
- term.clearLine()
- write("< Quit Game >")
- chosen=3
- elseif chosen==3 then
- term.setTextColor(colors.blue)
- term.setCursorPos(21,10)
- term.clearLine()
- write(" Quit Game ")
- term.setTextColor(colors.red)
- term.setCursorPos(21,8)
- term.clearLine()
- write("< Campain >")
- chosen=1
- end
- end
- end
- if chosen==3 then
- term.setBackgroundColor(colors.black)
- term.clear()
- shell.run("clear")
- error()
- elseif chosen==2 then
- --JOINING1
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1,19)
- write("scanning for open games")
- myT=os.startTimer(2)
- rednet.open("top")
- while true do
- event,a,b=os.pullEvent()
- if event=="timer" then
- join=false
- term.setCursorPos(1,19)
- term.clearLine()
- write("no open games found")
- sleep(1)
- break
- end
- if b=="joinTTT" then
- join=true
- player1=a
- enemy=tonumber(a)
- you="player2"
- for i=1,10 do
- rednet.send(player1,"joinedYou")
- sleep(0.1)
- end
- sleep(0.5)
- term.setCursorPos(1,19)
- term.clearLine()
- write("found ID:"..a.." to play with you")
- break
- end
- end
- term.setCursorPos(1,19)
- term.clearLine()
- term.write("sending join requests")
- noReplay=true
- if join==false then
- while noReplay==true do
- rednet.broadcast("joinTTT")
- t=os.startTimer(1.5)
- event,a,b=os.pullEvent()
- if event=="timer" then
- end
- if b=="joinedYou" then
- noReplay=true
- player2=a
- enemy=tonumber(a)
- you="player1"
- term.setCursorPos(1,19)
- write("found ID:"..a.." to join your Game")
- break
- end
- end
- else
- term.setCursorPos(1,19)
- term.clearLine()
- write("joined the game of ID:"..player1)
- end
- sleep(1)
- --JOINING
- --VARIABLES
- Cplayer1="red"
- Cplayer2="blue"
- won=0
- lost=0
- if you=="player1" then
- yourColor="red"
- hisColor="blue"
- else
- yourColor="blue"
- hisColor="red"
- end
- if you=="player1" then
- yourTurn=true
- elseif you=="player2" then
- yourTurn=false
- end
- --VARIABLES
- --Start
- while true do
- sleep(2)
- resetScreen()
- term.setBackgroundColor(colors[yourColor])
- term.setTextColor(colors.black)
- term.setCursorPos(4,17)
- write("You are "..you)
- term.setCursorPos(39,17)
- write(" wins:"..won)
- term.setCursorPos(39,18)
- write("loses:"..lost)
- showTurn(yourTurn)
- round=1
- S={}
- S[1]="b" S[2]="b" S[3]="b" S[4]="b" S[5]="b" S[6]="b" S[7]="b" S[8]="b" S[9]="b"
- victory=false
- --Start
- while round<=9 and victory==false do
- --YourTurn
- if yourTurn==true then
- rep=true
- while rep==true do
- event,n,x,y=os.pullEvent("mouse_click")
- testSquare(x,y)
- if S[Square]=="b" then
- drawSquare(Square,yourColor)
- sleep(0.5)
- rednet.send(enemy,Square)
- round=round+1
- rep=false
- S[Square]=yourColor
- testVictory(S,yourTurn)
- yourTurn=false
- showTurn(yourTurn)
- end
- end
- --HisTurn
- elseif yourTurn==false then
- noReplay=true
- while noReplay==true do
- event,id,msg=os.pullEvent("rednet_message")
- if id==enemy then
- Square=msg
- noReplay=false
- drawSquare(Square,hisColor)
- round=round+1
- S[Square]=hisColor
- testVictory(S,yourTurn)
- yourTurn=true
- showTurn(yourTurn)
- end
- end
- end
- end
- end
- --SINGLEPLAYER--
- --SINGLEPLAYER--
- elseif chosen==1 then
- --Variables
- yourTurn=true
- won=0
- lost=0
- streak=0
- term.setBackgroundColor(colors.black)
- term.clear()
- paintutils.drawFilledBox(1,1,51,19,colors.black)
- term.setCursorPos(21,4)
- term.setTextColor(colors.purple)
- write("chose Player")
- term.setCursorPos(23,7)
- term.setTextColor(colors.red)
- write("Player1")
- term.setCursorPos(23,9)
- term.setTextColor(colors.blue)
- write("Player2")
- e,n,x,y=os.pullEvent("mouse_click")
- if y==7 then you="Player1" yourColor="red" hisColor="blue" yC="red" hC="blue" end
- if y==9 then you="Player2" yourColor="blue" hisColor="red" yC="blue" hC="red" end
- --LOSGEHTSSSS
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- emptyField("white")
- paintutils.drawFilledBox(1,16,51,19,colors[yourColor])
- term.setCursorPos(4,17)
- term.setTextColor(colors.black)
- write("Streak: "..streak)
- term.setCursorPos(39,17)
- write(" wins:"..won)
- term.setCursorPos(39,18)
- write("loses:"..lost)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.purple)
- term.setCursorPos(48,1)
- write("Quit")
- round=1
- S={}
- S[1]="b" S[2]="b" S[3]="b" S[4]="b" S[5]="b" S[6]="b" S[7]="b" S[8]="b" S[9]="b"
- victory=false
- --1 Spiel
- while round<=9 and victory==false do
- showTurn(yourTurn)
- --yourTurn
- if yourTurn==true then
- rep=true
- while rep==true do
- event,n,x,y=os.pullEvent("mouse_click")
- testSquare(x,y)
- if S[Square]=="b" then
- drawSquare(Square,yourColor)
- round=round+1
- rep=false
- S[Square]=yourColor
- testVictory(S,yourTurn)
- sleep(1)
- yourTurn=false
- showTurn(yourTurn)
- if victory==true then
- streak=streak+1
- end
- end
- end
- elseif yourTurn==false then
- sleep(1)
- BotPlay()
- round=round+1
- testVictory(S,yourTurn)
- yourTurn=true
- sleep(1)
- showTurn(yourTurn)
- if victory==true then
- streak=0
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment