import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
/**
*
* @author Tarequzzaman Khan
*/
public class tictactoi extends JFrame {
int mat[][] = new int[4][4];
int count = 0, sum = 0;
int press1 = 0, press2 = 0, press3 = 0, press4 = 0, press5 = 0, press6 = 0, press7 = 0, press8 = 0, press9 = 0;
JPanel p1, p2, p3;
JButton button1, button2, button3, button4, button5, button6, button7, button8, button9;
JTextArea area;
JButton reset, cradit;
public tictactoi() {
super("Tic Tac Toe");
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
mat[i][j] = 0;
}
}
p1 = new JPanel();
p1.setLayout(new GridLayout(3, 3));
p1.setSize(100, 100);
button1 = new JButton();
button1.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button1);
button2 = new JButton();
button2.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button2);
button3 = new JButton();
button3.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button3);
button4 = new JButton();
button4.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button4);
button5 = new JButton();
button5.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button5);
button6 = new JButton();
button6.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button6);
button7 = new JButton();
button7.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button7);
button8 = new JButton();
button8.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button8);
button9 = new JButton();
button9.setFont(new Font("Times New Roman", Font.BOLD, 26));
p1.add(button9);
add(p1, BorderLayout.CENTER);
p2 = new JPanel();
;
area = new JTextArea(3, 25);
area.setFont(new Font("Times New Roman", Font.BOLD, 12));
area.setEditable(false);
p2.add(area);
p3 = new JPanel();
reset = new JButton("RESET");
cradit = new JButton("CRADIT");
p3.add(cradit);
p3.add(reset);
add(p3, BorderLayout.SOUTH);
add(p2, BorderLayout.NORTH);
button1.setBackground(new Color(240, 240, 240));
button2.setBackground(new Color(240, 240, 240));
button3.setBackground(new Color(240, 240, 240));
button4.setBackground(new Color(240, 240, 240));
button5.setBackground(new Color(240, 240, 240));
button6.setBackground(new Color(240, 240, 240));
button7.setBackground(new Color(240, 240, 240));
button8.setBackground(new Color(240, 240, 240));
button9.setBackground(new Color(240, 240, 240));
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
area.setEditable(false);
if (press1 == 0 && count % 2 == 0) {
count++;
press1 = 1;
button1.setText("X");
mat[1][1] = 1;
button1.setBackground(Color.PINK);
} else if (press1 == 0 && count % 2 == 1) {
count++;
press1 = 1;
button1.setText("0");
mat[1][1] = 2;
button1.setBackground(Color.yellow);
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
area.setEditable(false);
if (press2 == 0 && count % 2 == 0) {
count++;
press2 = 1;
button2.setText("X");
mat[1][2] = 1;
button2.setBackground(Color.PINK);
} else if (press2 == 0 && count % 2 == 1) {
count++;
press2 = 1;
button2.setText("0");
mat[1][2] = 2;
button2.setBackground(Color.yellow);
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
area.setEditable(false);
if (press3 == 0 && count % 2 == 0) {
count++;
press3 = 1;
button3.setText("X");
mat[1][3] = 1;
button3.setBackground(Color.PINK);
} else if (press3 == 0 && count % 2 == 1) {
count++;
press3 = 1;
button3.setText("0");
mat[1][3] = 2;
button3.setBackground(Color.yellow);
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if (press4 == 0 && count % 2 == 0) {
press4 = 1;
count++;
button4.setText("X");
button4.setBackground(Color.PINK);
mat[2][1] = 1;
} else if (press4 == 0 && count % 2 == 1) {
press4 = 1;
count++;
button4.setText("0");
button4.setBackground(Color.YELLOW);
mat[2][1] = 2;
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if (press5 == 0 && count % 2 == 0) {
press5 = 1;
count++;
button5.setText("X");
button5.setBackground(Color.PINK);
mat[2][2] = 1;
} else if (press5 == 0 && count % 2 == 1) {
press5 = 1;
count++;
button5.setText("0");
button5.setBackground(Color.YELLOW);
mat[2][2] = 2;
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if (press6 == 0 && count % 2 == 0) {
press6 = 1;
count++;
button6.setText("X");
button6.setBackground(Color.PINK);
mat[2][3] = 1;
} else if (press6 == 0 && count % 2 == 1) {
press6 = 1;
count++;
button6.setText("0");
button6.setBackground(Color.YELLOW);
mat[2][3] = 2;
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if (press7 == 0 && count % 2 == 0) {
press7 = 1;
count++;
button7.setText("X");
button7.setBackground(Color.PINK);
mat[3][1] = 1;
} else if (press7 == 0 && count % 2 == 1) {
press7 = 1;
count++;
button7.setText("0");
button7.setBackground(Color.YELLOW);
mat[3][1] = 2;
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if (press8 == 0 && count % 2 == 0) {
press8 = 1;
count++;
button8.setText("X");
button8.setBackground(Color.PINK);
mat[3][2] = 1;
} else if (press8 == 0 && count % 2 == 1) {
press8 = 1;
count++;
button8.setText("0");
button8.setBackground(Color.YELLOW);
mat[3][2] = 2;
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
if (press9 == 0 && count % 2 == 0) {
press9 = 1;
count++;
button9.setText("X");
button9.setBackground(Color.PINK);
mat[3][3] = 1;
} else if (press9 == 0 && count % 2 == 1) {
press9 = 1;
count++;
button9.setText("0");
button9.setBackground(Color.YELLOW);
mat[3][3] = 2;
}
sum = check(mat);
if (sum == 1) {
area.setText("Frist parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (sum == 2) {
area.setText("Second parson win");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
} else if (count == 9) {
area.setText("Draw");
area.append("\nPlease Press reset Button to Play Another Match");
press1 = press2 = press3 = press4 = press5 = press6 = press7 = press8 = press9 = 1;
}
} catch (Exception e) {
}
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
area.setEditable(false);
sum = 0;
count = 0;
press1 = 0;
press2 = 0;
press3 = 0;
press4 = 0;
press5 = 0;
press6 = 0;
press7 = 0;
press8 = 0;
press9 = 0;
area.setText("");
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
button1.setBackground(new Color(240, 240, 240));
button2.setBackground(new Color(240, 240, 240));
button3.setBackground(new Color(240, 240, 240));
button4.setBackground(new Color(240, 240, 240));
button5.setBackground(new Color(240, 240, 240));
button6.setBackground(new Color(240, 240, 240));
button7.setBackground(new Color(240, 240, 240));
button8.setBackground(new Color(240, 240, 240));
button9.setBackground(new Color(240, 240, 240));
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
mat[i][j] = 0;
}
}
} catch (Exception e) {
}
}
});
cradit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
area.setText("Tarequzzaman khan\nSylhet Engineering College");
} catch (Exception e) {
}
}
});
}
int check(int max[][]) {
if (max[1][1] == 1 && max[1][2] == 1 && max[1][3] == 1) {
return 1;
} else if (max[1][3] == 1 && max[2][3] == 1 && max[3][3] == 1) {
return 1;
} else if (max[1][1] == 1 && max[2][1] == 1 && max[3][1] == 1) {
return 1;
} else if (max[3][1] == 1 && max[3][2] == 1 && max[3][3] == 1) {
return 1;
} else if (max[2][1] == 1 && max[2][2] == 1 && max[2][3] == 1) {
return 1;
} else if (max[1][1] == 1 && max[2][2] == 1 && max[3][3] == 1) {
return 1;
} else if (max[1][3] == 1 && max[2][2] == 1 && max[3][1] == 1) {
return 1;
} else if (max[1][2] == 1 && max[2][2] == 1 && max[3][2] == 1) {
return 1;
} else if (max[1][1] == 2 && max[1][2] == 2 && max[1][3] == 2) {
return 2;
} else if (max[1][3] == 2 && max[2][3] == 2 && max[3][3] == 2) {
return 2;
} else if (max[1][1] == 2 && max[2][1] == 2 && max[3][1] == 2) {
return 2;
} else if (max[3][1] == 2 && max[3][2] == 2 && max[3][3] == 2) {
return 2;
} else if (max[2][1] == 2 && max[2][2] == 2 && max[2][3] == 2) {
return 2;
} else if (max[1][1] == 2 && max[2][2] == 2 && max[3][3] == 2) {
return 2;
} else if (max[1][3] == 2 && max[2][2] == 2 && max[3][1] == 2) {
return 2;
} else if (max[1][2] == 2 && max[2][2] == 2 && max[3][2] == 2) {
return 2;
}
return 3;
}
public static void main(String args[]) {
tictactoi box = new tictactoi();
box.setDefaultCloseOperation(box.EXIT_ON_CLOSE);
box.setResizable(false);
box.setSize(265, 265);
box.setVisible(true);
}
}