Legebatterie

Apps--TTT

Jan 4th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.45 KB | None | 0 0
  1. --Variables
  2.  
  3. --functions
  4. emptyField=function(c)
  5. paintutils.drawBox(18,2,33,14,colors[c])
  6. paintutils.drawBox(17,2,18,14,colors[c])
  7. paintutils.drawBox(33,2,34,14,colors[c])
  8.  
  9. paintutils.drawBox(23,2,23,14,colors[c])
  10. paintutils.drawBox(28,2,28,14,colors[c])
  11.  
  12. paintutils.drawBox(18,6,33,6,colors[c])
  13. paintutils.drawBox(18,10,33,10,colors[c])
  14. end
  15.  
  16. BotPlay=function()
  17. danger=false
  18. chance=false
  19. --kannBotVerlieren???
  20.  
  21. for i=0,2 do
  22. 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
  23. 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
  24. 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
  25.  
  26. if S[i+1]==yC and S[7+i]=="b" and S[1+i]==S[4+i] then danger=true sol=7+i end
  27. if S[i+4]==yC and S[1+i]=="b" and S[4+i]==S[7+i] then danger=true sol=1+i end
  28. if S[i+7]==yC and S[4+i]=="b" and S[1+i]==S[7+i] then danger=true sol=4+i end
  29. end
  30. if S[1]==yC and S[9]=="b" and S[1]==S[5] then danger=true sol=9 end
  31. if S[5]==yC and S[1]=="b" and S[5]==S[9] then danger=true sol=1 end
  32. if S[9]==yC and S[5]=="b" and S[1]==S[9] then danger=true sol=5 end
  33.  
  34. if S[3]==yC and S[7]=="b" and S[3]==S[5] then danger=true sol=7 end
  35. if S[5]==yC and S[3]=="b" and S[5]==S[7] then danger=true sol=3 end
  36. if S[7]==yC and S[5]=="b" and S[3]==S[7] then danger=true sol=5 end
  37.  
  38. --kannBotGewinnen???
  39. for i=0,2 do
  40. 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
  41. 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
  42. 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
  43.  
  44. if S[i+1]==hC and S[7+i]=="b" and S[1+i]==S[4+i] then chance=true sol=7+i end
  45. if S[i+4]==hC and S[1+i]=="b" and S[4+i]==S[7+i] then chance=true sol=1+i end
  46. if S[i+7]==hC and S[4+i]=="b" and S[1+i]==S[7+i] then chance=true sol=4+i end
  47. end
  48. if S[1]==hC and S[9]=="b" and S[1]==S[5] then chance=true sol=9 end
  49. if S[5]==hC and S[1]=="b" and S[5]==S[9] then chance=true sol=1 end
  50. if S[9]==hC and S[5]=="b" and S[1]==S[9] then chance=true sol=5 end
  51.  
  52. if S[3]==hC and S[7]=="b" and S[3]==S[5] then chance=true sol=7 end
  53. if S[5]==hC and S[3]=="b" and S[5]==S[7] then chance=true sol=3 end
  54. if S[7]==hC and S[5]=="b" and S[7]==S[3] then chance=true sol=5 end
  55.  
  56.  
  57. rand=math.random(1,10)
  58. if danger==true and chance==false then
  59. drawSquare(sol,hisColor)
  60. S[sol]=hisColor
  61. end
  62.  
  63. if chance==true then
  64. drawSquare(sol,hisColor)
  65. S[sol]=hisColor
  66. end
  67. if chance==false and danger==false then
  68. lol=true
  69. while lol==true do
  70. rand2=math.random(1,9)
  71. if S[rand2]=="b" then
  72. drawSquare(rand2,hisColor)
  73. S[rand2]=hisColor
  74. lol=false
  75. end
  76. end
  77. end
  78.  
  79. end
  80.  
  81. draw1=function(c1,x,y)
  82. paintutils.drawFilledBox(x,y,(x+3),(y+2),colors[c1])
  83. end
  84.  
  85. draw2=function(c2,x,y)
  86. paintutils.drawFilledBox(x,y,(x+3),(y+2),colors[c2])
  87. end
  88.  
  89. showTurn=function(yourTurn)
  90. paintutils.drawBox(1,1,25,1,colors.black)
  91. term.setCursorPos(2,1)
  92. if yourTurn==true then
  93. term.setTextColor(colors[yourColor])
  94. term.setBackgroundColor(colors.black)
  95. write("your Turn")
  96. else
  97. term.setTextColor(colors[hisColor])
  98. term.setBackgroundColor(colors.black)
  99. write("enemys Turn")
  100. end
  101. end
  102.  
  103. resetScreen=function()
  104. term.setBackgroundColor(colors.black)
  105. term.clear()
  106. emptyField("white")
  107. paintutils.drawFilledBox(1,16,51,19,colors[yourColor])
  108.  
  109. term.setBackgroundColor(colors.black)
  110. term.setTextColor(colors.red)
  111. term.setCursorPos(42,1)
  112. write("Quit Game")
  113. end
  114.  
  115. testSquare=function(x,y)
  116. if x>30 and y==1 then
  117. term.setBackgroundColor(colors.black)
  118. shell.run("clear")
  119. error()
  120. end
  121. if x>18 and x<23 and y>2 and y<6 then
  122. Square=1
  123. end
  124. if x>18 and x<23 and y>6 and y<10 then
  125. Square=2
  126. end
  127. if x>18 and x<23 and y>10 and y<14 then
  128. Square=3
  129. end
  130. if x>23 and x<28 and y>2 and y<6 then
  131. Square=4
  132. end
  133. if x>23 and x<28 and y>6 and y<10 then
  134. Square=5
  135. end
  136. if x>23 and x<28 and y>10 and y<14 then
  137. Square=6
  138. end
  139. if x>28 and x<33 and y>2 and y<6 then
  140. Square=7
  141. end
  142. if x>28 and x<33 and y>6 and y<10 then
  143. Square=8
  144. end
  145. if x>28 and x<33 and y>10 and y<14 then
  146. Square=9
  147. end
  148. end
  149.  
  150. testVictory=function(S,turn)
  151. if S[1]==S[2] and S[2]==S[3] and not(S[1]=="b") then
  152. if turn==true then victory=true won=won+1 sleep(2)
  153. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  154. if turn==false then victory=true lost=lost+1 sleep(2)
  155. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  156. end
  157. if S[4]==S[5] and S[5]==S[6] and not(S[5]=="b") then
  158. if turn==true then victory=true won=won+1 sleep(2)
  159. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  160. if turn==false then victory=true lost=lost+1 sleep(2)
  161. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  162. end
  163. if S[7]==S[8] and S[8]==S[9] and not(S[7]=="b") then
  164. if turn==true then victory=true won=won+1 sleep(2)
  165. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  166. if turn==false then victory=true lost=lost+1 sleep(2)
  167. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  168. end
  169. if S[1]==S[4] and S[4]==S[7] and not(S[1]=="b") then
  170. if turn==true then victory=true won=won+1 sleep(2)
  171. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  172. if turn==false then victory=true lost=lost+1 sleep(2)
  173. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  174. end
  175. if S[2]==S[5] and S[5]==S[8] and not(S[5]=="b") then
  176. if turn==true then victory=true won=won+1 sleep(2)
  177. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  178. if turn==false then victory=true lost=lost+1 sleep(2)
  179. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  180. end
  181. if S[3]==S[6] and S[6]==S[9] and not(S[3]=="b") then
  182. if turn==true then victory=true won=won+1 sleep(2)
  183. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  184. if turn==false then victory=true lost=lost+1 sleep(2)
  185. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  186. end
  187. if S[1]==S[5] and S[5]==S[9] and not(S[5]=="b") then
  188. if turn==true then victory=true won=won+1 sleep(2)
  189. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  190. if turn==false then victory=true lost=lost+1 sleep(2)
  191. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  192. end
  193. if S[7]==S[5] and S[5]==S[3] and not(S[5]=="b") then
  194. if turn==true then victory=true won=won+1 sleep(2)
  195. paintutils.drawFilledBox(11,7,41,10,colors[yourColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("Victory") end
  196. if turn==false then victory=true lost=lost+1 sleep(2)
  197. paintutils.drawFilledBox(11,7,41,10,colors[hisColor]) term.setCursorPos(23,8) term.setTextColor(colors.black) write("DEFEAT!") end
  198. end
  199.  
  200. end
  201.  
  202. drawSquare=function(S,c)
  203. if S==1 then
  204. paintutils.drawFilledBox(19,3,22,5,colors[c])
  205. elseif S==2 then
  206. paintutils.drawFilledBox(19,7,22,9,colors[c])
  207. elseif S==3 then
  208. paintutils.drawFilledBox(19,11,22,13,colors[c])
  209. elseif S==4 then
  210. paintutils.drawFilledBox(24,3,27,5,colors[c])
  211. elseif S==5 then
  212. paintutils.drawFilledBox(24,7,27,9,colors[c])
  213. elseif S==6 then
  214. paintutils.drawFilledBox(24,11,27,13,colors[c])
  215. elseif S==7 then
  216. paintutils.drawFilledBox(29,3,32,5,colors[c])
  217. elseif S==8 then
  218. paintutils.drawFilledBox(29,7,32,9,colors[c])
  219. elseif S==9 then
  220. paintutils.drawFilledBox(29,11,32,13,colors[c])
  221. end
  222. end
  223. --functions
  224.  
  225.  
  226. term.setBackgroundColor(colors.white)
  227. term.clear()
  228.  
  229. paintutils.drawFilledBox(1,1,51,5,colors.blue)
  230. paintutils.drawFilledBox(1,16,51,19,colors.blue)
  231.  
  232. term.setTextColor(colors.black)
  233. term.setBackgroundColor(colors.white)
  234. term.setCursorPos(20,9)
  235. write("Tic Tac Toe")
  236. term.setCursorPos(19,11)
  237. write("by MaxintoshOS")
  238.  
  239. sleep(1)
  240.  
  241. for i=1,26 do
  242. paintutils.drawBox(1,1,i,19,colors.black)
  243. paintutils.drawBox(51-i ,1,51,19)
  244. sleep(0.1)
  245. end
  246.  
  247. term.setTextColor(colors.red)
  248. term.setBackgroundColor(colors.black)
  249. term.setCursorPos(21,8)
  250. write("< Campain >")
  251. term.setTextColor(colors.blue)
  252. term.setCursorPos(20,9)
  253. write(" Multiplayer ")
  254. term.setCursorPos(21,10)
  255. write(" Quit Game ")
  256.  
  257. startscreen=true
  258. chosen=1
  259.  
  260. while startscreen==true do
  261. event,c=os.pullEvent("key")
  262.  
  263. if c==28 then
  264. startscreen=false
  265. else
  266.  
  267. if chosen==1 then
  268.  
  269. term.setTextColor(colors.blue)
  270. term.setCursorPos(22,8)
  271. term.clearLine()
  272. write(" Campain ")
  273. term.setCursorPos(19,9)
  274. term.setTextColor(colors.red)
  275. term.clearLine()
  276. write("< Multiplayer >")
  277. chosen=2
  278. elseif chosen==2 then
  279. term.setTextColor(colors.blue)
  280. term.setCursorPos(20,9)
  281. term.clearLine()
  282. write(" Multiplayer ")
  283. term.setTextColor(colors.red)
  284. term.setCursorPos(20,10)
  285. term.clearLine()
  286. write("< Quit Game >")
  287. chosen=3
  288. elseif chosen==3 then
  289. term.setTextColor(colors.blue)
  290. term.setCursorPos(21,10)
  291. term.clearLine()
  292. write(" Quit Game ")
  293. term.setTextColor(colors.red)
  294. term.setCursorPos(21,8)
  295. term.clearLine()
  296. write("< Campain >")
  297. chosen=1
  298. end
  299. end
  300. end
  301.  
  302. if chosen==3 then
  303. term.setBackgroundColor(colors.black)
  304. term.clear()
  305. shell.run("clear")
  306. error()
  307.  
  308. elseif chosen==2 then
  309. --JOINING1
  310. term.setBackgroundColor(colors.black)
  311. term.clear()
  312. term.setTextColor(colors.white)
  313. term.setCursorPos(1,19)
  314. write("scanning for open games")
  315.  
  316. myT=os.startTimer(2)
  317. rednet.open("top")
  318.  
  319. while true do
  320. event,a,b=os.pullEvent()
  321.  
  322. if event=="timer" then
  323. join=false
  324. term.setCursorPos(1,19)
  325. term.clearLine()
  326. write("no open games found")
  327. sleep(1)
  328. break
  329. end
  330.  
  331. if b=="joinTTT" then
  332. join=true
  333. player1=a
  334. enemy=tonumber(a)
  335. you="player2"
  336. for i=1,10 do
  337. rednet.send(player1,"joinedYou")
  338. sleep(0.1)
  339. end
  340. sleep(0.5)
  341. term.setCursorPos(1,19)
  342. term.clearLine()
  343. write("found ID:"..a.." to play with you")
  344. break
  345. end
  346.  
  347. end
  348.  
  349. term.setCursorPos(1,19)
  350. term.clearLine()
  351. term.write("sending join requests")
  352.  
  353. noReplay=true
  354.  
  355. if join==false then
  356. while noReplay==true do
  357. rednet.broadcast("joinTTT")
  358. t=os.startTimer(1.5)
  359. event,a,b=os.pullEvent()
  360. if event=="timer" then
  361. end
  362. if b=="joinedYou" then
  363. noReplay=true
  364. player2=a
  365. enemy=tonumber(a)
  366. you="player1"
  367. term.setCursorPos(1,19)
  368. write("found ID:"..a.." to join your Game")
  369. break
  370. end
  371. end
  372. else
  373. term.setCursorPos(1,19)
  374. term.clearLine()
  375. write("joined the game of ID:"..player1)
  376. end
  377.  
  378. sleep(1)
  379. --JOINING
  380.  
  381.  
  382. --VARIABLES
  383. Cplayer1="red"
  384. Cplayer2="blue"
  385. won=0
  386. lost=0
  387.  
  388. if you=="player1" then
  389. yourColor="red"
  390. hisColor="blue"
  391. else
  392. yourColor="blue"
  393. hisColor="red"
  394. end
  395.  
  396. if you=="player1" then
  397. yourTurn=true
  398. elseif you=="player2" then
  399. yourTurn=false
  400. end
  401. --VARIABLES
  402.  
  403. --Start
  404. while true do
  405. sleep(2)
  406. resetScreen()
  407.  
  408. term.setBackgroundColor(colors[yourColor])
  409. term.setTextColor(colors.black)
  410. term.setCursorPos(4,17)
  411. write("You are "..you)
  412.  
  413. term.setCursorPos(39,17)
  414. write(" wins:"..won)
  415. term.setCursorPos(39,18)
  416. write("loses:"..lost)
  417.  
  418.  
  419.  
  420. showTurn(yourTurn)
  421.  
  422. round=1
  423. S={}
  424. 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"
  425. victory=false
  426.  
  427.  
  428.  
  429. --Start
  430.  
  431. while round<=9 and victory==false do
  432.  
  433. --YourTurn
  434. if yourTurn==true then
  435. rep=true
  436. while rep==true do
  437. event,n,x,y=os.pullEvent("mouse_click")
  438. testSquare(x,y)
  439. if S[Square]=="b" then
  440. drawSquare(Square,yourColor)
  441. sleep(0.5)
  442. rednet.send(enemy,Square)
  443. round=round+1
  444. rep=false
  445. S[Square]=yourColor
  446. testVictory(S,yourTurn)
  447. yourTurn=false
  448. showTurn(yourTurn)
  449. end
  450. end
  451.  
  452.  
  453. --HisTurn
  454. elseif yourTurn==false then
  455. noReplay=true
  456. while noReplay==true do
  457. event,id,msg=os.pullEvent("rednet_message")
  458. if id==enemy then
  459. Square=msg
  460. noReplay=false
  461. drawSquare(Square,hisColor)
  462. round=round+1
  463. S[Square]=hisColor
  464. testVictory(S,yourTurn)
  465. yourTurn=true
  466. showTurn(yourTurn)
  467. end
  468. end
  469. end
  470. end
  471. end
  472.  
  473. --SINGLEPLAYER--
  474. --SINGLEPLAYER--
  475.  
  476.  
  477. elseif chosen==1 then
  478.  
  479. --Variables
  480. yourTurn=true
  481. won=0
  482. lost=0
  483. streak=0
  484.  
  485. term.setBackgroundColor(colors.black)
  486. term.clear()
  487. paintutils.drawFilledBox(1,1,51,19,colors.black)
  488. term.setCursorPos(21,4)
  489. term.setTextColor(colors.purple)
  490. write("chose Player")
  491. term.setCursorPos(23,7)
  492. term.setTextColor(colors.red)
  493. write("Player1")
  494. term.setCursorPos(23,9)
  495. term.setTextColor(colors.blue)
  496. write("Player2")
  497. e,n,x,y=os.pullEvent("mouse_click")
  498. if y==7 then you="Player1" yourColor="red" hisColor="blue" yC="red" hC="blue" end
  499. if y==9 then you="Player2" yourColor="blue" hisColor="red" yC="blue" hC="red" end
  500.  
  501. --LOSGEHTSSSS
  502. while true do
  503. term.setBackgroundColor(colors.black)
  504. term.clear()
  505. emptyField("white")
  506. paintutils.drawFilledBox(1,16,51,19,colors[yourColor])
  507.  
  508.  
  509.  
  510. term.setCursorPos(4,17)
  511. term.setTextColor(colors.black)
  512. write("Streak: "..streak)
  513.  
  514. term.setCursorPos(39,17)
  515. write(" wins:"..won)
  516. term.setCursorPos(39,18)
  517. write("loses:"..lost)
  518.  
  519. term.setBackgroundColor(colors.black)
  520. term.setTextColor(colors.purple)
  521. term.setCursorPos(48,1)
  522. write("Quit")
  523.  
  524.  
  525. round=1
  526. S={}
  527. 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"
  528. victory=false
  529.  
  530. --1 Spiel
  531.  
  532. while round<=9 and victory==false do
  533.  
  534. showTurn(yourTurn)
  535.  
  536. --yourTurn
  537. if yourTurn==true then
  538. rep=true
  539. while rep==true do
  540. event,n,x,y=os.pullEvent("mouse_click")
  541. testSquare(x,y)
  542. if S[Square]=="b" then
  543. drawSquare(Square,yourColor)
  544. round=round+1
  545. rep=false
  546. S[Square]=yourColor
  547. testVictory(S,yourTurn)
  548. sleep(1)
  549. yourTurn=false
  550. showTurn(yourTurn)
  551. if victory==true then
  552. streak=streak+1
  553. end
  554. end
  555. end
  556.  
  557. elseif yourTurn==false then
  558. sleep(1)
  559. BotPlay()
  560. round=round+1
  561. testVictory(S,yourTurn)
  562. yourTurn=true
  563. sleep(1)
  564. showTurn(yourTurn)
  565. if victory==true then
  566. streak=0
  567. end
  568. end
  569. end
  570. end
  571. end
Advertisement
Add Comment
Please, Sign In to add comment