3
\$\begingroup\$

So I got bored and made a simple noughts and crosses game with colours instead of symbols.

The colours are red and blue and I made the GUI in NetBeans with my own code in combination with the NetBeans GUI Editor.

My Code:

package com.coal;

import java.util.Random;


public class Noughts extends javax.swing.JFrame {


public Noughts() {
    if (ran == 0) {
        move = "Red";
    } else {
        move = "Blue";
    }
    initComponents();

}

private void checkForVictory() {
    //Horizontal Victories
    if (color1 == 1 && color2 == 1 && color3 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color1 == 2 && color2 == 2 && color3 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    if (color4 == 1 && color5 == 1 && color6 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color4 == 2 && color5 == 2 && color6 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    if (color7 == 1 && color8 == 1 && color9 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color7 == 2 && color8 == 2 && color9 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    //Vertical Victories
    if (color1 == 1 && color4 == 1 && color7 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color1 == 2 && color4 == 2 && color7 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    if (color2 == 1 && color5 == 1 && color8 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color2 == 2 && color5 == 2 && color8 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    if (color3 == 1 && color6 == 1 && color9 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color3 == 2 && color6 == 2 && color9 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    //Diagonal Victories
    if (color1 == 1 && color5 == 1 && color9 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color1 == 2 && color5 == 2 && color9 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }

    if (color3 == 1 && color5 == 1 && color7 == 1) {
        winner = "Red";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    if (color3 == 2 && color5 == 2 && color7 == 2) {
        winner = "Blue";
        System.out.println("Winner!" + winner);
        gameOver = true;
        jTextPane1.setText("Winner is " + winner);
    }
    //Draw Check
    if(color1 != 0 && color2 != 0 && color3 != 0&& color4 != 0&& color5 != 0&& color6 != 0&& color7 != 0&& color8 != 0&& color9 != 0 && winner == ""){
        System.out.println("Draw!");
        gameOver = true;
         jTextPane1.setText("Draw!");
    }
}

//0 is Plain, 1 is Red, 2 is Blue.
public static int color1 = 0;
public static int color2 = 0;
public static int color3 = 0;
public static int color4 = 0;
public static int color5 = 0;
public static int color6 = 0;
public static int color7 = 0;
public static int color8 = 0;
public static int color9 = 0;

public static String move;
public static Random random = new Random();
int ran = random.nextInt(2);

public static boolean gameOver = false;
public static String winner = "";


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    b1 = new javax.swing.JButton();
    b2 = new javax.swing.JButton();
    b3 = new javax.swing.JButton();
    b7 = new javax.swing.JButton();
    b5 = new javax.swing.JButton();
    b6 = new javax.swing.JButton();
    b4 = new javax.swing.JButton();
    b9 = new javax.swing.JButton();
    b8 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextPane1 = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Noughts & Crosses ");
    setResizable(false);

    b1.setLabel("");
    b1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b1ActionPerformed(evt);
        }
    });

    b2.setLabel("");
    b2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b2ActionPerformed(evt);
        }
    });

    b3.setLabel("");
    b3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b3ActionPerformed(evt);
        }
    });

    b7.setLabel("");
    b7.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b7ActionPerformed(evt);
        }
    });

    b5.setLabel("");
    b5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b5ActionPerformed(evt);
        }
    });

    b6.setLabel("");
    b6.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b6ActionPerformed(evt);
        }
    });

    b4.setLabel("");
    b4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b4ActionPerformed(evt);
        }
    });

    b9.setLabel("");
    b9.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b9ActionPerformed(evt);
        }
    });

    b8.setLabel("");
    b8.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            b8ActionPerformed(evt);
        }
    });

    jTextPane1.setEditable(false);
    jTextPane1.setBackground(new java.awt.Color(240, 240, 240));
    jTextPane1.setText("Move: " + move);
    jScrollPane1.setViewportView(jTextPane1);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(b4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(18, 18, 18)
                            .addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(b4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

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

private void b5ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color5 == 0) {
            if (move.equals("Red")) {
                b5.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color5 = 1;
            } else {
                b5.setBackground(new java.awt.Color(0, 0, 255));
                color5 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }

        checkForVictory();
    }
}                                  

private void b9ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color9 == 0) {

            if (move.equals("Red")) {

                b9.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color9 = 1;
            } else {

                b9.setBackground(new java.awt.Color(0, 0, 255));
                color9 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b8ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color8 == 0) {

            if (move.equals("Red")) {

                b8.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color8 = 1;
            } else {

                b8.setBackground(new java.awt.Color(0, 0, 255));
                color8 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b1ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color1 == 0) {

            if (move.equals("Red")) {

                b1.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color1 = 1;
            } else {

                b1.setBackground(new java.awt.Color(0, 0, 255));
                color1 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b7ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color7 == 0) {

            if (move.equals("Red")) {

                b7.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color7 = 1;
            } else {

                b7.setBackground(new java.awt.Color(0, 0, 255));
                color7 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color2 == 0) {

            if (move.equals("Red")) {

                b2.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color2 = 1;
            } else {

                b2.setBackground(new java.awt.Color(0, 0, 255));
                color2 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b3ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color3 == 0) {

            if (move.equals("Red")) {

                b3.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color3 = 1;
            } else {

                b3.setBackground(new java.awt.Color(0, 0, 255));
                color3 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b6ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color6 == 0) {

            if (move.equals("Red")) {

                b6.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color6 = 1;
            } else {

                b6.setBackground(new java.awt.Color(0, 0, 255));
                color6 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

private void b4ActionPerformed(java.awt.event.ActionEvent evt) {                                   
    // TODO add your handling code here:
    if (!gameOver) {
        if (color4 == 0) {

            if (move.equals("Red")) {

                b4.setBackground(new java.awt.Color(255, 0, 0));
                move = "Blue";
                jTextPane1.setText("Move: " + move);
                color4 = 1;
            } else {

                b4.setBackground(new java.awt.Color(0, 0, 255));
                color4 = 2;
                move = "Red";
                jTextPane1.setText("Move: " + move);

            }
        }
        checkForVictory();
    }
}                                  

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Noughts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Noughts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Noughts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Noughts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Noughts().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration                   
}

I would just like to know if I could make any improvements or if I made it too long for too simple a program.

It functions but is the most basic of basic programs.

\$\endgroup\$
2
\$\begingroup\$

remove code duplication

You have lots of duplicated code all over the program which could be reduced by the use of parameterized methods. E.g. the checkForVictory() method could be changed like this:

private void ckeckLine(int firstField, int middleField, int lastField, int playerColor, String playerName){
  if(firstField==playerColor&&middleField==playerColor&&lastField==playerColor){
       winner = playerName;
       System.out.println("Winner!" + winner);
       gameOver = true;
       jTextPane1.setText("Winner is " + winner);
  }
}


private void checkForVictoryForPlayer(int playerColor, String playerName ) {
    //Horizontal Victories
     ckeckLine(color1, color2, color3, playerColor ,playerName);
     ckeckLine(color4, color5, color6, playerColor ,playerName);
    // you get the idea?
}


private void checkForVictory(){
   checkForVictoryForPlayer(1, "Red");
   checkForVictoryForPlayer(2, "Blue");
   //Draw Check
   if(color1 != 0 && color2 != 0 && color3 != 0&& color4 != 0&& color5 != 0&& color6 != 0&& color7 != 0&& color8 != 0&& color9 != 0 && winner == ""){
      System.out.println("Draw!");
      gameOver = true;
       jTextPane1.setText("Draw!");
  }
}

Same is true for your b?actionPerformed() methods. There could be only one parameterized version instead of 9.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.