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


public class tictac2b1 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;


public tictac2b1(){
/*Create Window*/
window.setSize(300,300);
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);
}

/*Make The Window Visible*/
window.setVisible(true);
}

/**
When an object is clicked, perform an action.
@param a action event object
*/
public void actionPerformed(ActionEvent a) {

count++;

/*Calculate whose turn it is*/
if(count % 2 == 0){
letter = "1";

} else {
letter = "2";

}

/*Write the letter to the button and deactivate it*/
JButton pressedButton = (JButton)a.getSource();
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){
JOptionPane.showMessageDialog(null, letter + " wins the game!");
System.exit(0);
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
System.exit(0);
}
}

public static void main(String[] args){
tictac2b1 starter = new tictac2b1();
}
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

what have you tried so far, what does it do, what is it supposed to do?
looks to me like your teacher gave you that code and said:
'put an undo and hint option into the following code'

i am new to java, had to look for a code, and modify it using concept we havent covered , i managed to do a few changes to the code, the looks and stuff... hint i have a lil idea how to go about it, but dont know how to implement it
like for the winning condition like " if {0,_,2} 1st &3rd location are X, and its X's turn, light up the square for the 2nd location || if {0,_,2} 1st &3rd location are O, and its O's turn, light up the square for the 2nd location"....have 3 of these condition for each winning combination.
for the first winning combination {_,1,2} , {0,_,2} , {0,1,_} where the square relating to the "_" is light up if the other square value's are both X's or both O's.
for the first winning combination {_,4,5} , {3,_,5} , {3,4,_}, ect...

That explanation makes it more confusing, if anything. What we need in order to help you:

1. Explain what problem you are having.
2. A minimal code sample demonstrating the problem.
3. An explanation of what the code sample is supposed to be doing.
4. An explanation of what the code is currently doing.

As far as I can tell, you have given us none of that information.

This article has been dead for over six months. Start a new discussion instead.