KillianO92

Tic Tac Toe

Dec 20th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class TicTacToeSwing  implements ActionListener
  6. {
  7.     //Insantiate Variables
  8.     private JFrame window = new JFrame("Tic-Tac-Toe");
  9.     private JButton button1 = new JButton("");
  10.     private JButton button2 = new JButton("");
  11.     private JButton button3 = new JButton("");
  12.     private JButton button4 = new JButton("");
  13.     private JButton button5 = new JButton("");
  14.     private JButton button6 = new JButton("");
  15.     private JButton button7 = new JButton("");
  16.     private JButton button8 = new JButton("");
  17.     private JButton button9 = new JButton("");
  18.     private String letter = "";
  19.     private int count = 0;
  20.     private boolean win = false;
  21.  
  22.     public TicTacToeSwing()
  23.     {
  24.         //Create Window
  25.         window.setSize(300, 300);
  26.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         window.setLayout(new GridLayout(3, 3));
  28.  
  29.         //Add buttons
  30.         window.add(button1);
  31.         window.add(button2);
  32.         window.add(button3);
  33.         window.add(button4);
  34.         window.add(button5);
  35.         window.add(button6);
  36.         window.add(button7);
  37.         window.add(button8);
  38.         window.add(button9);
  39.  
  40.         //Add Action Listener
  41.         button1.addActionListener(this);
  42.         button2.addActionListener(this);
  43.         button3.addActionListener(this);
  44.         button4.addActionListener(this);
  45.         button5.addActionListener(this);
  46.         button6.addActionListener(this);
  47.         button7.addActionListener(this);
  48.         button8.addActionListener(this);
  49.         button9.addActionListener(this);
  50.  
  51.         //Make window visible
  52.         window.setVisible(true);
  53.     }
  54.  
  55.     public void actionPerformed(ActionEvent a)
  56.     {
  57.         count++;
  58.  
  59.         //Figure out who's turn it is
  60.         if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
  61.         {
  62.             letter = "X";
  63.         }
  64.         else if (count == 2 || count == 4 || count == 6 || count == 8)
  65.         {
  66.             letter = "O";
  67.         }
  68.  
  69.         //Display X's and O's
  70.         if (a.getSource() == button1)
  71.         {
  72.             button1.setText(letter);
  73.             button1.setEnabled(false);
  74.         }
  75.         else if (a.getSource() == button2)
  76.         {
  77.             button2.setText(letter);
  78.             button2.setEnabled(false);
  79.         }
  80.         else if (a.getSource() == button3)
  81.         {
  82.             button3.setText(letter);
  83.             button3.setEnabled(false);
  84.         }
  85.         else if (a.getSource() == button4)
  86.         {
  87.             button4.setText(letter);
  88.             button4.setEnabled(false);
  89.         }
  90.         else if (a.getSource() == button5)
  91.         {
  92.             button5.setText(letter);
  93.             button5.setEnabled(false);
  94.         }
  95.         else if (a.getSource() == button6)
  96.         {
  97.             button6.setText(letter);
  98.             button6.setEnabled(false);
  99.         }
  100.         else if (a.getSource() == button7)
  101.         {
  102.             button7.setText(letter);
  103.             button7.setEnabled(false);
  104.         }
  105.         else if (a.getSource() == button8)
  106.         {
  107.             button8.setText(letter);
  108.             button8.setEnabled(false);
  109.         }
  110.         else if (a.getSource() == button9)
  111.         {
  112.             button9.setText(letter);
  113.             button9.setEnabled(false);
  114.         }
  115.  
  116.  
  117.         //Determine who won
  118.         if(button1.getText() == button2.getText()
  119.                 && button2.getText() == button3.getText()
  120.                 && button1.getText() != "")
  121.         {
  122.             win = true;
  123.         }
  124.         else if(button4.getText() == button5.getText()
  125.                     && button5.getText() == button6.getText()
  126.                     && button4.getText() != "")
  127.         {
  128.             win = true;
  129.         }
  130.         else if(button7.getText() == button8.getText()
  131.                     && button8.getText() == button9.getText()
  132.                     && button7.getText() != "")
  133.         {
  134.             win = true;
  135.         }
  136.         else if(button1.getText() == button4.getText()
  137.                     && button4.getText() == button7.getText()
  138.                     && button1.getText() != "")
  139.         {
  140.             win = true;
  141.         }
  142.         else if(button2.getText() == button5.getText()
  143.                     && button5.getText() == button8.getText()
  144.                     && button2.getText() != "")
  145.         {
  146.             win = true;
  147.         }
  148.         else if(button3.getText() == button6.getText()
  149.                         && button6.getText() == button9.getText()
  150.                         && button3.getText() != "")
  151.         {
  152.                 win = true;
  153.         }
  154.         else if(button1.getText() == button5.getText()
  155.                     && button5.getText() == button9.getText()
  156.                     && button1.getText() != "")
  157.         {
  158.             win = true;
  159.         }
  160.         else if(button3.getText() == button5.getText()
  161.                     && button5.getText() == button7.getText()
  162.                     && button1.getText() != "")
  163.         {
  164.             win = true;
  165.         }
  166.         else
  167.         {
  168.             win = false;
  169.         }
  170.  
  171.         if (win == true)
  172.         {
  173.             JOptionPane.showMessageDialog(null,
  174.                                           letter + " Wins!");
  175.         }
  176.         else if (count == 9 && win == false)
  177.         {
  178.             JOptionPane.showMessageDialog(null,
  179.                                           "Tie Game!");
  180.         }
  181.     }
  182.  
  183.     public static void main(String[] args)
  184.     {
  185.         new TicTacToeSwing();
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment