Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. //ALAMON,Vianni Benjamin B.
  2. //16/09/2019
  3.  
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.*;
  8.  
  9. public class TicTacToe extends JPanel
  10. {
  11.     JButton buttons[] = new JButton[9];
  12.     int alternate = 0;
  13.    
  14.     public TicTacToe()
  15.     {
  16.       setLayout(new GridLayout(3,3));
  17.       initializebuttons();
  18.     }
  19.    
  20.     public void initializebuttons()
  21.     {
  22.         for(int i = 0; i <= 8; i++)
  23.         {
  24.             buttons[i] = new JButton();
  25.             buttons[i].setText("");
  26.             buttons[i].addActionListener(new buttonListener());
  27.            
  28.             add(buttons[i]);    
  29.         }
  30.     }
  31.     public void resetButtons()
  32.     {
  33.         for(int i = 0; i <= 8; i++)
  34.         {
  35.             buttons[i].setText("");
  36.         }
  37.     }
  38.    
  39.  
  40.     private class buttonListener implements ActionListener
  41.     {
  42.        
  43.         public void actionPerformed(ActionEvent e)
  44.         {
  45.            
  46.             JButton buttonClicked = (JButton)e.getSource();
  47.             if(alternate%2 == 0)
  48.                 buttonClicked.setText("X");
  49.             else
  50.                 buttonClicked.setText("O");
  51.            
  52.             if(checkForWin() == true)
  53.             {
  54.                 JOptionPane.showConfirmDialog(null, "Game Over.");
  55.                 resetButtons();
  56.             }
  57.                
  58.             alternate++;
  59.            
  60.         }
  61.        
  62.         public boolean checkForWin()
  63.         {
  64.            
  65.             if( checkAdjacent(0,1) && checkAdjacent(1,2) )
  66.                 return true;
  67.             else if( checkAdjacent(3,4) && checkAdjacent(4,5) )
  68.                 return true;
  69.             else if ( checkAdjacent(6,7) && checkAdjacent(7,8))
  70.                 return true;
  71.            
  72.            
  73.             else if ( checkAdjacent(0,3) && checkAdjacent(3,6))
  74.                 return true;  
  75.             else if ( checkAdjacent(1,4) && checkAdjacent(4,7))
  76.                 return true;
  77.             else if ( checkAdjacent(2,5) && checkAdjacent(5,8))
  78.                 return true;
  79.            
  80.            
  81.             else if ( checkAdjacent(0,4) && checkAdjacent(4,8))
  82.                 return true;  
  83.             else if ( checkAdjacent(2,4) && checkAdjacent(4,6))
  84.                 return true;
  85.             else
  86.                 return false;
  87.            
  88.            
  89.         }
  90.        
  91.         public boolean checkAdjacent(int a, int b)
  92.         {
  93.             if ( buttons[a].getText().equals(buttons[b].getText()) && !buttons[a].getText().equals("") )
  94.                 return true;
  95.             else
  96.                 return false;
  97.         }
  98.        
  99.     }
  100.    
  101.     public static void main(String[] args)
  102.     {
  103.         JFrame window = new JFrame("Tic-Tac-Toe");
  104.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105.         window.getContentPane().add(new TicTacToe());
  106.         window.setBounds(300,200,300,300);
  107.         window.setVisible(true);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement