login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > JS > Spellen > Tic-tac-toe

Tic-tac-toe

Auteur: Insane - 03 september 2004 - 19:22 - Gekeurd door: Dennisvb - Hits: 7032 - Aantal punten: 1.75 (4 stemmen)




Gewoon tussen de body tags plakken...oh jah, dit is TIC-TAC-TOE

Code:
PLaats dit tussen de BODY tags
  1. <SCRIPT>
  2.  
  3.  
  4. //if IE4/NS6, apply style
  5. if (document.all||document.getElementById){
  6. document.write('<style>.tictac{')
  7. document.write('width:50px;height:50px;')
  8. document.write('}</style>')
  9. }
  10.  
  11. var sqr1
  12. var sqr2
  13. var sqr3
  14. var sqr4
  15. var sqr5
  16. var sqr6
  17. var sqr7
  18. var sqr8
  19. var sqr9
  20. var sqr1T = 0
  21. var sqr2T = 0
  22. var sqr3T = 0
  23. var sqr4T = 0
  24. var sqr5T = 0
  25. var sqr6T = 0
  26. var sqr7T = 0
  27. var sqr8T = 0
  28. var sqr9T = 0
  29. var moveCount = 0
  30. var turn = 0
  31. var mode = 1
  32.  
  33. function vari()
  34. {
  35. sqr1 = document.tic.sqr1.value
  36. sqr2 = document.tic.sqr2.value
  37. sqr3 = document.tic.sqr3.value
  38. sqr4 = document.tic.sqr4.value
  39. sqr5 = document.tic.sqr5.value
  40. sqr6 = document.tic.sqr6.value
  41. sqr7 = document.tic.sqr7.value
  42. sqr8 = document.tic.sqr8.value
  43. sqr9 = document.tic.sqr9.value
  44. }
  45. function check()
  46. {
  47. if(sqr1 == " X " && sqr2 == " X " && sqr3 == " X ")
  48. {
  49. alert("You Win!")
  50. }
  51. else if(sqr4 == " X " && sqr5 == " X " && sqr6 == " X ")
  52. {
  53. alert("You Win!")
  54. }
  55. else if(sqr7 == " X " && sqr8 == " X " && sqr9 == " X ")
  56. {
  57. alert("You Win!")
  58. }
  59. else if(sqr1 == " X " && sqr5 == " X " && sqr9 == " X ")
  60. {
  61. alert("You Win!")
  62. }
  63. else if(sqr1 == " X " && sqr4 == " X " && sqr7 == " X ")
  64. {
  65. alert("You Win!")
  66. }
  67. else if(sqr2 == " X " && sqr5 == " X " && sqr8 == " X ")
  68. {
  69. alert("You Win!")
  70. }
  71. else if(sqr3 == " X " && sqr6 == " X " && sqr9 == " X ")
  72. {
  73. alert("You Win!")
  74. }
  75. else if(sqr1 == " X " && sqr5 == " X " && sqr9 == " X ")
  76. {
  77. alert("You Win!")
  78. }
  79. else if(sqr3 == " X " && sqr5 == " X " && sqr7 == " X ")
  80. {
  81. alert("You Win!")
  82. }
  83. else
  84. {
  85. winCheck()
  86. check2()
  87. drawCheck()
  88. }
  89. }
  90.  
  91. function check2()
  92. {
  93. vari()
  94. drawCheck()
  95. if(sqr1 == " O " && sqr2 == " O " && sqr3 == " O ")
  96. {
  97. alert("You Lose!")
  98. }
  99. else if(sqr4 == " O " && sqr5 == " O " && sqr6 == " O ")
  100. {
  101. alert("You Lose!")
  102. }
  103. else if(sqr7 == " O " && sqr8 == " O " && sqr9 == " O ")
  104. {
  105. alert("You Lose!")
  106. }
  107. else if(sqr1 == " O " && sqr5 == " O " && sqr9 == " O ")
  108. {
  109. alert("You Lose!")
  110. }
  111. else if(sqr1 == " O " && sqr4 == " O " && sqr7 == " O ")
  112. {
  113. alert("You Lose!")
  114. }
  115. else if(sqr2 == " O " && sqr5 == " O " && sqr8 == " O ")
  116. {
  117. alert("You Lose!")
  118. }
  119. else if(sqr3 == " O " && sqr6 == " O " && sqr9 == " O ")
  120. {
  121. alert("You Lose!")
  122. }
  123. else if(sqr1 == " O " && sqr5 == " O " && sqr9 == " O ")
  124. {
  125. alert("You Lose!")
  126. }
  127. else if(sqr3 == " O " && sqr5 == " O " && sqr7 == " O ")
  128. {
  129. alert("You Lose!")
  130. }
  131. }
  132.  
  133. function player1Check()
  134. {
  135. if(sqr1 == " X " && sqr2 == " X " && sqr3 == " X ")
  136. {
  137. alert("Player 1 wins!")
  138. }
  139. else if(sqr4 == " X " && sqr5 == " X " && sqr6 == " X ")
  140. {
  141. alert("Player 1 wins!")
  142. }
  143. else if(sqr7 == " X " && sqr8 == " X " && sqr9 == " X ")
  144. {
  145. alert("Player 1 wins!")
  146. }
  147. else if(sqr1 == " X " && sqr5 == " X " && sqr9 == " X ")
  148. {
  149. alert("Player 1 wins!")
  150. }
  151. else if(sqr1 == " X " && sqr4 == " X " && sqr7 == " X ")
  152. {
  153. alert("Player 1 wins!")
  154. }
  155. else if(sqr2 == " X " && sqr5 == " X " && sqr8 == " X ")
  156. {
  157. alert("Player 1 wins!")
  158. }
  159. else if(sqr3 == " X " && sqr6 == " X " && sqr9 == " X ")
  160. {
  161. alert("Player 1 wins!")
  162. }
  163. else if(sqr1 == " X " && sqr5 == " X " && sqr9 == " X ")
  164. {
  165. alert("Player 1 wins!")
  166. }
  167. else if(sqr3 == " X " && sqr5 == " X " && sqr7 == " X ")
  168. {
  169. alert("Player 1 wins!")
  170. }
  171. else
  172. {
  173. player2Check()
  174. drawCheck()
  175. }
  176. }
  177.  
  178. function player2Check()
  179. {
  180. vari()
  181. drawCheck()
  182. if(sqr1 == " O " && sqr2 == " O " && sqr3 == " O ")
  183. {
  184. alert("Player 2 wins!")
  185. }
  186. else if(sqr4 == " O " && sqr5 == " O " && sqr6 == " O ")
  187. {
  188. alert("Player 2 wins!")
  189. }
  190. else if(sqr7 == " O " && sqr8 == " O " && sqr9 == " O ")
  191. {
  192. alert("Player 2 wins!")
  193. }
  194. else if(sqr1 == " O " && sqr5 == " O " && sqr9 == " O ")
  195. {
  196. alert("Player 2 wins!")
  197. }
  198. else if(sqr1 == " O " && sqr4 == " O " && sqr7 == " O ")
  199. {
  200. alert("Player 2 wins!")
  201. }
  202. else if(sqr2 == " O " && sqr5 == " O " && sqr8 == " O ")
  203. {
  204. alert("Player 2 wins!")
  205. }
  206. else if(sqr3 == " O " && sqr6 == " O " && sqr9 == " O ")
  207. {
  208. alert("Player 2 wins!")
  209. }
  210. else if(sqr1 == " O " && sqr5 == " O " && sqr9 == " O ")
  211. {
  212. alert("Player 2 wins!")
  213. }
  214. else if(sqr3 == " O " && sqr5 == " O " && sqr7 == " O ")
  215. {
  216. alert("Player 2 wins!")
  217. }
  218. }
  219.  
  220. function drawCheck()
  221. {
  222. vari()
  223. moveCount = sqr1T + sqr2T + sqr3T + sqr4T + sqr5T + sqr6T + sqr7T + sqr8T + sqr9T
  224. if(moveCount == 9)
  225. {
  226. alert("Draw")
  227. }
  228. }
  229.  
  230. function winCheck()
  231. {
  232. check2()
  233. if(sqr1 == " O " && sqr2 == " O " && sqr3T == 0 && turn == 1)
  234. {
  235. document.tic.sqr3.value = " O "
  236. sqr3T = 1;
  237. turn = 0;
  238. }
  239. else if(sqr2 == " O " && sqr3 == " O " && sqr1T == 0 && turn == 1)
  240. {
  241. document.tic.sqr1.value = " O "
  242. sqr1T = 1;
  243. turn = 0;
  244. }
  245. else if(sqr4 == " O " && sqr5 == " O " && sqr6T == 0 && turn == 1)
  246. {
  247. document.tic.sqr6.value = " O "
  248. sqr6T = 1;
  249. turn = 0;
  250. }
  251. else if(sqr5 == " O " && sqr6 == " O " && sqr4T == 0 && turn == 1)
  252. {
  253. document.tic.sqr4.value = " O "
  254. sqr4T = 1;
  255. turn = 0;
  256. }
  257. else if(sqr7 == " O " && sqr8 == " O " && sqr9T == 0 && turn == 1)
  258. {
  259. document.tic.sqr9.value = " O "
  260. sqr9T = 1;
  261. turn = 0;
  262. }
  263. else if(sqr8 == " O " && sqr9 == " O " && sqr7T == 0 && turn == 1)
  264. {
  265. document.tic.sqr7.value = " O "
  266. sqr7T = 1;
  267. turn = 0;
  268. }
  269. else if(sqr1 == " O " && sqr5 == " O " && sqr9T == 0 && turn == 1)
  270. {
  271. document.tic.sqr9.value = " O "
  272. sqr9T = 1;
  273. turn = 0;
  274. }
  275. else if(sqr5 == " O " && sqr9 == " O " && sqr1T == 0 && turn == 1)
  276. {
  277. document.tic.sqr1.value = " O "
  278. sqr1T = 1;
  279. turn = 0;
  280. }
  281. else if(sqr3 == " O " && sqr5 == " O " && sqr7T == 0 && turn == 1)
  282. {
  283. document.tic.sqr7.value = " O "
  284. sqr7T = 1;
  285. turn = 0;
  286. }
  287. else if(sqr7 == " O " && sqr5 == " O " && sqr3T == 0 && turn == 1)
  288. {
  289. document.tic.sqr3.value = " O "
  290. sqr3T = 1;
  291. turn = 0;
  292. }
  293. else if(sqr1 == " O " && sqr3 == " O " && sqr2T == 0 && turn == 1)
  294. {
  295. document.tic.sqr2.value = " O "
  296. sqr2T = 1;
  297. turn = 0;
  298. }
  299. else if(sqr4 == " O " && sqr6 == " O " && sqr5T == 0 && turn == 1)
  300. {
  301. document.tic.sqr5.value = " O "
  302. sqr5T = 1;
  303. turn = 0;
  304. }
  305. else if(sqr7 == " O " && sqr9 == " O " && sqr8T == 0 && turn == 1)
  306. {
  307. document.tic.sqr8.value = " O "
  308. sqr8T = 1;
  309. turn = 0;
  310. }
  311. else if(sqr1 == " O " && sqr7 == " O " && sqr4T == 0 && turn == 1)
  312. {
  313. document.tic.sqr4.value = " O "
  314. sqr4T = 1;
  315. turn = 0;
  316. }
  317. else if(sqr2 == " O " && sqr8 == " O " && sqr5T == 0 && turn == 1)
  318. {
  319. document.tic.sqr5.value = " O "
  320. sqr5T = 1;
  321. turn = 0;
  322. }
  323. else if(sqr3 == " O " && sqr9 == " O " && sqr6T == 0 && turn == 1)
  324. {
  325. document.tic.sqr6.value = " O "
  326. sqr6T = 1;
  327. turn = 0;
  328. }
  329. else if(sqr1 == " O " && sqr5 == " O " && sqr9T == 0 && turn == 1)
  330. {
  331. document.tic.sqr9.value = " O "
  332. sqr9T = 1;
  333. turn = 0;
  334. }
  335. else if(sqr4 == " O " && sqr7 == " O " && sqr1T == 0 && turn == 1)
  336. {
  337. document.tic.sqr1.value = " O "
  338. sqr1T = 1;
  339. turn = 0;
  340. }
  341. else if(sqr5 == " O " && sqr8 == " O " && sqr2T == 0 && turn == 1)
  342. {
  343. document.tic.sqr2.value = " O "
  344. sqr2T = 1;
  345. turn = 0;
  346. }
  347. else if(sqr6 == " O " && sqr9 == " O " && sqr3T == 0 && turn == 1)
  348. {
  349. document.tic.sqr3.value = " O "
  350. sqr3T = 1;
  351. turn = 0;
  352. }
  353. else if(sqr1 == " O " && sqr4 == " O " && sqr7T == 0 && turn == 1)
  354. {
  355. document.tic.sqr7.value = " O "
  356. sqr7T = 1;
  357. turn = 0;
  358. }
  359. else if(sqr2 == " O " && sqr5 == " O " && sqr8T == 0 && turn == 1)
  360. {
  361. document.tic.sqr8.value = " O "
  362. sqr8T = 1;
  363. turn = 0;
  364. }
  365. else if(sqr3 == " O " && sqr6 == " O " && sqr9T == 0 && turn == 1)
  366. {
  367. document.tic.sqr9.value = " O "
  368. sqr9T = 1;
  369. turn = 0;
  370. }
  371. else if(sqr1 == " O " && sqr9 == " O " && sqr5T == 0 && turn == 1)
  372. {
  373. document.tic.sqr5.value = " O "
  374. sqr5T = 1;
  375. turn = 0;
  376. }
  377. else if(sqr3 == " O " && sqr7 == " O " && sqr5T == 0 && turn == 1)
  378. {
  379. document.tic.sqr5.value = " O "
  380. sqr5T = 1;
  381. turn = 0;
  382. }
  383. else
  384. {
  385. computer()
  386. }
  387. check2()
  388. }
  389. function computer()
  390. {
  391. check2()
  392. if(sqr1 == " X " && sqr2 == " X " && sqr3T == 0 && turn == 1)
  393. {
  394. document.tic.sqr3.value = " O "
  395. sqr3T = 1;
  396. turn = 0;
  397. }
  398. else if(sqr2 == " X " && sqr3 == " X " && sqr1T == 0 && turn == 1)
  399. {
  400. document.tic.sqr1.value = " O "
  401. sqr1T = 1;
  402. turn = 0;
  403. }
  404. else if(sqr4 == " X " && sqr5 == " X " && sqr6T == 0 && turn == 1)
  405. {
  406. document.tic.sqr6.value = " O "
  407. sqr6T = 1;
  408. turn = 0;
  409. }
  410. else if(sqr5 == " X " && sqr6 == " X " && sqr4T == 0 && turn == 1)
  411. {
  412. document.tic.sqr4.value = " O "
  413. sqr4T = 1;
  414. turn = 0;
  415. }
  416. else if(sqr7 == " X " && sqr8 == " X " && sqr9T == 0 && turn == 1)
  417. {
  418. document.tic.sqr9.value = " O "
  419. sqr9T = 1;
  420. turn = 0;
  421. }
  422. else if(sqr8 == " X " && sqr9 == " X " && sqr7T == 0 && turn == 1)
  423. {
  424. document.tic.sqr7.value = " O "
  425. sqr7T = 1;
  426. turn = 0;
  427. }
  428. else if(sqr1 == " X " && sqr5 == " X " && sqr9T == 0 && turn == 1)
  429. {
  430. document.tic.sqr9.value = " O "
  431. sqr9T = 1;
  432. turn = 0;
  433. }
  434. else if(sqr5 == " X " && sqr9 == " X " && sqr1T == 0 && turn == 1)
  435. {
  436. document.tic.sqr1.value = " O "
  437. sqr1T = 1;
  438. turn = 0;
  439. }
  440. else if(sqr3 == " X " && sqr5 == " X " && sqr7T == 0 && turn == 1)
  441. {
  442. document.tic.sqr7.value = " O "
  443. sqr7T = 1;
  444. turn = 0;
  445. }
  446. else if(sqr7 == " X " && sqr5 == " X " && sqr3T == 0 && turn == 1)
  447. {
  448. document.tic.sqr3.value = " O "
  449. sqr3T = 1;
  450. turn = 0;
  451. }
  452. else if(sqr1 == " X " && sqr3 == " X " && sqr2T == 0 && turn == 1)
  453. {
  454. document.tic.sqr2.value = " O "
  455. sqr2T = 1;
  456. turn = 0;
  457. }
  458. else if(sqr4 == " X " && sqr6 == " X " && sqr5T == 0 && turn == 1)
  459. {
  460. document.tic.sqr5.value = " O "
  461. sqr5T = 1;
  462. turn = 0;
  463. }
  464. else if(sqr7 == " X " && sqr9 == " X " && sqr8T == 0 && turn == 1)
  465. {
  466. document.tic.sqr8.value = " O "
  467. sqr8T = 1;
  468. turn = 0;
  469. }
  470. else if(sqr1 == " X " && sqr7 == " X " && sqr4T == 0 && turn == 1)
  471. {
  472. document.tic.sqr4.value = " O "
  473. sqr4T = 1;
  474. turn = 0;
  475. }
  476. else if(sqr2 == " X " && sqr8 == " X " && sqr5T == 0 && turn == 1)
  477. {
  478. document.tic.sqr5.value = " O "
  479. sqr5T = 1;
  480. turn = 0;
  481. }
  482. else if(sqr3 == " X " && sqr9 == " X " && sqr6T == 0 && turn == 1)
  483. {
  484. document.tic.sqr6.value = " O "
  485. sqr6T = 1;
  486. turn = 0;
  487. }
  488. else if(sqr1 == " X " && sqr5 == " X " && sqr9T == 0 && turn == 1)
  489. {
  490. document.tic.sqr9.value = " O "
  491. sqr9T = 1;
  492. turn = 0;
  493. }
  494. else if(sqr4 == " X " && sqr7 == " X " && sqr1T == 0 && turn == 1)
  495. {
  496. document.tic.sqr1.value = " O "
  497. sqr1T = 1;
  498. turn = 0;
  499. }
  500. else if(sqr5 == " X " && sqr8 == " X " && sqr2T == 0 && turn == 1)
  501. {
  502. document.tic.sqr2.value = " O "
  503. sqr2T = 1;
  504. turn = 0;
  505. }
  506. else if(sqr6 == " X " && sqr9 == " X " && sqr3T == 0 && turn == 1)
  507. {
  508. document.tic.sqr3.value = " O "
  509. sqr3T = 1;
  510. turn = 0;
  511. }
  512. else if(sqr1 == " X " && sqr4 == " X " && sqr7T == 0 && turn == 1)
  513. {
  514. document.tic.sqr7.value = " O "
  515. sqr7T = 1;
  516. turn = 0;
  517. }
  518. else if(sqr2 == " X " && sqr5 == " X " && sqr8T == 0 && turn == 1)
  519. {
  520. document.tic.sqr8.value = " O "
  521. sqr8T = 1;
  522. turn = 0;
  523. }
  524. else if(sqr3 == " X " && sqr6 == " X " && sqr9T == 0 && turn == 1)
  525. {
  526. document.tic.sqr9.value = " O "
  527. sqr9T = 1;
  528. turn = 0;
  529. }
  530. else if(sqr1 == " X " && sqr9 == " X " && sqr5T == 0 && turn == 1)
  531. {
  532. document.tic.sqr5.value = " O "
  533. sqr5T = 1;
  534. turn = 0;
  535. }
  536. else if(sqr3 == " X " && sqr7 == " X " && sqr5T == 0 && turn == 1)
  537. {
  538. document.tic.sqr5.value = " O "
  539. sqr5T = 1;
  540. turn = 0;
  541. }
  542. else
  543. {
  544. AI()
  545. }
  546. check2()
  547. }
  548.  
  549. function AI()
  550. {
  551. vari()
  552. if(document.tic.sqr5.value == " " && turn == 1)
  553. {
  554. document.tic.sqr5.value = " O "
  555. turn = 0
  556. sqr5T = 1
  557. }
  558. else if(document.tic.sqr1.value == " " && turn == 1)
  559. {
  560. document.tic.sqr1.value = " O "
  561. turn = 0
  562. sqr1T = 1
  563. }
  564. else if(document.tic.sqr9.value == " " && turn == 1)
  565. {
  566. document.tic.sqr9.value = " O "
  567. turn = 0
  568. sqr9T = 1
  569. }
  570. else if(document.tic.sqr6.value == " " && turn == 1)
  571. {
  572. document.tic.sqr6.value = " O "
  573. turn = 0
  574. sqr6T = 1
  575. }
  576. else if(document.tic.sqr2.value == " " && turn == 1)
  577. {
  578. document.tic.sqr2.value = " O "
  579. turn = 0
  580. sqr2T = 1
  581. }
  582. else if(document.tic.sqr8.value == " " && turn == 1)
  583. {
  584. document.tic.sqr8.value = " O "
  585. turn = 0
  586. sqr8T = 1
  587. }
  588. else if(document.tic.sqr3.value == " " && turn == 1)
  589. {
  590. document.tic.sqr3.value = " O "
  591. turn = 0
  592. sqr3T = 1
  593. }
  594. else if(document.tic.sqr7.value == " " && turn == 1)
  595. {
  596. document.tic.sqr7.value = " O "
  597. turn = 0
  598. sqr7T = 1
  599. }
  600. else if(document.tic.sqr4.value == " " && turn == 1)
  601. {
  602. document.tic.sqr4.value = " O "
  603. turn = 0
  604. sqr4T = 1
  605. }
  606. check2()
  607. }
  608.  
  609. function reset()
  610. {
  611. document.tic.sqr1.value = " "
  612. document.tic.sqr2.value = " "
  613. document.tic.sqr3.value = " "
  614. document.tic.sqr4.value = " "
  615. document.tic.sqr5.value = " "
  616. document.tic.sqr6.value = " "
  617. document.tic.sqr7.value = " "
  618. document.tic.sqr8.value = " "
  619. document.tic.sqr9.value = " "
  620. sqr1T = 0
  621. sqr2T = 0
  622. sqr3T = 0
  623. sqr4T = 0
  624. sqr5T = 0
  625. sqr6T = 0
  626. sqr7T = 0
  627. sqr8T = 0
  628. sqr9T = 0
  629. vari()
  630. turn = 0
  631. moveCount = 0
  632. }
  633.  
  634. function resetter()
  635. {
  636. }
  637. </SCRIPT>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.135s