Guest User

infinite while loop?

a guest
Dec 27th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.60 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. class TTT extends JFrame { //DO NOT TOUCH!!!
  9.    
  10.     //makes the array for the buttons
  11.     JButton spots[ ] = new JButton[ 9];
  12.     //keeps track of who's turn it is
  13.     int turn = 0;
  14.     //counts the number of lines
  15.     int lineCount;
  16.     //lets it go again
  17.     boolean go = true;
  18.     //gets the images for the X's and O's
  19.     ImageIcon red = new ImageIcon("x.PNG");
  20.     ImageIcon blue = new ImageIcon("o.PNG");
  21.     ImageIcon blank = new ImageIcon("blank.PNG");
  22.  
  23.     public static void main (String []args ) {
  24.         TTT frame = new TTT(); //DO NOT TOUCH!!!
  25.         frame.setVisible(true);
  26.     }
  27.    
  28.     public TTT() {
  29.         //set the frame default properties
  30.         setTitle ("Tic Tac Toe");
  31.         setSize ( 308, 308 );
  32.         setLocationRelativeTo ( null );
  33.         setResizable(false);
  34.         //register 'Exit upon closing' as a default close operation
  35.         setDefaultCloseOperation( EXIT_ON_CLOSE );
  36.  
  37.         changeBkColor( );
  38.     }
  39.    
  40.     public void changeBkColor() {
  41.         while (go) {
  42.             //declares some variables that we will use later
  43.             int newLine = 0;
  44.             lineCount = 0;
  45.             //change background color to white
  46.             Container contentPane = getContentPane();
  47.             contentPane.setBackground(Color.WHITE);
  48.             contentPane.setLayout(null);
  49.             //puts the buttons on the screen
  50.             for (int i = 0; i < spots.length; i++) {
  51.                 //make it first appear as a blank image
  52.                 spots[ i] = new JButton (blank);
  53.                 //checks if it needs a new row
  54.                 if (i == 3 || i == 6) {
  55.                     newLine++;
  56.                     lineCount = 0;
  57.                 }
  58.                 //sets the positions of the buttons
  59.                 spots[ i].setBounds(lineCount*100, newLine*100, 100, 100);
  60.                 //add it to the container
  61.                 contentPane.add(spots[ i]);
  62.                 spots[ i].addActionListener(new ActionListener() {
  63.                     public void actionPerformed(ActionEvent e) {
  64.                         //check button pressed
  65.                         for (int i = 0; i < spots.length; i++) {
  66.                             if(e.getSource()==spots[ i]) {
  67.                                 //check turn
  68.                                 if (turn%2==0) {
  69.                                     spots[ i].setIcon(red);
  70.                                 } else {
  71.                                     spots[ i].setIcon(blue);
  72.                                 }
  73.                                 //disable the button so it can't be re-pressed
  74.                                 spots[ i].removeActionListener(this);
  75.                             }
  76.                         }
  77.                         turn++;
  78.                         //checks for wins
  79.                         for(int i = 0; i < 3; i++) {
  80.                             if (spots[ i].getIcon()==red &&                //checks for verticle x win
  81.                                 spots[ i+3].getIcon()==red &&
  82.                                 spots[ i+6].getIcon()==red) {
  83.                                     int again1 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
  84.                                     if (again1 == JOptionPane.YES_OPTION) {
  85.                                         go = true;
  86.                                     } if (again1 == JOptionPane.NO_OPTION) {
  87.                                         JOptionPane.showMessageDialog(null, "Okay then. Bye!");
  88.                                         go = false;
  89.                                     }
  90.                             }else if (spots[ i].getIcon()==blue &&     //checks for verticle o win
  91.                                         spots[ i+3].getIcon()==blue &&
  92.                                         spots[ i+6].getIcon()==blue) {
  93.                                             int again2 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
  94.                                             if (again2 == JOptionPane.YES_OPTION) {
  95.                                                 go = true;
  96.                                             } if (again2 == JOptionPane.NO_OPTION) {
  97.                                                 JOptionPane.showMessageDialog(null, "Okay then. Bye!");
  98.                                                 go = false;
  99.                                             }
  100.                             }else if (spots[ i*3].getIcon()==red &&    //checks for horizontal x win
  101.                                         spots[ (i*3)+1].getIcon()==red &&
  102.                                         spots[ (i*3)+2].getIcon()==red) {
  103.                                             int again3 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
  104.                                             if (again3 == JOptionPane.YES_OPTION) {
  105.                                                 go = true;
  106.                                             } if (again3 == JOptionPane.NO_OPTION) {
  107.                                                 JOptionPane.showMessageDialog(null, "Okay then. Bye!");
  108.                                                 go = false;
  109.                                             }
  110.                             }else if (spots[ i*3].getIcon()==blue &&   //checks for horizontal o win
  111.                                         spots[ (i*3)+1].getIcon()==blue &&
  112.                                         spots[ (i*3)+2].getIcon()==blue) {
  113.                                             int again4 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
  114.                                             if (again4 == JOptionPane.YES_OPTION) {
  115.                                                 go = true;
  116.                                             } if (again4 == JOptionPane.NO_OPTION) {
  117.                                                 JOptionPane.showMessageDialog(null, "Okay then. Bye!");
  118.                                                 go = false;
  119.                                             }
  120.                             }else if (spots[ i].getIcon()==red &&      //checks for diagnol x win
  121.                                         spots[ 4].getIcon()==red &&
  122.                                         spots[ 8-i].getIcon()==red) {
  123.                                             int again5 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
  124.                                             if (again5 == JOptionPane.YES_OPTION) {
  125.                                                 go = true;
  126.                                             } if (again5 == JOptionPane.NO_OPTION) {
  127.                                                 JOptionPane.showMessageDialog(null, "Okay then. Bye!");
  128.                                                 go = false;
  129.                                             }
  130.                             }else if (spots[ i].getIcon()==blue &&    //checks for diagnol o win
  131.                                         spots[ 4].getIcon()==blue &&
  132.                                         spots[ 8-i].getIcon()==blue) {
  133.                                             int again6 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
  134.                                             if (again6 == JOptionPane.YES_OPTION) {
  135.                                                 go = true;
  136.                                             } if (again6 == JOptionPane.NO_OPTION) {
  137.                                                 JOptionPane.showMessageDialog(null, "Okay then. Bye!");
  138.                                                 go = false;
  139.                                             }
  140.                             } else {
  141.                                 if (spots[ i].getIcon()!=blank &&
  142.                                     spots[ i+1].getIcon()!=blank &&
  143.                                     spots[ i+2].getIcon()!=blank &&
  144.                                     spots[ i+3].getIcon()!=blank &&
  145.                                     spots[ i+4].getIcon()!=blank &&
  146.                                     spots[ i+5].getIcon()!=blank &&
  147.                                     spots[ i+6].getIcon()!=blank &&
  148.                                     spots[ i+7].getIcon()!=blank &&
  149.                                     spots[ i+8].getIcon()!=blank ) {
  150.                                         JOptionPane.showMessageDialog(null, "Cat's game!");
  151.                                         System.exit(0);
  152.                                 }
  153.                             }
  154.                         }
  155.                     }
  156.                 });
  157.                 lineCount++;
  158.             }
  159.         } if (!go) {
  160.             //get content pane,set background color, and set the layout manager to null
  161.             Container contentPane = getContentPane();
  162.             contentPane.setBackground(Color.WHITE);
  163.             contentPane.setLayout(null);
  164.             //make the JButton
  165.             JButton endButton = new JButton("Click to close");
  166.             endButton.addActionListener(new ActionListener() {
  167.                 public void actionPerformed(ActionEvent e) {
  168.                     System.exit(0);
  169.                 }
  170.             });
  171.             endButton.setBounds( 100, 100, 100, 100 );
  172.             contentPane.add( endButton );
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment