TicTacToe - JAVA
TicTacToe - JAVA
TicTacToe - JAVA
============================================
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
if(P1.equals("") || P2.equals("")){
JOptionPane.showMessageDialog(null,"Player Name is
not Blank !!!");
System.exit(0);
}
}
public TicTac(){
playersName();
//J-FRAME
final String VERSION = "1.0";
JFrame f=new JFrame("Tic Tac Toe"+VERSION);
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
//////J-PANELS///////
p=new JPanel(null);
p1=new JPanel(null);
p2=new JPanel(null);
p3=new JPanel(null);
p4=new JPanel(null);
p5=new JPanel(null);
p6=new JPanel(null);
p7=new JPanel(null);
p8=new JPanel(null);
p9=new JPanel(null);
//Panels Set Bounds
p.setBounds(0,0,350,350);
p1.setBounds(227,148,43,45);
p2.setBounds(274,148,63,45);
p3.setBounds(341,148,46,45);
p4.setBounds(227,197,43,49);
p5.setBounds(274,197,63,49);
p6.setBounds(341,197,46,49);
p7.setBounds(227,250,43,51);
p8.setBounds(274,250,63,51);
p9.setBounds(341,250,46,51);
////////////BUTTON////////
newGame=new JButton("NEW GAME");
exit=new JButton("EXIT");
////////////LABELS/////////////
l =new JLabel(new ImageIcon("santa.jpg"));
l1=new JLabel(new ImageIcon("x.jpg"));
l2=new JLabel(new ImageIcon("0.jpg"));
imageP1=new JLabel(new ImageIcon("0.jpg"));
imageP2=new JLabel(new ImageIcon("x.jpg"));
labelTurn=new JLabel();
labelTurn.setText(P1+"`s TURN");
won2=new JLabel("Won");
loss2=new JLabel("Loss");
draw2=new JLabel("Draw");
won1=new JLabel("Won");
loss1=new JLabel("Loss");
draw1=new JLabel("Draw");
wonScore1=new JLabel("0");
wonScore2=new JLabel("0");
lossScore1=new JLabel("0");
lossScore2=new JLabel("0");
drawScore1=new JLabel("0");
drawScore2=new JLabel("0");
lPlayer1=new JLabel(P1);
lPlayer2=new JLabel(P2);
l01=new JLabel();
l02=new JLabel();
l03=new JLabel();
l04=new JLabel();
l05=new JLabel();
l06=new JLabel();
l07=new JLabel();
l08=new JLabel();
l09=new JLabel();
labelP1.setBounds(60,100,80,30);
labelP2.setBounds(510,100,80,30);
lPlayer1.setBounds(60,115,90,40);
lPlayer2.setBounds(520,115,90,40);
won1.setBounds(20,200,80,30);
loss1.setBounds(70,200,80,30);
draw1.setBounds(120,200,80,30);
won2.setBounds(480,200,80,30);
loss2.setBounds(530,200,80,30);
draw2.setBounds(580,200,80,30);
wonScore1.setBounds(30,220,80,30);
lossScore1.setBounds(80,220,80,30);
drawScore1.setBounds(130,220,80,30);
wonScore2.setBounds(490,220,80,30);
lossScore2.setBounds(540,220,80,30);
drawScore2.setBounds(590,220,80,30);
labelTurn.setBounds(283,0,150,30);
newGame.setBounds(0,0,100,30);
exit.setBounds(516,0,100,30);
//////////////Color////////////////
labelP1.setForeground(Color.RED);
labelP2.setForeground(Color.green);
lPlayer1.setForeground(Color.RED);
lPlayer2.setForeground(Color.green);
labelP1.setForeground(Color.RED);
labelP2.setForeground((Color.green));
labelTurn.setForeground(Color.WHITE);
won1.setForeground(Color.RED);
won2.setForeground(Color.green);
loss1.setForeground(Color.RED);
loss2.setForeground(Color.green);
draw1.setForeground(Color.RED);
draw2.setForeground(Color.green);
wonScore1.setForeground(Color.WHITE);
wonScore2.setForeground(Color.WHITE);
lossScore1.setForeground(Color.WHITE);
lossScore2.setForeground(Color.WHITE);
drawScore1.setForeground(Color.WHITE);
drawScore2.setForeground(Color.WHITE);
exit.setForeground(Color.DARK_GRAY);
newGame.setForeground(Color.DARK_GRAY);
p1.setBackground(Color.BLACK);
p2.setBackground(Color.BLACK);
p3.setBackground(Color.BLACK);
p4.setBackground(Color.BLACK);
p5.setBackground(Color.BLACK);
p6.setBackground(Color.BLACK);
p7.setBackground(Color.BLACK);
p8.setBackground(Color.BLACK);
p9.setBackground(Color.BLACK);
newGame.addActionListener(this);
exit.addActionListener(this);
}
////////////ACTION LISTENER//////////////
public void actionPerformed(ActionEvent e){
//Object s=e.getSource();
if(e.getSource()==newGame){
l01.setIcon(new ImageIcon(""));
l02.setIcon(new ImageIcon(""));
l03.setIcon(new ImageIcon(""));
l04.setIcon(new ImageIcon(""));
l05.setIcon(new ImageIcon(""));
l06.setIcon(new ImageIcon(""));
l07.setIcon(new ImageIcon(""));
l08.setIcon(new ImageIcon(""));
l09.setIcon(new ImageIcon(""));
p1.remove(l01);
p2.remove(l02);
p3.remove(l03);
p1Won=0;
p1Loss=0;
p1Draw=0;
p2Won=0;
p2Loss=0;
p2Draw=0;
wonScore1.setText(""+p1Won);
wonScore2.setText(""+p2Won);
lossScore1.setText(""+p1Loss);
lossScore2.setText(""+p2Loss);
drawScore1.setText(""+p1Draw);
drawScore2.setText(""+p2Draw);
click=1;
s1=2;s2=3;s3=4;s4=5;s5=6;s6=7;s7=8;s8=9;s9=10;
check=1;
str=P1;
labelTurn.setText(P1+"`s Turn");
}
else if(e.getSource()==exit){
int option = JOptionPane.showConfirmDialog(null,
"Are you sure you want to exit?","Exit Game"
,JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION)
System.exit(0);
}
}
////////////EVENT LISTENER/HANDLER//////////
//First Column
if((evt.getSource()==p1)&&a1==0){
click++;
a1=1;
if(click%2==0){
l01.setIcon(new ImageIcon("x.jpg"));
p1.add(l01);
s1=0;
check=1;
str=P1;
}
else{
l01.setIcon(new ImageIcon("0.jpg"));
p1.add(l01);
s1=1;
check=2;
str=P2;
}
}
//Second Column
else if((evt.getSource()==p2)&&a2==0){
click++;
a2=1;
if(click%2==0){
l02.setIcon(new ImageIcon("x.jpg"));
p2.add(l02);
s2=0;
check=1;
str=P1;
}
else{
l02.setIcon(new ImageIcon("0.jpg"));
p2.add(l02);
s2=1;
check=2;
str=P2;
}
}
//Thrid Column
else if((evt.getSource()==p3)&&a3==0){
click++;
a3=1;
if(click%2==0){
l03.setIcon(new ImageIcon("x.jpg"));
p3.add(l03);
s3=0;
check=1;
str=P1;
}
else{
l03.setIcon(new ImageIcon("0.jpg"));
p3.add(l03);
s3=1;
check=2;
str=P2;
}
}
//Fourth Column
if((evt.getSource()==p4)&&a4==0){
click++;
a4=1;
if(click%2==0){
l04.setIcon(new ImageIcon("x.jpg"));
p4.add(l04);
s4=0;
check=1;
str=P1;
}
else{
l04.setIcon(new ImageIcon("0.jpg"));
p4.add(l04);
s4=1;
check=2;
str=P2;
}
}
//Fifth Column
else if((evt.getSource()==p5)&&a5==0){
click++;
a5=1;
if(click%2==0){
l05.setIcon(new ImageIcon("x.jpg"));
p5.add(l05);
s5=0;
check=1;
str=P1;
}
else{
l05.setIcon(new ImageIcon("0.jpg"));
p5.add(l05);
s5=1;
check=2;
str=P2;
}
}
//Sixth Column
else if((evt.getSource()==p6)&&a6==0){
click++;
a6=1;
if(click%2==0){
l06.setIcon(new ImageIcon("x.jpg"));
p6.add(l06);
s6=0;
check=1;
str=P1;
}
else{
l06.setIcon(new ImageIcon("0.jpg"));
p6.add(l06);
s6=1;
check=2;
str=P2;
}
}
//Seventh Column
if((evt.getSource()==p7)&&a7==0){
click++;
a7=1;
if(click%2==0){
l07.setIcon(new ImageIcon("x.jpg"));
p7.add(l07);
s7=0;
check=1;
str=P1;
}
else{
l07.setIcon(new ImageIcon("0.jpg"));
p7.add(l07);
s7=1;
check=2;
str=P2;
}
}
//Eight Column
else if((evt.getSource()==p8)&&a8==0){
click++;
a8=1;
if(click%2==0){
l08.setIcon(new ImageIcon("x.jpg"));
p8.add(l08);
s8=0;
check=1;
str=P1;
}
else{
l08.setIcon(new ImageIcon("0.jpg"));
p8.add(l08);
s8=1;
check=2;
str=P2;
}
}
//Ninth Column
else if((evt.getSource()==p9)&&a9==0){
click++;
a9=1;
if(click%2==0){
l09.setIcon(new ImageIcon("x.jpg"));
p9.add(l09);
s9=0;
check=1;
str=P1;
}
else{
l09.setIcon(new ImageIcon("0.jpg"));
p9.add(l09);
s9=1;
check=2;
str=P2;
}
}
}
if(s1==1&&s2==1&&s3==1||s1==1&&s4==1&&s7==1||s2==
1&&s5==1&&s8==1||s3==1&&s6==1&&s9==1||s4==1&&s5==1&
&s6==1||s7==1&&s8==1&&s9==1||s7==1&&s5==1&&s3==1||s1
==1&&s5==1&&s9==1){
JOptionPane.showMessageDialog(null,P1+"
CONGRATULATIONS U WON");
p1Won++;
wonScore1.setText(""+p1Won);
p2Loss++;
lossScore2.setText(""+p2Loss);
l01.setIcon(new ImageIcon(""));
l02.setIcon(new ImageIcon(""));
l03.setIcon(new ImageIcon(""));
l04.setIcon(new ImageIcon(""));
l05.setIcon(new ImageIcon(""));
l06.setIcon(new ImageIcon(""));
l07.setIcon(new ImageIcon(""));
l08.setIcon(new ImageIcon(""));
l09.setIcon(new ImageIcon(""));
p1.remove(l01);
p2.remove(l02);
p3.remove(l03);
click=1;
s1=2;s2=3;s3=4;s4=5;s5=6;s6=7;s7=8;s8=9;s9=10;
check=1;
str=P1;
labelTurn.setText(P1+"`s Turn");
}
if(s1==0&&s2==0&&s3==0||s1==0&&s4==0&&s7==0||s2==
0&&s5==0&&s8==0||s3==0&&s6==0&&s9==0||s4==0&&s5==0&
&s6==0||s7==0&&s8==0&&s9==0||s7==0&&s5==0&&s3==0||s1
==0&&s5==0&&s9==0){
JOptionPane.showMessageDialog(null,P2+"
CONGRATULATIONS U WON");
p2Won++;
wonScore2.setText(""+p2Won);
p1Loss++;
lossScore1.setText(""+p1Loss);
l01.setIcon(new ImageIcon(""));
l02.setIcon(new ImageIcon(""));
l03.setIcon(new ImageIcon(""));
l04.setIcon(new ImageIcon(""));
l05.setIcon(new ImageIcon(""));
l06.setIcon(new ImageIcon(""));
l07.setIcon(new ImageIcon(""));
l08.setIcon(new ImageIcon(""));
l09.setIcon(new ImageIcon(""));
p1.remove(l01);
p2.remove(l02);
p3.remove(l03);
click=1;
s1=2;s2=3;s3=4;s4=5;s5=6;s6=7;s7=8;s8=9;s9=10;
check=1;
str=P1;
labelTurn.setText(P1+"`s Turn");
}
if(click==9){
JOptionPane.showMessageDialog(null,"DRAW !!!");
p1Draw++;
drawScore1.setText(""+p1Draw);
p2Draw++;
drawScore2.setText(""+p2Draw);
l01.setIcon(new ImageIcon(""));
l02.setIcon(new ImageIcon(""));
l03.setIcon(new ImageIcon(""));
l04.setIcon(new ImageIcon(""));
l05.setIcon(new ImageIcon(""));
l06.setIcon(new ImageIcon(""));
l07.setIcon(new ImageIcon(""));
l08.setIcon(new ImageIcon(""));
l09.setIcon(new ImageIcon(""));
click=1;
s1=2;s2=3;s3=4;s4=5;s5=6;s6=7;s7=8;s8=9;s9=10;
check=1;
str=P1;
labelTurn.setText(P1+"`s Turn");
}
class Main{
public static void main(String P[]){
TicTac t=new TicTac();
}
}
http://www.ravianeducation.blogspot.com
FARHAN: 03008855006