Solved

Tic Tac Toe Trouble

Posted on 2011-02-24
33
1,485 Views
Last Modified: 2013-12-14
I am working on a project for my computer science class, and I am having  issues with my program.  I had an open question yesterday that someone was very helpful with, but now I am adding new methods and getting new problems.

I have a winCheck() method, which is supposed to change the label on a button at the bottom of my window to say if someone has won the game.  Also, I have an AI that should change that same label to say "Cat!" if the game is a tie, and it has run out of potential moves.  This label is not changing for some reason, and I can't find the cause of that problem.

Any help would be immensely appreciated.
/*
 * 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, dallas chaney, brad hammond
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    int turn=0;
    int consecutive=0;
    String gameSetting="";
    String toCheck="";

    Random myRandom = new Random();
    JButton board[][]= new JButton[rows][cols];
    ArrayList<JButton> boardList;

    /** 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;

        boardList = new ArrayList<JButton>();

        nextGame();

    }

    /** 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();
        gameTog = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tic Tac Toe");
        setBackground(new java.awt.Color(255, 255, 255));

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        gameTog.setText("1P");
        gameTog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameTogActionPerformed(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(gameTog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .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())
        );
        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))
                .add(18, 18, 18)
                .add(gameTog, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        
    public void nextGame()
    {
        for(int i=0;i<rows;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols;j++)
            {
                board[i][j].setLabel("");
                boardList.add(board[i][j]);
            }
        }
        gameTog.setText("1P");
        gameSetting=gameTog.getText();
    }
    public void boardCheck()
    {
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<rows;j++)
            {
                if(!(board[i][j]).getLabel().equals(""))
                    boardList.remove(board[i][j]);
            }
        }
    }
    public void winCheck()
    {
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)             //checks horizontal wins
        {
            consecutive=0;
            toCheck="";
            for(int j=0;j<cols;j++)
            {
                if(j==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().matches(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                    gameTog.setLabel(toCheck+" Wins!  Play Again?");
            }
        }
        for(int j=0;j<rows;j++)             //check vertically
        {
            consecutive=0;
            toCheck="";
            for(int i=0;i<cols;i++)
            {
                if(i==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                    gameTog.setLabel(toCheck+" Wins!  Play Again?");
            }
        }
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)     //check first diagonal
        {
            if(i==0&&(!(board[i][i]).getLabel().equals("")))
            {
                toCheck=board[i][i].getLabel();
                consecutive++;
            }
                else if(board[i][i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
            if(consecutive==rows)
                gameTog.setLabel(toCheck+" Wins!  Play Again?");
        }
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)     //check last diagonal
        {
            if(i==0&&(!(board[i][2-i].getLabel().equals(""))))
            {
                toCheck=board[i][2-i].getLabel();
                consecutive++;
            }
                else if(board[i][2-i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                    consecutive++;
                    else;
            if(consecutive==rows)
                gameTog.setLabel(toCheck+" Wins!  Play Again?");
        }
        gameTog.setLabel("Next Turn");
    }
    public boolean horizontalOffense()
    {
        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
            {
                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()
    {
        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
    {
        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()
    {
        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()
    {
        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()
    {
        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()
    {
        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()
    {
        boardCheck();
        do
        {
            if(boardList.size()>0)
                boardList.get(myRandom.nextInt(boardList.size())).setLabel("O");
            else
                gameTog.setLabel("Cat!");
        }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("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(tm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void trActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(tr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(ml.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    ml.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    ml.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                ml.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(mm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(mr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void blActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(bl.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(bm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void brActionPerformed(java.awt.event.ActionEvent evt) {                                   
        if(br.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    br.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    br.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                br.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }                                  

    private void gameTogActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if(gameTog.getLabel().equals("1P"))
        {
            gameTog.setLabel("2P");
            gameSetting=gameTog.getLabel();
        }
            else if(gameTog.getLabel().equals("2P"))
            {
                gameTog.setLabel("1P");
                gameSetting=gameTog.getLabel();
            }
                else if(gameTog.getLabel().equals(toCheck+" Wins!  Play Again?")||gameTog.getLabel().equals("Cat!"))
                {
                    nextGame();
                }

    }                                       

    /**
    * @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 gameTog;
    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
  • 13
  • 11
  • 8
  • +1
33 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34974186
I wanted to look at your code, but I'm missing GroupLayout: org.jdesktop....
I'm using java 1.6 it has GroupLayout, but I guess it is different.
Do you use netbeans?

If you know some jar which could help me, let me know

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974193
Yes, I see you use netbeans.
I wanted to compile it in IntelliJ
0
 

Author Comment

by:prototrumpet
ID: 34974270
Yes, I'm using netbeans.  The package is included in the IDE.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974304
Even in netbeans 6.9.1 I don't have it - probably I need to add some jar
0
 

Author Comment

by:prototrumpet
ID: 34974396
I looked through it, and it seems I am using swing-layout-1.0.4.jar
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974420
Yes, I figured out by now.
It seems that I can compile
0
 

Author Comment

by:prototrumpet
ID: 34974438
Oh, ok then, sorry, I'm new to GUI's and using netbeans.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974449
And I never used netbeans either
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974499
So you mean that at the moment that it should declare the winner, it just hangs - that;s waht you observe?
At least that's what I seem to observe
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974586
I think the reason you never see Wins! Play again - is because
your method inthe end finishes with this operrator:
gameTog.setLabel("Next Turn");
So you do not notice when t changes  to win - it is immediately overwritten by Next Turn.
I commented out this line gameTog.setLabel("Next Turn");
 and now I see wins, but i I never see Next Turn.
So can you figure out where witihin the loop you place  gameTog.setLabel("Next Turn");
so that it shows them when neecssary?

Let me know if this is useful, or I'm misunderstanding your problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34974753
Your checking is not correct. Here's one of the trickier ones - checking the diagonals. Do the same in reverse for the other. The horizontals should be easier
String s = board[0][0].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = s.equals(board[i][col++].getLabel());

	    if (!fullDiagonal) {
		break;
	    }
	}

	if (fullDiagonal) {
	    gameTog.setLabel(s + " Wins!  Play Again?");
	    return;
	}

Open in new window

0
 

Author Comment

by:prototrumpet
ID: 34974800
I tried commenting it out and noticed that it will only say that O wins, even when X wins.  I know that the loop logic is solid, but I have noticed one thing about the program and that is that it only sets the Labels when it finishes every line of code under the button action.  I think that is messing up my system of checking the labels, what do you think?
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 34974963
Like with what we were doing with the move checking methods, you should return from the wincheck method straight away, as soon as you find a win. That way you can leave the sellabel("Next Turn") at the bottom and that should work.

Also, I think you are not seeing "Cat!" because you still have the do-while loop. Now that you are selecting an empty square from the list, you don't need to loop at all. At the moment, if you get to the loop, there is no way to get out of it, so it hangs there.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34974966
Yes, it is very much possible, that your checks are not in all cases OK see comments of CEHJ), only at first I could not see Wins at all.
So that was because it was overqwritten by Next Turn.
I'm also not sure ,why you use these threads in the mamin method - ut seems to me
that sometimes there are some problems with these.
I just removed that part just left one line new ....setVisible() and
it stopped hanging after the win (or maybe almost stopped hanging)

0
 

Author Comment

by:prototrumpet
ID: 34976065
I made the changes that everyone has mentioned, but I am still having the exact same results ):

Here is the updated 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, dallas chaney, brad hammond
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    int turn=0;
    int consecutive=0;
    String gameSetting="";
    String toCheck="";

    Random myRandom = new Random();
    JButton board[][]= new JButton[rows][cols];
    ArrayList<JButton> boardList;

    /** 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;

        boardList = new ArrayList<JButton>();

        nextGame();

    }

    /** 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();
        gameTog = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tic Tac Toe");
        setBackground(new java.awt.Color(255, 255, 255));

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        gameTog.setText("1P");
        gameTog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameTogActionPerformed(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(gameTog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .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())
        );
        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))
                .add(18, 18, 18)
                .add(gameTog, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public void nextGame()
    {
        for(int i=0;i<rows;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols;j++)
            {
                board[i][j].setLabel("");
                boardList.add(board[i][j]);
            }
        }
        gameTog.setText("1P");
        gameSetting=gameTog.getText();
    }
    public void boardCheck()
    {
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<rows;j++)
            {
                if(!(board[i][j]).getLabel().equals(""))
                    boardList.remove(board[i][j]);
            }
        }
    }
    public boolean horizontalCheck()
    {
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)             //checks horizontal wins
        {
            consecutive=0;
            toCheck="";
            for(int j=0;j<cols;j++)
            {
                if(j==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().matches(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }

            }
        }
        return false;
    }
    public boolean verticalCheck()
    {
        for(int j=0;j<rows;j++)             //check vertically
        {
            consecutive=0;
            toCheck="";
            for(int i=0;i<cols;i++)
            {
                if(i==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
        }
        return false;
    }

    public boolean firstDiagCheck()
    {
        /*consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)     //check first diagonal
        {
            if(i==0&&(!(board[i][i]).getLabel().equals("")))
            {
                toCheck=board[i][i].getLabel();
                consecutive++;
            }
                else if(board[i][i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        String s = board[0][0].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = s.equals(board[i][col++].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!s.equals(""))
                {
                    gameTog.setLabel(s + " Wins!  Play Again?");
                    return true;
                }
            }
	}
    }
    public boolean secondDiagCheck()
    {
        /*consecutive=0;
        toCheck=""; 
        for(int i=0;i<rows;i++)     //check last diagonal
        {
            if(i==0&&(!(board[i][2-i].getLabel().equals(""))))
            {
                toCheck=board[i][2-i].getLabel();
                consecutive++;
            }
                else if(board[i][2-i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                    consecutive++;
                    else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        String s = board[0][2].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = s.equals(board[i][col--].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!s.equals(""))
                {
                    gameTog.setLabel(s + " Wins!  Play Again?");
                    return true;
                }
            }
	}
    }
    
    public void winCheck()
    {

        if(!horizontalCheck())
            if(!verticalCheck())
                if(!firstDiagCheck())
                    if(!secondDiagCheck())
                        gameTog.setLabel("Next Turn");
    }
    public boolean horizontalOffense()
    {
        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
            {
                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()
    {
        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
    {
        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()
    {
        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()
    {
        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()
    {
        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()
    {
        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 void RandomMove()
    {
        boardCheck();
        if(boardList.size()>0)
            boardList.get(myRandom.nextInt(boardList.size())).setLabel("O");
        else
            gameTog.setLabel("Cat!");
    }


    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("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    ml.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    ml.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                ml.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    br.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    br.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                br.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void gameTogActionPerformed(java.awt.event.ActionEvent evt) {
        if(gameTog.getLabel().equals("1P"))
        {
            gameTog.setLabel("2P");
            gameSetting=gameTog.getLabel();
        }
            else if(gameTog.getLabel().equals("2P"))
            {
                gameTog.setLabel("1P");
                gameSetting=gameTog.getLabel();
            }
                else if(gameTog.getLabel().equals(toCheck+" Wins!  Play Again?")||gameTog.getLabel().equals("Cat!"))
                {
                    nextGame();
                }

    }

    /**
    * @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 gameTog;
    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 47

Expert Comment

by:for_yan
ID: 34976132
For the begiining my compiler says that firstdiagCheck and secondDiagCheck
not always have required return values - did you have the same?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:prototrumpet
ID: 34976198
I wasn't getting any complaints from the compiler...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34976216
That is strange; it reports error to me, if I don't add either return true; or return false
as the last lines of both firstDiagCheck and secondDiagCheck


0
 
LVL 35

Expert Comment

by:mccarl
ID: 34976226
Well, you should be!! :) Or else, the code you have is different to what you posted here.

Anyway, if you put a "return false;" at the bottom of both the DiagCheck methods, it seems to work ok for me.

And then, to fix the "Cat!" no coming up at the end of a drawn game, you need to put this

         if(!gameTog.getLabel().equals("Cat!"))

As the first line in the winCheck() method. The problem is that the com() function runs and ends up filtering down to the randommove() method, it can't move so it updates the label to "Cat!" but then in each button actionlistener wincheck is always called after com() and then only outcomes of wincheck can be setting the label to a "win" message or to "Next turn"
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34976239
I think it also alloow to make one more move after the win by both X and O. Is this correct behavior?
0
 
LVL 35

Expert Comment

by:mccarl
ID: 34976245
However, to support the 2P mode properly, you should move that DrawnCheck from Com() to winCheck(). As winCheck() is really gameEndedCheck() and a drawn game is just another way for the game to end. That way even when the game is 2P (i.e when Com() isn't being called) you can still know if the game is drawn and allow a restart!
0
 

Author Comment

by:prototrumpet
ID: 34976458
Ok, I found the missing return statement, I didn't notice it because it let me compile, and it only underlined one of the curly brackets and I overlooked it.
I think this is the last hurdle, for some reason I am getting this error now, when I compile.  Thanks for all of your help btw.

Exception in thread "main" java.lang.NoClassDefFoundError: newtictactoe/Main
Caused by: java.lang.ClassNotFoundException: newtictactoe.Main
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Here is my code at this point:
package newtictactoe;

/*
 * 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, dallas chaney, brad hammond
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    int turn=0;
    int consecutive=0;
    String gameSetting="";
    String toCheck="";

    Random myRandom = new Random();
    JButton board[][]= new JButton[rows][cols];
    ArrayList<JButton> boardList;

    /** 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;

        boardList = new ArrayList<JButton>();

        nextGame();

    }

    /** 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();
        gameTog = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tic Tac Toe");
        setBackground(new java.awt.Color(255, 255, 255));

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        gameTog.setText("1P");
        gameTog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameTogActionPerformed(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(gameTog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .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())
        );
        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))
                .add(18, 18, 18)
                .add(gameTog, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public void nextGame()
    {
        for(int i=0;i<rows;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols;j++)
            {
                board[i][j].setLabel("");
                boardList.add(board[i][j]);
            }
        }
        gameTog.setText("1P");
        gameSetting=gameTog.getText();
    }
    public void boardCheck()
    {
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<rows;j++)
            {
                if(!(board[i][j]).getLabel().equals(""))
                    boardList.remove(board[i][j]);
            }
        }
    }
    public boolean horizontalCheck()
    {
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)             //checks horizontal wins
        {
            consecutive=0;
            toCheck="";
            for(int j=0;j<cols;j++)
            {
                if(j==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().matches(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }

            }
        }
        return false;
    }
    public boolean verticalCheck()
    {
        for(int j=0;j<rows;j++)             //check vertically
        {
            consecutive=0;
            toCheck="";
            for(int i=0;i<cols;i++)
            {
                if(i==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
        }
        return false;
    }

    public boolean firstDiagCheck()
    {
        /*consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)     //check first diagonal
        {
            if(i==0&&(!(board[i][i]).getLabel().equals("")))
            {
                toCheck=board[i][i].getLabel();
                consecutive++;
            }
                else if(board[i][i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        String s = board[0][0].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = s.equals(board[i][col++].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!s.equals(""))
                {
                    gameTog.setLabel(s + " Wins!  Play Again?");
                    return true;
                }
            }
	}
        return false;
    }
    public boolean secondDiagCheck()
    {
        /*consecutive=0;
        toCheck=""; 
        for(int i=0;i<rows;i++)     //check last diagonal
        {
            if(i==0&&(!(board[i][2-i].getLabel().equals(""))))
            {
                toCheck=board[i][2-i].getLabel();
                consecutive++;
            }
                else if(board[i][2-i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                    consecutive++;
                    else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        String s = board[0][2].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = s.equals(board[i][col--].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!s.equals(""))
                {
                    gameTog.setLabel(s + " Wins!  Play Again?");
                    return true;
                }
            }
	}
        return false;
    }
    
    public void winCheck()
    {

        if(!horizontalCheck())
            if(!verticalCheck())
                if(!firstDiagCheck())
                    if(!secondDiagCheck())
                        gameTog.setLabel("Next Turn");
    }
    public boolean horizontalOffense()
    {
        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
            {
                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()
    {
        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
    {
        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()
    {
        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()
    {
        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()
    {
        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()
    {
        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 void RandomMove()
    {
        boardCheck();
        if(boardList.size()>0)
            boardList.get(myRandom.nextInt(boardList.size())).setLabel("O");
        else
            gameTog.setLabel("Cat!");
    }


    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("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    ml.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    ml.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                ml.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins!  Play"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    br.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    br.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                br.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void gameTogActionPerformed(java.awt.event.ActionEvent evt) {
        if(gameTog.getLabel().equals("1P"))
        {
            gameTog.setLabel("2P");
            gameSetting=gameTog.getLabel();
        }
            else if(gameTog.getLabel().equals("2P"))
            {
                gameTog.setLabel("1P");
                gameSetting=gameTog.getLabel();
            }
                else if(gameTog.getLabel().equals(toCheck+" Wins!  Play Again?")||gameTog.getLabel().equals("Cat!"))
                {
                    nextGame();
                }

    }

    /**
    * @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 gameTog;
    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

Expert Comment

by:mccarl
ID: 34976479
Your using Netbeans aren't you? If so, right click on your project, go to properties, go to the "Run" section on the left, click the Browse button to the right of Main class, and choose the version of gameGUI that you want to run.
0
 

Author Comment

by:prototrumpet
ID: 34976503
OMG, I did that, and there was only one option, so I selected that one.  Now, the project has gone back to some older state, now the winCheck label change isn't working anymore.  sigh.  I'm going to have to root through this code again and see where the problem is at.  Do you know if there is a way to undo the change that I made?
0
 

Author Comment

by:prototrumpet
ID: 34976515
Nevermind, I got it back.  That was a scare and a half.  Thanks guys for the help.  I was actually having major problems with that error on another project, and I just had to give up and use BlueJ to do my work.  I didn't know you could locate the main file so easily.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 34976557
Yeah, it probably not the best to keep different versions of the same class in the one project. That's not really what packages are for, and it can sometimes confuse you if you don't know what is going on. If you do want to save a snapshot of what the code is like at a point in time (and you don't have a versioning system like SVN,CVS, etc) just copy the file to somewhere outside the project and then maybe add the date to the name, so you also know when it was a snapshot of. eg..

gameGUI.java
         -- lives inside your project and is the only one inside the project


gameGUI-2011-02-22-0943.java
gameGUI-2011-02-22-1521.java
gameGUI-2011-02-23-0705.java
gameGUI-2011-02-25-0423.java
         -- All live outside your project so you and Netbeans can't get them confused and automatically start using the old code.
0
 

Author Comment

by:prototrumpet
ID: 34976559
This may be my final question for you guys.  In my button events, I had a condition in my if statement that required the button label to be blank, and for the gameTog button label to not have toCheck+" Wins!  Play Again?"

This is so the player (and the computer but I have some changes to make for that to apply to it as well) can't continue moving when the game is over.

Here is my code, any ideas?
package newtictactoe;

/*
 * 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, dallas chaney, brad hammond
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    int turn=0;
    int consecutive=0;
    String gameSetting="";
    String toCheck="";

    Random myRandom = new Random();
    JButton board[][]= new JButton[rows][cols];
    ArrayList<JButton> boardList;

    /** 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;

        boardList = new ArrayList<JButton>();

        nextGame();

    }

    /** 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();
        gameTog = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tic Tac Toe");
        setBackground(new java.awt.Color(255, 255, 255));

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        gameTog.setText("1P");
        gameTog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameTogActionPerformed(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(gameTog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .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())
        );
        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))
                .add(18, 18, 18)
                .add(gameTog, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public void nextGame()
    {
        for(int i=0;i<rows;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols;j++)
            {
                board[i][j].setLabel("");
                boardList.add(board[i][j]);
            }
        }
        gameTog.setText("1P");
        gameSetting=gameTog.getText();
    }
    public void boardCheck()
    {
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<rows;j++)
            {
                if(!(board[i][j]).getLabel().equals(""))
                    boardList.remove(board[i][j]);
            }
        }
    }
    public boolean horizontalCheck()
    {
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)             //checks horizontal wins
        {
            consecutive=0;
            toCheck="";
            for(int j=0;j<cols;j++)
            {
                if(j==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().matches(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }

            }
        }
        return false;
    }
    public boolean verticalCheck()
    {
        for(int j=0;j<rows;j++)             //check vertically
        {
            consecutive=0;
            toCheck="";
            for(int i=0;i<cols;i++)
            {
                if(i==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
        }
        return false;
    }

    public boolean firstDiagCheck()
    {
        /*consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)     //check first diagonal
        {
            if(i==0&&(!(board[i][i]).getLabel().equals("")))
            {
                toCheck=board[i][i].getLabel();
                consecutive++;
            }
                else if(board[i][i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        toCheck = board[0][0].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = toCheck.equals(board[i][col++].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!toCheck.equals(""))
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
	}
        return false;
    }
    public boolean secondDiagCheck()
    {
        /*consecutive=0;
        toCheck=""; 
        for(int i=0;i<rows;i++)     //check last diagonal
        {
            if(i==0&&(!(board[i][2-i].getLabel().equals(""))))
            {
                toCheck=board[i][2-i].getLabel();
                consecutive++;
            }
                else if(board[i][2-i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                    consecutive++;
                    else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        toCheck = board[0][2].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = toCheck.equals(board[i][col--].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!toCheck.equals(""))
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
	}
        return false;
    }
    
    public void winCheck()
    {

        if(!horizontalCheck())
            if(!verticalCheck())
                if(!firstDiagCheck())
                    if(!secondDiagCheck())
                        gameTog.setLabel("Next Turn");
    }
    public boolean horizontalOffense()
    {
        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
            {
                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()
    {
        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
    {
        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()
    {
        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()
    {
        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()
    {
        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()
    {
        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 void RandomMove()
    {
        boardCheck();
        if(boardList.size()>0)
            boardList.get(myRandom.nextInt(boardList.size())).setLabel("O");
        else
            gameTog.setLabel("Cat!");
    }


    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("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    ml.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    ml.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                ml.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mr.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bl.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bm.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    br.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    br.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                br.setLabel("X");
                winCheck();
                com();
                winCheck();
            }
        }
    }

    private void gameTogActionPerformed(java.awt.event.ActionEvent evt) {
        if(gameTog.getLabel().equals("1P"))
        {
            gameTog.setLabel("2P");
            gameSetting=gameTog.getLabel();
        }
            else if(gameTog.getLabel().equals("2P"))
            {
                gameTog.setLabel("1P");
                gameSetting=gameTog.getLabel();
            }
                else if(gameTog.getLabel().equals(toCheck+" Wins!  Play Again?")||gameTog.getLabel().equals("Cat!"))
                {
                    nextGame();
                }

    }

    /**
    * @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 gameTog;
    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

Expert Comment

by:mccarl
ID: 34976615
Not sure exactly what the question was here!! :)  But I will assume that it is related to the use of the toCheck variable. But what you are really after is just knowing if the game is finished, yeah? Well then instead of

 getLabel().equals(toCheck+" Wins! Play Again?")

use...

getLabel().endsWith("Wins! Play Again?")



Also, another point on just general improvements that you could do to this code, you really shouldn't use the label's on all these buttons to control the logic of what to do. You should have a separate object that has variables to track the boardState (ie. X, O or empty at each square) and the gameState (playMode, turn, gameWon, gameDrawn, etc). All your logic is based off these state objects, and updates these state objects, and then after each move, you update the visual board to correspond to the state objects.

0
 

Author Comment

by:prototrumpet
ID: 34976657
I have updated my code to what I thought should also prevent the computer from moving after the player has made the winning move, but for some reason it still takes the extra move, and the player can continue making choices after the game should be ended.

For example, if you select in this order: top left, bottom middle, and then middle left, you will win, but the computer will continue moving, also having 3 in a row on the right side of the board.

Here is the updated code:
package newtictactoe;

/*
 * 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, dallas chaney, brad hammond
 */
