View Single Post
  #13 (permalink)  
Old 10-25-2007, 11:25 AM
The Midnighter The Midnighter is offline
Newbie
 
Join Date: Oct 2007
Posts: 14
Rep Power: 0
The Midnighter is on a distinguished road
Default Hmm...

I'm having trouble creating a working win function, so I googled it.. and here I am.

I know this thread is meretriciously old, but I really need this help lol.
Here's my current code:

Code:
package tictactoe;

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

public class TicTacToeForm extends javax.swing.JFrame implements ActionListener
{
    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 String turn = "";
    private String config[] = new String[10];
    private boolean win = false;
    private int count = 0;
    
    public TicTacToeForm()
    {
        initComponents();
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);
        jButton4.addActionListener(this);
        jButton5.addActionListener(this);
        jButton6.addActionListener(this);
        jButton7.addActionListener(this);
        jButton8.addActionListener(this);
        jButton9.addActionListener(this);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents()
    {
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TicTacToe");
        setName("jFrame");
        jButton2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 75, 70));

        jButton3.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton3ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 10, 75, 70));

        jButton4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton4ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 75, 70));

        jButton5.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton5ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 90, 75, 70));

        jButton6.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton6ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 90, 75, 70));

        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 75, 70));

        jButton7.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton7ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 75, 70));

        jButton8.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton8ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 170, 75, 70));

        jButton9.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton9ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, 75, 70));

        pack();
    }// </editor-fold>                        
    
    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton9.setText(turn);
        jButton9.setEnabled(false);
        config[9] = turn;
    }                                        
    
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton8.setText(turn);
        jButton8.setEnabled(false);
        config[8] = turn;
    }                                        
    
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton7.setText(turn);
        jButton7.setEnabled(false);
        config[7] = turn;
    }                                        
    
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton6.setText(turn);
        jButton6.setEnabled(false);
        config[6] = turn;
    }                                        
    
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton5.setText(turn);
        jButton5.setEnabled(false);
        config[5] = turn;
    }                                        
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton4.setText(turn);
        jButton4.setEnabled(false);
        config[4] = turn;
    }                                        
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton3.setText(turn);
        jButton3.setEnabled(false);
        config[3] = turn;
    }                                        
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton2.setText(turn);
        jButton2.setEnabled(false);
        config[2] = turn;
    }                                        
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        jButton1.setText(turn);
        jButton1.setEnabled(false);
        config[1] = turn;
    }                                        
    
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new TicTacToeForm().setVisible(true);
            }
        });
    }
    
    public boolean checkWinner()
    {
        for(int i=0; i<=7; i++)
        {
            if( config[winCombinations[i][0]].equals(config[winCombinations[i][1]]) &&
                    config[winCombinations[i][1]].equals(config[winCombinations[i][2]]) &&
                    config[winCombinations[i][0]] != "")
            {
                win = true;
            }
        }
        return win;
    }
    
    public void actionPerformed(ActionEvent a)
    {
        count++;
        
        if(count %2 == 0)
        {
            turn = "O";
        }
        else
        {
            turn = "X";
        }
        
    }
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    // End of variables declaration                   
}
Right now, my checkWinner(); function doesn't work at all, I tried to adapt a few ideas from this thread, but it didn't help a whole lot.
Any help is appreciated.
Reply With Quote