Advertisement
Guest User

Untitled

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