import javax.swing.*;
import java.util.*;

public class gameGUI extends javax.swing.JFrame
{
    int rows=3;
    int cols=3;
    int turn=0;
    int consecutive=0;
    String gameSetting="";
    String toCheck="";

    Random myRandom = new Random();
    JButton board[][]= new JButton[rows][cols];
    ArrayList<JButton> boardList;

    /** 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;

        boardList = new ArrayList<JButton>();

        nextGame();

    }

    /** 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();
        gameTog = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tic Tac Toe");
        setBackground(new java.awt.Color(255, 255, 255));

        tl.setFont(new java.awt.Font("Lucida Grande", 0, 80));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        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));
        br.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brActionPerformed(evt);
            }
        });

        gameTog.setText("1P");
        gameTog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameTogActionPerformed(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(gameTog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .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())
        );
        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))
                .add(18, 18, 18)
                .add(gameTog, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    public void nextGame()
    {
        for(int i=0;i<rows;i++)                   //resets labels on buttons
        {
            for(int j=0;j<cols;j++)
            {
                board[i][j].setLabel("");
                boardList.add(board[i][j]);
            }
        }
        gameTog.setText("1P");
        gameSetting=gameTog.getText();
    }
    public void boardCheck()
    {
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<rows;j++)
            {
                if(!(board[i][j]).getLabel().equals(""))
                    boardList.remove(board[i][j]);
            }
        }
    }
    public boolean horizontalCheck()
    {
        consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)             //checks horizontal wins
        {
            consecutive=0;
            toCheck="";
            for(int j=0;j<cols;j++)
            {
                if(j==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().matches(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }

            }
        }
        return false;
    }
    public boolean verticalCheck()
    {
        for(int j=0;j<rows;j++)             //check vertically
        {
            consecutive=0;
            toCheck="";
            for(int i=0;i<cols;i++)
            {
                if(i==0&&(!(board[i][j].getLabel().equals(""))))
                {
                    toCheck=board[i][j].getLabel();
                    consecutive++;
                }
                    else if(board[i][j].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
                if(consecutive==rows)
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
        }
        return false;
    }

    public boolean firstDiagCheck()
    {
        /*consecutive=0;
        toCheck="";
        for(int i=0;i<rows;i++)     //check first diagonal
        {
            if(i==0&&(!(board[i][i]).getLabel().equals("")))
            {
                toCheck=board[i][i].getLabel();
                consecutive++;
            }
                else if(board[i][i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                        consecutive++;
                        else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        toCheck = board[0][0].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = toCheck.equals(board[i][col++].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!toCheck.equals(""))
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
	}
        return false;
    }
    public boolean secondDiagCheck()
    {
        /*consecutive=0;
        toCheck=""; 
        for(int i=0;i<rows;i++)     //check last diagonal
        {
            if(i==0&&(!(board[i][2-i].getLabel().equals(""))))
            {
                toCheck=board[i][2-i].getLabel();
                consecutive++;
            }
                else if(board[i][2-i].getLabel().equals(toCheck)&&(!(toCheck.equals(""))))
                    consecutive++;
                    else;
            if(consecutive==rows)
            {
                gameTog.setLabel(toCheck + " Wins!  Play Again?");
                return true;
            }
        }
        return false;*/
        toCheck = board[0][2].getLabel();
        boolean fullDiagonal = true;
	int col = 1;
	//check first diagonal
	for (int i = 1; i < rows; i++) {
	    fullDiagonal = toCheck.equals(board[i][col--].getLabel());

	    if (!fullDiagonal) {
		return false;
	    }
	}

	if (fullDiagonal) {
            {
                if(!toCheck.equals(""))
                {
                    gameTog.setLabel(toCheck + " Wins!  Play Again?");
                    return true;
                }
            }
	}
        return false;
    }
    
    public boolean catCheck()
    {
        boolean cat=true;
        for(int i=0;i<rows;i++)
        {
            for(int j=0; j<rows; j++)
            {
               cat=!board[i][j].getLabel().equals("");
               if(!cat)
               {
                   return false;
               }
            }
        }
        if(cat)
        {
            gameTog.setLabel("Cat!");
            return true;
        }
        return false;
    }
    public void winCheck()
    {

        if(!horizontalCheck())
            if(!verticalCheck())
                if(!firstDiagCheck())
                    if(!secondDiagCheck())
                        if(!catCheck())
                            gameTog.setLabel("Next Turn");
    }
    public boolean horizontalOffense()
    {
        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
            {
                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()
    {
        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
    {
        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()
    {
        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()
    {
        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()
    {
        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()
    {
        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 void RandomMove()
    {
        boardCheck();
        if(boardList.size()>0)
            boardList.get(myRandom.nextInt(boardList.size())).setLabel("O");
        else if(boardList.size()==0)
            gameTog.setLabel("Cat!");
    }


    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("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tl.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void tmActionPerformed(java.awt.event.ActionEvent evt) {
        if(tm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tm.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void trActionPerformed(java.awt.event.ActionEvent evt) {
        if(tr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    tr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    tr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                tr.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void mlActionPerformed(java.awt.event.ActionEvent evt) {
        if(ml.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    ml.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    ml.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                ml.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void mmActionPerformed(java.awt.event.ActionEvent evt) {
        if(mm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mm.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void mrActionPerformed(java.awt.event.ActionEvent evt) {
        if(mr.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    mr.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    mr.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                mr.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void blActionPerformed(java.awt.event.ActionEvent evt) {
        if(bl.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bl.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bl.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bl.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void bmActionPerformed(java.awt.event.ActionEvent evt) {
        if(bm.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    bm.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    bm.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                bm.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void brActionPerformed(java.awt.event.ActionEvent evt) {
        if(br.getLabel().equals("")&&(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?"))))
        {
            if(gameSetting.equals("2P"))
                if(turn==0)
                {
                    turn = 1;
                    br.setLabel("X");
                    winCheck();
                }
                else if(turn==1)
                {
                    turn = 0;
                    br.setLabel("O");
                    winCheck();
                }
                    else;
            if(gameSetting.equals("1P"))
            {
                br.setLabel("X");
                winCheck();
                if(!(gameTog.getLabel().equals(toCheck+" Wins! Play Again?")))
                {
                    com();
                    winCheck();
                }
            }
        }
    }

    private void gameTogActionPerformed(java.awt.event.ActionEvent evt) {
        if(gameTog.getLabel().equals("1P"))
        {
            gameTog.setLabel("2P");
            gameSetting=gameTog.getLabel();
        }
            else if(gameTog.getLabel().equals("2P"))
            {
                gameTog.setLabel("1P");
                gameSetting=gameTog.getLabel();
            }
                else if(gameTog.getLabel().equals(toCheck+" Wins!  Play Again?")||gameTog.getLabel().equals("Cat!"))
                {
                    nextGame();
                }

    }

    /**
    * @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 gameTog;
    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

Expert Comment

by:mccarl
ID: 34976715
You've illustrated my point about not relying on the labels to drive your logic PERFECTLY!! :)

Look at the number of spaces in between "Wins!" and "Play Again?", both in the winCheck methods that sets the label and in the actionPerformed methods that are checking the labels!!!

And then consider, if you ever want to change what these labels say in the future, you have to go through and change EVERY instance of that string in your program. You can relieve this a little by defining these strings once at the top of your program and using that, but still... much nicer to go with what I wrote in the previous post.
0
 

Author Comment

by:prototrumpet
ID: 34976766
I see, so I will definitely define those strings at the top and make those changes.  Not that I don't agree with you about using different  objects to represent the boardState, but I already got this far with the labels for the logic, and the project is so close to the close.  I will definitely redo this tomorrow with your suggestion, tracking and updating the boardstate, but I need to get this to run as planned just for tonight.  Your suggestions have been a great help :)
0
 

Author Comment

by:prototrumpet
ID: 34976817
And I am all fixed up!  Like you said, I defined a string wins for the " Wins!..." string in my if statements, and that fixed everything :)  
0
 
LVL 35

Expert Comment

by:mccarl
ID: 34976826
:) Glad to hear!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 67
topping3 challenge 14 52
unix in java example 9 43
ArrayIndexOutOfBoundException 9 39
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

9 Experts available now in Live!

Get 1:1 Help Now