Solved

Array issues

Posted on 2011-02-22
17
510 Views
Last Modified: 2013-12-14
I am getting an infinite loop at lines 378 to 389 and I don't understand why.  I am basically trying to make a tic tac toe game, and these random number generators are being used in the loop to find a JButton in the array that has a label of "" or an empty string, therefore able to be used in the game by the computer move.  I have done everything I can to catch why it won't exit the infinite loop and I am stumped.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gameGUI.java
 *
 * Created on Feb 22, 2011, 11:37:10 AM
 */

/**
 *
 * @author cliffordworkman
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    
    JButton board[][]= new JButton[rows][cols];

    /** Creates new form gameGUI */
    public gameGUI()
    {
        initComponents();        
        
        board[0][0]=tl;
        board[0][1]=tm;
        board[0][2]=tr;
        board[1][0]=ml;
        board[1][1]=mm;         //Here I occupy the board array with JButtons
        board[1][2]=mr;
        board[2][0]=bl;
        board[2][1]=bm;
        board[2][2]=br;

        for(int i=0;i<rows-1;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols-1;j++)
                board[i][j].setLabel("");
        }
    }

    /** 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() {

        tl = new javax.swing.JButton();
        tm = new javax.swing.JButton();
        tr = new javax.swing.JButton();
        mr = new javax.swing.JButton();
        ml = new javax.swing.JButton();
        mm = new javax.swing.JButton();
        bm = new javax.swing.JButton();
        bl = new javax.swing.JButton();
        br = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tlActionPerformed(evt);
            }
        });

        tm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tmActionPerformed(evt);
            }
        });

        tr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trActionPerformed(evt);
            }
        });

        mr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mrActionPerformed(evt);
            }
        });

        ml.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        ml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mlActionPerformed(evt);
            }
        });

        mm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mmActionPerformed(evt);
            }
        });

        bm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmActionPerformed(evt);
            }
        });

        bl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blActionPerformed(evt);
            }
        });

        br.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public void Com()
    {
        int exitLoop=0;
        do
        {
            int consecutive = 0;
            for(int i=0;i<rows-1&&exitLoop<1;i++)             //horizontal offense move checker
            {
                consecutive=0;
                System.out.println("I made it to 185!");
                for(int j=0;j<cols-1&&exitLoop<1;j++)
                {
                    System.out.println("I made it to 188!");
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols-1&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 196!");
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }
            for(int j=0;j<rows-1&&exitLoop<1;j++)             //vertical offense move checker
            {
                System.out.println("I made it to 208!");
                consecutive=0;
                for(int i=0;i<cols-1&&exitLoop<1;i++)
                {
                    System.out.println("I made it to 212!");
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows-1&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 220!");
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }

            consecutive=0;
            //exitLoop=0;
            for(int i=0;i<rows-1&&exitLoop<1;i++)             //this set of loops checks for the winning move in the tl to br diagonal
            {
                System.out.println("I made it to 235!");
                if(board[i][i].getLabel().equals("O"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows-1&&exitLoop<1;j++)
                    {
                        System.out.println("I made it to 242!");
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            exitLoop++;
                        }
                    }
                }
            }
            if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[2][0].setLabel("O");
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[2][0].setLabel("O");
                }
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[1][1].setLabel("O");
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[1][1].setLabel("O");
                }
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals("O"))
                {
                    board[0][2].setLabel("O");
                }
                if(board[1][1].getLabel().equals("X"))
                {
                    board[0][2].setLabel("O");
                }
            }
            //exitLoop=0;
            for(int i=0;i<rows&&exitLoop<1;i++)             //horizontal defense move checker
            {
                System.out.println("I made it to 287!");
                consecutive=0;
                for(int j=0;j<cols&&exitLoop<1;j++)
                {
                    System.out.println("I made it to 291!");
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }
            for(int j=0;j<rows-1&&exitLoop<1;j++)             //vertical defense move checker
            {
                consecutive=0;
                System.out.println("I made it to 311!");
                for(int i=0;i<cols-1&&exitLoop<1;i++)
                {
                    System.out.println("I made it to 314!");
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows-1&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            System.out.println("I made it to 322!");
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }

            consecutive=0;
            //exitLoop=0;
            for(int i=0;i<rows-1&&exitLoop<1;i++)             //this set of loops checks for the winning move to prevent in the tl to br diagonal
            {
                System.out.println("I made it to 337!");
                if(board[i][i].getLabel().equals("X"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows-1&&exitLoop<1;j++)
                    {
                        System.out.println("I made it to 344!");
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            exitLoop++;
                        }
                    }
                }
            }
            if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals(""))
                board[2][0].setLabel("O");
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals(""))
                board[1][1].setLabel("O");
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals(""))
                board[0][2].setLabel("O");
            }

            if(exitLoop<1)
            {
                int x,y;
                do
                {
                    System.out.println("I made it to 374!");
                    Random ranX = new Random();
                    Random ranY = new Random();
                    x = ranX.nextInt(3);
                    y = ranY.nextInt(3);
                    System.out.println("X = "+x+"\nY = "+y);
               //     System.out.println(!(board[x][y].getLabel().equals("")));
               //     System.out.println((board[x][y].getLabel().equals("")));

                }while((board[x][y].getLabel().equals("")));

                board[x][y].setLabel("O");
            }
            System.out.println("I made it to 383!");
        }while(exitLoop<1);
    }
    private void tlActionPerformed(java.awt.event.ActionEvent evt) {
        if(tl.getLabel().equals(""))
        {
            tl.setLabel("X");
            
            Com();
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals(""))
        {
            tm.setLabel("X");
            Com();
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals(""))
        {
            tr.setLabel("X");
            Com();
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals(""))
        {
            ml.setLabel("X");
            Com();
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals(""))
        {
            mm.setLabel("X");
            Com();
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals(""))
        {
            mr.setLabel("X");
            Com();
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals(""))
        {
            bl.setLabel("X");
            Com();
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals(""))
        {
            bm.setLabel("X");
            Com();
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals(""))
        {
            br.setLabel("X");
            Com();
        }
    }

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

    // Variables declaration - do not modify
    private javax.swing.JButton bl;
    private javax.swing.JButton bm;
    private javax.swing.JButton br;
    private javax.swing.JButton ml;
    private javax.swing.JButton mm;
    private javax.swing.JButton mr;
    private javax.swing.JButton tl;
    private javax.swing.JButton tm;
    private javax.swing.JButton tr;
    // End of variables declaration

}

Open in new window

0
Comment
Question by:prototrumpet
  • 10
  • 7
17 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
What is the function of the "do" loop that starts on line 185 (referring to the line numbers above) which is the outermost loop that wraps all the decision making, and what is the function of exitloop?

I'm not sure if the idea is to "try" different moves, and hence that is why there is a loop around the whole lot. However, I doubt that is the case, as in the specific decision making sections within that loop, you are actually making real changes to the state of the board (not to some object that holds the state of things that you are trying). I guess where I am going with this, is that apart from your infinite looping issue, I can see possibilities where the Com() will make multiple moves in each turn (a little unfair for the human player!)

The reason I bring this up is that I think that all those problems are related. What I suggest is that you refactor a little bit and move each of those sections of decision making (vertical, horizontal, diagonal, etc) into their own subroutines, and make those subroutines function in a standard way where they make their decision and then return that decision which may be "I want to place the O at x,y" or it may be "I am not making a decision, go on to the next function to try and make a decision"

For example, at the moment in the first 3 sections (horizontal, vertical and tl to br diagonal) the loops are all checking exitloop and setting exitloop when a move is made, but then you have some if statements (257, 268, 279) that do check if you have already made a move (not checking exitloop) and they also don't set exitloop if any of them end up making a move (ie. setting a label to "O" anywhere)
0
 

Author Comment

by:prototrumpet
Comment Utility
I managed to fix the infinite loop issue, and now see exactly what you warned me of, having the com() make two moves rather than just one (against the rules).  as far as breaking down into subroutines goes, I am not sure that I have done that before in class (at least not intentionally).  Do you define these things, so that there is a return type, or what?  If it is too complicated I will find another way, there is no need to try to teach me a semester's worth of computer science and put you to that trouble.  If you can though, could you show me an example of creating a subroutine?

thanks for your help.

also, here is the updated version of my code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gameGUI.java
 *
 * Created on Feb 22, 2011, 11:37:10 AM
 */

/**
 *
 * @author cliffordworkman
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    
    JButton board[][]= new JButton[rows][cols];

    /** Creates new form gameGUI */
    public gameGUI()
    {
        initComponents();        
        
        board[0][0]=tl;
        board[0][1]=tm;
        board[0][2]=tr;
        board[1][0]=ml;
        board[1][1]=mm;         //Here I occupy the board array with JButtons
        board[1][2]=mr;
        board[2][0]=bl;
        board[2][1]=bm;
        board[2][2]=br;

        for(int i=0;i<rows-1;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols-1;j++)
                board[i][j].setLabel("");
        }
    }

    /** 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() {

        tl = new javax.swing.JButton();
        tm = new javax.swing.JButton();
        tr = new javax.swing.JButton();
        mr = new javax.swing.JButton();
        ml = new javax.swing.JButton();
        mm = new javax.swing.JButton();
        bm = new javax.swing.JButton();
        bl = new javax.swing.JButton();
        br = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tlActionPerformed(evt);
            }
        });

        tm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tmActionPerformed(evt);
            }
        });

        tr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trActionPerformed(evt);
            }
        });

        mr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mrActionPerformed(evt);
            }
        });

        ml.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        ml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mlActionPerformed(evt);
            }
        });

        mm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mmActionPerformed(evt);
            }
        });

        bm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmActionPerformed(evt);
            }
        });

        bl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blActionPerformed(evt);
            }
        });

        br.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public void Com()
    {
        int exitLoop=0;
        do
        {
            int consecutive = 0;
            for(int i=0;i<rows-1&&exitLoop<1;i++)             //horizontal offense move checker
            {
                consecutive=0;
                System.out.println("I made it to 185!");
                for(int j=0;j<cols-1&&exitLoop<1;j++)
                {
                    System.out.println("I made it to 188!");
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols-1&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 196!");
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }
            for(int j=0;j<rows-1&&exitLoop<1;j++)             //vertical offense move checker
            {
                System.out.println("I made it to 208!");
                consecutive=0;
                for(int i=0;i<cols-1&&exitLoop<1;i++)
                {
                    System.out.println("I made it to 212!");
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows-1&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 220!");
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }

            consecutive=0;
            //exitLoop=0;
            for(int i=0;i<rows-1&&exitLoop<1;i++)             //this set of loops checks for the winning move in the tl to br diagonal
            {
                System.out.println("I made it to 235!");
                if(board[i][i].getLabel().equals("O"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows-1&&exitLoop<1;j++)
                    {
                        System.out.println("I made it to 242!");
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            exitLoop++;
                        }
                    }
                }
            }
            if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[2][0].setLabel("O");
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[2][0].setLabel("O");
                }
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[1][1].setLabel("O");
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[1][1].setLabel("O");
                }
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals("O"))
                {
                    board[0][2].setLabel("O");
                }
                if(board[1][1].getLabel().equals("X"))
                {
                    board[0][2].setLabel("O");
                }
            }
            //exitLoop=0;
            for(int i=0;i<rows&&exitLoop<1;i++)             //horizontal defense move checker
            {
                System.out.println("I made it to 287!");
                consecutive=0;
                for(int j=0;j<cols&&exitLoop<1;j++)
                {
                    System.out.println("I made it to 291!");
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }
            for(int j=0;j<rows-1&&exitLoop<1;j++)             //vertical defense move checker
            {
                consecutive=0;
                System.out.println("I made it to 311!");
                for(int i=0;i<cols-1&&exitLoop<1;i++)
                {
                    System.out.println("I made it to 314!");
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows-1&&exitLoop<1;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            System.out.println("I made it to 322!");
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                exitLoop++;
                            }
                        }
                    }
                }
            }

            consecutive=0;
            //exitLoop=0;
            for(int i=0;i<rows-1&&exitLoop<1;i++)             //this set of loops checks for the winning move to prevent in the tl to br diagonal
            {
                System.out.println("I made it to 337!");
                if(board[i][i].getLabel().equals("X"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows-1&&exitLoop<1;j++)
                    {
                        System.out.println("I made it to 344!");
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            exitLoop++;
                        }
                    }
                }
            }
            if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals(""))
                board[2][0].setLabel("O");
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals(""))
                board[1][1].setLabel("O");
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals(""))
                board[0][2].setLabel("O");
            }

            if(exitLoop<1)
            {
                int x,y;
                exitLoop=0;
                do
                {
                    System.out.println("I made it to 374!");
                    Random ranX = new Random();
                    Random ranY = new Random();
                    x = ranX.nextInt(3);
                    y = ranY.nextInt(3);
                    System.out.println("X = "+x+"\nY = "+y);
               //     System.out.println(!(board[x][y].getLabel().equals("")));
               //     System.out.println((board[x][y].getLabel().equals("")));
                    if(board[x][y].getLabel().matches(""))
                    {
                        board[x][y].setLabel("O");
                        exitLoop++;
                    }
                }while(exitLoop<1);

                //board[x][y].setLabel("O");
            }
            System.out.println("I made it to 383!");   
        }while(exitLoop<1);
    }
    private void tlActionPerformed(java.awt.event.ActionEvent evt) {
        if(tl.getLabel().equals(""))
        {
            tl.setLabel("X");
            
            Com();
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals(""))
        {
            tm.setLabel("X");
            Com();
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals(""))
        {
            tr.setLabel("X");
            Com();
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals(""))
        {
            ml.setLabel("X");
            Com();
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals(""))
        {
            mm.setLabel("X");
            Com();
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals(""))
        {
            mr.setLabel("X");
            Com();
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals(""))
        {
            bl.setLabel("X");
            Com();
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals(""))
        {
            bm.setLabel("X");
            Com();
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals(""))
        {
            br.setLabel("X");
            Com();
        }
    }

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

    // Variables declaration - do not modify
    private javax.swing.JButton bl;
    private javax.swing.JButton bm;
    private javax.swing.JButton br;
    private javax.swing.JButton ml;
    private javax.swing.JButton mm;
    private javax.swing.JButton mr;
    private javax.swing.JButton tl;
    private javax.swing.JButton tm;
    private javax.swing.JButton tr;
    // End of variables declaration

}

Open in new window

0
 

Author Comment

by:prototrumpet
Comment Utility
Actually, I think I figured out what you mean by subroutines, that was just another term for a method, right?

Dividing up into different methods would make the logic easier, as I could call all of those methods from the body of the com() method.  Awesome, I'm starting on that now, thanks.  If I'm totally wrong though, I guess we will know soon enough
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
Comment Utility
Believe it or not, you already have on line 182, Com() is a subroutine. The only difference is that Com() doesn't return anything, that's what the "void" keyword means.

So here is a basic overview of what I am thinking...

 
public void Com()
{
    if(tryHorizontalOffense())
        return();

    if(tryVerticalOffense())
        return();

    // do similar for each type of move to try
}

public boolean tryHorizontalOffense()
{
            for(int i=0;i<rows-1;i++)           //horizontal offense move checker
            {
                consecutive=0;
                System.out.println("I made it to 185!");
                for(int j=0;j<cols-1;j++)
                {
                    System.out.println("I made it to 188!");
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols-1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 196!");
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
}

public boolean tryVerticalOffense()
{
    // Do similar to above: put you logic for vertical offense move
    //  in here, but take out exitloop stuff and return true if
    //  you make a move, or return false if you don't make a move
}

// And do the same for all the types of moves to try

Open in new window

0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Sorry, yeah, subroutine, method, function... all "basically" the same thing just different names.
0
 

Author Comment

by:prototrumpet
Comment Utility
Okay, I have totally revamped it, but I am now running into the problem again that the computer will sometimes move more than once, and I don't see what is causing it.  Here is the code:
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Didn't get the code...
0
 

Author Comment

by:prototrumpet
Comment Utility
Ok, let me try that again, sorry:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gameGUI.java
 *
 * Created on Feb 22, 2011, 11:37:10 AM
 */

/**
 *
 * @author cliffordworkman
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    
    JButton board[][]= new JButton[rows][cols];

    /** Creates new form gameGUI */
    public gameGUI()
    {
        initComponents();        
        
        board[0][0]=tl;
        board[0][1]=tm;
        board[0][2]=tr;
        board[1][0]=ml;
        board[1][1]=mm;         //Here I occupy the board array with JButtons
        board[1][2]=mr;
        board[2][0]=bl;
        board[2][1]=bm;
        board[2][2]=br;

        for(int i=0;i<rows-1;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols-1;j++)
                board[i][j].setLabel("");
        }
    }

    /** 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() {

        tl = new javax.swing.JButton();
        tm = new javax.swing.JButton();
        tr = new javax.swing.JButton();
        mr = new javax.swing.JButton();
        ml = new javax.swing.JButton();
        mm = new javax.swing.JButton();
        bm = new javax.swing.JButton();
        bl = new javax.swing.JButton();
        br = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tlActionPerformed(evt);
            }
        });

        tm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tmActionPerformed(evt);
            }
        });

        tr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trActionPerformed(evt);
            }
        });

        mr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mrActionPerformed(evt);
            }
        });

        ml.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        ml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mlActionPerformed(evt);
            }
        });

        mm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mmActionPerformed(evt);
            }
        });

        bm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmActionPerformed(evt);
            }
        });

        bl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blActionPerformed(evt);
            }
        });

        br.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public boolean horizontalOffense()
    {
        int consecutive = 0;
            for(int i=0;i<rows-1;i++)             //horizontal offense move checker
            {
                consecutive=0;
                for(int j=0;j<cols-1;j++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols-1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 196!");
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalOffense()
    {
       for(int j=0;j<cols-1;j++)             //vertical offense move checker
            {
                int consecutive=0;
                for(int i=0;i<rows-1;i++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                    {
                        consecutive++;
                        if(consecutive>=2)

                        {

                            for(int k=0;k<rows-1;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to get 3 in a row!
                            {
                                System.out.println("I made it to 220!");
                                if((board[k][j].getLabel()).equals(""))
                                {
                                    board[k][j].setLabel("O");
                                    return true;
                                }
                            }
                        }
                    }
                }
            } 
       return false;
    }
    public boolean firstDiagOffense()
    {
        int consecutive=0;
        for(int i=0;i<rows-1;i++)             //this set of loops checks for the winning move in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("O"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows-1;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean secondDiag()          //this one checks offense and defense at the same time
    {
        if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[2][0].setLabel("O");
                    return true;
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[2][0].setLabel("O");
                    return true;
                }
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[1][1].setLabel("O");
                    return true;
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[1][1].setLabel("O");
                    return true;
                }
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals("O"))
                {
                    board[0][2].setLabel("O");
                    return true;
                }
                if(board[1][1].getLabel().equals("X"))
                {
                    board[0][2].setLabel("O");
                    return true;
                }
            }
        return false;
    }
    public boolean horizontalDefense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //horizontal defense move checker
            {
                consecutive=0;
                for(int j=0;j<cols-1;j++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols-1;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalDefense()
    {
        int consecutive=0;
        for(int j=0;j<rows-1;j++)             //vertical defense move checker
            {
                consecutive=0;
                for(int i=0;i<cols-1;i++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows-1;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    
    public boolean diagonalDefense()
    {
        int consecutive=0;
            for(int i=0;i<rows-1;i++)             //this set of loops checks for the winning move to prevent in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("X"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows-1;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean diagonalMove()
    {
        if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements prioratize a strategy
        {
            if(board[0][2].getLabel().equals(""))
            {
                board[2][0].setLabel("O");
                return true;
            }
        }
        if(board[0][2].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[0][2].getLabel().equals(""))
            {
                board[1][1].setLabel("O");
                return true;
            }
        }
        if(board[1][1].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[1][1].getLabel().equals(""))
            {
                board[0][2].setLabel("O");
                return true;
            }
        }
        return false;
    }
    public boolean RandomMove()
    {
        int x,y;
        do
        {
            Random ranX = new Random();
            Random ranY = new Random();
            x = ranX.nextInt(3);
            y = ranY.nextInt(3);
            if(board[x][y].getLabel().matches(""))
            {
                board[x][y].setLabel("O");
                return true;
            }
        }while(!(RandomMove()));
        return false;
    }
    
    public void Com()
    {
        if(horizontalOffense())
            horizontalOffense();
            else if(verticalOffense())
                verticalOffense();
                else if(firstDiagOffense())
                    firstDiagOffense();
                    else if(secondDiag())
                        secondDiag();
                        else if(horizontalDefense())
                            horizontalDefense();
                            else if(verticalDefense())
                                verticalDefense();
                                else if(diagonalDefense())
                                    diagonalDefense();
                                    else if(diagonalMove())
                                        diagonalMove();
                                        else if(RandomMove())
                                            RandomMove();
                                            else;
    }
    private void tlActionPerformed(java.awt.event.ActionEvent evt) {
        if(tl.getLabel().equals(""))
        {
            tl.setLabel("X");
            
            Com();
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals(""))
        {
            tm.setLabel("X");
            Com();
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals(""))
        {
            tr.setLabel("X");
            Com();
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals(""))
        {
            ml.setLabel("X");
            Com();
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals(""))
        {
            mm.setLabel("X");
            Com();
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals(""))
        {
            mr.setLabel("X");
            Com();
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals(""))
        {
            bl.setLabel("X");
            Com();
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals(""))
        {
            bm.setLabel("X");
            Com();
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals(""))
        {
            br.setLabel("X");
            Com();
        }
    }

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

    // Variables declaration - do not modify
    private javax.swing.JButton bl;
    private javax.swing.JButton bm;
    private javax.swing.JButton br;
    private javax.swing.JButton ml;
    private javax.swing.JButton mm;
    private javax.swing.JButton mr;
    private javax.swing.JButton tl;
    private javax.swing.JButton tm;
    private javax.swing.JButton tr;
    // End of variables declaration

}

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
Comment Utility
It can move more than once because you are (potentially) calling each move method more than once. For example, lets just take the lines 424 and 425. It is saying, "try the horizontalOffense move and then if that method actually made a move (ie. it returned true), try the horizontalOffense move" and so it can possibly move twice.

Basically the horizontalOffsense() method (and likewise for all others), attempts to make a move and executes that move, all in the one method call. You don't need to call it once to "see" if it will make a move and then call it again to actually execute that move.

So either code, like I showed in my earlier comment, or if you would rather do it as a nested if similar to what you have done then it would look like...

 
if(!horizontalOffense())
   if(!verticalOffense())
      if(!.....
         ....
         ....
         ....
                   if(!diagonalMove())
                      RandomMove();

Open in new window


You can read that as "try a horizontalOffense move, if we DIDN'T move, try a verticalOffense move, if we DIDN'T move, ......, try a diagonalMove, if we DIDN'T move do a randomMove"

Hope it helps...
0
 

Author Comment

by:prototrumpet
Comment Utility
I'm sorry I must be torturing you, but there are still weird results.  I made the changes you suggested (and thanks for that btw), however now the computer does not always make a move.  Also sometimes its move overrides my own for the same square on the same click.  This happens most frequently when clicking the center square.  I don't understand this at all, because the conditions for being able to occupy a square in all of my methods and button actions are that the square be empty (have no label).

Here is the new code:
0
 

Author Comment

by:prototrumpet
Comment Utility
for some reason, it didn't attach again:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gameGUI.java
 *
 * Created on Feb 22, 2011, 11:37:10 AM
 */

/**
 *
 * @author cliffordworkman
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    
    JButton board[][]= new JButton[rows][cols];

    /** Creates new form gameGUI */
    public gameGUI()
    {
        initComponents();        
        
        board[0][0]=tl;
        board[0][1]=tm;
        board[0][2]=tr;
        board[1][0]=ml;
        board[1][1]=mm;         //Here I occupy the board array with JButtons
        board[1][2]=mr;
        board[2][0]=bl;
        board[2][1]=bm;
        board[2][2]=br;

        for(int i=0;i<rows-1;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols-1;j++)
                board[i][j].setLabel("");
        }
    }

    /** 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() {

        tl = new javax.swing.JButton();
        tm = new javax.swing.JButton();
        tr = new javax.swing.JButton();
        mr = new javax.swing.JButton();
        ml = new javax.swing.JButton();
        mm = new javax.swing.JButton();
        bm = new javax.swing.JButton();
        bl = new javax.swing.JButton();
        br = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tlActionPerformed(evt);
            }
        });

        tm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tmActionPerformed(evt);
            }
        });

        tr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trActionPerformed(evt);
            }
        });

        mr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mrActionPerformed(evt);
            }
        });

        ml.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        ml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mlActionPerformed(evt);
            }
        });

        mm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mmActionPerformed(evt);
            }
        });

        bm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmActionPerformed(evt);
            }
        });

        bl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blActionPerformed(evt);
            }
        });

        br.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public boolean horizontalOffense()
    {
        int consecutive = 0;
            for(int i=0;i<rows;i++)             //horizontal offense move checker
            {
                consecutive=0;
                for(int j=0;j<cols;j++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 196!");
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalOffense()
    {
       for(int j=0;j<rows;j++)             //vertical offense move checker
            {
                int consecutive=0;
                for(int i=0;i<cols;i++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 220!");
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            } 
       return false;
    }
    public boolean firstDiagOffense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //this set of loops checks for the winning move in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("O"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean secondDiag()          //this one checks offense and defense at the same time
    {
        if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[2][0].setLabel("O");
                    return true;
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[2][0].setLabel("O");
                    return true;
                }
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    board[1][1].setLabel("O");
                    return true;
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    board[1][1].setLabel("O");
                    return true;
                }
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals("O"))
                {
                    board[0][2].setLabel("O");
                    return true;
                }
                if(board[1][1].getLabel().equals("X"))
                {
                    board[0][2].setLabel("O");
                    return true;
                }
            }
        return false;
    }
    public boolean horizontalDefense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //horizontal defense move checker
            {
                consecutive=0;
                for(int j=0;j<cols;j++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalDefense()
    {
        int consecutive=0;
        for(int j=0;j<rows;j++)             //vertical defense move checker
            {
                consecutive=0;
                for(int i=0;i<cols;i++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    
    public boolean diagonalDefense()
    {
        int consecutive=0;
            for(int i=0;i<rows;i++)             //this set of loops checks for the winning move to prevent in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("X"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean diagonalMove()
    {
        if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements prioratize a strategy
        {
            if(board[0][2].getLabel().equals(""))
            {
                board[2][0].setLabel("O");
                return true;
            }
        }
        if(board[0][2].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[0][2].getLabel().equals(""))
            {
                board[1][1].setLabel("O");
                return true;
            }
        }
        if(board[1][1].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[1][1].getLabel().equals(""))
            {
                board[0][2].setLabel("O");
                return true;
            }
        }
        return false;
    }
    public boolean RandomMove()
    {
        int x,y;
        do
        {
            Random ranX = new Random();
            Random ranY = new Random();
            x = ranX.nextInt(3);
            y = ranY.nextInt(3);
            if(board[x][y].getLabel().matches(""))
            {
                board[x][y].setLabel("O");
                return true;
            }
        }while(!(RandomMove()));
        return false;
    }
    
    public void Com()
    {
        if(!(horizontalOffense()))
            if(!(verticalOffense()))
                if(!(firstDiagOffense()))
                    if(!(secondDiag()))
                        if(!(horizontalDefense()))
                            if(!(verticalDefense()))
                                if(!(diagonalDefense()))
                                    if(!(diagonalMove()))
                                        RandomMove();
    }
    private void tlActionPerformed(java.awt.event.ActionEvent evt) {
        if(tl.getLabel().equals(""))
        {
            tl.setLabel("X");
            
            Com();
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals(""))
        {
            tm.setLabel("X");
            Com();
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals(""))
        {
            tr.setLabel("X");
            Com();
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals(""))
        {
            ml.setLabel("X");
            Com();
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals(""))
        {
            mm.setLabel("X");
            Com();
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals(""))
        {
            mr.setLabel("X");
            Com();
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals(""))
        {
            bl.setLabel("X");
            Com();
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals(""))
        {
            bm.setLabel("X");
            Com();
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals(""))
        {
            br.setLabel("X");
            Com();
        }
    }

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

    // Variables declaration - do not modify
    private javax.swing.JButton bl;
    private javax.swing.JButton bm;
    private javax.swing.JButton br;
    private javax.swing.JButton ml;
    private javax.swing.JButton mm;
    private javax.swing.JButton mr;
    private javax.swing.JButton tl;
    private javax.swing.JButton tm;
    private javax.swing.JButton tr;
    // End of variables declaration

}

Open in new window

0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
Comment Utility
I don't understand this at all, because the conditions for being able to occupy a square in all of my methods and button actions are that the square be empty (have no label).

Not so, if you have a look at your secondDiag and diagonalMove methods, you CAN occupy squares that you haven't checked yet. Also, I don't really understand why one diagonal is done with loops and the other with a bunch of if statements. Also, I haven't worked out in my mind exactly what those loops are doing but your are right, they aren't the source of the current problem.

Also, the computer probably is ALWAYS making a move, it just may be that it is overwriting it's own moves as well as yours, and therefore you just don't see that on the board, ie. an O overwritten by an O
0
 

Author Comment

by:prototrumpet
Comment Utility
Thank you for pointing out the unchecked moves in the diagonal methods, I would have never caught that, because based on the previous code structure (before the subroutines came into the picture) the structure of loops prevented the unchecked moves in the later methods.  I didn't realize that the barrier was no longer there when I split the Com() class into subroutines.

that fixed everything, I believe.  I ran several test games, and ran into one last issue, the computer still went twice on me occasionally, so I am going to investigate the methods more closely and hopefully get to the bottom of that.

Here is the corrected code thus far if you are interested:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gameGUI.java
 *
 * Created on Feb 22, 2011, 11:37:10 AM
 */

