Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- public class TTT extends JFrame implements ActionListener { //DO NOT TOUCH!!!
- //makes the array for the buttons
- JButton spots[ ] = new JButton[ 9];
- //keeps track of who's turn it is
- int turn = 0;
- //lets it go again
- boolean go = true;
- //gets the images for the X's and O's
- ImageIcon red = new ImageIcon("x.PNG");
- ImageIcon blue = new ImageIcon("o.PNG");
- ImageIcon blank = new ImageIcon("blank.PNG");
- public static void main (String []args ) {
- TTT frame = new TTT(); //DO NOT TOUCH!!!
- frame.setVisible(true);
- }
- public TTT( ) { //DO NOT TOUCH!!!
- //set the frame default properties
- setTitle ("Tic Tac Toe");
- setSize ( 308, 308 );
- setLocationRelativeTo ( null );
- setResizable(false);
- //register 'Exit upon closing' as a default close operation
- setDefaultCloseOperation( EXIT_ON_CLOSE );
- changeBkColor( );
- }
- private void changeBkColor() {
- while (go) {
- //declares some variables that we will use later
- int newLine = 0;
- int lineCount = 0;
- //change background color to white
- Container contentPane = getContentPane();
- contentPane.setBackground(Color.WHITE);
- contentPane.setLayout(null);
- //puts the buttons on the screen
- for (int i = 0; i < spots.length; i++) {
- //make it first appear as a blank image
- spots[ i] = new JButton (blank);
- //checks if it needs a new row
- if (i == 3 || i == 6) {
- newLine++;
- lineCount = 0;
- }
- //sets the positions of the buttons
- spots[ i].setBounds(lineCount*100, newLine*100, 100, 100);
- //add it to the container
- contentPane.add(spots[ i]);
- spots[ i].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- public void run() {
- //check button pressed
- for (int i = 0; i < spots.length; i++) {
- if(e.getSource()==spots[ i]) {
- //check turn
- if (turn%2==0) {
- spots[ i].setIcon(red);
- } else {
- spots[ i].setIcon(blue);
- }
- //disable the button so it can't be re-pressed
- spots[ i].removeActionListener(this);
- }
- }
- turn++;
- //checks for wins
- for(int i = 0; i < 3; i++) {
- if (spots[ i].getIcon()==red && //checks for verticle x win
- spots[ i+3].getIcon()==red &&
- spots[ i+6].getIcon()==red) {
- int again1 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
- if (again1 == JOptionPane.YES_OPTION) {
- go = true;
- } if (again1 == JOptionPane.NO_OPTION) {
- JOptionPane.showMessageDialog(null, "Okay then. Bye!");
- go = false;
- }
- }else if (spots[ i].getIcon()==blue && //checks for verticle o win
- spots[ i+3].getIcon()==blue &&
- spots[ i+6].getIcon()==blue) {
- int again2 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
- if (again2 == JOptionPane.YES_OPTION) {
- go = true;
- } if (again2 == JOptionPane.NO_OPTION) {
- JOptionPane.showMessageDialog(null, "Okay then. Bye!");
- go = false;
- }
- }else if (spots[ i*3].getIcon()==red && //checks for horizontal x win
- spots[ (i*3)+1].getIcon()==red &&
- spots[ (i*3)+2].getIcon()==red) {
- int again3 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
- if (again3 == JOptionPane.YES_OPTION) {
- go = true;
- } if (again3 == JOptionPane.NO_OPTION) {
- JOptionPane.showMessageDialog(null, "Okay then. Bye!");
- go = false;
- }
- }else if (spots[ i*3].getIcon()==blue && //checks for horizontal o win
- spots[ (i*3)+1].getIcon()==blue &&
- spots[ (i*3)+2].getIcon()==blue) {
- int again4 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
- if (again4 == JOptionPane.YES_OPTION) {
- go = true;
- } if (again4 == JOptionPane.NO_OPTION) {
- JOptionPane.showMessageDialog(null, "Okay then. Bye!");
- go = false;
- }
- }else if (spots[ i].getIcon()==red && //checks for diagnol x win
- spots[ 4].getIcon()==red &&
- spots[ 8-i].getIcon()==red) {
- int again5 = JOptionPane.showConfirmDialog(null, "X Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
- if (again5 == JOptionPane.YES_OPTION) {
- go = true;
- } if (again5 == JOptionPane.NO_OPTION) {
- JOptionPane.showMessageDialog(null, "Okay then. Bye!");
- go = false;
- }
- }else if (spots[ i].getIcon()==blue && //checks for diagnol o win
- spots[ 4].getIcon()==blue &&
- spots[ 8-i].getIcon()==blue) {
- int again6 = JOptionPane.showConfirmDialog(null, "O Wins! Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
- if (again6 == JOptionPane.YES_OPTION) {
- go = true;
- } if (again6 == JOptionPane.NO_OPTION) {
- JOptionPane.showMessageDialog(null, "Okay then. Bye!");
- go = false;
- }
- }
- }
- lineCount++;
- }
- }
- });
- }
- } if (!go) {
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement