Im looking for the simpliest TTT source code, 3x3 matrix...Human vs Human. If anyone have it, please show me the script.
Created Jan 18, 2006
Nils Christian Svihus Here is a TicTacToe applet/application hybrid.
It runs as an applet in a browser and as an application when launched from the command prompt.
It runs as an applet in a browser and as an application when launched from the command prompt.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A tictactoe button
*/
class TTTBtn extends JButton {
private int btnNo=-1;
private boolean hit=false;
private int value=-1;
public TTTBtn (int btnNo) {
super();
this.btnNo=btnNo;
}
public void setHit (boolean p1p2) {
super.setText(p1p2 ? "X" : "O");
value= p1p2 ? 1 : 4;
}
public int getValue() {
return value;
}
public boolean isHit () {
return value > 0;
}
public void reset() {
setText("");
value=-1;
}
public int getButtonNo () {
return btnNo;
}
}
/**
* A tictactoe applet
*/
public class TicTacToe
extends JApplet
implements ActionListener {
JLabel messagesLabel = new JLabel("X goes first");
JPanel displayPanel = new JPanel(new GridLayout(3, 3));
private TTTBtn[] bb = new TTTBtn[9];
private int [][] winLine={
{0,1,2}, // horizontal
{3,4,5},
{6,7,8},
{0,3,6}, // vertical
{1,4,7},
{2,5,8},
{0,4,8}, // diagonal
{6,4,2}
};
private JButton resetBtn=new JButton("Reset");
private boolean player1 = true;
public void init() {
messagesLabel.setOpaque(true);
messagesLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(messagesLabel, BorderLayout.NORTH);
getContentPane().add(resetBtn, BorderLayout.SOUTH);
getContentPane().add(displayPanel, BorderLayout.CENTER);
resetBtn.addActionListener(this);
for (int i = 0; i < 9; i++) {
bb[i] = new TTTBtn(i);
bb[i].addActionListener(this);
displayPanel.add(bb[i]);
}
}
public int getWinner() {
for (int i=0; i<winLine.length; i++) {
int ret = lineWin(winLine[i]);
if (ret > 0) return ret;
}
return -1;
}
private int lineWin(int [] pos) {
int sum=
bb[pos[0]].getValue()+
bb[pos[1]].getValue()+
bb[pos[2]].getValue();
if (sum==3) return 1;
if (sum==12) return 2;
return -1;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetBtn) {
resetGame();
}
else if (e.getSource()instanceof TTTBtn) {
TTTBtn btn = (TTTBtn) e.getSource();
if (!btn.isHit()) {
if (haveWinner(btn)) {
enableButtons(false);
}
}
}
}
private void enableButtons(boolean enabled) {
for (int i=0; i<bb.length; i++) {
bb[i].setEnabled(enabled);
}
}
private boolean haveWinner(TTTBtn btn) {
bb[btn.getButtonNo()].setHit(player1);
int winner = getWinner();
if (winner > 0) {
messagesLabel.setBackground(Color.green);
messagesLabel.setText("Player " + winner + " wins");
return true;
}
else {
player1 = !player1;
return false;
}
}
private void resetGame() {
for (int i=0; i<bb.length; i++) {
bb[i].reset();
}
player1=true;
messagesLabel.setBackground(Color.lightGray);
messagesLabel.setText("X goes first");
enableButtons(true);
}
/**
* Test driver main
*/
public static void main(String[] args) {
JFrame f=new JFrame("Tic Tac Toe");
f.getContentPane().setLayout(new GridLayout());
TicTacToe ttt=new TicTacToe();
f.getContentPane().add(ttt);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
ttt.init();
f.setBounds(100,100,300,270);
f.setVisible(true);
}
}