/**
 *
 * @author cliffordworkman
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    
    JButton board[][]= new JButton[rows][cols];

    /** Creates new form gameGUI */
    public gameGUI()
    {
        initComponents();        
        
        board[0][0]=tl;
        board[0][1]=tm;
        board[0][2]=tr;
        board[1][0]=ml;
        board[1][1]=mm;         //Here I occupy the board array with JButtons
        board[1][2]=mr;
        board[2][0]=bl;
        board[2][1]=bm;
        board[2][2]=br;

        for(int i=0;i<rows-1;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols-1;j++)
                board[i][j].setLabel("");
        }
    }

    /** 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() {

        tl = new javax.swing.JButton();
        tm = new javax.swing.JButton();
        tr = new javax.swing.JButton();
        mr = new javax.swing.JButton();
        ml = new javax.swing.JButton();
        mm = new javax.swing.JButton();
        bm = new javax.swing.JButton();
        bl = new javax.swing.JButton();
        br = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tlActionPerformed(evt);
            }
        });

        tm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tmActionPerformed(evt);
            }
        });

        tr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trActionPerformed(evt);
            }
        });

        mr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mrActionPerformed(evt);
            }
        });

        ml.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        ml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mlActionPerformed(evt);
            }
        });

        mm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mmActionPerformed(evt);
            }
        });

        bm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmActionPerformed(evt);
            }
        });

        bl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blActionPerformed(evt);
            }
        });

        br.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public boolean horizontalOffense()
    {
        int consecutive = 0;
            for(int i=0;i<rows;i++)             //horizontal offense move checker
            {
                consecutive=0;
                for(int j=0;j<cols;j++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 196!");
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalOffense()
    {
       for(int j=0;j<rows;j++)             //vertical offense move checker
            {
                int consecutive=0;
                for(int i=0;i<cols;i++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            System.out.println("I made it to 220!");
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            } 
       return false;
    }
    public boolean firstDiagOffense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //this set of loops checks for the winning move in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("O"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean secondDiag()          //this one checks offense and defense at the same time
    {
        if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    if(board[2][0].getLabel().equals(""))
                    {
                        board[2][0].setLabel("O");
                        return true;
                    }
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    if(board[2][0].getLabel().equals(""))
                    {
                        board[2][0].setLabel("O");
                        return true;
                    }
                }
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    if(board[1][1].getLabel().equals(""))
                    {
                        board[1][1].setLabel("O");
                        return true;
                    }
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    if(board[1][1].getLabel().equals(""))
                    {
                        board[1][1].setLabel("O");
                        return true;
                    }
                }
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals("O"))
                {
                    if(board[0][2].getLabel().equals(""))
                    {
                        board[0][2].setLabel("O");
                        return true;
                    }
                }
                if(board[1][1].getLabel().equals("X"))
                {
                    if(board[0][2].getLabel().equals(""))
                    {
                        board[0][2].setLabel("O");
                        return true;
                    }
                }
            }
        return false;
    }
    public boolean horizontalDefense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //horizontal defense move checker
            {
                consecutive=0;
                for(int j=0;j<cols;j++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalDefense()
    {
        int consecutive=0;
        for(int j=0;j<rows;j++)             //vertical defense move checker
            {
                consecutive=0;
                for(int i=0;i<cols;i++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    
    public boolean diagonalDefense()
    {
        int consecutive=0;
            for(int i=0;i<rows;i++)             //this set of loops checks for the winning move to prevent in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("X"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean diagonalMove()
    {
        if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements prioratize a strategy
        {
            if(board[0][2].getLabel().equals(""))
            {
                if(board[2][0].getLabel().equals(""))
                    {
                        board[2][0].setLabel("O");
                        return true;
                    }
            }
        }
        if(board[0][2].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[0][2].getLabel().equals(""))
            {
                if(board[1][1].getLabel().equals(""))
                    {
                        board[1][1].setLabel("O");
                        return true;
                    }
            }
        }
        if(board[1][1].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[1][1].getLabel().equals(""))
            {
                if(board[0][2].getLabel().equals(""))
                    {
                        board[0][2].setLabel("O");
                        return true;
                    }
            }
        }
        return false;
    }
    public boolean RandomMove()
    {
        int x,y;
        do
        {
            Random ranX = new Random();
            Random ranY = new Random();
            x = ranX.nextInt(3);
            y = ranY.nextInt(3);
            if(board[x][y].getLabel().matches(""))
            {
                board[x][y].setLabel("O");
                return true;
            }
        }while(!(RandomMove()));
        return false;
    }
    
    public void Com()
    {
        if(!(horizontalOffense()))
            if(!(verticalOffense()))
                if(!(firstDiagOffense()))
                    if(!(secondDiag()))
                        if(!(horizontalDefense()))
                            if(!(verticalDefense()))
                                if(!(diagonalDefense()))
                                    if(!(diagonalMove()))
                                        RandomMove();
    }
    private void tlActionPerformed(java.awt.event.ActionEvent evt) {
        if(tl.getLabel().equals(""))
        {
            tl.setLabel("X");
            
            Com();
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals(""))
        {
            tm.setLabel("X");
            Com();
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals(""))
        {
            tr.setLabel("X");
            Com();
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals(""))
        {
            ml.setLabel("X");
            Com();
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals(""))
        {
            mm.setLabel("X");
            Com();
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals(""))
        {
            mr.setLabel("X");
            Com();
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals(""))
        {
            bl.setLabel("X");
            Com();
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals(""))
        {
            bm.setLabel("X");
            Com();
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals(""))
        {
            br.setLabel("X");
            Com();
        }
    }

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

    // Variables declaration - do not modify
    private javax.swing.JButton bl;
    private javax.swing.JButton bm;
    private javax.swing.JButton br;
    private javax.swing.JButton ml;
    private javax.swing.JButton mm;
    private javax.swing.JButton mr;
    private javax.swing.JButton tl;
    private javax.swing.JButton tm;
    private javax.swing.JButton tr;
    // End of variables declaration

}

Open in new window

0
 

Author Comment

by:prototrumpet
Comment Utility
I forgot to mention my reasoning for the if statements in the other diagonal method:

It was easy to run through the nested for loops to reach each significant part of the array, but I couldn't think of a reasonable way to get to board[0][2], board[1][1], board[2][0] in a loop ):

I thought it might just be easier to do the if statements
0
 

Author Closing Comment

by:prototrumpet
Comment Utility
You saved me, and I owe you big time.  Thanks a lot!
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
The double moves, are because you are calling RandomMove recursively (line 442). I am guessing what you are really after is

do
{
...
}
while(true);

(And remove the return false, after that). What that means is that it will just keep looping until it finds an empty square, and the return true inside the loop is the only way it finishes. Now the problem with this is that theoretically, the random number generators could return you indexes to non-empty squares for indefinite period of time before it finds an empty one. Agreed that it is unlikely to take too long but the possibility is there that it could never find an empty square.

Normally, how this would be done is you would firstly go through the whole board, and note down locations that are empty and store these in a list (called say emptySquareList), then you would generate a random number between 0 and sizeOf(emptySquareList) - 1, and then you use that random number as an index into your list, pick the location out that it is pointing to and then you can put your "O" in that location.

Imagine that at the moment, there are only 2 squares left and the computer has to generate the random number to try and "find" one of those 2 squares, that is a 2 in 9 chance of finding one on each attempt. Not too bad, but imagine that you made the board bigger, say 5 by 5, and you get down to the last 2 squares again, it has only a 2 in 25 chance of lucking out and picking one of those 2 squares. It could take much longer.

BTW, just some other observations, I think this game only makes sense on a square board, ie 3x3, 4x4, etc. so instead of defining rows AND cols, I would suggest just defining say, size, or boardSize, and using that instead. That, plus renaming some of the i, j, k variables would make some of those loops easier to understand I think! It also would get you closer to being able to make it different sizes, down the track.

As for your issue with doing the opposite diagonal with loops, at the moment you do the main diagonal with one loop, i, from 0 to 2 and then check board[ i ][ i ] (which correctly gets you board[0][0], board[1][1] and board[2][2]). Well you can do it similarly with a loop, i, from 0 to 2 and then check board[ i ][2-i] (which gets you board[0][2], board[1][1] and board[2][0]).

Actually, it really should be board[ i ][boardSize-1-i] but also, all your consecutive>=2 should be consecutive>=(boardSize-1) and your random number generators should be ranX.nextInt(boardSize)

Also, while we are on the random number generators, you don't need to create them everytime you go around the loop, you can call nextInt on the same Random instance and you will still get a different number each time. And therefore, for the same reason, you don't need separate ones for X and Y, so just have one Random() created outside the loop and call nextInt on that one instance twice to get your x and y numbers.

Hope that all makes a little sense! :)
0
 

Author Comment

by:prototrumpet
Comment Utility
I have been working on your changes, and I haven't implemented the emptySquareList yet, but I did convert all of my checks into loops.  I had to add a winCheck() method, though, and I'm am having problems with it.  On the first click of the game, I think the winCheck method is closing the program.  I don't see where it is causing this at all.  The winCheck() method is between lines 182 and 223, the rest is older code that I commented out.  Here is the code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gameGUI.java
 *
 * Created on Feb 22, 2011, 11:37:10 AM
 */

/**
 *
 * @author cliffordworkman
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    
    JButton board[][]= new JButton[rows][cols];

    /** Creates new form gameGUI */
    public gameGUI()
    {
        initComponents();        
        
        board[0][0]=tl;
        board[0][1]=tm;
        board[0][2]=tr;
        board[1][0]=ml;
        board[1][1]=mm;         //Here I occupy the board array with JButtons
        board[1][2]=mr;
        board[2][0]=bl;
        board[2][1]=bm;
        board[2][2]=br;

        for(int i=0;i<rows-1;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols-1;j++)
                board[i][j].setLabel("");
        }
    }

    /** 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() {

        tl = new javax.swing.JButton();
        tm = new javax.swing.JButton();
        tr = new javax.swing.JButton();
        mr = new javax.swing.JButton();
        ml = new javax.swing.JButton();
        mm = new javax.swing.JButton();
        bm = new javax.swing.JButton();
        bl = new javax.swing.JButton();
        br = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tlActionPerformed(evt);
            }
        });

        tm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tmActionPerformed(evt);
            }
        });

        tr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        tr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trActionPerformed(evt);
            }
        });

        mr.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mrActionPerformed(evt);
            }
        });

        ml.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        ml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mlActionPerformed(evt);
            }
        });

        mm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        mm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mmActionPerformed(evt);
            }
        });

        bm.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmActionPerformed(evt);
            }
        });

        bl.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        bl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blActionPerformed(evt);
            }
        });

        br.setFont(new java.awt.Font("Lucida Grande", 0, 80)); // NOI18N
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ml, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(br, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public boolean winCheck()
    {
        for(int i=0;i<rows;i++)         //checks horizontal endgame
        {
            if(board[i][0].getLabel().equals(board[i][1].getLabel())&&board[i][2].getLabel().equals(board[i][0].getLabel()));
            {
                if (!(board[i][0].getLabel().equals("")))
                {
                    System.exit(0);
                    return true;                   
                }
            }
        }
        for(int i=0;i<cols;i++)         //checks vertical endgame
        {
            if(board[0][i].getLabel().equals(board[1][i].getLabel())&&board[2][i].getLabel().equals(board[0][i].getLabel()));
            {
                if (!(board[0][i].getLabel().equals("")))
                {
                    System.exit(0);
                    return true;
                }
            }
        }
        if(board[0][0].getLabel().equals(board[1][1].getLabel())&&board[2][2].getLabel().equals(board[0][0].getLabel()))    //checks vert 1
        {
            if(!(board[1][1].getLabel().equals("")))
                {
                    System.exit(0);
                    return true;
                }

        }
        if(board[0][2].getLabel().equals(board[1][1].getLabel())&&board[2][0].getLabel().equals(board[0][2].getLabel()))    //checks vert 2
        {
            if(!(board[1][1].getLabel().equals("")))
                {
                    System.exit(0);
                    return true;
                }
        }
        return false;
        /*if(board[0][0] == board[0][1] && board[0][1] == board[0][2])
        {
            if(board[0][0].getLabel().equals(""))
               return "";// human win
            else if(board[0][0].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[1][0] == board[1][1] && board[1][1] == board[1][2])
        {
            if(board[1][0].getLabel().equals(""))
               return "";// human win
            else if(board[1][0].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[2][0] == board[2][1] && board[2][1] == board[2][2])
        {
            if(board[2][0].getLabel().equals(""))
               return "";// human win
            else if(board[2][0].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[0][0] == board[1][0] && board[1][0] == board[2][0])
        {
            if(board[0][0].getLabel().equals(""))
               return "";// human win
            else if(board[0][0].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[0][1] == board[1][1] && board[1][1] == board[2][1])
        {
            if(board[0][1].getLabel().equals(""))
               return "";// human win
            else if(board[0][1].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[0][2] == board[1][2] && board[1][2] == board[2][2])
        {
            if(board[0][2].getLabel().equals(""))
               return "";// human win
            else if(board[0][2].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
        {
            if(board[0][0].getLabel().equals(""))
               return "";// human win
            else if(board[0][0].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
        {
            if(board[0][2].getLabel().equals(""))
               return "";// human win
            else if(board[0][2].getLabel().equals("X"))
                return "You Win!";
            else return "You Lose!";
        }
        else
            return "Next Turn";*/
    }
    public boolean horizontalOffense()
    {
        int consecutive = 0;
            for(int i=0;i<rows;i++)             //horizontal offense move checker
            {
                consecutive=0;
                for(int j=0;j<cols;j++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalOffense()
    {
       for(int j=0;j<rows;j++)             //vertical offense move checker
            {
                int consecutive=0;
                for(int i=0;i<cols;i++)
                {
                    if((board[i][j].getLabel()).equals("O"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to get 3 in a row!
                        {
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            } 
       return false;
    }
    public boolean firstDiagOffense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //this set of loops checks for the winning move in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("O"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean secondDiag()          //this one checks offense and defense at the same time
    {
       /* if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements check the other diagonal, first checking offensively, then defensively
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    if(board[2][0].getLabel().equals(""))
                    {
                        board[2][0].setLabel("O");
                        return true;
                    }
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    if(board[2][0].getLabel().equals(""))
                    {
                        board[2][0].setLabel("O");
                        return true;
                    }
                }
            }
            if(board[0][2].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[0][2].getLabel().equals("O"))
                {
                    if(board[1][1].getLabel().equals(""))
                    {
                        board[1][1].setLabel("O");
                        return true;
                    }
                }
                if(board[0][2].getLabel().equals("X"))
                {
                    if(board[1][1].getLabel().equals(""))
                    {
                        board[1][1].setLabel("O");
                        return true;
                    }
                }
            }
            if(board[1][1].getLabel().equals(board[2][0].getLabel()))
            {
                if(board[1][1].getLabel().equals("O"))
                {
                    if(board[0][2].getLabel().equals(""))
                    {
                        board[0][2].setLabel("O");
                        return true;
                    }
                }
                if(board[1][1].getLabel().equals("X"))
                {
                    if(board[0][2].getLabel().equals(""))
                    {
                        board[0][2].setLabel("O");
                        return true;
                    }
                }
            }
        return false;*/
        int consecutive=0;
        for(int i=0;i<rows;i++)
        {
            if(board[i][2-i].getLabel().equals("O"))
            {
                consecutive++;
            }
            if(consecutive>=rows-1)
                for(i=0;i<rows;i++)
                {
                    if(board[i][2-i].getLabel().equals(""))
                    {
                        board[i][2 - i].setLabel("O");
                        return true;
                    }
                }
        }
        consecutive=0;
        for(int i=0;i<rows;i++)
        {
            if(board[i][2-i].getLabel().equals("X"))
            {
                consecutive++;
            }
            if(consecutive>=rows-1)
                for(i=0;i<rows;i++)
                {
                    if(board[i][2-i].getLabel().equals(""))
                    {
                        board[i][2 - i].setLabel("O");
                        return true;
                    }
                }
        }
        return false;
    }
    public boolean horizontalDefense()
    {
        int consecutive=0;
        for(int i=0;i<rows;i++)             //horizontal defense move checker
            {
                consecutive=0;
                for(int j=0;j<cols;j++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<cols;k++)             //this loop uses the new int k to cycle back through the specific row to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[i][k].getLabel()).equals(""))
                            {
                                board[i][k].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    public boolean verticalDefense()
    {
        int consecutive=0;
        for(int j=0;j<rows;j++)             //vertical defense move checker
            {
                consecutive=0;
                for(int i=0;i<cols;i++)
                {
                    if((board[i][j].getLabel()).equals("X"))
                        consecutive++;
                    if(consecutive>=2)
                    {

                        for(int k=0;k<rows;k++)             //this loop uses the new int k to cycle back through the specific column to find the unnoccupied element in the array, to prevent 3 in a row!
                        {
                            if((board[k][j].getLabel()).equals(""))
                            {
                                board[k][j].setLabel("O");
                                return true;
                            }
                        }
                    }
                }
            }
        return false;
    }
    
    public boolean diagonalDefense()
    {
        int consecutive=0;
            for(int i=0;i<rows;i++)             //this set of loops checks for the winning move to prevent in the tl to br diagonal
            {
                if(board[i][i].getLabel().equals("X"))
                    consecutive++;
                if(consecutive>=2)
                {
                    for(int j=0;j<rows;j++)
                    {
                        if(board[j][j].getLabel().equals(""))
                        {
                            board[j][j].setLabel("O");
                            return true;
                        }
                    }
                }
            }
        return false;
    }
    public boolean diagonalMove()
    {
        /*if(board[0][2].getLabel().equals(board[1][1].getLabel()))           //these if statements prioratize a strategy
        {
            if(board[0][2].getLabel().equals(""))
            {
                if(board[2][0].getLabel().equals(""))
                    {
                        board[2][0].setLabel("O");
                        return true;
                    }
            }
        }
        if(board[0][2].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[0][2].getLabel().equals(""))
            {
                if(board[1][1].getLabel().equals(""))
                    {
                        board[1][1].setLabel("O");
                        return true;
                    }
            }
        }
        if(board[1][1].getLabel().equals(board[2][0].getLabel()))
        {
            if(board[1][1].getLabel().equals(""))
            {
                if(board[0][2].getLabel().equals(""))
                    {
                        board[0][2].setLabel("O");
                        return true;
                    }
            }
        }
        return false;*/
        int consecutive=0;
        for(int i=0;i<rows;i++)
        {
            if(board[i][i].getLabel().equals(""))
            {
                consecutive++;
            }
            if(consecutive>=rows-1)
                for(i=0;i<rows-1;i++)
                {
                    if(board[i][i].getLabel().equals(""))
                    {
                        board[i][i].setLabel("O");
                        return true;
                    }
                }
        }
        for(int i=0;i<rows;i++)
        {
            if(board[i][2-i].getLabel().equals(""))
            {
                consecutive++;
            }
            if(consecutive>=rows-1)
                for(i=0;i<rows;i++)
                {
                    if(board[i][2-i].getLabel().equals(""))
                    {
                        board[i][2 - i].setLabel("O");
                        return true;
                    }
                }
        }
        return false;
    }
    public boolean RandomMove()
    {
        int x,y;
        do
        {
            Random ranX = new Random();
            Random ranY = new Random();
            x = ranX.nextInt(3);
            y = ranY.nextInt(3);
            if(board[x][y].getLabel().matches(""))
            {
                board[x][y].setLabel("O");
                return true;
            }
        }while(true);
        
    }
    
    public void com()
    {
        if(!(horizontalOffense()))
            if(!(verticalOffense()))
                if(!(firstDiagOffense()))
                    if(!(secondDiag()))
                        if(!(horizontalDefense()))
                            if(!(verticalDefense()))
                                if(!(diagonalDefense()))
                                    if(!(diagonalMove()))
                                        RandomMove();
    }
    private void tlActionPerformed(java.awt.event.ActionEvent evt) {
        if(tl.getLabel().equals(""))
        {
            tl.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals(""))
        {
            tm.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals(""))
        {
            tr.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals(""))
        {
            ml.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals(""))
        {
            mm.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals(""))
        {
            mr.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals(""))
        {
            bl.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals(""))
        {
            bm.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals(""))
        {
            br.setLabel("X");
            System.out.println((winCheck()));
            com();
            System.out.println(winCheck());
        }
    }

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

    // Variables declaration - do not modify
    private javax.swing.JButton bl;
    private javax.swing.JButton bm;
    private javax.swing.JButton br;
    private javax.swing.JButton ml;
    private javax.swing.JButton mm;
    private javax.swing.JButton mr;
    private javax.swing.JButton tl;
    private javax.swing.JButton tm;
    private javax.swing.JButton tr;
    // End of variables declaration

}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now