Advertisement
Kozaki09

Untitled

Apr 28th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.48 KB | Source Code | 0 0
  1. package Java.TicTacToe;
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6.  
  7. public class GUI extends JFrame implements ActionListener {
  8.     private JPanel panelGame;
  9.     private JPanel panelMenu[];
  10.     private JPanel playerPanel[];
  11.     private JButton resetBtn;
  12.     private JButton buttonGame[][];
  13.     private JLabel playerLabel[];
  14.     private JLabel playerSymbol[];
  15.     private JLabel playerScore[];
  16.  
  17.     private static String currentTurn = "X";
  18.     private static String playerName[] = {"", ""};
  19.     private static int checkCounter = 0;
  20.     private static int score[] = {0, 0};
  21.     private static int turn = 0;
  22.    
  23.     public GUI() {
  24.         for (int i = 0; i < 2; i++) {
  25.             do {
  26.                 playerName[i] = JOptionPane.showInputDialog("<html>Enter Player " + (i + 1) + " Name:<br/>(MAX OF 5)</html>");
  27.                 if (playerName[i].equals("") || String.valueOf(playerName[i].charAt(0)).equals(" ")) {
  28.                    JOptionPane.showMessageDialog(null, "Must enter a valid name!", "Invalid Name!", JOptionPane.DEFAULT_OPTION);
  29.                 }
  30.                 } while(playerName[i].equals("") || playerName[i].equals(" "));
  31.  
  32.             playerName[i].trim();
  33.             if(playerName[i].length() > 5) {
  34.                 playerName[i] = playerName[i].substring(0, 5);
  35.             }
  36.         }
  37.  
  38.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  39.         setSize(450, 500);
  40.         setLayout(new BorderLayout());
  41.         setResizable(false);
  42.         setLocationRelativeTo(null);
  43.  
  44.         panelGame = new JPanel();
  45.         panelMenu = new JPanel[4];
  46.  
  47.         panelMenu[0] = new JPanel(new GridLayout(1, 3));
  48.         for (int i = 1; i < 4; i++) {
  49.             panelMenu[i] = new JPanel();
  50.             panelMenu[0].add(panelMenu[i]);
  51.         }
  52.  
  53.         panelGame.setLayout(new GridLayout(3, 3));
  54.         panelMenu[0].setPreferredSize(new Dimension(450, 50));;
  55.         panelMenu[1].setLayout(new FlowLayout(FlowLayout.LEFT, 20, 5));
  56.         panelMenu[2].setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
  57.         panelMenu[3].setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 5));
  58.  
  59.         add(panelGame, BorderLayout.CENTER);
  60.         add(panelMenu[0], BorderLayout.NORTH);
  61.  
  62.         playerPanel = new JPanel[2];
  63.         playerLabel = new JLabel[2];
  64.         playerSymbol = new JLabel[2];
  65.         playerScore = new JLabel[2];
  66.  
  67.         for (int i = 0; i < 2; i++) {
  68.             playerPanel[i] = new JPanel();
  69.             playerLabel[i] = new JLabel("<html><b>Player " + String.valueOf(i + 1) + "</b><br/><center>" + playerName[i] + "</center></html>", SwingConstants.CENTER);
  70.             playerLabel[i].setFont(new Font("Arial", Font.PLAIN, 15));
  71.             playerScore[i] = new JLabel(String.valueOf(score[i]), SwingConstants.CENTER);
  72.             playerScore[i].setFont(new Font("Arial", Font.BOLD, 32));
  73.             playerPanel[i].add(playerLabel[i]);
  74.         }
  75.  
  76.         playerSymbol[0] = new JLabel("X");
  77.         playerSymbol[0].setFont(new Font("Arial", Font.BOLD, 18));
  78.         playerSymbol[1] = new JLabel("O");
  79.         playerSymbol[1].setFont(new Font("Arial", Font.BOLD, 18));
  80.        
  81.         resetBtn = new JButton("RESET");
  82.         resetBtn.addActionListener(this);
  83.  
  84.         panelMenu[1].add(playerScore[0]);
  85.         panelMenu[1].add(playerLabel[0]);
  86.  
  87.         panelMenu[2].add(playerSymbol[0]);
  88.         panelMenu[2].add(resetBtn);
  89.         panelMenu[2].add(playerSymbol[1]);
  90.  
  91.         panelMenu[3].add(playerLabel[1]);
  92.         panelMenu[3].add(playerScore[1]);
  93.  
  94.         buttonGame = new JButton[3][3];
  95.         for (int i = 0; i < 3; i++) {
  96.             for (int n = 0; n < 3; n++) {
  97.                 buttonGame[i][n] = new JButton(/*String.valueOf(i) + String.valueOf(n)*/);
  98.                 buttonGame[i][n].addActionListener(this);
  99.                 buttonGame[i][n].setFont(new Font("Arial", Font.BOLD, 32));
  100.                 buttonGame[i][n].setOpaque(false);
  101.                 buttonGame[i][n].setOpaque(false);
  102.                 buttonGame[i][n].setContentAreaFilled(false);    
  103.                 buttonGame[i][n].setPreferredSize(new Dimension(150, 150));
  104.                 panelGame.add(buttonGame[i][n]);
  105.             }
  106.         }
  107.  
  108.         setVisible(true);
  109.     }
  110.    
  111.     @Override
  112.     public void actionPerformed (ActionEvent e) {
  113.         JButton btnPressed = (JButton) e.getSource();
  114.         if (btnPressed == resetBtn) {
  115.             setDefault();
  116.         } else {
  117.             playerPress(btnPressed);
  118.         }
  119.     }
  120.  
  121.     private void setDefault() {
  122.         for (int i = 0; i < 3; i++) {
  123.             for (int n = 0; n < 3; n++) {
  124.                 buttonGame[i][n].setText("");
  125.                 buttonGame[i][n].setEnabled(true);
  126.             }
  127.         }
  128.        
  129.         playerScore[0].setText(String.valueOf(score[0]));
  130.         playerScore[1].setText(String.valueOf(score[1]));
  131.         turn = 0;
  132.         currentTurn = "X";
  133.     }
  134.  
  135.     private void playerPress(JButton btnPressed) {
  136.         btnPressed.setText(currentTurn);
  137.         btnPressed.setEnabled(false);
  138.         if (checkWin() == 0) {
  139.             endGame((currentTurn == "X") ? 0 : 1);
  140.         } else if(turn == 8) {
  141.             endGame(2);
  142.         } else {
  143.             turn++;
  144.             currentTurn = (currentTurn == "X") ? "O" : "X";
  145.         }
  146.     }
  147.  
  148.     private int checkWin() {
  149.         //check horizontal
  150.         for (int i = 0; i < 3; i++) {
  151.             checkCounter = 0;
  152.  
  153.             for(int n = 0; n < 3; n++) {
  154.                 if(buttonGame[i][n].getText().equals(currentTurn)) {
  155.                     checkCounter++;
  156.                 } else {
  157.                     break;
  158.                 }
  159.             }
  160.  
  161.             if (checkCounter == 3) {
  162.                 checkCounter = 0;
  163.                 return 0;
  164.             }
  165.         }
  166.  
  167.         //check vertical
  168.         for (int i = 0; i < 3; i++) {
  169.             checkCounter = 0;
  170.  
  171.             for(int n = 0; n < 3; n++) {
  172.                 if(buttonGame[n][i].getText().equals(currentTurn)) {
  173.                     checkCounter++;
  174.                 } else {
  175.                     break;
  176.                 }
  177.             }
  178.  
  179.             if (checkCounter == 3) {
  180.                 checkCounter = 0;
  181.                 return 0;
  182.             }
  183.         }
  184.  
  185.         //check top left to bottom right
  186.         checkCounter = 0;
  187.         for (int i = 0; i < 3; i++) {
  188.             if(buttonGame[i][i].getText().equals(currentTurn)) {
  189.                 checkCounter++;
  190.             } else {
  191.                 break;
  192.             }
  193.  
  194.             if (checkCounter == 3) {
  195.                 checkCounter = 0;
  196.                 return 0;
  197.             }
  198.         }
  199.        
  200.         //check top right to bottom left
  201.         checkCounter = 0;
  202.         for (int i = 0, n = 2; i < 3; i++, n--) {
  203.             if(buttonGame[n][i].getText().equals(currentTurn)) {
  204.                 checkCounter++;
  205.             } else {
  206.                 break;
  207.             }
  208.  
  209.             if (checkCounter == 3) {
  210.                 checkCounter = 0;
  211.                 return 0;
  212.             }
  213.         }
  214.  
  215.         return 1;
  216.     }
  217.  
  218.     private void endGame(int i) {
  219.         switch (i) {
  220.             case 0:
  221.             case 1:
  222.                 JOptionPane.showMessageDialog(null, "Winnter is: " + playerName[i], "Match Won", JOptionPane.DEFAULT_OPTION);
  223.                 score[i]++;
  224.                 break;
  225.             case 2:
  226.                 JOptionPane.showMessageDialog(null, "Match is a draw", "Draw", JOptionPane.DEFAULT_OPTION);
  227.            
  228.         }
  229.         setDefault();
  230.     }
  231. }
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement