Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class TicTacToe implements ActionListener
{
//Instance Variables
private int[][] winCombinations = new int[][] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
{0, 4, 8}, {2, 4, 6} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;
private static int startCount =0;
JMenuBar menu = new JMenuBar();
JMenuItem newGame = new JMenuItem("New Game"),
instr = new JMenuItem("Instructions"),
exit = new JMenuItem("Exit"),
name = new JMenuItem("Change Name");
static String x = "X";
static String y = "Y";
 
public TicTacToe()
{
//Create Window
window.setSize(370,370);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
 
//Add Buttons To The Window
for(int i=0; i<=8; i++)
{
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}
 
menu.add(newGame);
menu.add(name);
menu.add(instr);
menu.add(exit);
 
name.addActionListener(this);
newGame.addActionListener(this);
exit.addActionListener(this);
instr.addActionListener(this);
window.setJMenuBar(menu);
//Make The Window Visible
window.setVisible(true);
}
 
public void setName()
{
x = JOptionPane.showInputDialog(null, "Enter Name of player X: ", "", 1);
y = JOptionPane.showInputDialog(null, "Enter Name of player O: ", "", 1);
if(x==null)
{
x = "X";
}
if(y==null)
{
y = "O";
}
if(x.length()==0)
{
x = "X";
}
if(y.length()==0)
{
y = "O";
}
JOptionPane.showMessageDialog(null, "Your names have been set\nTo change your names click on the Change name button in the menu bar","Name Changed!!!",JOptionPane.INFORMATION_MESSAGE);
}
 
/**
When an object is clicked, perform an action.
@param a action event object
*/
public void actionPerformed(ActionEvent a)
{
Object source = a.getSource();
//System.out.println(str1+"\t"+str2);

if(source == newGame)
{
int answer = JOptionPane.showConfirmDialog(null, "Your current game will not be saved...\nContinue Anyways??", "Do you want to start a new game?", JOptionPane.YES_NO_OPTION);
 
if (answer == JOptionPane.YES_OPTION)
{
this.clearIt();
}
 
}
else if(source == name)
{
this.setName();
 
}
else if(source == instr)
{
JOptionPane.showMessageDialog(null, "Your goal is to be the first player to get 3 X's or O's in a row. (horizontally, diagonally, or vertically)","Instructions",JOptionPane.INFORMATION_MESSAGE);
}
else if(source == exit)
{
int answer = JOptionPane.showConfirmDialog(null, "EXIT", "Are You sure you want to exit??", JOptionPane.YES_NO_OPTION);
 
if (answer == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Thank you " + x+ " and " + y + " for playing");
System.exit(0);
}
}
else
{
count++;
 
/*Calculate whose turn it is*/
if(count % 2 == 0)
{
letter = "O";
}
else
{
letter = "X";
}
 
/*Write the letter to the button and deactivate it*/
JButton pressedButton = (JButton)source;
pressedButton.setText(letter);
pressedButton.setEnabled(false);
 
/*Determine who won*/
for(int i=0; i<=7; i++)
{
if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
buttons[winCombinations[i][0]].getText() != "")
{
win = true;
}
}
 
/*Show a dialog when game is over*/
if(win == true)
{
if(letter.equals("X"))
letter = x;
else
letter = y;
JOptionPane.showMessageDialog(null, letter + " wins the game!");
int answer = JOptionPane.showConfirmDialog(null, "Start", "Do you want to start a new game", JOptionPane.YES_NO_OPTION);
 
if (answer == JOptionPane.YES_OPTION)
{
this.clearIt();
}
else
{
JOptionPane.showMessageDialog(null, "Thank you " + x+ " and " + y + " for playing");
System.exit(0);
}
 
}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "The game was tie!");
int answer = JOptionPane.showConfirmDialog(null, "Start", "Do you want to start a new game", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
{
this.clearIt();
}
else
{
JOptionPane.showMessageDialog(null, "Thank you " + x+ " and " + y + " for playing");
System.exit(0);
}
 
}
}
}
 
public void clearIt()
{
 
window.setVisible(false);
//this.window = null;
this.startIt();
}
 
public void startIt()
{
new TicTacToe();
 
}
 
public static void main(String[] args)
{
 
TicTacToe starter = new TicTacToe();
starter.setName();
}
}
 
[Edit]Code block added[/Edit]
Posted 7-Nov-12 3:11am
Edited 7-Nov-12 3:14am
ProgramFOX109.2K
v2
Comments
ridoy at 7-Nov-12 9:43am
   
so where is your problem?!
Phantom Code at 7-Nov-12 9:45am
   
i want to change the X n O symbol into image..Can or not?
ridoy at 7-Nov-12 10:08am
   
ofcourse can..

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Instead of JButtons you need to create JLabels. Then add the corresponding image to the JLabel. A mouse Listener on the JLabel/Image will capture the mouse event of clicking on it.
 
How to use Icons[^]
 
How to Write a Mouse Listener[^]
 
A bit more OOP would help though.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 319
1 OriginalGriff 280
2 Sergey Alexandrovich Kryukov 250
3 Peter Leow 171
4 Abhinav S 168
0 Sergey Alexandrovich Kryukov 8,944
1 OriginalGriff 5,280
2 Peter Leow 3,985
3 Maciej Los 3,535
4 Abhinav S 3,218


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid