8
\$\begingroup\$

As a school project in my navigator 12 class, I've decided to try and learn Java. I thought that I'd try and make the Game of Life, because it would be a good way to start learning Java (watched a bunch of Java tutorials).

Could I get your feedback on this program I wrote? I would like input on if everything is inputted properly and what I should've done differently, and if there is more effective and efficient ways to do a certain task. Also, just any type feedback would be helpful.

I made the game so the sides of the board would be connected to each other, and so the simulation would be contained(it will loop to the other side). I also have a slider to change the speed the game iterates. Other features are randomizing the board, and play and stop buttons. You can also, change the size of the board, with inputting the size you want it to generate.

Main

public class Main {


    public static void main(String[] args) {

        new Settingsboard();

    }

}

Settings

public class Settingsboard extends JFrame{

    JFrame Frame2 = new JFrame();
    JLabel label1, label2, value1, value2;
    JTextField x_Value, y_Value;
    JButton button1, button2, button3, clear;
    static JCheckBox random;
    int run = 0;
    static JSlider howManyTimes;
    int delay = 1000; // 1000 ms == 1 second
    javax.swing.Timer myTimer = new javax.swing.Timer(delay, 
               new MyTimerActionListener());

    public Settingsboard(){

           Frame2.setSize(400,400);
           JPanel thePanel = new JPanel();
           //JPanel thePanel2 = new JPanel();

           label1 = new JLabel("Set Size Of Board: ");
           thePanel.add(label1);

           value1 = new JLabel("        Width: ");
           thePanel.add(value1);
               x_Value = new JTextField("", 5);
           thePanel.add(x_Value);

           value2 = new JLabel("                                            Height: "); 
           thePanel.add(value2);
           y_Value = new JTextField("", 5);
           thePanel.add(y_Value);

           button1 = new JButton("Generate Board");
           button1.addActionListener(
                   new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource() == button1){
                                if(run == 0){
                                    int number1 = Integer.parseInt(x_Value.getText());
                                    int number2 = Integer.parseInt(y_Value.getText());

                                    new Board(number1,number2);
                                    run = 1;
                                }
                                else{JOptionPane.showMessageDialog(null, "Board is already running", "InfoBox: ", JOptionPane.INFORMATION_MESSAGE);}
                            }
                        }
                      }
                    );
           thePanel.add(button1);



           button2 = new JButton("Play");
           button2.addActionListener(
                   new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource() == button2){

                                myTimer.start();

                            }
                        }
                      }
                    );
           thePanel.add(button2);



           button3 = new JButton("Stop");
           button3.addActionListener(
                   new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if(e.getSource() == button3){
                                myTimer.stop();

                            }
                        }
                      }
                    );
           thePanel.add(button3);
           random = new JCheckBox("Randomize?");
           thePanel.add(random);

           label2 = new JLabel("     Speed of loop (in milliseconds):  ");
            thePanel.add(label2);

            howManyTimes = new JSlider(0, 1000, 1000);
            howManyTimes.setMinorTickSpacing(50);
            howManyTimes.setMajorTickSpacing(250);
            howManyTimes.setPaintTicks(true);
            howManyTimes.setPaintLabels(true);
            ListenForSlider lForSlider = new ListenForSlider();
            howManyTimes.addChangeListener(lForSlider);
            thePanel.add(howManyTimes);


            clear = new JButton("Reset board");
               clear.addActionListener(
                       new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                if(e.getSource() == clear){




                                    for(int y=0; y<Board.lengthT; y++){
                                        for(int x=0; x<Board.widthT; x++){

                                                if(Settingsboard.random.isSelected()) {
                                                    int random = (int )(Math.random() * 2);
                                                    if(random == 0){
                                                        Board.grid[x][y].setBackground(Color.red);
                                                        Board.boolBoard[x][y] = false;
                                                    }
                                                    else if(random == 1){
                                                        Board.grid[x][y].setBackground(Color.blue);
                                                        Board.boolBoard[x][y] = true;
                                                    }
                                                }
                                                else{
                                                    Board.grid[x][y].setBackground(Color.red);
                                                    Board.boolBoard[x][y] = false;
                                                }

                                        }
                                }




                                }
                            }
                          }
                        );
               thePanel.add(clear);



           Frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           Frame2.add(thePanel);
           Frame2.setVisible(true);
       }


    private class ListenForSlider implements ChangeListener{

        @Override
        public void stateChanged(ChangeEvent e) {

            // Check if the source of the event was the button

            if(e.getSource() == howManyTimes){

                delay = howManyTimes.getValue();
                String speed = Integer.toString(delay);
                System.out.print(speed);
                if(delay == 0){
                    System.out.print("hit");
                    speed = "As fast as it will go!";
                }
                label2.setText("     Speed of loop(in milliseconds):  " + speed );
                myTimer.setDelay(delay);
            }

        }

    }






}

