TicTacToe



Source Code:
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;
public class tictactoe
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new TicTacToePanel());
frame.pack();
frame.setTitle("TicTacToe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class TicTacToePanel extends JPanel
{
private final ImageIcon iconx = new ImageIcon(("x.gif"));//iamge for x
private final ImageIcon icono = new ImageIcon(("o.gif"));//image for o
private final ImageIcon icone = new ImageIcon(("e.gif"));//no image
private ImageIcon turn = iconx;
private JLabel jlblstatus = new JLabel("X's turn");
private JButton jbtnew = new JButton("Newgame");
private JButton[][] buttons = new JButton[3][3];
TicTacToePanel()
{
JPanel jpcells = new JPanel();
jpcells.setLayout(new GridLayout(3,3));
for(int j=0;j<3;j++)
for(int i=0;i<3;i++)
{
JButton jb = new JButton(icone);
jb.setFocusable(false);
jb.addActionListener(new clickListener());
jb.setRolloverIcon(turn);
jb.setBorder(new LineBorder(Color.black));
jpcells.add(jb);
buttons[j][i] = jb;
}
JPanel jp = new JPanel(new BorderLayout());
jp.add(jlblstatus, BorderLayout.CENTER);
jp.add(jbtnew, BorderLayout.EAST);
jbtnew.setFocusable(false);
jbtnew.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
start();
}
});
setLayout(new BorderLayout());
add(jpcells,BorderLayout.CENTER);
add(jp, BorderLayout.SOUTH);
}
public void start()
{
turn=iconx;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
buttons[i][j].setRolloverIcon(turn);
buttons[i][j].setIcon(icone);
}
jlblstatus.setText("X's turn");
}
public void setRolloverIcons()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(buttons[i][j].getIcon()==icone)
buttons[i][j].setRolloverIcon(turn);
}
}
public boolean checkWin()
{
for(int i=0;i<3;i++)
{
if((buttons[i][0].getIcon() == buttons[i][1].getIcon()) && (buttons[i][1].getIcon() == buttons[i][2].getIcon()) && buttons[i][0].getIcon()!=icone)
return true;
if((buttons[0][i].getIcon() == buttons[1][i].getIcon()) && (buttons[1][i].getIcon() == buttons[2][i].getIcon()) && buttons[0][i].getIcon()!=icone)
return true;
}
if((buttons[0][0].getIcon() == buttons[1][1].getIcon()) && (buttons[1][1].getIcon() == buttons[2][2].getIcon()) && buttons[1][1].getIcon()!=icone)
return true;
if((buttons[2][0].getIcon() == buttons[1][1].getIcon()) && (buttons[1][1].getIcon() == buttons[2][0].getIcon()) && buttons[1][1].getIcon()!=icone)
return true;
return false;
}
public boolean checkDraw()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(buttons[i][j].getIcon()==icone)
return false;
}
return true;
}
class clickListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae)
{
JButton temp = (JButton)ae.getSource();
if(temp.getIcon() == icone && !checkWin())
{
temp.setIcon(turn);
temp.setRolloverIcon(turn);
jlblstatus.setText((turn==iconx?'O':'X')+"'s turn");
turn = turn == iconx ? icono : iconx;
setRolloverIcons();
}
if(checkWin())
{
jlblstatus.setText((turn==iconx?'O':'X')+" Wins..! Gameover");
turn=icone;
setRolloverIcons();
}
else if(checkDraw())
jlblstatus.setText("Match Draw..! Gameover");
}
}
public Dimension getPreferredSize()
{
return new Dimension(210,175);
}
}
Comments
Post a Comment