Guest User

yair

a guest
Oct 23rd, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.91 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5.  
  6. import javax.swing.*;
  7.  
  8.  
  9. public class AITTT {
  10. public static boolean playerTurn = true;
  11. public static boolean comTurn = false;
  12. public static String x = "X";
  13. public static String o = "O";
  14. public static JFrame frame;
  15. public static JPanel panel;
  16. public JButton res;
  17. public int count = 0;
  18. JButton[] b;
  19. ActionListener[] l = new ActionListener[9];
  20.  
  21.  
  22. public void changeTurns()
  23. {
  24. playerTurn = !playerTurn;
  25. comTurn = !comTurn;
  26. }
  27. public void endGame()
  28. {
  29. playerTurn = false;
  30. comTurn = false;
  31. }
  32. public void comWinLogic()
  33. {
  34. for(int i = 0 ;i<9&& comTurn == true;i++)
  35. {
  36.  
  37. if(i == 0 || i == 3 || i == 6 )
  38. {
  39. if(b[i].getText() == o&&b[i+1].getText() == o)
  40. {
  41. if(b[i+2].getText() == "")
  42. {
  43. b[i+2].setText(o);
  44. endGame();
  45. }
  46. } else
  47. if(b[i].getText() == o&&b[i+2].getText() == o)
  48. if(b[i+1].getText() == "")
  49. {
  50. b[i+1].setText(o);
  51. endGame();
  52. }
  53. } else
  54. if(i == 2 || i == 5 || i == 8 )
  55. {
  56. if(b[i].getText() == o&&b[i-1].getText() == o)
  57. {
  58. if(b[i-2].getText() == "")
  59. {
  60. b[i-2].setText(o);
  61. endGame();
  62. }
  63. } else
  64. if(b[i].getText() == o&&b[i-2].getText() == o)
  65. if(b[i-1].getText() == "")
  66. {
  67. b[i-1].setText(o);
  68. endGame();
  69. }
  70.  
  71. } else
  72. if(i == 0 || i == 2 || i == 3 )
  73. {
  74. if(b[i].getText() == o&&b[i+3].getText() == o)
  75. {
  76. if(b[i+6].getText() == "")
  77. {
  78. b[i+6].setText(o);
  79. endGame();
  80. }
  81. } else
  82. if(b[i].getText() == o&&b[i+6].getText() == o)
  83. if(b[i+3].getText() == "")
  84. {
  85. b[i+3].setText(o);
  86. endGame();
  87. }
  88. } else
  89. if(i == 6 || i == 7 || i == 8 )
  90. {
  91. if(b[i].getText() == o&&b[i-3].getText() == o)
  92. {
  93. if(b[i-6].getText() == "")
  94. {
  95. b[i-6].setText(o);
  96. endGame();
  97. }
  98. } else
  99. if(b[i].getText() == o&&b[i-6].getText() == o)
  100. if(b[i-3].getText() == "")
  101. {
  102. b[i-3].setText(o);
  103. endGame();
  104. }
  105.  
  106. } else
  107. if(i == 0)
  108. {
  109. if(b[i].getText() == o&&b[i+4].getText() == o)
  110. {
  111. if(b[i+8].getText() == "")
  112. {
  113. b[i+8].setText(o);
  114. endGame();
  115. }
  116. } else
  117. if(b[i].getText() == o&&b[i+8].getText() == o)
  118. {
  119. if(b[i+4].getText() == "")
  120. {
  121. b[i+4].setText(o);
  122. endGame();
  123. }
  124. } else if(b[i+4].getText() == o&&b[i+8].getText() == o)
  125. if(b[i].getText() == "")
  126. {
  127. b[i].setText(o);
  128. endGame();
  129. }
  130.  
  131. } else
  132. if(i == 2)
  133. {
  134. if(b[i].getText() == o&&b[i+2].getText() == o)
  135. {
  136. if(b[i+4].getText() == "")
  137. {
  138. b[i+4].setText(o);
  139. endGame();
  140. }
  141. } else
  142. if(b[i].getText() == o&&b[i+4].getText() == o)
  143. {
  144. if(b[i+2].getText() == "")
  145. {
  146. b[i+2].setText(o);
  147. endGame();
  148. }
  149. } else if(b[i+2].getText() == o&&b[i+4].getText() == o)
  150. if(b[i].getText() == "")
  151. {
  152. b[i].setText(o);
  153. endGame();
  154. }
  155.  
  156. } else {comStopPlayer();}
  157. }
  158.  
  159.  
  160. }
  161.  
  162. public void addActionListeners()
  163. {
  164. l[0] = new ActionListener(){
  165.  
  166. public void actionPerformed(ActionEvent e) {
  167.  
  168. if(playerTurn)
  169. {
  170. if(b[0].getText() == "")
  171. b[0].setText(x);
  172.  
  173. changeTurns();
  174. }
  175. comTurn();
  176.  
  177. checkforwin();
  178.  
  179. }
  180. };
  181. /*button 2*/
  182. l[1] = new ActionListener(){
  183.  
  184. public void actionPerformed(ActionEvent args0) {
  185. // TODO Auto-generated method stub
  186. if(playerTurn)
  187. {
  188. if(b[1].getText() == "")
  189. b[1].setText(x);
  190.  
  191. changeTurns();
  192. }
  193. comTurn();
  194.  
  195. checkforwin();
  196. }
  197. };
  198. /*button 3*/
  199. l[2] = new ActionListener(){
  200.  
  201. public void actionPerformed(ActionEvent args0) {
  202. // TODO Auto-generated method stub
  203. if(playerTurn)
  204. {
  205. if(b[2].getText() == "")
  206. b[2].setText(x);
  207.  
  208. changeTurns();
  209. }
  210. comTurn();
  211.  
  212. checkforwin();
  213. }
  214. };
  215. /*button 4*/
  216. l[3] = new ActionListener(){
  217.  
  218. public void actionPerformed(ActionEvent args0) {
  219. // TODO Auto-generated method stub
  220. if(playerTurn)
  221. {
  222. if(b[3].getText() == "")
  223. b[3].setText(x);
  224.  
  225. changeTurns();
  226. }
  227. comTurn();
  228.  
  229. checkforwin();
  230. }
  231. };
  232. /*button 5*/
  233. l[4] = new ActionListener(){
  234.  
  235. public void actionPerformed(ActionEvent args0) {
  236. // TODO Auto-generated method stub
  237. if(playerTurn)
  238. {
  239. if(b[4].getText() == "")
  240. b[4].setText(x);
  241.  
  242. changeTurns();
  243. }
  244. comTurn();
  245.  
  246. checkforwin();
  247. }
  248. };
  249. /*button 6*/
  250. l[5] = new ActionListener(){
  251.  
  252. public void actionPerformed(ActionEvent args0) {
  253. // TODO Auto-generated method stub
  254. if(playerTurn)
  255. {
  256. if(b[5].getText() == "")
  257. b[5].setText(x);
  258.  
  259. changeTurns();
  260. }
  261. comTurn();
  262.  
  263. checkforwin();
  264. }
  265. };
  266. /*button 7*/
  267. l[6] = new ActionListener(){
  268.  
  269. public void actionPerformed(ActionEvent args0) {
  270. // TODO Auto-generated method stub
  271. if(playerTurn)
  272. {
  273. if(b[6].getText() == "")
  274. b[6].setText(x);
  275.  
  276. changeTurns();
  277. }
  278. comTurn();
  279.  
  280. checkforwin();
  281. }
  282. };
  283. /*button 8*/
  284. l[7] = new ActionListener(){
  285.  
  286. public void actionPerformed(ActionEvent args0){
  287. // TODO Auto-generated method stub
  288. if(playerTurn)
  289. {
  290. if(b[7].getText() == "")
  291. b[7].setText(x);
  292.  
  293. changeTurns();
  294. }
  295. comTurn();
  296.  
  297. checkforwin();
  298. }
  299. };
  300. /*button 9*/
  301. l[8] = new ActionListener(){
  302.  
  303. public void actionPerformed(ActionEvent args0) {
  304. // TODO Auto-generated method stub
  305. if(playerTurn)
  306. {
  307. if(b[8].getText() == "")
  308. b[8].setText(x);
  309.  
  310. changeTurns();
  311. }
  312. comTurn();
  313.  
  314. checkforwin();
  315. }
  316. };
  317. }
  318. public void checkforwin()
  319. {
  320. if(b[0].getText() == x&&b[1].getText() == x&&b[2].getText() == x)
  321. {
  322. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  323.  
  324. endGame();
  325. }else
  326. if(b[3].getText() == x&&b[4].getText() == x&&b[5].getText() == x)
  327. {
  328. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  329.  
  330. endGame();
  331. }else
  332. if(b[6].getText() == x&&b[7].getText() == x&&b[8].getText() == x)
  333. {
  334. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  335.  
  336. endGame();
  337. }else
  338. if(b[0].getText() == x&&b[3].getText() == x&&b[6].getText() == x)
  339. {
  340. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  341.  
  342. endGame();
  343. }else
  344. if(b[1].getText() == x&&b[4].getText() == x&&b[7].getText() == x)
  345. {
  346. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  347.  
  348. endGame();
  349. }else
  350. if(b[2].getText() == x&&b[5].getText() == x&&b[8].getText() == x)
  351. {
  352. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  353.  
  354. endGame();
  355. }else
  356. if(b[0].getText() == x&&b[4].getText() == x&&b[8].getText() == x)
  357. {
  358. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  359.  
  360. endGame();
  361. }else
  362. if(b[2].getText() == x&&b[4].getText() == x&&b[6].getText() == x)
  363. {
  364. JOptionPane.showMessageDialog(frame,"The X IS THE WINNER !");
  365.  
  366. endGame();
  367. }
  368.  
  369. //endGame();
  370.  
  371. if(b[0].getText() == o&&b[1].getText() == o&&b[2].getText() == o)
  372. {
  373. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  374.  
  375. endGame();
  376. }else
  377. if(b[3].getText() == o&&b[4].getText() == o&&b[5].getText() == o)
  378. {
  379. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  380.  
  381. endGame();
  382. }else
  383. if(b[6].getText() == o&&b[7].getText() == o&&b[8].getText() == o)
  384. {
  385. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  386.  
  387. endGame();
  388. }else
  389. if(b[0].getText() == o&&b[3].getText() == o&&b[6].getText() == o)
  390. {
  391. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  392.  
  393. endGame();
  394. }else
  395. if(b[1].getText() == o&&b[4].getText() == o&&b[7].getText() == o)
  396. {
  397. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  398.  
  399. endGame();
  400. }else
  401. if(b[2].getText() == o&&b[5].getText() == o&&b[8].getText() == o)
  402. {
  403. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  404.  
  405. endGame();
  406. }else
  407. if(b[0].getText() == o&&b[4].getText() == o&&b[8].getText() == o)
  408. {
  409. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  410.  
  411. endGame();
  412. }else
  413. if(b[2].getText() == o&&b[4].getText() == o&&b[6].getText() == o)
  414. {
  415. JOptionPane.showMessageDialog(frame,"The O IS THE WINNER !");
  416.  
  417. endGame();
  418. }
  419. }
  420.  
  421.  
  422.  
  423. public void addButtons()
  424. {
  425. addActionListeners();
  426.  
  427. b = new JButton[9];
  428. for( int i = 0;i<9;i++)
  429. {
  430. b[i] = new JButton("");
  431. b[i].setSize(80,80);
  432. int xlocation = ((i%3)*85)+70;
  433. int ylocation = ((i/3)*85)+70;
  434. b[i].setLocation(xlocation,ylocation);
  435. panel.add(b[i]);
  436. b[i].addActionListener(l[i]);
  437. b[i].setFont(new Font("Arial",50,50));
  438. }
  439. }
  440. public void createFrame()
  441. {
  442. frame = new JFrame("Java Game");
  443. frame.setVisible(true);
  444. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  445. frame.setSize(400,400);
  446. frame.setResizable(false);
  447. /*JPanel*/
  448. panel = new JPanel();
  449. panel.setLayout(null);
  450. frame.add(panel);
  451. panel.setBackground(Color.ORANGE);
  452. /*Buttons*/
  453. addButtons();
  454. }
  455. public void comFirstTurns()
  456. {
  457. while(count == 0 && comTurn)
  458. {
  459. if(comTurn)
  460. {
  461. if(b[4].getText() == ""){
  462. b[4].setText(o);
  463. changeTurns();
  464. } else
  465. if(b[4].getText() == x){
  466. int random = (int)(Math.random()*4)+1;
  467. switch(random)
  468. {
  469. case 1:if(b[0].getText() == "")
  470. b[0].setText(o);
  471. changeTurns();
  472. break;
  473. case 2:if(b[2].getText() == "")
  474. b[2].setText(o);
  475. changeTurns();
  476. break;
  477. case 3:if(b[6].getText() == "")
  478. b[6].setText(o);
  479. changeTurns();
  480. break;
  481. case 4:if(b[8].getText() == "")
  482. b[8].setText(o);
  483. changeTurns();
  484. break;
  485. }
  486. } else {comWinLogic(); comStopPlayer();}
  487. }//computer Turn.
  488. count ++;
  489. }
  490. }
  491. public void comStopPlayer()
  492. {
  493. for(int i = 0 ;i<9 && comTurn == true;i++)
  494. {
  495.  
  496.  
  497. if(i == 0 || i == 3 || i == 6 )
  498. {
  499. if(b[i].getText() == x&&b[i+1].getText() == x)
  500. {
  501. if(b[i+2].getText() == "")
  502. {
  503. b[i+2].setText(o);
  504. changeTurns();
  505. }
  506. } else
  507. if(b[i].getText() == x&&b[i+2].getText() == x)
  508. if(b[i+1].getText() == "")
  509. {
  510. b[i+1].setText(o);
  511. changeTurns();
  512. }
  513. } else
  514. if(i == 2 || i == 5 || i == 8 )
  515. {
  516. if(b[i].getText() == x&&b[i-1].getText() == x)
  517. {
  518. if(b[i-2].getText() == "")
  519. {
  520. b[i-2].setText(o);
  521. changeTurns();
  522. }
  523. } else
  524. if(b[i].getText() == x&&b[i-2].getText() == x)
  525. if(b[i-1].getText() == "")
  526. {
  527. b[i-1].setText(o);
  528. changeTurns();
  529. }
  530.  
  531. } else
  532. if(i == 0 || i == 2 || i == 3 )
  533. {
  534. if(b[i].getText() == x&&b[i+3].getText() == x)
  535. {
  536. if(b[i+6].getText() == "")
  537. {
  538. b[i+6].setText(o);
  539. changeTurns();
  540. }
  541. } else
  542. if(b[i].getText() == x&&b[i+6].getText() == x)
  543. if(b[i+3].getText() == "")
  544. {
  545. b[i+3].setText(o);
  546. changeTurns();
  547. }
  548. } else
  549. if(i == 6 || i == 7 || i == 8 )
  550. {
  551. if(b[i].getText() == x&&b[i-3].getText() == x)
  552. {
  553. if(b[i-6].getText() == "")
  554. {
  555. b[i-6].setText(o);
  556. changeTurns();
  557. }
  558. } else
  559. if(b[i].getText() == x&&b[i-6].getText() == x)
  560. if(b[i-3].getText() == "")
  561. {
  562. b[i-3].setText(o);
  563. changeTurns();
  564. }
  565.  
  566. } else
  567. if(i == 0)
  568. {
  569. if(b[i].getText() == x&&b[i+4].getText() == x)
  570. {
  571. if(b[i+8].getText() == "")
  572. {
  573. b[i+8].setText(o);
  574. changeTurns();
  575. }
  576. } else
  577. if(b[i].getText() == x&&b[i+8].getText() == x)
  578. {
  579. if(b[i+4].getText() == "")
  580. {
  581. b[i+4].setText(o);
  582. changeTurns();
  583. }
  584. } else if(b[i+4].getText() == x&&b[i+8].getText() == x)
  585. if(b[i].getText() == "")
  586. {
  587. b[i].setText(o);
  588. changeTurns();
  589. }
  590.  
  591. } else
  592. if(i == 2)
  593. {
  594. if(b[i].getText() == x &&b[i+2].getText() == x)
  595. {
  596. if(b[i+4].getText() == "")
  597. {
  598. b[i+4].setText(o);
  599. changeTurns();
  600. }
  601. } else
  602. if(b[i].getText() == x &&b[i+4].getText() == x)
  603. {
  604. if(b[i+2].getText() == "")
  605. {
  606. b[i+2].setText(o);
  607. changeTurns();
  608. }
  609. } else if(b[i+2].getText() == x&&b[i+4].getText() == x)
  610. if(b[i].getText() == "")
  611. {
  612. b[i].setText(o);
  613. changeTurns();
  614. }
  615.  
  616. } else {comFirstTurns();}
  617. }
  618.  
  619.  
  620.  
  621.  
  622. }
  623.  
  624. public void comTurn()
  625. {
  626. comWinLogic();
  627. comStopPlayer();
  628. comFirstTurns();
  629. }
  630.  
  631.  
  632.  
  633. AITTT()
  634. {
  635. createFrame();
  636.  
  637.  
  638.  
  639. res = new JButton("RESTART GAME");
  640. panel.add(res);
  641. res.setSize(130, 30);
  642. res.setLocation(132, 30);
  643. res.setForeground(Color.red);
  644. res.setFont(new Font("Tahoma Bold",12,12));
  645. res.addActionListener(new ActionListener(){
  646. public void actionPerformed(ActionEvent e){
  647. for(int i = 0;i<9;i++)
  648. {
  649. b[i].setText("");
  650. }
  651. playerTurn = true;
  652. comTurn = false;
  653. count = 0;
  654. }
  655. });
  656.  
  657. }//constractor
  658. public static void main(String args[])
  659. {
  660. new AITTT();
  661. }
  662. }
Advertisement
Add Comment
Please, Sign In to add comment