Board

public class Board extends JFrame{


    JFrame Frame = new JFrame(); //creates frame

    static JButton[][] grid; //names the grid of buttons
    JTextArea textArea1;
    static int lengthT = 0;;
    static int widthT = 0;
    static Boolean[][] boolBoard;

    List<String> onXY_Values = new ArrayList<String>();

    public Board(int width, int length){ //constructor

        boolBoard = new Boolean [width][length];

        Frame.setSize(1000,1000);

        Frame.setLocationRelativeTo(null);

        Frame.setTitle("The Game of Life");
        ListenForKeys lForKeys = new ListenForKeys();
        widthT = width;
        lengthT = length;
        Frame.setLayout(new GridLayout(width,length)); //set layout
            grid = new JButton[width][length]; //allocate the size of grid
            for(int y=0; y<length; y++){
                    for(int x=0; x<width; x++){
                            grid[x][y]=new JButton("("+x+","+y+")"); //creates new button    
                            Frame.add(grid[x][y]); //adds button to grid
                            grid[x][y].setBorderPainted(false);
                            grid[x][y].setContentAreaFilled(false);



                            if(Settingsboard.random.isSelected()) {
                                int random = (int )(Math.random() * 2);
                                if(random == 0){
                                    grid[x][y].setBackground(Color.red);
                                    boolBoard[x][y] = false;
                                }
                                else if(random == 1){
                                    grid[x][y].setBackground(Color.blue);
                                    boolBoard[x][y] = true;
                                }
                            }
                            else{
                                grid[x][y].setBackground(Color.red);
                                boolBoard[x][y] = false;
                            }




                            grid[x][y].setForeground(Color.black);
                            grid[x][y].setOpaque(true);
                            grid[x][y].setPreferredSize(new Dimension(30, 30));
                            grid[x][y].setBorder(BorderFactory.createLineBorder(Color.black, 2));
                            grid[x][y].setMargin(new Insets(0, 0, 0, 0));
                            ListenForButton lForButton = new ListenForButton();
                            grid[x][y].addActionListener(lForButton);
                            grid[x][y].addKeyListener(lForKeys);

                    }
            }
            Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Frame.pack(); //sets appropriate size for frame
            Frame.setVisible(true); //makes frame visible

    }

    public static void main(String[] args) {


    }




    private class ListenForButton implements ActionListener{

        public void actionPerformed(ActionEvent e){

            String xString = e.getActionCommand();
            String yString = e.getActionCommand();

            int index = xString.indexOf(',');
            xString = xString.substring(0, index);
            String yStringRemove = xString;
            xString = xString.replace("(", "");

            int x = Integer.parseInt(xString);

            yString = yString.replace(yStringRemove, "");
            yString = yString.replace(",", "");
            yString = yString.replace(")", "");

            int y = Integer.parseInt(yString);

             ((JButton)e.getSource()).setEnabled(true);

            if(e.getSource() == grid[x][y]){

                grid[x][y].getActionCommand();
                if(!boolBoard[x][y]){//turning on
                    grid[x][y].setBackground(Color.blue);
                    onXY_Values.add(x + "," + y);
                    boolBoard[x][y] = true;
                }

                else if(boolBoard[x][y]){//turning off
                    grid[x][y].setBackground(Color.red);
                    onXY_Values.remove(x + "," + y);
                    boolBoard[x][y] = false;
                }

            }

        }
    }


    private class ListenForKeys implements KeyListener{

        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
            if( e.getKeyChar() == 10 ){
                Logic.logic(lengthT,widthT,grid,boolBoard);
            }

        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
        }
    }
}

