A simple way to make an interactive multiplayer tic tac toe using swings

Posted on September 24, 2010

0


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* GameEnv.java
*
* Created on 22 Sep, 2010, 4:10:51 PM
*/

/**
*
* @author r-themaster
*/
public class GameEnv extends javax.swing.JFrame {

/** Creates new form GameEnv */
public GameEnv() {
initComponents();
player=1;
put=”   “;
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings(“unchecked”)
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {

b1 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
b4 = new javax.swing.JButton();
b5 = new javax.swing.JButton();
b6 = new javax.swing.JButton();
b7 = new javax.swing.JButton();
b8 = new javax.swing.JButton();
b9 = new javax.swing.JButton();
l1 = new javax.swing.JLabel();
l2 = new javax.swing.JLabel();
b2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});

b1.setText(“jButton1”);
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});

b3.setText(“jButton3”);
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});

b4.setText(“jButton4”);
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});

b5.setText(“jButton5”);
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});

b6.setText(“jButton6”);
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});

b7.setText(“jButton7”);
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});

b8.setText(“jButton8”);
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});

b9.setText(“jButton9”);
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});

l1.setFont(new java.awt.Font(“DejaVu Sans”, 1, 24)); // NOI18N
l1.setText(“Result Awaited…..”);

l2.setFont(new java.awt.Font(“DejaVu Sans”, 2, 24)); // NOI18N
l2.setText(“jLabel1”);

b2.setText(“jButton1″);
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed1(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(180, 180, 180)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(b7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b9))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b1)
.addComponent(b4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b2)
.addComponent(b5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b6)
.addComponent(b3))))
.addContainerGap(162, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(92, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(l2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
.addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(78, 78, 78))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(b3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(73, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void setStatus()
{
if(player==1)
put=”x”;
else
put=”o”;
}
private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b8.setText(put);
b8.setEnabled(false);
check();
}

private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b7.setText(put);
b7.setEnabled(false);
check();
}

private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b6.setText(put);
b6.setEnabled(false);
check();
}

private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b5.setText(put);
b5.setEnabled(false);
check();
}

private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b4.setText(put);
b4.setEnabled(false);
check();
}

private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b3.setText(put);
b3.setEnabled(false);
check();
}

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b1.setText(put);
b1.setEnabled(false);
check();
}

private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b9.setText(put);
b9.setEnabled(false);
check();
}

private void b2ActionPerformed1(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setStatus();
b2.setText(put);
b2.setEnabled(false);
check();

}

private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
// setStatus();
check();
b1.setText(put);
b2.setText(put);

b3.setText(put);
b4.setText(put);
b5.setText(put);
b6.setText(put);
b7.setText(put);
b8.setText(put);
b9.setText(put);

}

public  static void disableAll()
{

b1.setEnabled(false);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
b5.setEnabled(false);
b6.setEnabled(false);
b7.setEnabled(false);
b8.setEnabled(false);
b9.setEnabled(false);

}//end od disable all
public static  void check()
{

if(player==1)
l2.setText(“Player 1 play”);
else
l2.setText(“Player 2 play”);
if(player==1)
player=2;
else
player=1;

String s[]=new String[9];
s[0]=b1.getText();
s[1]=b2.getText();
s[2]=b3.getText();
s[3]=b4.getText();
s[4]=b5.getText();
s[5]=b6.getText();
s[6]=b7.getText();
s[7]=b8.getText();
s[8]=b9.getText();

// b2.setEnabled(true);
int c=0;
for(int i=0;i<9;i++)
{
System.out.println(s[i]);
if(i==0 || i==3 || i==6)
{
if((s[i].equalsIgnoreCase(“x”)&&s[i+1].equalsIgnoreCase(“x”)&&s[i+2].equalsIgnoreCase(“x”)) || (s[i].equalsIgnoreCase(“o”)&&s[i+1].equalsIgnoreCase(“o”)&&s[i+2].equalsIgnoreCase(“o”)))
{
l1.setText(” The winner is player “+player);
disableAll();
}
}

if(i==0||i==1||i==2)
if((s[i].equalsIgnoreCase(“x”)&&s[i+3].equalsIgnoreCase(“x”)&&s[i+6].equalsIgnoreCase(“x”)) || (s[i].equalsIgnoreCase(“o”)&&s[i+3].equalsIgnoreCase(“o”)&&s[i+6].equalsIgnoreCase(“o”)))
{
l1.setText(” The winner is player “+player);
disableAll();
}

if((s[0].equalsIgnoreCase(“x”)&&s[4].equalsIgnoreCase(“x”)&&s[8].equalsIgnoreCase(“x”)) || (s[0].equalsIgnoreCase(“o”)&&s[4].equalsIgnoreCase(“o”)&&s[8].equalsIgnoreCase(“o”)))
{
l1.setText(” The winner is player “+player);
disableAll();
}
if((s[2].equalsIgnoreCase(“x”)&&s[4].equalsIgnoreCase(“x”)&&s[6].equalsIgnoreCase(“x”)) || (s[2].equalsIgnoreCase(“o”)&&s[4].equalsIgnoreCase(“o”)&&s[6].equalsIgnoreCase(“o”)))
{
l1.setText(” The winner is player “+player);
disableAll();
}

}

}//end of check

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GameEnv().setVisible(true);

}
});

}

// Variables declaration – do not modify
private static javax.swing.JButton b1;
private static javax.swing.JButton b2;
private static javax.swing.JButton b3;
private static javax.swing.JButton b4;
private static javax.swing.JButton b5;
private static javax.swing.JButton b6;
private static javax.swing.JButton b7;
private static javax.swing.JButton b8;
private static javax.swing.JButton b9;
private static javax.swing.JLabel l1;
private static javax.swing.JLabel l2;
// End of variables declaration

private static int player;
private static String put;

}

Advertisements
Posted in: Netbeans