TicTacToe - JAVA

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Tic Tac Toe

============================================

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class TicTac implements MouseListener,ActionListener {


public void playersName(){
P1=JOptionPane.showInputDialog("ENTER PLAYER 1: ");

P2=JOptionPane.showInputDialog("ENTER PLAYER 2: ");

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"));

labelP1=new JLabel("PLAYER 1 ");


labelP2=new JLabel("PLAYER 2 ");

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();

/////Labels Set Bounds///////


l01.setBounds(0,0,45,45);
l02.setBounds(10,0,40,42);
l03.setBounds(0,0,45,45);
l04.setBounds(0,0,40,42);
l05.setBounds(10,0,45,45);
l06.setBounds(0,0,40,42);
l07.setBounds(0,0,45,45);
l08.setBounds(10,0,40,42);
l09.setBounds(0,0,45,45);
l.setBounds(135,0,350,350);
l1.setBounds(0,0,40,42);
l2.setBounds(0,0,40,42);
imageP1.setBounds(70,150,40,42);
imageP2.setBounds(520,150,41,42);

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);

/////Add Labels on Panel/////


p.add(l);
p.add(imageP1);
p.add(imageP2);
p.add(labelP1);
p.add(labelP2);
p.add(lPlayer1);
p.add(lPlayer2);
p.add(won1);
p.add(won2);
p.add(loss1);
p.add(loss2);
p.add(draw1);
p.add(draw2);
p.add(wonScore1);
p.add(wonScore2);
p.add(lossScore1);
p.add(lossScore2);
p.add(drawScore1);
p.add(drawScore2);
p.add(newGame);
p.add(exit);
p.add(labelTurn);

/////Add Panels on Panel/////


p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
p.add(p5);
p.add(p6);
p.add(p7);
p.add(p8);
p.add(p9);
p.setBackground(Color.black);
f.add(p,BorderLayout.CENTER);
f.setBounds(250,250,620,350);
f.setVisible(true);
str=P1;
////////REGISTRATION OF EVENT//////////
p1.addMouseListener(this);
p2.addMouseListener(this);
p3.addMouseListener(this);
p4.addMouseListener(this);
p5.addMouseListener(this);
p6.addMouseListener(this);
p7.addMouseListener(this);
p8.addMouseListener(this);
p9.addMouseListener(this);

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//////////

public void mouseClicked(MouseEvent evt){


{
if(str.equals(P1)&&check==1)
labelTurn.setText(P2+"`s TURN");
else if(str.equals(P2)&&check==2)
labelTurn.setText(P1+"`s TURN");

//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");
}

public void mouseEntered(MouseEvent evt){


if(evt.getSource()==p1 && (s1!=1&&s1!=0))
p1.setBackground(Color.red);
else if(evt.getSource()==p2&& (s2!=1&&s2!=0))
p2.setBackground(Color.BLUE);
else if(evt.getSource()==p3&& (s3!=1&&s3!=0))
p3.setBackground(Color.yellow);
else if(evt.getSource()==p4&& (s4!=1&&s4!=0))
p4.setBackground(Color.ORANGE);
else if(evt.getSource()==p5&& (s5!=1&&s5!=0))
p5.setBackground(Color.cyan);
else if(evt.getSource()==p6&& (s6!=1&&s6!=0))
p6.setBackground(Color.LIGHT_GRAY);
else if(evt.getSource()==p7&& (s7!=1&&s7!=0))
p7.setBackground(Color.green);
else if(evt.getSource()==p8&& (s8!=1&&s8!=0))
p8.setBackground(Color.PINK);
else if(evt.getSource()==p9&& (s9!=1&&s9!=0))
p9.setBackground(Color.MAGENTA);
}

public void mouseExited(MouseEvent evt){


if(evt.getSource()==p1)
p1.setBackground(Color.WHITE);
else if(evt.getSource()==p2)
p2.setBackground(Color.WHITE);
else if(evt.getSource()==p3)
p3.setBackground(Color.WHITE);
else if(evt.getSource()==p4)
p4.setBackground(Color.WHITE);
else if(evt.getSource()==p5)
p5.setBackground(Color.WHITE);
else if(evt.getSource()==p6)
p6.setBackground(Color.WHITE);
else if(evt.getSource()==p7)
p7.setBackground(Color.WHITE);
else if(evt.getSource()==p8)
p8.setBackground(Color.WHITE);
else if(evt.getSource()==p9)
p9.setBackground(Color.WHITE);
}

public void mousePressed(MouseEvent evt){}


public void mouseReleased(MouseEvent evt){}
private JLabel
label,labelP1,labelP2,labelTurn,l,l1,l2,lPlayer1,lPlayer2,imageP1,ima
geP2,l01,l02,l03,l04,l05,l06,l07,l08,l09;
private JLabel
won1,won2,loss1,loss2,draw1,draw2,wonScore1,wonScore2,lossSco
re1,lossScore2,drawScore1,drawScore2;
private JPanel p,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10;
private String P1,P2,str;
private JButton newGame,exit;
private int
p1Won=0,p2Won=0,p1Loss=0,p2Loss=0,p1Draw=0,p2Draw=0;
private int click=0,check=1;
private int
a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0,a8=0,a9=0;
private int
s1=2,s2=3,s3=4,s4=5,s5=6,s6=7,s7=8,s8=9,s9=10;

class Main{
public static void main(String P[]){
TicTac t=new TicTac();
}
}

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006

You might also like