Timer

public class MyTimerActionListener implements ActionListener {

    @Override


    public void actionPerformed(ActionEvent e) {
        int widthT = Board.widthT;
        int lengthT = Board.lengthT;
        JButton[][] grid = Board.grid;
        Boolean [][] boolBoard = Board.boolBoard;
        Logic.logic(lengthT, widthT, grid, boolBoard);
    }

}

Logic

public class Logic {





    public static void logic(int lengthT, int widthT, JButton[][] grid, Boolean[][] boolBoard) {

        List<Integer> turnOnX = new ArrayList<Integer>();
        List<Integer> turnOnY = new ArrayList<Integer>();
        List<Integer> turnOffX = new ArrayList<Integer>();
        List<Integer> turnOffY = new ArrayList<Integer>();
        int fullspace = 0;






        for(int w = 0; w < 1; w++){//right bottom piece
            int j = lengthT - 1;
            int i = widthT - 1;
            if(!boolBoard[i][j]){//off
                fullspace = 0;
                if(boolBoard[i- 1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i- 1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i-1][j-lengthT+1]){
                    fullspace++;
                }


                if(fullspace == 3){
                    turnOnX.add(i);
                    turnOnY.add(j);
                }
            }


            else if(boolBoard[i][j]){
                fullspace = 0;
                if(boolBoard[i- 1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i- 1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i-1][j-lengthT+1]){
                    fullspace++;
                }


                if(fullspace < 2){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
                else if(fullspace > 3){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
            }
       }






        for(int w = 0; w < 1; w++){//left bottom piece
            int j = lengthT - 1;
            int i = w;
            if(!boolBoard[i][j]){//off
                fullspace = 0;
                if(boolBoard[i+widthT- 1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i+widthT- 1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i+widthT-1][j-lengthT+1]){
                    fullspace++;
                }

                if(fullspace == 3){
                    turnOnX.add(i);
                    turnOnY.add(j);
                }
            }


            else if(boolBoard[i][j]){
                fullspace = 0;
                if(boolBoard[i+widthT- 1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i+widthT- 1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j-lengthT+1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j-1]){
                    fullspace++;
                }

                if(boolBoard[i+widthT-1][j-lengthT+1]){
                    fullspace++;
                }


                if(fullspace < 2){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
                else if(fullspace > 3){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
            }
       }








        for(int w = 0; w < 1; w++){//right top piece
            int j = 0;
            int i = widthT - 1;
            if(!boolBoard[i][j]){//off
                fullspace = 0;
                if(boolBoard[i- 1][j+lengthT-1]){
                    fullspace++;
                }


                if(boolBoard[i- 1][j]){
                    fullspace++;
                }


                if(boolBoard[i][j+lengthT-1]){
                    fullspace++;
                }


                if(boolBoard[i-widthT+1][j+1]){
                    fullspace++;
                }


                if(boolBoard[i-widthT+1][j]){
                    fullspace++;
                }


                if(boolBoard[i][j+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i-1][j+1]){
                    fullspace++;
                }

                if(fullspace == 3){
                    turnOnX.add(i);
                    turnOnY.add(j);
                }
            }


            else if(boolBoard[i][j]){
                fullspace = 0;
                if(boolBoard[i- 1][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i- 1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j+1]){
                    fullspace++;
                }

                if(boolBoard[i-widthT+1][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i-1][j+1]){
                    fullspace++;
                }

                if(fullspace < 2){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
                else if(fullspace > 3){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
            }
       }









        for(int w = 0; w < 1; w++){//left top piece
            int j = 0;
            int i = w;
            if(!boolBoard[i][j]){//off
                fullspace = 0;
                if(boolBoard[i + widthT - 1][j+lengthT-1]){
                    fullspace++;
                }


                if(boolBoard[i + widthT- 1][j]){
                    fullspace++;
                }


                if(boolBoard[i][j+lengthT-1]){
                    fullspace++;
                }


                if(boolBoard[i+1][j+1]){
                    fullspace++;
                }


                if(boolBoard[i+1][j]){
                    fullspace++;
                }


                if(boolBoard[i][j+1]){
                    fullspace++;
                }


                if(boolBoard[i+1][j+lengthT-1]){
                    fullspace++;
                }


                if(boolBoard[i+ widthT-1][j+1]){
                    fullspace++;
                }

                if(fullspace == 3){
                    turnOnX.add(i);
                    turnOnY.add(j);
                }
            }


            else if(boolBoard[i][j]){
                fullspace = 0;
                if(boolBoard[i+ widthT - 1][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i+ widthT- 1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j+1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j]){
                    fullspace++;
                }

                if(boolBoard[i][j+1]){
                    fullspace++;
                }

                if(boolBoard[i+1][j+lengthT-1]){
                    fullspace++;
                }

                if(boolBoard[i+ widthT-1][j+1]){
                    fullspace++;
                }


                if(fullspace < 2){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
                else if(fullspace > 3){
                    turnOffX.add(i);
                    turnOffY.add(j);
                }
            }
       }        







for(int w = 1; w < widthT - 1; w++){//top bar
    int j = 0;
    int i = w;
    if(!boolBoard[i][j]){//off
        fullspace = 0;
        if(boolBoard[i - 1][j+lengthT-1]){
            fullspace++;
        }


        if(boolBoard[i - 1][j]){
            fullspace++;
        }


        if(boolBoard[i][j+lengthT-1]){
            fullspace++;
        }

        if(boolBoard[i+1][j+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j]){
            fullspace++;
        }


        if(boolBoard[i][j+1]){
            fullspace++;
        }


        if(boolBoard[i+1][j+lengthT-1]){
            fullspace++;
        }


        if(boolBoard[i-1][j+1]){
            fullspace++;
        }

        if(fullspace == 3){
            turnOnX.add(i);
            turnOnY.add(j);
        }
    }





    else if(boolBoard[i][j]){
        fullspace = 0;
        if(boolBoard[i - 1][j+lengthT-1]){
            fullspace++;
        }

        if(boolBoard[i - 1][j]){
            fullspace++;
        }

        if(boolBoard[i][j+lengthT-1]){
            fullspace++;
        }

        if(boolBoard[i+1][j+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j]){
            fullspace++;
        }

        if(boolBoard[i][j+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j+lengthT-1]){
            fullspace++;
        }

        if(boolBoard[i-1][j+1]){
            fullspace++;
        }


        if(fullspace < 2){
            turnOffX.add(i);
            turnOffY.add(j);
        }
        else if(fullspace > 3){
            turnOffX.add(i);
            turnOffY.add(j);
        }
    }
}







for(int w = 1; w < widthT - 1; w++){//bottom bar
    int j = lengthT - 1;
    int i = w;
    if(!boolBoard[i][j]){//off
        fullspace = 0;
        if(boolBoard[i - 1][j - 1]){
            fullspace++;
        }


        if(boolBoard[i - 1][j]){
            fullspace++;
        }


        if(boolBoard[i][j - 1]){
            fullspace++;
        }


        if(boolBoard[i+1][j-lengthT+1]){
            fullspace++;
        }


        if(boolBoard[i+1][j]){
            fullspace++;
        }


        if(boolBoard[i][j-lengthT+1]){
            fullspace++;
        }


        if(boolBoard[i+1][j - 1]){
            fullspace++;
        }


        if(boolBoard[i-1][j-lengthT+1]){
            fullspace++;
        }

        if(fullspace == 3){
            turnOnX.add(i);
            turnOnY.add(j);
        }
    }



    else if(boolBoard[i][j]){
        fullspace = 0;
        if(boolBoard[i - 1][j-1]){
            fullspace++;
        }

        if(boolBoard[i - 1][j]){
            fullspace++;
        }

        if(boolBoard[i][j-1]){
            fullspace++;
        }

        if(boolBoard[i+1][j-lengthT+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j]){
            fullspace++;
        }

        if(boolBoard[i][j-lengthT+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j-1]){
            fullspace++;
        }

        if(boolBoard[i-1][j-lengthT+1]){
            fullspace++;
        }


        if(fullspace < 2){
            turnOffX.add(i);
            turnOffY.add(j);
        }
        else if(fullspace > 3){
            turnOffX.add(i);
            turnOffY.add(j);
        }
    }
}








for(int l = 1; l < lengthT - 1; l++){//left bar
    int i = 0;
    int j = l;
    if(!boolBoard[i][j]){//off
        fullspace = 0;
        if(boolBoard[i + widthT -1][j-1]){
            fullspace++;
        }


        if(boolBoard[i + widthT -1][j]){
            fullspace++;
        }


        if(boolBoard[i][j-1]){
            fullspace++;
        }


        if(boolBoard[i+1][j+1]){
            fullspace++;
        }


        if(boolBoard[i+1][j]){
            fullspace++;
        }


        if(boolBoard[i][j+1]){
            fullspace++;
        }


        if(boolBoard[i+1][j-1]){
            fullspace++;
        }


        if(boolBoard[i + widthT -1][j+1]){
            fullspace++;
        }

        if(fullspace == 3){
            turnOnX.add(i);
            turnOnY.add(j);
        }
    }





    else if(boolBoard[i][j]){
        fullspace = 0;
        if(boolBoard[i + widthT -1][j-1]){
            fullspace++;
        }

        if(boolBoard[i + widthT -1][j]){
            fullspace++;
        }

        if(boolBoard[i][j-1]){
            fullspace++;
        }

        if(boolBoard[i+1][j+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j]){
            fullspace++;
        }

        if(boolBoard[i][j+1]){
            fullspace++;
        }

        if(boolBoard[i+1][j-1]){
            fullspace++;
        }

        if(boolBoard[i + widthT -1][j+1]){
            fullspace++;
        }


        if(fullspace < 2){
            turnOffX.add(i);
            turnOffY.add(j);
        }
        else if(fullspace > 3){
            turnOffX.add(i);
            turnOffY.add(j);
        }
    }
}








for(int l = 1; l < lengthT - 1; l++){//right bar
    int i = widthT -1;
    int j = l;
    if(!boolBoard[i][j]){//off
        fullspace = 0;
        if(boolBoard[i-1][j-1]){
            fullspace++;
        }


        if(boolBoard[i-1][j]){
            fullspace++;
        }


        if(boolBoard[i][j-1]){
            fullspace++;
        }


        if(boolBoard[i-widthT+1][j+1]){
            fullspace++;
        }


        if(boolBoard[i-widthT+1][j]){
            fullspace++;
        }


        if(boolBoard[i][j+1]){
            fullspace++;
        }


        if(boolBoard[i-widthT+1][j-1]){
            fullspace++;
        }


        if(boolBoard[i-1][j+1]){
            fullspace++;
        }

        if(fullspace == 3){
            turnOnX.add(i);
            turnOnY.add(j);
        }
    }





    else if(boolBoard[i][j]){
        fullspace = 0;
        if(boolBoard[i-1][j-1]){
            fullspace++;
        }

        if(boolBoard[i-1][j]){
            fullspace++;
        }

        if(boolBoard[i][j-1]){
            fullspace++;
        }

        if(boolBoard[i-widthT+1][j+1]){
            fullspace++;
        }

        if(boolBoard[i-widthT+1][j]){
            fullspace++;
        }

        if(boolBoard[i][j+1]){
            fullspace++;
        }

        if(boolBoard[i-widthT+1][j-1]){
            fullspace++;
        }

        if(boolBoard[i-1][j+1]){
            fullspace++;
        }


        if(fullspace < 2){
            turnOffX.add(i);
            turnOffY.add(j);
        }
        else if(fullspace > 3){
            turnOffX.add(i);
            turnOffY.add(j);
        }
    }
}







for(int i = 1; i < widthT-1; i++)//checking empty spaces *main body*
{
    for(int j = 1; j < lengthT-1; j++)
    {
        if(!boolBoard[i][j]){//off
            fullspace = 0;
            if(boolBoard[i - 1][j-1]){
                fullspace++;
            }


            if(boolBoard[i - 1][j]){
                fullspace++;
            }


            if(boolBoard[i][j-1]){
                fullspace++;
            }


            if(boolBoard[i+1][j+1]){
                fullspace++;
            }


            if(boolBoard[i+1][j]){
                fullspace++;
            }


            if(boolBoard[i][j+1]){
                fullspace++;
            }


            if(boolBoard[i+1][j-1]){
                fullspace++;
            }


            if(boolBoard[i-1][j+1]){
                fullspace++;
            }


            if(fullspace == 3){
                turnOnX.add(i);
                turnOnY.add(j);
            }
        }





        else if(boolBoard[i][j]){
            fullspace = 0;
            if(boolBoard[i - 1][j-1]){
                fullspace++;
            }

            if(boolBoard[i - 1][j]){
                fullspace++;
            }

            if(boolBoard[i][j-1]){
                fullspace++;
            }

            if(boolBoard[i+1][j+1]){
                fullspace++;
            }

            if(boolBoard[i+1][j]){
                fullspace++;
            }

            if(boolBoard[i][j+1]){
                fullspace++;
            }

            if(boolBoard[i+1][j-1]){
                fullspace++;
            }

            if(boolBoard[i-1][j+1]){
                fullspace++;
            }


            if(fullspace < 2){
                turnOffX.add(i);
                turnOffY.add(j);
            }
            else if(fullspace > 3){
                turnOffX.add(i);
                turnOffY.add(j);
            }
        }
    }
    }


for (int i = 0; i < turnOnX.size(); i++){
    grid[ turnOnX.get(i) ][ turnOnY.get(i) ].setBackground(Color.blue);
    boolBoard[turnOnX.get(i)][turnOnY.get(i)] = true;
}

for (int i = 0; i < turnOffX.size(); i++){
    grid[ turnOffX.get(i) ][ turnOffY.get(i) ].setBackground(Color.red);
    boolBoard[turnOffX.get(i)][turnOffY.get(i)] = false;
}

}






}
\$\endgroup\$
  • 2
    \$\begingroup\$ I thought first programs are like System.out.println("Hello World"); When did first programs become games? People must be getting smarter! \$\endgroup\$ – barlop Feb 3 '14 at 3:54
12
+50
\$\begingroup\$

So there's a lot to go though in here. I think that as a beginner project you can be proud of your code... there are a number of things I would change, but, on the whole, it is systematic, logical, and well-formatted. In my opinion this is far more preferable than 'clever' code that is hard to read and understand...

So, choosing some things that you should work on:

  • It appears you have used a code-editor to help you (intelliJ maybe, or eclipse?). This is a good thing, because it will help you to debug your programs, and it can help you refactor with fewer errors.
  • I would start by renaming some of your J* components... label1, label2, button1, etc. do not help much.
  • Frame2 should have a lower-case 'f', and why do you need two JFrames? Every time you have Frame2 you should instead have this (the SettingsBoard)
  • Using spaces in Labels to get the justification right, is wrong: value1 = new JLabel(" Width: "); should be solved by using the correct size and alignment settings in your LayoutManager
  • JCheckBox random and JSlider should not be static, should they?

  • in your Board class you have multiple JFrames as well. The Board class itself is a JFrame, so use it.

  • In the Logic method, there is a huge amount of duplicate code.... there's good ways to reduce it... I'll describe a system which I would not expect you (as a beginner) to think of, but it's neat, concise, and really makes things easy...

Your goal is to 'wrap' the game of life, so cells on the left margin are impacted by cells on the right, and top and bottom too.... You can use the modulo operator.... and a 'width' (or height) offset. And this way you an eliminate 90% of your code. Consider this function:

private static final int[][] offsets = {
        {-1, -1}, // up left
        {-1,  0}, //    left
        {-1,  1}, // dn left
        { 0, -1}, // up 
        { 0,  1}, // dn
        { 1, -1}, // up right
        { 1,  0}, //    right
        { 1,  1}  // dn right
    }

private static final int getAliveNeighbors(int x, int y, int width, int height, Boolean[][] boolboard) {

    int alive = 0;
    // look around us.... how many cells are alive
    for (int[] offset : offsets) {
        if (boolboard[(x + offset[0] + width) % width][(y + offset[1] + height) % height]) {
            alive++;
        }
    }
    return alive;
}

The above function is 'clever'. It adds the width to the coordinate, and then gets the remainder when dividing by the width. For example, if your grid is 10x10, and you are looking for life at cell 0,0, then you want to check for life in the cell to the left (-1, 0) then you really want to look in cell (9, 0), which is ((0 + 10 - 1) % 10 == 9 , 0)

Now we have an 'easy' function for counting the amount of life, you can use that to massively simplify your 'Logic' code.

\$\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.