Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by Tyler ( 18 years ago )
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Font;
public class TicFrame extends JFrame implements ActionListener {
public TicFrame() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
int counter = 0, xWinCount = 0, oWinCount = 0;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetBUT) {
resetGame();
} else {
makeMove((JButton) e.getSource());
}
}
public void resetGame() {
counter = 0;
ttt1_1BUT.setText("");
ttt1_2BUT.setText("");
ttt1_3BUT.setText("");
ttt2_1BUT.setText("");
ttt2_2BUT.setText("");
ttt2_3BUT.setText("");
ttt3_1BUT.setText("");
ttt3_2BUT.setText("");
ttt3_3BUT.setText("");
ttt1_1BUT.setEnabled(true);
ttt1_2BUT.setEnabled(true);
ttt1_3BUT.setEnabled(true);
ttt2_1BUT.setEnabled(true);
ttt2_2BUT.setEnabled(true);
ttt2_3BUT.setEnabled(true);
ttt3_1BUT.setEnabled(true);
ttt3_2BUT.setEnabled(true);
ttt3_3BUT.setEnabled(true);
}
public void makeMove(JButton j) {
if (counter % 2 == 0) {
j.setText("X");
} else {
j.setText("O");
}
j.setEnabled(false);
counter++;
checkWin();
}
public void checkWin() {
String temp1 = ttt1_1BUT.getText();
String temp2 = ttt1_2BUT.getText();
String temp3 = ttt1_3BUT.getText();
String temp4 = ttt2_1BUT.getText();
String temp5 = ttt2_2BUT.getText();
String temp6 = ttt2_3BUT.getText();
String temp7 = ttt3_1BUT.getText();
String temp8 = ttt3_2BUT.getText();
String temp9 = ttt3_3BUT.getText();
if (temp1.equals("X") && temp2.equals("X") && temp3.equals("X")) {
xWins();
return;
}
if (temp4.equals("X") && temp5.equals("X") && temp6.equals("X")) {
xWins();
return;
}
if (temp7.equals("X") && temp8.equals("X") && temp9.equals("X")) {
xWins();
return;
}
if (temp1.equals("X") && temp4.equals("X") && temp7.equals("X")) {
xWins();
return;
}
if (temp2.equals("X") && temp5.equals("X") && temp8.equals("X")) {
xWins();
return;
}
if (temp3.equals("X") && temp6.equals("X") && temp9.equals("X")) {
xWins();
return;
}
if (temp1.equals("X") && temp5.equals("X") && temp9.equals("X")) {
xWins();
return;
}
if (temp3.equals("X") && temp5.equals("X") && temp7.equals("X")) {
xWins();
return;
}
if (temp1.equals("O") && temp2.equals("O") && temp3.equals("O")) {
oWins();
return;
}
if (temp4.equals("O") && temp5.equals("O") && temp6.equals("O")) {
oWins();
return;
}
if (temp7.equals("O") && temp8.equals("O") && temp9.equals("O")) {
oWins();
return;
}
if (temp1.equals("O") && temp4.equals("O") && temp7.equals("O")) {
oWins();
return;
}
if (temp2.equals("O") && temp5.equals("O") && temp8.equals("O")) {
oWins();
return;
}
if (temp3.equals("O") && temp6.equals("O") && temp9.equals("O")) {
oWins();
return;
}
if (temp1.equals("O") && temp5.equals("O") && temp9.equals("O")) {
oWins();
return;
}
if (temp3.equals("O") && temp5.equals("O") && temp7.equals("O")) {
oWins();
return;
}
if (counter == 9) {
JOptionPane.showMessageDialog(null, "Tie Game", "NO WINNER :(",
JOptionPane.INFORMATION_MESSAGE);
disableAll();
}
}
public void xWins() {
JOptionPane.showMessageDialog(null, "X is teh WinRAR", "WINNER!!!",
JOptionPane.INFORMATION_MESSAGE);
disableAll();
xWinCount++;
Integer temp = new Integer(xWinCount);
xWinCountTF.setText(temp.toString());
}
public void oWins() {
JOptionPane.showMessageDialog(null, "O is teh WinRAR", "WINNER!!!",
JOptionPane.INFORMATION_MESSAGE);
disableAll();
oWinCount++;
Integer temp = new Integer(oWinCount);
oWinCountTF.setText(temp.toString());
}
public void disableAll() {
ttt1_1BUT.setEnabled(false);
ttt1_2BUT.setEnabled(false);
ttt1_3BUT.setEnabled(false);
ttt2_1BUT.setEnabled(false);
ttt2_2BUT.setEnabled(false);
ttt2_3BUT.setEnabled(false);
ttt3_1BUT.setEnabled(false);
ttt3_2BUT.setEnabled(false);
ttt3_3BUT.setEnabled(false);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jPanel1.setBounds(new Rectangle(67, 26, 350, 350));
jPanel1.setLayout(gridLayout1);
gridLayout1.setColumns(3);
gridLayout1.setRows(3);
resetBUT.setBounds(new Rectangle(67, 452, 79, 23));
resetBUT.setText("Reset");
ttt1_1BUT.setBackground(SystemColor.desktop);
ttt1_1BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt1_1BUT.setForeground(UIManager.getColor("ToolBar.floatingBackground"));
ttt1_1BUT.setHorizontalTextPosition(SwingConstants.CENTER);
ttt1_2BUT.setBackground(SystemColor.desktop);
ttt1_2BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt1_3BUT.setBackground(SystemColor.desktop);
ttt1_3BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt2_1BUT.setBackground(SystemColor.desktop);
ttt2_1BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt2_2BUT.setBackground(SystemColor.desktop);
ttt2_2BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt2_3BUT.setBackground(SystemColor.desktop);
ttt2_3BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt3_3BUT.setBackground(SystemColor.desktop);
ttt3_3BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt3_1BUT.setBackground(SystemColor.desktop);
ttt3_1BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
ttt3_2BUT.setBackground(SystemColor.desktop);
ttt3_2BUT.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 75));
xWinCountTF.setEditable(false);
xWinCountTF.setText("0");
xWinCountTF.setHorizontalAlignment(SwingConstants.CENTER);
xWinCountTF.setBounds(new Rectangle(77, 422, 28, 19));
oWinCountTF.setEditable(false);
oWinCountTF.setText("0");
oWinCountTF.setHorizontalAlignment(SwingConstants.CENTER);
oWinCountTF.setBounds(new Rectangle(357, 422, 28, 19));
jLabel1.setText("jLabel1");
jLabel1.setBounds(new Rectangle( -42, 288, 34, 14));
xWinsLBL.setToolTipText("");
xWinsLBL.setText("X Wins");
xWinsLBL.setBounds(new Rectangle(73, 396, 39, 19));
oWinsLBL.setToolTipText("");
oWinsLBL.setText("O Wins");
oWinsLBL.setBounds(new Rectangle(352, 396, 39, 19));
this.getContentPane().add(jPanel1, null);
jPanel1.add(ttt1_1BUT);
jPanel1.add(ttt1_2BUT);
jPanel1.add(ttt1_3BUT);
jPanel1.add(ttt2_1BUT);
jPanel1.add(ttt2_2BUT);
jPanel1.add(ttt2_3BUT);
jPanel1.add(ttt3_1BUT);
jPanel1.add(ttt3_2BUT);
jPanel1.add(ttt3_3BUT);
this.getContentPane().add(resetBUT);
this.getContentPane().add(jLabel1);
this.getContentPane().add(oWinCountTF);
this.getContentPane().add(oWinsLBL);
this.getContentPane().add(xWinCountTF);
this.getContentPane().add(xWinsLBL);
resetBUT.addActionListener(this);
ttt1_1BUT.addActionListener(this);
ttt1_2BUT.addActionListener(this);
ttt1_3BUT.addActionListener(this);
ttt2_1BUT.addActionListener(this);
ttt2_2BUT.addActionListener(this);
ttt2_3BUT.addActionListener(this);
ttt3_1BUT.addActionListener(this);
ttt3_2BUT.addActionListener(this);
ttt3_3BUT.addActionListener(this);
}
JPanel jPanel1 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
JButton ttt1_1BUT = new JButton();
JButton ttt1_3BUT = new JButton();
JButton ttt3_3BUT = new JButton();
JButton ttt1_2BUT = new JButton();
JButton ttt2_2BUT = new JButton();
JButton ttt3_2BUT = new JButton();
JButton ttt2_1BUT = new JButton();
JButton ttt3_1BUT = new JButton();
JButton ttt2_3BUT = new JButton();
JButton resetBUT = new JButton();
JButton jButton6 = new JButton();
JTextField xWinCountTF = new JTextField();
JTextField oWinCountTF = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel xWinsLBL = new JLabel();
JLabel oWinsLBL = new JLabel();
}
Revise this Paste