Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Java.TicTacToe;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- public class GUI extends JFrame implements ActionListener {
- private JPanel panelGame;
- private JPanel panelMenu[];
- private JPanel playerPanel[];
- private JButton resetBtn;
- private JButton buttonGame[][];
- private JLabel playerLabel[];
- private JLabel playerSymbol[];
- private JLabel playerScore[];
- private static String currentTurn = "X";
- private static String playerName[] = {"", ""};
- private static int checkCounter = 0;
- private static int score[] = {0, 0};
- private static int turn = 0;
- public GUI() {
- for (int i = 0; i < 2; i++) {
- do {
- playerName[i] = JOptionPane.showInputDialog("<html>Enter Player " + (i + 1) + " Name:<br/>(MAX OF 5)</html>");
- if (playerName[i].equals("") || String.valueOf(playerName[i].charAt(0)).equals(" ")) {
- JOptionPane.showMessageDialog(null, "Must enter a valid name!", "Invalid Name!", JOptionPane.DEFAULT_OPTION);
- }
- } while(playerName[i].equals("") || playerName[i].equals(" "));
- playerName[i].trim();
- if(playerName[i].length() > 5) {
- playerName[i] = playerName[i].substring(0, 5);
- }
- }
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setSize(450, 500);
- setLayout(new BorderLayout());
- setResizable(false);
- setLocationRelativeTo(null);
- panelGame = new JPanel();
- panelMenu = new JPanel[4];
- panelMenu[0] = new JPanel(new GridLayout(1, 3));
- for (int i = 1; i < 4; i++) {
- panelMenu[i] = new JPanel();
- panelMenu[0].add(panelMenu[i]);
- }
- panelGame.setLayout(new GridLayout(3, 3));
- panelMenu[0].setPreferredSize(new Dimension(450, 50));;
- panelMenu[1].setLayout(new FlowLayout(FlowLayout.LEFT, 20, 5));
- panelMenu[2].setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
- panelMenu[3].setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 5));
- add(panelGame, BorderLayout.CENTER);
- add(panelMenu[0], BorderLayout.NORTH);
- playerPanel = new JPanel[2];
- playerLabel = new JLabel[2];
- playerSymbol = new JLabel[2];
- playerScore = new JLabel[2];
- for (int i = 0; i < 2; i++) {
- playerPanel[i] = new JPanel();
- playerLabel[i] = new JLabel("<html><b>Player " + String.valueOf(i + 1) + "</b><br/><center>" + playerName[i] + "</center></html>", SwingConstants.CENTER);
- playerLabel[i].setFont(new Font("Arial", Font.PLAIN, 15));
- playerScore[i] = new JLabel(String.valueOf(score[i]), SwingConstants.CENTER);
- playerScore[i].setFont(new Font("Arial", Font.BOLD, 32));
- playerPanel[i].add(playerLabel[i]);
- }
- playerSymbol[0] = new JLabel("X");
- playerSymbol[0].setFont(new Font("Arial", Font.BOLD, 18));
- playerSymbol[1] = new JLabel("O");
- playerSymbol[1].setFont(new Font("Arial", Font.BOLD, 18));
- resetBtn = new JButton("RESET");
- resetBtn.addActionListener(this);
- panelMenu[1].add(playerScore[0]);
- panelMenu[1].add(playerLabel[0]);
- panelMenu[2].add(playerSymbol[0]);
- panelMenu[2].add(resetBtn);
- panelMenu[2].add(playerSymbol[1]);
- panelMenu[3].add(playerLabel[1]);
- panelMenu[3].add(playerScore[1]);
- buttonGame = new JButton[3][3];
- for (int i = 0; i < 3; i++) {
- for (int n = 0; n < 3; n++) {
- buttonGame[i][n] = new JButton(/*String.valueOf(i) + String.valueOf(n)*/);
- buttonGame[i][n].addActionListener(this);
- buttonGame[i][n].setFont(new Font("Arial", Font.BOLD, 32));
- buttonGame[i][n].setOpaque(false);
- buttonGame[i][n].setOpaque(false);
- buttonGame[i][n].setContentAreaFilled(false);
- buttonGame[i][n].setPreferredSize(new Dimension(150, 150));
- panelGame.add(buttonGame[i][n]);
- }
- }
- setVisible(true);
- }
- @Override
- public void actionPerformed (ActionEvent e) {
- JButton btnPressed = (JButton) e.getSource();
- if (btnPressed == resetBtn) {
- setDefault();
- } else {
- playerPress(btnPressed);
- }
- }
- private void setDefault() {
- for (int i = 0; i < 3; i++) {
- for (int n = 0; n < 3; n++) {
- buttonGame[i][n].setText("");
- buttonGame[i][n].setEnabled(true);
- }
- }
- playerScore[0].setText(String.valueOf(score[0]));
- playerScore[1].setText(String.valueOf(score[1]));
- turn = 0;
- currentTurn = "X";
- }
- private void playerPress(JButton btnPressed) {
- btnPressed.setText(currentTurn);
- btnPressed.setEnabled(false);
- if (checkWin() == 0) {
- endGame((currentTurn == "X") ? 0 : 1);
- } else if(turn == 8) {
- endGame(2);
- } else {
- turn++;
- currentTurn = (currentTurn == "X") ? "O" : "X";
- }
- }
- private int checkWin() {
- //check horizontal
- for (int i = 0; i < 3; i++) {
- checkCounter = 0;
- for(int n = 0; n < 3; n++) {
- if(buttonGame[i][n].getText().equals(currentTurn)) {
- checkCounter++;
- } else {
- break;
- }
- }
- if (checkCounter == 3) {
- checkCounter = 0;
- return 0;
- }
- }
- //check vertical
- for (int i = 0; i < 3; i++) {
- checkCounter = 0;
- for(int n = 0; n < 3; n++) {
- if(buttonGame[n][i].getText().equals(currentTurn)) {
- checkCounter++;
- } else {
- break;
- }
- }
- if (checkCounter == 3) {
- checkCounter = 0;
- return 0;
- }
- }
- //check top left to bottom right
- checkCounter = 0;
- for (int i = 0; i < 3; i++) {
- if(buttonGame[i][i].getText().equals(currentTurn)) {
- checkCounter++;
- } else {
- break;
- }
- if (checkCounter == 3) {
- checkCounter = 0;
- return 0;
- }
- }
- //check top right to bottom left
- checkCounter = 0;
- for (int i = 0, n = 2; i < 3; i++, n--) {
- if(buttonGame[n][i].getText().equals(currentTurn)) {
- checkCounter++;
- } else {
- break;
- }
- if (checkCounter == 3) {
- checkCounter = 0;
- return 0;
- }
- }
- return 1;
- }
- private void endGame(int i) {
- switch (i) {
- case 0:
- case 1:
- JOptionPane.showMessageDialog(null, "Winnter is: " + playerName[i], "Match Won", JOptionPane.DEFAULT_OPTION);
- score[i]++;
- break;
- case 2:
- JOptionPane.showMessageDialog(null, "Match is a draw", "Draw", JOptionPane.DEFAULT_OPTION);
- }
- setDefault();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement