import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TicTacToe extends Applet implements ActionListener {
Panel[][] panels;
String[][] board;
String[][] used;
Button place;
TextField row;
TextField column;
String current;
public TicTacToe() {
setLayout(new GridLayout(4,3));
panels = new Panel[3][3];
for (int i=0;i<3;i++)
for (int j=0;j<3;j++) {
panels[i][j] = new Panel(null);
panels[i][j].setBackground(Color.white);
add(panels[i][j]);
}
board = new String[3][3];
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
board[i][j] = "";
used = new String[3][3];
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
used[i][j] = "no";
row = new TextField(5);
column = new TextField(5);
add(row);
add(column);
place = new Button("Place");
add(place);
place.addActionListener(this);
current = "x";
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == place) {
int therow = new Integer(row.getText()).intValue();
int thecolumn = new Integer(column.getText()).intValue();
System.out.println(therow + " " + thecolumn);
System.out.println(current);
if (used[therow-1][thecolumn-1].equals("no")) {
if (current.equals("x"))
panels[therow-1][thecolumn-1].setBackground(Color.red);
else
panels[therow-1][thecolumn-1].setBackground(Color.yellow);
board[therow-1][thecolumn-1] = current;
used[therow-1][thecolumn-1] = "yes";
current = current.equals("x") ? "o" : "x";
}
}
}
}
In order to see this program work, right click on the two links below and
save each one into the c:\temp directory.