My Tic Tac Toe Program.....
My Own Program...
*No Bugs
*No Error
*2 Players
*Scoreboard
*Player Name
*Automatic Player Name (If name leave or set as blank)
*Its Working......
package tictactoe;
/**
*
* @author DicksonPaL21
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe {
private int xCount = 0, oCount = 0;
private JButton btn1 = new JButton();
private JButton btn2 = new JButton();
private JButton btn3 = new JButton();
private JButton btn4 = new JButton();
private JButton btn5 = new JButton();
private JButton btn6 = new JButton();
private JButton btn7 = new JButton();
private JButton btn8 = new JButton();
private JButton btn9 = new JButton();
private JLabel Player = new JLabel("Player");
private JLabel Player1 = new JLabel();
private JLabel Player2 = new JLabel();
private JLabel Score = new JLabel("Score");
private JLabel ScoreP1 = new JLabel("0");
private JLabel ScoreP2 = new JLabel("0");
private JFrame frame = new JFrame();
private JPanel T_Panel = new JPanel();
private JPanel D_Panel = new JPanel();
public TicTacToe() {
Customization();
Player1.setText(JOptionPane.showInputDialog(null, "Player 1", "Player Name", JOptionPane.OK_OPTION));
Player2.setText(JOptionPane.showInputDialog(null, "Player 2", "Player Name", JOptionPane.OK_OPTION));
if (Player1.getText().equalsIgnoreCase("")) {
Player1.setText("Player 1");
}
if (Player2.getText().equalsIgnoreCase("")) {
Player2.setText("Player 2");
}
Player1.setForeground(Color.green);
Player2.setForeground(Color.green);
Player1.setEnabled(true);
Player2.setEnabled(false);
}
p
public void Customization() {
frame.setTitle("Tic Tac Toe");
frame.setSize(700, 550);
frame.setLayout(new BorderLayout(0, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(T_Panel);
frame.add(D_Panel, BorderLayout.SOUTH);
T_Panel.setLayout(new GridLayout(3, 2));
T_Panel.add(Player);
T_Panel.add(Score);
T_Panel.add(Player1);
T_Panel.add(ScoreP1);
T_Panel.add(Player2);
T_Panel.add(ScoreP2);
D_Panel.setPreferredSize(new Dimension(frame.getWidth(), 450));
D_Panel.setLayout(new GridLayout(3, 3, 2, 2));
D_Panel.add(btn1); D_Panel.add(btn2); D_Panel.add(btn3);
D_Panel.add(btn4); D_Panel.add(btn5); D_Panel.add(btn6);
D_Panel.add(btn7); D_Panel.add(btn8); D_Panel.add(btn9);
btn1.setForeground(Color.LIGHT_GRAY);
btn1.setFont(new Font("Tahoma", Font.BOLD, 90));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn1.setText("X");
btn1.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn1.setText("O");
btn1.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn2.setForeground(Color.LIGHT_GRAY);
btn2.setFont(new Font("Tahoma", Font.BOLD, 90));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn2.setText("X");
btn2.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn2.setText("O");
btn2.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn3.setForeground(Color.LIGHT_GRAY);
btn3.setFont(new Font("Tahoma", Font.BOLD, 90));
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn3.setText("X");
btn3.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn3.setText("O");
btn3.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn4.setForeground(Color.LIGHT_GRAY);
btn4.setFont(new Font("Tahoma", Font.BOLD, 90));
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn4.setText("X");
btn4.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn4.setText("O");
btn4.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn5.setForeground(Color.LIGHT_GRAY);
btn5.setFont(new Font("Tahoma", Font.BOLD, 90));
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn5.setText("X");
btn5.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn5.setText("O");
btn5.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn6.setForeground(Color.LIGHT_GRAY);
btn6.setFont(new Font("Tahoma", Font.BOLD, 90));
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn6.setText("X");
btn6.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn6.setText("O");
btn6.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn7.setForeground(Color.LIGHT_GRAY);
btn7.setFont(new Font("Tahoma", Font.BOLD, 90));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn7.setText("X");
btn7.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn7.setText("O");
btn7.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn8.setForeground(Color.LIGHT_GRAY);
btn8.setFont(new Font("Tahoma", Font.BOLD, 90));
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn8.setText("X");
btn8.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn8.setText("O");
btn8.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
btn9.setForeground(Color.LIGHT_GRAY);
btn9.setFont(new Font("Tahoma", Font.BOLD, 90));
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn9.setText("X");
btn9.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn9.setText("O");
btn9.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});
}
public void Scoring() {
if ( btn1.getText() == "X" && btn2.getText() == "X" && btn3.getText() == "X" ||
btn4.getText() == "X" && btn5.getText() == "X" && btn6.getText() == "X" ||
btn7.getText() == "X" && btn8.getText() == "X" && btn9.getText() == "X" ||
btn1.getText() == "X" && btn4.getText() == "X" && btn7.getText() == "X" ||
btn2.getText() == "X" && btn5.getText() == "X" && btn8.getText() == "X" ||
btn3.getText() == "X" && btn6.getText() == "X" && btn9.getText() == "X" ||
btn1.getText() == "X" && btn5.getText() == "X" && btn9.getText() == "X" ||
btn3.getText() == "X" && btn5.getText() == "X" && btn7.getText() == "X") {
xCount++; ScoreP1.setText(Integer.toString(xCount));
JOptionPane.showMessageDialog(null, "Player 1 wins", "TIC TAC TOE" ,JOptionPane.INFORMATION_MESSAGE);
Reset();
} else
if ( btn1.getText() == "O" && btn2.getText() == "O" && btn3.getText() == "O" ||
btn4.getText() == "O" && btn5.getText() == "O" && btn6.getText() == "O" ||
btn7.getText() == "O" && btn8.getText() == "O" && btn9.getText() == "O" ||
btn1.getText() == "O" && btn4.getText() == "O" && btn7.getText() == "O" ||
btn2.getText() == "O" && btn5.getText() == "O" && btn8.getText() == "O" ||
btn3.getText() == "O" && btn6.getText() == "O" && btn9.getText() == "O" ||
btn1.getText() == "O" && btn5.getText() == "O" && btn9.getText() == "O" ||
btn3.getText() == "O" && btn5.getText() == "O" && btn7.getText() == "O") {
oCount++; ScoreP2.setText(Integer.toString(oCount));
JOptionPane.showMessageDialog(null, "Player 2 wins", "TIC TAC TOE" ,JOptionPane.INFORMATION_MESSAGE);
Reset();
}
}
public void Reset() {
btn1.setText(null);
btn2.setText(null);
btn3.setText(null);
btn4.setText(null);
btn5.setText(null);
btn6.setText(null);
btn7.setText(null);
btn8.setText(null);
btn9.setText(null);
}
public static void main(String[] args) {
try {
new TicTacToe().frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}