Advertisement
Guest User

run sub-class help?

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